13 var $urlArr, $mMaxTitles;
15 function __construct( $urlArr = Array(), $maxTitles = false ) {
16 global $wgMaxSquidPurgeTitles;
17 if ( $maxTitles === false ) {
18 $this->mMaxTitles
= $wgMaxSquidPurgeTitles;
20 $this->mMaxTitles
= $maxTitles;
22 if ( count( $urlArr ) > $this->mMaxTitles
) {
23 $urlArr = array_slice( $urlArr, 0, $this->mMaxTitles
);
25 $this->urlArr
= $urlArr;
28 static function newFromLinksTo( &$title ) {
29 $fname = 'SquidUpdate::newFromLinksTo';
30 wfProfileIn( $fname );
32 # Get a list of URLs linking to this page
33 $dbr = wfGetDB( DB_SLAVE
);
34 $res = $dbr->select( array( 'links', 'page' ),
35 array( 'page_namespace', 'page_title' ),
37 'pl_namespace' => $title->getNamespace(),
38 'pl_title' => $title->getDBkey(),
41 $blurlArr = $title->getSquidURLs();
42 if ( $dbr->numRows( $res ) <= $this->mMaxTitles
) {
43 while ( $BL = $dbr->fetchObject ( $res ) )
45 $tobj = Title
::makeTitle( $BL->page_namespace
, $BL->page_title
) ;
46 $blurlArr[] = $tobj->getInternalURL();
49 $dbr->freeResult ( $res ) ;
51 wfProfileOut( $fname );
52 return new SquidUpdate( $blurlArr );
55 static function newFromTitles( &$titles, $urlArr = array() ) {
56 global $wgMaxSquidPurgeTitles;
57 if ( count( $titles ) > $wgMaxSquidPurgeTitles ) {
58 $titles = array_slice( $titles, 0, $wgMaxSquidPurgeTitles );
60 foreach ( $titles as $title ) {
61 $urlArr[] = $title->getInternalURL();
63 return new SquidUpdate( $urlArr );
66 static function newSimplePurge( &$title ) {
67 $urlArr = $title->getSquidURLs();
68 return new SquidUpdate( $urlArr );
72 SquidUpdate
::purge( $this->urlArr
);
75 /* Purges a list of Squids defined in $wgSquidServers.
76 $urlArr should contain the full URLs to purge as values
77 (example: $urlArr[] = 'http://my.host/something')
78 XXX report broken Squids per mail or log */
80 static function purge( $urlArr ) {
81 global $wgSquidServers, $wgHTCPMulticastAddress, $wgHTCPPort;
83 /*if ( (@$wgSquidServers[0]) == 'echo' ) {
84 echo implode("<br />\n", $urlArr) . "<br />\n";
88 if( empty( $urlArr ) ) {
92 if ( $wgHTCPMulticastAddress && $wgHTCPPort ) {
93 return SquidUpdate
::HTCPPurge( $urlArr );
96 $fname = 'SquidUpdate::purge';
97 wfProfileIn( $fname );
99 $maxsocketspersquid = 8; // socket cap per Squid
100 $urlspersocket = 400; // 400 seems to be a good tradeoff, opening a socket takes a while
101 $firsturl = SquidUpdate
::expand( $urlArr[0] );
103 $urlArr = array_values($urlArr);
104 $sockspersq = max(ceil(count($urlArr) / $urlspersocket ),1);
105 if ($sockspersq == 1) {
106 /* the most common case */
107 $urlspersocket = count($urlArr);
108 } else if ($sockspersq > $maxsocketspersquid ) {
109 $urlspersocket = ceil(count($urlArr) / $maxsocketspersquid);
110 $sockspersq = $maxsocketspersquid;
112 $totalsockets = count($wgSquidServers) * $sockspersq;
115 /* this sets up the sockets and tests the first socket for each server. */
116 for ($ss=0;$ss < count($wgSquidServers);$ss++
) {
119 while ($so < $sockspersq && !$failed) {
121 /* first socket for this server, do the tests */
122 @list
($server, $port) = explode(':', $wgSquidServers[$ss]);
123 if(!isset($port)) $port = 80;
124 #$this->debug("Opening socket to $server:$port");
125 $error = $errstr = false;
126 $socket = @fsockopen
($server, $port, $error, $errstr, 3);
130 $totalsockets -= $sockspersq;
132 $msg = 'PURGE ' . $firsturl . " HTTP/1.0\r\n".
133 "Connection: Keep-Alive\r\n\r\n";
135 @fputs
($socket,$msg);
136 #$this->debug("...");
137 $res = @fread
($socket,512);
139 /* Squid only returns http headers with 200 or 404 status,
140 if there's more returned something's wrong */
141 if (strlen($res) > 250) {
144 $totalsockets -= $sockspersq;
146 @stream_set_blocking
($socket,false);
147 $sockets[] = $socket;
151 /* open the remaining sockets for this server */
152 list($server, $port) = explode(':', $wgSquidServers[$ss]);
153 if(!isset($port)) $port = 80;
154 $socket = @fsockopen
($server, $port, $error, $errstr, 2);
155 @stream_set_blocking
($socket,false);
156 $sockets[] = $socket;
162 if ($urlspersocket > 0) {
163 /* now do the heavy lifting. The fread() relies on Squid returning only the headers */
164 for ($r=0;$r < $urlspersocket;$r++
) {
165 for ($s=0;$s < $totalsockets;$s++
) {
169 while (strlen($res) < 100 && $esc < 200 ) {
170 $res .= @fread
($sockets[$s],512);
175 $urindex = $r +
$urlspersocket * ($s - $sockspersq * floor($s / $sockspersq));
176 $url = SquidUpdate
::expand( $urlArr[$urindex] );
177 $msg = 'PURGE ' . $url . " HTTP/1.0\r\n".
178 "Connection: Keep-Alive\r\n\r\n";
180 @fputs
($sockets[$s],$msg);
185 #$this->debug("Reading response...");
186 foreach ($sockets as $socket) {
189 while (strlen($res) < 100 && $esc < 200 ) {
190 $res .= @fread
($socket,1024);
198 wfProfileOut( $fname );
201 static function HTCPPurge( $urlArr ) {
202 global $wgHTCPMulticastAddress, $wgHTCPMulticastTTL, $wgHTCPPort;
203 $fname = 'SquidUpdate::HTCPPurge';
204 wfProfileIn( $fname );
206 $htcpOpCLR = 4; // HTCP CLR
208 // FIXME PHP doesn't support these socket constants (include/linux/in.h)
209 if( !defined( "IPPROTO_IP" ) ) {
210 define( "IPPROTO_IP", 0 );
211 define( "IP_MULTICAST_LOOP", 34 );
212 define( "IP_MULTICAST_TTL", 33 );
215 // pfsockopen doesn't work because we need set_sock_opt
216 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
218 // Set socket options
219 socket_set_option( $conn, IPPROTO_IP
, IP_MULTICAST_LOOP
, 0 );
220 if ( $wgHTCPMulticastTTL != 1 )
221 socket_set_option( $conn, IPPROTO_IP
, IP_MULTICAST_TTL
,
222 $wgHTCPMulticastTTL );
224 foreach ( $urlArr as $url ) {
225 if( !is_string( $url ) ) {
226 throw new MWException( 'Bad purge URL' );
228 $url = SquidUpdate
::expand( $url );
230 // Construct a minimal HTCP request diagram
232 // Opcode 'CLR', no response desired, no auth
233 $htcpTransID = rand();
235 $htcpSpecifier = pack( 'na4na*na8n',
236 4, 'HEAD', strlen( $url ), $url,
239 $htcpDataLen = 8 +
2 +
strlen( $htcpSpecifier );
240 $htcpLen = 4 +
$htcpDataLen +
2;
242 // Note! Squid gets the bit order of the first
243 // word wrong, wrt the RFC. Apparently no other
244 // implementation exists, so adapt to Squid
245 $htcpPacket = pack( 'nxxnCxNxxa*n',
246 $htcpLen, $htcpDataLen, $htcpOpCLR,
247 $htcpTransID, $htcpSpecifier, 2);
250 wfDebug( "Purging URL $url via HTCP\n" );
251 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
252 $wgHTCPMulticastAddress, $wgHTCPPort );
255 $errstr = socket_strerror( socket_last_error() );
256 wfDebug( "SquidUpdate::HTCPPurge(): Error opening UDP socket: $errstr\n" );
258 wfProfileOut( $fname );
261 function debug( $text ) {
262 global $wgDebugSquid;
263 if ( $wgDebugSquid ) {
269 * Expand local URLs to fully-qualified URLs using the internal protocol
270 * and host defined in $wgInternalServer. Input that's already fully-
271 * qualified will be passed through unchanged.
273 * This is used to generate purge URLs that may be either local to the
274 * main wiki or include a non-native host, such as images hosted on a
275 * second internal server.
277 * Client functions should not need to call this.
281 static function expand( $url ) {
282 global $wgInternalServer;
283 if( $url != '' && $url{0} == '/' ) {
284 return $wgInternalServer . $url;