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 $links = $dbr->tableName( 'links' );
36 $page = $dbr->tableName( 'page' );
38 $sql = "SELECT page_namespace,page_title FROM $links,$page WHERE l_to={$id} and l_from=page_id" ;
39 $res = $dbr->query( $sql, $fname ) ;
40 $blurlArr = $title->getSquidURLs();
41 if ( $dbr->numRows( $res ) <= $this->mMaxTitles
) {
42 while ( $BL = $dbr->fetchObject ( $res ) )
44 $tobj = Title
::makeTitle( $BL->page_namespace
, $BL->page_title
) ;
45 $blurlArr[] = $tobj->getInternalURL();
48 $dbr->freeResult ( $res ) ;
50 wfProfileOut( $fname );
51 return new SquidUpdate( $blurlArr );
54 /* static */ function newFromBrokenLinksTo( &$title ) {
55 $fname = 'SquidUpdate::newFromBrokenLinksTo';
56 wfProfileIn( $fname );
58 # Get a list of URLs linking to this (currently non-existent) page
59 $dbr =& wfGetDB( DB_SLAVE
);
60 $brokenlinks = $dbr->tableName( 'brokenlinks' );
61 $page = $dbr->tableName( 'page' );
62 $encTitle = $dbr->addQuotes( $title->getPrefixedDBkey() );
64 $sql = "SELECT page_namespace,page_title FROM $brokenlinks,$cur WHERE bl_to={$encTitle} AND bl_from=page_id";
65 $res = $dbr->query( $sql, $fname );
67 if ( $dbr->numRows( $res ) <= $this->mMaxTitles
) {
68 while ( $BL = $dbr->fetchObject( $res ) )
70 $tobj = Title
::makeTitle( $BL->page_namespace
, $BL->page_title
);
71 $blurlArr[] = $tobj->getInternalURL();
74 $dbr->freeResult( $res );
75 wfProfileOut( $fname );
76 return new SquidUpdate( $blurlArr );
79 /* static */ function newSimplePurge( &$title ) {
80 $urlArr = $title->getSquidURLs();
81 return new SquidUpdate( $blurlArr );
85 SquidUpdate
::purge( $this->urlArr
);
88 /* Purges a list of Squids defined in $wgSquidServers.
89 $urlArr should contain the full URLs to purge as values
90 (example: $urlArr[] = 'http://my.host/something')
91 XXX report broken Squids per mail or log */
93 /* static */ function purge( $urlArr ) {
94 global $wgSquidServers, $wgHTCPMulticastAddress, $wgHTCPPort;
96 if ( $wgSquidServers == 'echo' ) {
97 echo implode("<br />\n", $urlArr);
101 if ( $wgHTCPMulticastAddress && $wgHTCPPort )
102 SquidUpdate
::HTCPPurge( $urlArr );
104 $fname = 'SquidUpdate::purge';
105 wfProfileIn( $fname );
107 $maxsocketspersquid = 8; // socket cap per Squid
108 $urlspersocket = 400; // 400 seems to be a good tradeoff, opening a socket takes a while
109 $firsturl = $urlArr[0];
111 $urlArr = array_values($urlArr);
112 $sockspersq = max(ceil(count($urlArr) / $urlspersocket ),1);
113 if ($sockspersq == 1) {
114 /* the most common case */
115 $urlspersocket = count($urlArr);
116 } else if ($sockspersq > $maxsocketspersquid ) {
117 $urlspersocket = ceil(count($urlArr) / $maxsocketspersquid);
118 $sockspersq = $maxsocketspersquid;
120 $totalsockets = count($wgSquidServers) * $sockspersq;
123 /* this sets up the sockets and tests the first socket for each server. */
124 for ($ss=0;$ss < count($wgSquidServers);$ss++
) {
127 while ($so < $sockspersq && !$failed) {
129 /* first socket for this server, do the tests */
130 @list
($server, $port) = explode(':', $wgSquidServers[$ss]);
131 if(!isset($port)) $port = 80;
132 #$this->debug("Opening socket to $server:$port");
133 $socket = @fsockopen
($server, $port, $error, $errstr, 3);
137 $totalsockets -= $sockspersq;
139 $msg = 'PURGE ' . $firsturl . " HTTP/1.0\r\n".
140 "Connection: Keep-Alive\r\n\r\n";
142 @fputs
($socket,$msg);
143 #$this->debug("...");
144 $res = @fread
($socket,512);
146 /* Squid only returns http headers with 200 or 404 status,
147 if there's more returned something's wrong */
148 if (strlen($res) > 250) {
151 $totalsockets -= $sockspersq;
153 @stream_set_blocking
($socket,false);
154 $sockets[] = $socket;
158 /* open the remaining sockets for this server */
159 list($server, $port) = explode(':', $wgSquidServers[$ss]);
160 if(!isset($port)) $port = 80;
161 $sockets[] = @fsockopen
($server, $port, $error, $errstr, 2);
162 @stream_set_blocking
($sockets[$s],false);
168 if ($urlspersocket > 0) {
169 /* now do the heavy lifting. The fread() relies on Squid returning only the headers */
170 for ($r=0;$r < $urlspersocket;$r++
) {
171 for ($s=0;$s < $totalsockets;$s++
) {
175 while (strlen($res) < 100 && $esc < 200 ) {
176 $res .= @fread
($sockets[$s],512);
181 $urindex = $r +
$urlspersocket * ($s - $sockspersq * floor($s / $sockspersq));
182 $msg = 'PURGE ' . $urlArr[$urindex] . " HTTP/1.0\r\n".
183 "Connection: Keep-Alive\r\n\r\n";
185 @fputs
($sockets[$s],$msg);
190 #$this->debug("Reading response...");
191 foreach ($sockets as $socket) {
194 while (strlen($res) < 100 && $esc < 200 ) {
195 $res .= @fread
($socket,1024);
203 wfProfileOut( $fname );
206 /* static */ function HTCPPurge( $urlArr ) {
207 global $wgHTCPMulticastAddress, $wgHTCPMulticastTTL, $wgHTCPPort;
208 $fname = 'SquidUpdate::HTCPPurge';
209 wfProfileIn( $fname );
211 $htcpOpCLR = 4; // HTCP CLR
213 // FIXME PHP doesn't support these socket constants (include/linux/in.h)
214 define( "IPPROTO_IP", 0 );
215 define( "IP_MULTICAST_LOOP", 34 );
216 define( "IP_MULTICAST_TTL", 33 );
218 // pfsockopen doesn't work because we need set_sock_opt
219 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
221 // Set socket options
222 socket_set_option( $conn, IPPROTO_IP
, IP_MULTICAST_LOOP
, 0 );
223 if ( $wgHTCPMulticastTTL != 1 )
224 socket_set_option( $conn, IPPROTO_IP
, IP_MULTICAST_TTL
,
225 $wgHTCPMulticastTTL );
227 foreach ( $urlArr as $url ) {
228 // Construct a minimal HTCP request diagram
230 // Opcode 'CLR', no response desired, no auth
231 $htcpTransID = rand();
233 $htcpSpecifier = pack( 'na4na*na8n',
234 4, 'NONE', strlen( $url ), $url,
237 $htcpDataLen = 8 +
2 +
strlen( $htcpSpecifier );
238 $htcpLen = 4 +
$htcpDataLen +
2;
240 // Note! Squid gets the bit order of the first
241 // word wrong, wrt the RFC. Apparently no other
242 // implementation exists, so adapt to Squid
243 $htcpPacket = pack( 'nxxnCxNxxa*n',
244 $htcpLen, $htcpDataLen, $htcpOpCLR,
245 $htcpTransID, $htcpSpecifier, 2);
248 debug( "Purging URL $url via HTCP\n" );
249 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
250 $wgHTCPMulticastAddress, $wgHTCPPort );
253 $errstr = socket_strerror( socket_last_error() );
254 debug( "SquidUpdate::HTCPPurge(): Error opening UDP socket: $errstr\n" );
256 wfProfileOut( $fname );
259 function debug( $text ) {
260 global $wgDebugSquid;
261 if ( $wgDebugSquid ) {