680b0f418c042c54c9ff209dbfd92927e763e397
12 public $urlArr; // FIXME : is it really public ?
15 function SquidUpdate( $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 $id = $title->getArticleID();
35 $dbr =& wfGetDB( DB_SLAVE
);
36 $res = $dbr->select( array( 'links', 'page' ),
37 array( 'page_namespace', 'page_title' ),
39 'pl_namespace' => $title->getNamespace(),
40 'pl_title' => $title->getDbKey(),
43 $blurlArr = $title->getSquidURLs();
44 if ( $dbr->numRows( $res ) <= $this->mMaxTitles
) {
45 while ( $BL = $dbr->fetchObject ( $res ) )
47 $tobj = Title
::makeTitle( $BL->page_namespace
, $BL->page_title
) ;
48 $blurlArr[] = $tobj->getInternalURL();
51 $dbr->freeResult ( $res ) ;
53 wfProfileOut( $fname );
54 return new SquidUpdate( $blurlArr );
57 /* static */ function newFromTitles( &$titles, $urlArr = array() ) {
58 foreach ( $titles as $title ) {
59 $urlArr[] = $title->getInternalURL();
61 return new SquidUpdate( $urlArr );
64 /* static */ function newSimplePurge( &$title ) {
65 $urlArr = $title->getSquidURLs();
66 return new SquidUpdate( $urlArr );
70 SquidUpdate
::purge( $this->urlArr
);
73 /* Purges a list of Squids defined in $wgSquidServers.
74 $urlArr should contain the full URLs to purge as values
75 (example: $urlArr[] = 'http://my.host/something')
76 XXX report broken Squids per mail or log */
78 /* static */ function purge( $urlArr ) {
79 global $wgSquidServers, $wgHTCPMulticastAddress, $wgHTCPPort;
81 if ( $wgSquidServers == 'echo' ) {
82 echo implode("<br />\n", $urlArr);
86 if ( $wgHTCPMulticastAddress && $wgHTCPPort )
87 SquidUpdate
::HTCPPurge( $urlArr );
89 $fname = 'SquidUpdate::purge';
90 wfProfileIn( $fname );
92 $maxsocketspersquid = 8; // socket cap per Squid
93 $urlspersocket = 400; // 400 seems to be a good tradeoff, opening a socket takes a while
94 $firsturl = $urlArr[0];
96 $urlArr = array_values($urlArr);
97 $sockspersq = max(ceil(count($urlArr) / $urlspersocket ),1);
98 if ($sockspersq == 1) {
99 /* the most common case */
100 $urlspersocket = count($urlArr);
101 } else if ($sockspersq > $maxsocketspersquid ) {
102 $urlspersocket = ceil(count($urlArr) / $maxsocketspersquid);
103 $sockspersq = $maxsocketspersquid;
105 $totalsockets = count($wgSquidServers) * $sockspersq;
108 /* this sets up the sockets and tests the first socket for each server. */
109 for ($ss=0;$ss < count($wgSquidServers);$ss++
) {
112 while ($so < $sockspersq && !$failed) {
114 /* first socket for this server, do the tests */
115 @list
($server, $port) = explode(':', $wgSquidServers[$ss]);
116 if(!isset($port)) $port = 80;
117 #$this->debug("Opening socket to $server:$port");
118 $error = $errstr = false;
119 $socket = @fsockopen
($server, $port, $error, $errstr, 3);
123 $totalsockets -= $sockspersq;
125 $msg = 'PURGE ' . $firsturl . " HTTP/1.0\r\n".
126 "Connection: Keep-Alive\r\n\r\n";
128 @fputs
($socket,$msg);
129 #$this->debug("...");
130 $res = @fread
($socket,512);
132 /* Squid only returns http headers with 200 or 404 status,
133 if there's more returned something's wrong */
134 if (strlen($res) > 250) {
137 $totalsockets -= $sockspersq;
139 @stream_set_blocking
($socket,false);
140 $sockets[] = $socket;
144 /* open the remaining sockets for this server */
145 list($server, $port) = explode(':', $wgSquidServers[$ss]);
146 if(!isset($port)) $port = 80;
147 $sockets[$so+
1] = @fsockopen
($server, $port, $error, $errstr, 2);
148 @stream_set_blocking
($sockets[$so+
1],false);
154 if ($urlspersocket > 0) {
155 /* now do the heavy lifting. The fread() relies on Squid returning only the headers */
156 for ($r=0;$r < $urlspersocket;$r++
) {
157 for ($s=0;$s < $totalsockets;$s++
) {
161 while (strlen($res) < 100 && $esc < 200 ) {
162 $res .= @fread
($sockets[$s],512);
167 $urindex = $r +
$urlspersocket * ($s - $sockspersq * floor($s / $sockspersq));
168 $msg = 'PURGE ' . $urlArr[$urindex] . " 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 define( "IPPROTO_IP", 0 );
201 define( "IP_MULTICAST_LOOP", 34 );
202 define( "IP_MULTICAST_TTL", 33 );
204 // pfsockopen doesn't work because we need set_sock_opt
205 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
207 // Set socket options
208 socket_set_option( $conn, IPPROTO_IP
, IP_MULTICAST_LOOP
, 0 );
209 if ( $wgHTCPMulticastTTL != 1 )
210 socket_set_option( $conn, IPPROTO_IP
, IP_MULTICAST_TTL
,
211 $wgHTCPMulticastTTL );
213 foreach ( $urlArr as $url ) {
214 // Construct a minimal HTCP request diagram
216 // Opcode 'CLR', no response desired, no auth
217 $htcpTransID = rand();
219 $htcpSpecifier = pack( 'na4na*na8n',
220 4, 'NONE', strlen( $url ), $url,
223 $htcpDataLen = 8 +
2 +
strlen( $htcpSpecifier );
224 $htcpLen = 4 +
$htcpDataLen +
2;
226 // Note! Squid gets the bit order of the first
227 // word wrong, wrt the RFC. Apparently no other
228 // implementation exists, so adapt to Squid
229 $htcpPacket = pack( 'nxxnCxNxxa*n',
230 $htcpLen, $htcpDataLen, $htcpOpCLR,
231 $htcpTransID, $htcpSpecifier, 2);
234 wfDebug( "Purging URL $url via HTCP\n" );
235 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
236 $wgHTCPMulticastAddress, $wgHTCPPort );
239 $errstr = socket_strerror( socket_last_error() );
240 wfDebug( "SquidUpdate::HTCPPurge(): Error opening UDP socket: $errstr\n" );
242 wfProfileOut( $fname );
245 function debug( $text ) {
246 global $wgDebugSquid;
247 if ( $wgDebugSquid ) {