MediaWiki  REL1_20
SquidUpdate.php
Go to the documentation of this file.
00001 <?php
00028 class SquidUpdate {
00029         var $urlArr, $mMaxTitles;
00030 
00035         function __construct( $urlArr = array(), $maxTitles = false ) {
00036                 global $wgMaxSquidPurgeTitles;
00037                 if ( $maxTitles === false ) {
00038                         $this->mMaxTitles = $wgMaxSquidPurgeTitles;
00039                 } else {
00040                         $this->mMaxTitles = $maxTitles;
00041                 }
00042                 $urlArr = array_unique( $urlArr ); // Remove duplicates
00043                 if ( count( $urlArr ) > $this->mMaxTitles ) {
00044                         $urlArr = array_slice( $urlArr, 0, $this->mMaxTitles );
00045                 }
00046                 $this->urlArr = $urlArr;
00047         }
00048 
00054         static function newFromLinksTo( &$title ) {
00055                 global $wgMaxSquidPurgeTitles;
00056                 wfProfileIn( __METHOD__ );
00057 
00058                 # Get a list of URLs linking to this page
00059                 $dbr = wfGetDB( DB_SLAVE );
00060                 $res = $dbr->select( array( 'links', 'page' ),
00061                         array( 'page_namespace', 'page_title' ),
00062                         array(
00063                                 'pl_namespace' => $title->getNamespace(),
00064                                 'pl_title'     => $title->getDBkey(),
00065                                 'pl_from=page_id' ),
00066                         __METHOD__ );
00067                 $blurlArr = $title->getSquidURLs();
00068                 if ( $dbr->numRows( $res ) <= $wgMaxSquidPurgeTitles ) {
00069                         foreach ( $res as $BL ) {
00070                                 $tobj = Title::makeTitle( $BL->page_namespace, $BL->page_title ) ;
00071                                 $blurlArr[] = $tobj->getInternalURL();
00072                         }
00073                 }
00074 
00075                 wfProfileOut( __METHOD__ );
00076                 return new SquidUpdate( $blurlArr );
00077         }
00078 
00087         static function newFromTitles( $titles, $urlArr = array() ) {
00088                 global $wgMaxSquidPurgeTitles;
00089                 $i = 0;
00090                 foreach ( $titles as $title ) {
00091                         $urlArr[] = $title->getInternalURL();
00092                         if ( $i++ > $wgMaxSquidPurgeTitles ) {
00093                                 break;
00094                         }
00095                 }
00096                 return new SquidUpdate( $urlArr );
00097         }
00098 
00104         static function newSimplePurge( &$title ) {
00105                 $urlArr = $title->getSquidURLs();
00106                 return new SquidUpdate( $urlArr );
00107         }
00108 
00112         function doUpdate() {
00113                 SquidUpdate::purge( $this->urlArr );
00114         }
00115 
00125         static function purge( $urlArr ) {
00126                 global $wgSquidServers, $wgHTCPMulticastRouting;
00127 
00128                 if( !$urlArr ) {
00129                         return;
00130                 }
00131 
00132                 if ( $wgHTCPMulticastRouting ) {
00133                         SquidUpdate::HTCPPurge( $urlArr );
00134                 }
00135 
00136                 wfProfileIn( __METHOD__ );
00137 
00138                 $urlArr = array_unique( $urlArr ); // Remove duplicates
00139                 $maxSocketsPerSquid = 8; //  socket cap per Squid
00140                 $urlsPerSocket = 400; // 400 seems to be a good tradeoff, opening a socket takes a while
00141                 $socketsPerSquid = ceil( count( $urlArr ) / $urlsPerSocket );
00142                 if ( $socketsPerSquid > $maxSocketsPerSquid ) {
00143                         $socketsPerSquid = $maxSocketsPerSquid;
00144                 }
00145 
00146                 $pool = new SquidPurgeClientPool;
00147                 $chunks = array_chunk( $urlArr, ceil( count( $urlArr ) / $socketsPerSquid ) );
00148                 foreach ( $wgSquidServers as $server ) {
00149                         foreach ( $chunks as $chunk ) {
00150                                 $client = new SquidPurgeClient( $server );
00151                                 foreach ( $chunk as $url ) {
00152                                         $client->queuePurge( $url );
00153                                 }
00154                                 $pool->addClient( $client );
00155                         }
00156                 }
00157                 $pool->run();
00158 
00159                 wfProfileOut( __METHOD__ );
00160         }
00161 
00166         static function HTCPPurge( $urlArr ) {
00167                 global $wgHTCPMulticastRouting, $wgHTCPMulticastTTL;
00168                 wfProfileIn( __METHOD__ );
00169 
00170                 $htcpOpCLR = 4; // HTCP CLR
00171 
00172                 // @todo FIXME: PHP doesn't support these socket constants (include/linux/in.h)
00173                 if( !defined( "IPPROTO_IP" ) ) {
00174                         define( "IPPROTO_IP", 0 );
00175                         define( "IP_MULTICAST_LOOP", 34 );
00176                         define( "IP_MULTICAST_TTL", 33 );
00177                 }
00178 
00179                 // pfsockopen doesn't work because we need set_sock_opt
00180                 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
00181                 if ( $conn ) {
00182                         // Set socket options
00183                         socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_LOOP, 0 );
00184                         if ( $wgHTCPMulticastTTL != 1 )
00185                                 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_TTL,
00186                                         $wgHTCPMulticastTTL );
00187 
00188                         $urlArr = array_unique( $urlArr ); // Remove duplicates
00189                         foreach ( $urlArr as $url ) {
00190                                 if( !is_string( $url ) ) {
00191                                         throw new MWException( 'Bad purge URL' );
00192                                 }
00193                                 $url = SquidUpdate::expand( $url );
00194                                 $conf = self::getRuleForURL( $url, $wgHTCPMulticastRouting );
00195                                 if ( !$conf ) {
00196                                         wfDebug( "No HTCP rule configured for URL $url , skipping\n" );
00197                                         continue;
00198                                 }
00199                                 if ( !isset( $conf['host'] ) || !isset( $conf['port'] ) ) {
00200                                         throw new MWException( "Invalid HTCP rule for URL $url\n" );
00201                                 }
00202 
00203                                 // Construct a minimal HTCP request diagram
00204                                 // as per RFC 2756
00205                                 // Opcode 'CLR', no response desired, no auth
00206                                 $htcpTransID = rand();
00207 
00208                                 $htcpSpecifier = pack( 'na4na*na8n',
00209                                         4, 'HEAD', strlen( $url ), $url,
00210                                         8, 'HTTP/1.0', 0 );
00211 
00212                                 $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
00213                                 $htcpLen = 4 + $htcpDataLen + 2;
00214 
00215                                 // Note! Squid gets the bit order of the first
00216                                 // word wrong, wrt the RFC. Apparently no other
00217                                 // implementation exists, so adapt to Squid
00218                                 $htcpPacket = pack( 'nxxnCxNxxa*n',
00219                                         $htcpLen, $htcpDataLen, $htcpOpCLR,
00220                                         $htcpTransID, $htcpSpecifier, 2);
00221 
00222                                 // Send out
00223                                 wfDebug( "Purging URL $url via HTCP\n" );
00224                                 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
00225                                         $conf['host'], $conf['port'] );
00226                         }
00227                 } else {
00228                         $errstr = socket_strerror( socket_last_error() );
00229                         wfDebug( __METHOD__ . "(): Error opening UDP socket: $errstr\n" );
00230                 }
00231                 wfProfileOut( __METHOD__ );
00232         }
00233 
00249         static function expand( $url ) {
00250                 return wfExpandUrl( $url, PROTO_INTERNAL );
00251         }
00252         
00259         static function getRuleForURL( $url, $rules ) {
00260                 foreach ( $rules as $regex => $routing ) {
00261                         if ( $regex === '' || preg_match( $regex, $url ) ) {
00262                                 return $routing;
00263                         }
00264                 }
00265                 return false;
00266         }
00267         
00268 }