10 var $urlArr, $mMaxTitles;
12 function __construct( $urlArr = Array(), $maxTitles = false ) {
13 global $wgMaxSquidPurgeTitles;
14 if ( $maxTitles === false ) {
15 $this->mMaxTitles
= $wgMaxSquidPurgeTitles;
17 $this->mMaxTitles
= $maxTitles;
19 if ( count( $urlArr ) > $this->mMaxTitles
) {
20 $urlArr = array_slice( $urlArr, 0, $this->mMaxTitles
);
22 $this->urlArr
= $urlArr;
25 static function newFromLinksTo( &$title ) {
26 $fname = 'SquidUpdate::newFromLinksTo';
27 wfProfileIn( $fname );
29 # Get a list of URLs linking to this page
30 $dbr = wfGetDB( DB_SLAVE
);
31 $res = $dbr->select( array( 'links', 'page' ),
32 array( 'page_namespace', 'page_title' ),
34 'pl_namespace' => $title->getNamespace(),
35 'pl_title' => $title->getDbKey(),
38 $blurlArr = $title->getSquidURLs();
39 if ( $dbr->numRows( $res ) <= $this->mMaxTitles
) {
40 while ( $BL = $dbr->fetchObject ( $res ) )
42 $tobj = Title
::makeTitle( $BL->page_namespace
, $BL->page_title
) ;
43 $blurlArr[] = $tobj->getInternalURL();
46 $dbr->freeResult ( $res ) ;
48 wfProfileOut( $fname );
49 return new SquidUpdate( $blurlArr );
52 static function newFromTitles( &$titles, $urlArr = array() ) {
53 global $wgMaxSquidPurgeTitles;
54 if ( count( $titles ) > $wgMaxSquidPurgeTitles ) {
55 $titles = array_slice( $titles, 0, $wgMaxSquidPurgeTitles );
57 foreach ( $titles as $title ) {
58 $urlArr[] = $title->getInternalURL();
60 return new SquidUpdate( $urlArr );
63 static function newSimplePurge( &$title ) {
64 $urlArr = $title->getSquidURLs();
65 return new SquidUpdate( $urlArr );
69 SquidUpdate
::purge( $this->urlArr
);
72 /* Purges a list of Squids defined in $wgSquidServers.
73 $urlArr should contain the full URLs to purge as values
74 (example: $urlArr[] = 'http://my.host/something')
75 XXX report broken Squids per mail or log */
77 static function purge( $urlArr ) {
78 global $wgSquidServers, $wgHTCPMulticastAddress, $wgHTCPPort;
80 /*if ( (@$wgSquidServers[0]) == 'echo' ) {
81 echo implode("<br />\n", $urlArr) . "<br />\n";
85 if ( $wgHTCPMulticastAddress && $wgHTCPPort )
86 SquidUpdate
::HTCPPurge( $urlArr );
88 $fname = 'SquidUpdate::purge';
89 wfProfileIn( $fname );
91 $maxsocketspersquid = 8; // socket cap per Squid
92 $urlspersocket = 400; // 400 seems to be a good tradeoff, opening a socket takes a while
93 $firsturl = SquidUpdate
::expand( $urlArr[0] );
95 $urlArr = array_values($urlArr);
96 $sockspersq = max(ceil(count($urlArr) / $urlspersocket ),1);
97 if ($sockspersq == 1) {
98 /* the most common case */
99 $urlspersocket = count($urlArr);
100 } else if ($sockspersq > $maxsocketspersquid ) {
101 $urlspersocket = ceil(count($urlArr) / $maxsocketspersquid);
102 $sockspersq = $maxsocketspersquid;
104 $totalsockets = count($wgSquidServers) * $sockspersq;
107 /* this sets up the sockets and tests the first socket for each server. */
108 for ($ss=0;$ss < count($wgSquidServers);$ss++
) {
111 while ($so < $sockspersq && !$failed) {
113 /* first socket for this server, do the tests */
114 @list
($server, $port) = explode(':', $wgSquidServers[$ss]);
115 if(!isset($port)) $port = 80;
116 #$this->debug("Opening socket to $server:$port");
117 $error = $errstr = false;
118 $socket = @fsockopen
($server, $port, $error, $errstr, 3);
122 $totalsockets -= $sockspersq;
124 $msg = 'PURGE ' . $firsturl . " HTTP/1.0\r\n".
125 "Connection: Keep-Alive\r\n\r\n";
127 @fputs
($socket,$msg);
128 #$this->debug("...");
129 $res = @fread
($socket,512);
131 /* Squid only returns http headers with 200 or 404 status,
132 if there's more returned something's wrong */
133 if (strlen($res) > 250) {
136 $totalsockets -= $sockspersq;
138 @stream_set_blocking
($socket,false);
139 $sockets[] = $socket;
143 /* open the remaining sockets for this server */
144 list($server, $port) = explode(':', $wgSquidServers[$ss]);
145 if(!isset($port)) $port = 80;
146 $sockets[$so+
1] = @fsockopen
($server, $port, $error, $errstr, 2);
147 @stream_set_blocking
($sockets[$so+
1],false);
153 if ($urlspersocket > 0) {
154 /* now do the heavy lifting. The fread() relies on Squid returning only the headers */
155 for ($r=0;$r < $urlspersocket;$r++
) {
156 for ($s=0;$s < $totalsockets;$s++
) {
160 while (strlen($res) < 100 && $esc < 200 ) {
161 $res .= @fread
($sockets[$s],512);
166 $urindex = $r +
$urlspersocket * ($s - $sockspersq * floor($s / $sockspersq));
167 $url = SquidUpdate
::expand( $urlArr[$urindex] );
168 $msg = 'PURGE ' . $url . " HTTP/1.0\r\n".
169 "Connection: Keep-Alive\r\n\r\n";
171 @fputs
($sockets[$s],$msg);
176 #$this->debug("Reading response...");
177 foreach ($sockets as $socket) {
180 while (strlen($res) < 100 && $esc < 200 ) {
181 $res .= @fread
($socket,1024);
189 wfProfileOut( $fname );
192 static function HTCPPurge( $urlArr ) {
193 global $wgHTCPMulticastAddress, $wgHTCPMulticastTTL, $wgHTCPPort;
194 $fname = 'SquidUpdate::HTCPPurge';
195 wfProfileIn( $fname );
197 $htcpOpCLR = 4; // HTCP CLR
199 // FIXME PHP doesn't support these socket constants (include/linux/in.h)
200 if( !defined( "IPPROTO_IP" ) ) {
201 define( "IPPROTO_IP", 0 );
202 define( "IP_MULTICAST_LOOP", 34 );
203 define( "IP_MULTICAST_TTL", 33 );
206 // pfsockopen doesn't work because we need set_sock_opt
207 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
209 // Set socket options
210 socket_set_option( $conn, IPPROTO_IP
, IP_MULTICAST_LOOP
, 0 );
211 if ( $wgHTCPMulticastTTL != 1 )
212 socket_set_option( $conn, IPPROTO_IP
, IP_MULTICAST_TTL
,
213 $wgHTCPMulticastTTL );
215 foreach ( $urlArr as $url ) {
216 if( !is_string( $url ) ) {
217 wfDebugDieBacktrace( 'Bad purge URL' );
219 $url = SquidUpdate
::expand( $url );
221 // Construct a minimal HTCP request diagram
223 // Opcode 'CLR', no response desired, no auth
224 $htcpTransID = rand();
226 $htcpSpecifier = pack( 'na4na*na8n',
227 4, 'HEAD', strlen( $url ), $url,
230 $htcpDataLen = 8 +
2 +
strlen( $htcpSpecifier );
231 $htcpLen = 4 +
$htcpDataLen +
2;
233 // Note! Squid gets the bit order of the first
234 // word wrong, wrt the RFC. Apparently no other
235 // implementation exists, so adapt to Squid
236 $htcpPacket = pack( 'nxxnCxNxxa*n',
237 $htcpLen, $htcpDataLen, $htcpOpCLR,
238 $htcpTransID, $htcpSpecifier, 2);
241 wfDebug( "Purging URL $url via HTCP\n" );
242 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
243 $wgHTCPMulticastAddress, $wgHTCPPort );
246 $errstr = socket_strerror( socket_last_error() );
247 wfDebug( "SquidUpdate::HTCPPurge(): Error opening UDP socket: $errstr\n" );
249 wfProfileOut( $fname );
252 function debug( $text ) {
253 global $wgDebugSquid;
254 if ( $wgDebugSquid ) {
260 * Expand local URLs to fully-qualified URLs using the internal protocol
261 * and host defined in $wgInternalServer. Input that's already fully-
262 * qualified will be passed through unchanged.
264 * This is used to generate purge URLs that may be either local to the
265 * main wiki or include a non-native host, such as images hosted on a
266 * second internal server.
268 * Client functions should not need to call this.
272 static function expand( $url ) {
273 global $wgInternalServer;
274 if( $url != '' && $url{0} == '/' ) {
275 return $wgInternalServer . $url;