MediaWiki
REL1_21
|
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 ( $res->numRows() <= $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 wfDebug( "Squid purge: " . implode( ' ', $urlArr ) . "\n" ); 00133 00134 if ( $wgHTCPMulticastRouting ) { 00135 SquidUpdate::HTCPPurge( $urlArr ); 00136 } 00137 00138 wfProfileIn( __METHOD__ ); 00139 00140 $urlArr = array_unique( $urlArr ); // Remove duplicates 00141 $maxSocketsPerSquid = 8; // socket cap per Squid 00142 $urlsPerSocket = 400; // 400 seems to be a good tradeoff, opening a socket takes a while 00143 $socketsPerSquid = ceil( count( $urlArr ) / $urlsPerSocket ); 00144 if ( $socketsPerSquid > $maxSocketsPerSquid ) { 00145 $socketsPerSquid = $maxSocketsPerSquid; 00146 } 00147 00148 $pool = new SquidPurgeClientPool; 00149 $chunks = array_chunk( $urlArr, ceil( count( $urlArr ) / $socketsPerSquid ) ); 00150 foreach ( $wgSquidServers as $server ) { 00151 foreach ( $chunks as $chunk ) { 00152 $client = new SquidPurgeClient( $server ); 00153 foreach ( $chunk as $url ) { 00154 $client->queuePurge( $url ); 00155 } 00156 $pool->addClient( $client ); 00157 } 00158 } 00159 $pool->run(); 00160 00161 wfProfileOut( __METHOD__ ); 00162 } 00163 00168 static function HTCPPurge( $urlArr ) { 00169 global $wgHTCPMulticastRouting, $wgHTCPMulticastTTL; 00170 wfProfileIn( __METHOD__ ); 00171 00172 $htcpOpCLR = 4; // HTCP CLR 00173 00174 // @todo FIXME: PHP doesn't support these socket constants (include/linux/in.h) 00175 if( !defined( "IPPROTO_IP" ) ) { 00176 define( "IPPROTO_IP", 0 ); 00177 define( "IP_MULTICAST_LOOP", 34 ); 00178 define( "IP_MULTICAST_TTL", 33 ); 00179 } 00180 00181 // pfsockopen doesn't work because we need set_sock_opt 00182 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP ); 00183 if ( $conn ) { 00184 // Set socket options 00185 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_LOOP, 0 ); 00186 if ( $wgHTCPMulticastTTL != 1 ) 00187 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_TTL, 00188 $wgHTCPMulticastTTL ); 00189 00190 $urlArr = array_unique( $urlArr ); // Remove duplicates 00191 foreach ( $urlArr as $url ) { 00192 if( !is_string( $url ) ) { 00193 throw new MWException( 'Bad purge URL' ); 00194 } 00195 $url = SquidUpdate::expand( $url ); 00196 $conf = self::getRuleForURL( $url, $wgHTCPMulticastRouting ); 00197 if ( !$conf ) { 00198 wfDebug( "No HTCP rule configured for URL $url , skipping\n" ); 00199 continue; 00200 } 00201 if ( !isset( $conf['host'] ) || !isset( $conf['port'] ) ) { 00202 throw new MWException( "Invalid HTCP rule for URL $url\n" ); 00203 } 00204 00205 // Construct a minimal HTCP request diagram 00206 // as per RFC 2756 00207 // Opcode 'CLR', no response desired, no auth 00208 $htcpTransID = rand(); 00209 00210 $htcpSpecifier = pack( 'na4na*na8n', 00211 4, 'HEAD', strlen( $url ), $url, 00212 8, 'HTTP/1.0', 0 ); 00213 00214 $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier ); 00215 $htcpLen = 4 + $htcpDataLen + 2; 00216 00217 // Note! Squid gets the bit order of the first 00218 // word wrong, wrt the RFC. Apparently no other 00219 // implementation exists, so adapt to Squid 00220 $htcpPacket = pack( 'nxxnCxNxxa*n', 00221 $htcpLen, $htcpDataLen, $htcpOpCLR, 00222 $htcpTransID, $htcpSpecifier, 2); 00223 00224 // Send out 00225 wfDebug( "Purging URL $url via HTCP\n" ); 00226 socket_sendto( $conn, $htcpPacket, $htcpLen, 0, 00227 $conf['host'], $conf['port'] ); 00228 } 00229 } else { 00230 $errstr = socket_strerror( socket_last_error() ); 00231 wfDebug( __METHOD__ . "(): Error opening UDP socket: $errstr\n" ); 00232 } 00233 wfProfileOut( __METHOD__ ); 00234 } 00235 00251 static function expand( $url ) { 00252 return wfExpandUrl( $url, PROTO_INTERNAL ); 00253 } 00254 00261 static function getRuleForURL( $url, $rules ) { 00262 foreach ( $rules as $regex => $routing ) { 00263 if ( $regex === '' || preg_match( $regex, $url ) ) { 00264 return $routing; 00265 } 00266 } 00267 return false; 00268 } 00269 }