12 var $urlArr, $mMaxTitles;
14 function SquidUpdate( $urlArr = Array(), $maxTitles = false ) {
15 global $wgMaxSquidPurgeTitles;
16 if ( $maxTitles === false ) {
17 $this->mMaxTitles
= $wgMaxSquidPurgeTitles;
19 $this->mMaxTitles
= $maxTitles;
21 if ( count( $urlArr ) > $this->mMaxTitles
) {
22 $urlArr = array_slice( $urlArr, 0, $this->mMaxTitles
);
24 $this->urlArr
= $urlArr;
27 /* static */ function newFromLinksTo( &$title ) {
28 $fname = 'SquidUpdate::newFromLinksTo';
29 wfProfileIn( $fname );
31 # Get a list of URLs linking to this page
32 $id = $title->getArticleID();
34 $dbr =& wfGetDB( DB_SLAVE
);
35 $res = $dbr->select( array( 'links', 'page' ),
36 array( 'page_namespace', 'page_title' ),
38 'pl_namespace' => $title->getNamespace(),
39 'pl_title' => $title->getDbKey(),
42 $blurlArr = $title->getSquidURLs();
43 if ( $dbr->numRows( $res ) <= $this->mMaxTitles
) {
44 while ( $BL = $dbr->fetchObject ( $res ) )
46 $tobj = Title
::makeTitle( $BL->page_namespace
, $BL->page_title
) ;
47 $blurlArr[] = $tobj->getInternalURL();
50 $dbr->freeResult ( $res ) ;
52 wfProfileOut( $fname );
53 return new SquidUpdate( $blurlArr );
56 /* static */ function newFromTitles( &$titles, $urlArr = array() ) {
57 global $wgMaxSquidPurgeTitles;
58 if ( count( $titles ) > $wgMaxSquidPurgeTitles ) {
59 $titles = array_slice( $titles, 0, $wgMaxSquidPurgeTitles );
61 foreach ( $titles as $title ) {
62 $urlArr[] = $title->getInternalURL();
64 return new SquidUpdate( $urlArr );
67 /* static */ function newSimplePurge( &$title ) {
68 $urlArr = $title->getSquidURLs();
69 return new SquidUpdate( $urlArr );
73 SquidUpdate
::purge( $this->urlArr
);
76 /* Purges a list of Squids defined in $wgSquidServers.
77 $urlArr should contain the full URLs to purge as values
78 (example: $urlArr[] = 'http://my.host/something')
79 XXX report broken Squids per mail or log */
81 /* static */ function purge( $urlArr ) {
82 global $wgSquidServers, $wgHTCPMulticastAddress, $wgHTCPPort;
84 /*if ( (@$wgSquidServers[0]) == 'echo' ) {
85 echo implode("<br />\n", $urlArr) . "<br />\n";
89 if ( $wgHTCPMulticastAddress && $wgHTCPPort )
90 SquidUpdate
::HTCPPurge( $urlArr );
92 $fname = 'SquidUpdate::purge';
93 wfProfileIn( $fname );
95 $maxsocketspersquid = 8; // socket cap per Squid
96 $urlspersocket = 400; // 400 seems to be a good tradeoff, opening a socket takes a while
97 $firsturl = SquidUpdate
::expand( $urlArr[0] );
99 $urlArr = array_values($urlArr);
100 $sockspersq = max(ceil(count($urlArr) / $urlspersocket ),1);
101 if ($sockspersq == 1) {
102 /* the most common case */
103 $urlspersocket = count($urlArr);
104 } else if ($sockspersq > $maxsocketspersquid ) {
105 $urlspersocket = ceil(count($urlArr) / $maxsocketspersquid);
106 $sockspersq = $maxsocketspersquid;
108 $totalsockets = count($wgSquidServers) * $sockspersq;
111 /* this sets up the sockets and tests the first socket for each server. */
112 for ($ss=0;$ss < count($wgSquidServers);$ss++
) {
115 while ($so < $sockspersq && !$failed) {
117 /* first socket for this server, do the tests */
118 @list
($server, $port) = explode(':', $wgSquidServers[$ss]);
119 if(!isset($port)) $port = 80;
120 #$this->debug("Opening socket to $server:$port");
121 $error = $errstr = false;
122 $socket = @fsockopen
($server, $port, $error, $errstr, 3);
126 $totalsockets -= $sockspersq;
128 $msg = 'PURGE ' . $firsturl . " HTTP/1.0\r\n".
129 "Connection: Keep-Alive\r\n\r\n";
131 @fputs
($socket,$msg);
132 #$this->debug("...");
133 $res = @fread
($socket,512);
135 /* Squid only returns http headers with 200 or 404 status,
136 if there's more returned something's wrong */
137 if (strlen($res) > 250) {
140 $totalsockets -= $sockspersq;
142 @stream_set_blocking
($socket,false);
143 $sockets[] = $socket;
147 /* open the remaining sockets for this server */
148 list($server, $port) = explode(':', $wgSquidServers[$ss]);
149 if(!isset($port)) $port = 80;
150 $sockets[$so+
1] = @fsockopen
($server, $port, $error, $errstr, 2);
151 @stream_set_blocking
($sockets[$so+
1],false);
157 if ($urlspersocket > 0) {
158 /* now do the heavy lifting. The fread() relies on Squid returning only the headers */
159 for ($r=0;$r < $urlspersocket;$r++
) {
160 for ($s=0;$s < $totalsockets;$s++
) {
164 while (strlen($res) < 100 && $esc < 200 ) {
165 $res .= @fread
($sockets[$s],512);
170 $urindex = $r +
$urlspersocket * ($s - $sockspersq * floor($s / $sockspersq));
171 $url = SquidUpdate
::expand( $urlArr[$urindex] );
172 $msg = 'PURGE ' . $url . " HTTP/1.0\r\n".
173 "Connection: Keep-Alive\r\n\r\n";
175 @fputs
($sockets[$s],$msg);
180 #$this->debug("Reading response...");
181 foreach ($sockets as $socket) {
184 while (strlen($res) < 100 && $esc < 200 ) {
185 $res .= @fread
($socket,1024);
193 wfProfileOut( $fname );
196 /* static */ function HTCPPurge( $urlArr ) {
197 global $wgHTCPMulticastAddress, $wgHTCPMulticastTTL, $wgHTCPPort;
198 $fname = 'SquidUpdate::HTCPPurge';
199 wfProfileIn( $fname );
201 $htcpOpCLR = 4; // HTCP CLR
203 // FIXME PHP doesn't support these socket constants (include/linux/in.h)
204 define( "IPPROTO_IP", 0 );
205 define( "IP_MULTICAST_LOOP", 34 );
206 define( "IP_MULTICAST_TTL", 33 );
208 // pfsockopen doesn't work because we need set_sock_opt
209 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
211 // Set socket options
212 socket_set_option( $conn, IPPROTO_IP
, IP_MULTICAST_LOOP
, 0 );
213 if ( $wgHTCPMulticastTTL != 1 )
214 socket_set_option( $conn, IPPROTO_IP
, IP_MULTICAST_TTL
,
215 $wgHTCPMulticastTTL );
217 foreach ( $urlArr as $url ) {
218 $url = SquidUpdate
::expand( $url );
220 // Construct a minimal HTCP request diagram
222 // Opcode 'CLR', no response desired, no auth
223 $htcpTransID = rand();
225 $htcpSpecifier = pack( 'na4na*na8n',
226 4, 'NONE', strlen( $url ), $url,
229 $htcpDataLen = 8 +
2 +
strlen( $htcpSpecifier );
230 $htcpLen = 4 +
$htcpDataLen +
2;
232 // Note! Squid gets the bit order of the first
233 // word wrong, wrt the RFC. Apparently no other
234 // implementation exists, so adapt to Squid
235 $htcpPacket = pack( 'nxxnCxNxxa*n',
236 $htcpLen, $htcpDataLen, $htcpOpCLR,
237 $htcpTransID, $htcpSpecifier, 2);
240 wfDebug( "Purging URL $url via HTCP\n" );
241 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
242 $wgHTCPMulticastAddress, $wgHTCPPort );
245 $errstr = socket_strerror( socket_last_error() );
246 wfDebug( "SquidUpdate::HTCPPurge(): Error opening UDP socket: $errstr\n" );
248 wfProfileOut( $fname );
251 function debug( $text ) {
252 global $wgDebugSquid;
253 if ( $wgDebugSquid ) {
259 * Expand local URLs to fully-qualified URLs using the internal protocol
260 * and host defined in $wgInternalServer. Input that's already fully-
261 * qualified will be passed through unchanged.
263 * This is used to generate purge URLs that may be either local to the
264 * main wiki or include a non-native host, such as images hosted on a
265 * second internal server.
267 * Client functions should not need to call this.
271 static function expand( $url ) {
272 global $wgInternalServer;
273 if( $url != '' && $url{0} == '/' ) {
274 return $wgInternalServer . $url;