9 * Handles purging appropriate Squid URLs given a title (or titles)
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 );
56 * Create a SquidUpdate from an array of Title objects, or a TitleArray object
58 static function newFromTitles( $titles, $urlArr = array() ) {
59 global $wgMaxSquidPurgeTitles;
61 foreach ( $titles as $title ) {
62 $urlArr[] = $title->getInternalURL();
63 if ( $i++
> $wgMaxSquidPurgeTitles ) {
67 return new SquidUpdate( $urlArr );
70 static function newSimplePurge( &$title ) {
71 $urlArr = $title->getSquidURLs();
72 return new SquidUpdate( $urlArr );
76 SquidUpdate
::purge( $this->urlArr
);
79 /* Purges a list of Squids defined in $wgSquidServers.
80 $urlArr should contain the full URLs to purge as values
81 (example: $urlArr[] = 'http://my.host/something')
82 XXX report broken Squids per mail or log */
84 static function purge( $urlArr ) {
85 global $wgSquidServers, $wgHTCPMulticastAddress, $wgHTCPPort;
87 /*if ( (@$wgSquidServers[0]) == 'echo' ) {
88 echo implode("<br />\n", $urlArr) . "<br />\n";
92 if( empty( $urlArr ) ) {
96 if ( $wgHTCPMulticastAddress && $wgHTCPPort ) {
97 return SquidUpdate
::HTCPPurge( $urlArr );
100 $fname = 'SquidUpdate::purge';
101 wfProfileIn( $fname );
103 $maxsocketspersquid = 8; // socket cap per Squid
104 $urlspersocket = 400; // 400 seems to be a good tradeoff, opening a socket takes a while
105 $firsturl = SquidUpdate
::expand( $urlArr[0] );
107 $urlArr = array_values($urlArr);
108 $sockspersq = max(ceil(count($urlArr) / $urlspersocket ),1);
109 if ($sockspersq == 1) {
110 /* the most common case */
111 $urlspersocket = count($urlArr);
112 } else if ($sockspersq > $maxsocketspersquid ) {
113 $urlspersocket = ceil(count($urlArr) / $maxsocketspersquid);
114 $sockspersq = $maxsocketspersquid;
116 $totalsockets = count($wgSquidServers) * $sockspersq;
119 /* this sets up the sockets and tests the first socket for each server. */
120 for ($ss=0;$ss < count($wgSquidServers);$ss++
) {
123 while ($so < $sockspersq && !$failed) {
125 /* first socket for this server, do the tests */
126 @list
($server, $port) = explode(':', $wgSquidServers[$ss]);
127 if(!isset($port)) $port = 80;
128 #$this->debug("Opening socket to $server:$port");
129 $error = $errstr = false;
130 $socket = @fsockopen
($server, $port, $error, $errstr, 3);
134 $totalsockets -= $sockspersq;
136 $msg = 'PURGE ' . $firsturl . " HTTP/1.0\r\n".
137 "Connection: Keep-Alive\r\n\r\n";
139 @fputs
($socket,$msg);
140 #$this->debug("...");
141 $res = @fread
($socket,512);
143 /* Squid only returns http headers with 200 or 404 status,
144 if there's more returned something's wrong */
145 if (strlen($res) > 250) {
148 $totalsockets -= $sockspersq;
150 @stream_set_blocking
($socket,false);
151 $sockets[] = $socket;
155 /* open the remaining sockets for this server */
156 list($server, $port) = explode(':', $wgSquidServers[$ss]);
157 if(!isset($port)) $port = 80;
158 $socket = @fsockopen
($server, $port, $error, $errstr, 2);
159 @stream_set_blocking
($socket,false);
160 $sockets[] = $socket;
166 if ($urlspersocket > 0) {
167 /* now do the heavy lifting. The fread() relies on Squid returning only the headers */
168 for ($r=0;$r < $urlspersocket;$r++
) {
169 for ($s=0;$s < $totalsockets;$s++
) {
173 while (strlen($res) < 100 && $esc < 200 ) {
174 $res .= @fread
($sockets[$s],512);
179 $urindex = $r +
$urlspersocket * ($s - $sockspersq * floor($s / $sockspersq));
180 $url = SquidUpdate
::expand( $urlArr[$urindex] );
181 $msg = 'PURGE ' . $url . " HTTP/1.0\r\n".
182 "Connection: Keep-Alive\r\n\r\n";
184 @fputs
($sockets[$s],$msg);
189 #$this->debug("Reading response...");
190 foreach ($sockets as $socket) {
193 while (strlen($res) < 100 && $esc < 200 ) {
194 $res .= @fread
($socket,1024);
202 wfProfileOut( $fname );
205 static function HTCPPurge( $urlArr ) {
206 global $wgHTCPMulticastAddress, $wgHTCPMulticastTTL, $wgHTCPPort;
207 $fname = 'SquidUpdate::HTCPPurge';
208 wfProfileIn( $fname );
210 $htcpOpCLR = 4; // HTCP CLR
212 // FIXME PHP doesn't support these socket constants (include/linux/in.h)
213 if( !defined( "IPPROTO_IP" ) ) {
214 define( "IPPROTO_IP", 0 );
215 define( "IP_MULTICAST_LOOP", 34 );
216 define( "IP_MULTICAST_TTL", 33 );
219 // pfsockopen doesn't work because we need set_sock_opt
220 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
222 // Set socket options
223 socket_set_option( $conn, IPPROTO_IP
, IP_MULTICAST_LOOP
, 0 );
224 if ( $wgHTCPMulticastTTL != 1 )
225 socket_set_option( $conn, IPPROTO_IP
, IP_MULTICAST_TTL
,
226 $wgHTCPMulticastTTL );
228 foreach ( $urlArr as $url ) {
229 if( !is_string( $url ) ) {
230 throw new MWException( 'Bad purge URL' );
232 $url = SquidUpdate
::expand( $url );
234 // Construct a minimal HTCP request diagram
236 // Opcode 'CLR', no response desired, no auth
237 $htcpTransID = rand();
239 $htcpSpecifier = pack( 'na4na*na8n',
240 4, 'HEAD', strlen( $url ), $url,
243 $htcpDataLen = 8 +
2 +
strlen( $htcpSpecifier );
244 $htcpLen = 4 +
$htcpDataLen +
2;
246 // Note! Squid gets the bit order of the first
247 // word wrong, wrt the RFC. Apparently no other
248 // implementation exists, so adapt to Squid
249 $htcpPacket = pack( 'nxxnCxNxxa*n',
250 $htcpLen, $htcpDataLen, $htcpOpCLR,
251 $htcpTransID, $htcpSpecifier, 2);
254 wfDebug( "Purging URL $url via HTCP\n" );
255 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
256 $wgHTCPMulticastAddress, $wgHTCPPort );
259 $errstr = socket_strerror( socket_last_error() );
260 wfDebug( "SquidUpdate::HTCPPurge(): Error opening UDP socket: $errstr\n" );
262 wfProfileOut( $fname );
265 function debug( $text ) {
266 global $wgDebugSquid;
267 if ( $wgDebugSquid ) {
273 * Expand local URLs to fully-qualified URLs using the internal protocol
274 * and host defined in $wgInternalServer. Input that's already fully-
275 * qualified will be passed through unchanged.
277 * This is used to generate purge URLs that may be either local to the
278 * main wiki or include a non-native host, such as images hosted on a
279 * second internal server.
281 * Client functions should not need to call this.
285 static function expand( $url ) {
286 global $wgInternalServer;
287 if( $url != '' && $url{0} == '/' ) {
288 return $wgInternalServer . $url;