E_STRICT fixlets: properly mark some static methods as static
[lhc/web/wiklou.git] / includes / SquidUpdate.php
1 <?php
2 /**
3 * See deferred.txt
4 */
5
6 /**
7 *
8 */
9 class SquidUpdate {
10 var $urlArr, $mMaxTitles;
11
12 function __construct( $urlArr = Array(), $maxTitles = false ) {
13 global $wgMaxSquidPurgeTitles;
14 if ( $maxTitles === false ) {
15 $this->mMaxTitles = $wgMaxSquidPurgeTitles;
16 } else {
17 $this->mMaxTitles = $maxTitles;
18 }
19 if ( count( $urlArr ) > $this->mMaxTitles ) {
20 $urlArr = array_slice( $urlArr, 0, $this->mMaxTitles );
21 }
22 $this->urlArr = $urlArr;
23 }
24
25 static function newFromLinksTo( &$title ) {
26 $fname = 'SquidUpdate::newFromLinksTo';
27 wfProfileIn( $fname );
28
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' ),
33 array(
34 'pl_namespace' => $title->getNamespace(),
35 'pl_title' => $title->getDbKey(),
36 'pl_from=page_id' ),
37 $fname );
38 $blurlArr = $title->getSquidURLs();
39 if ( $dbr->numRows( $res ) <= $this->mMaxTitles ) {
40 while ( $BL = $dbr->fetchObject ( $res ) )
41 {
42 $tobj = Title::makeTitle( $BL->page_namespace, $BL->page_title ) ;
43 $blurlArr[] = $tobj->getInternalURL();
44 }
45 }
46 $dbr->freeResult ( $res ) ;
47
48 wfProfileOut( $fname );
49 return new SquidUpdate( $blurlArr );
50 }
51
52 static function newFromTitles( &$titles, $urlArr = array() ) {
53 global $wgMaxSquidPurgeTitles;
54 if ( count( $titles ) > $wgMaxSquidPurgeTitles ) {
55 $titles = array_slice( $titles, 0, $wgMaxSquidPurgeTitles );
56 }
57 foreach ( $titles as $title ) {
58 $urlArr[] = $title->getInternalURL();
59 }
60 return new SquidUpdate( $urlArr );
61 }
62
63 static function newSimplePurge( &$title ) {
64 $urlArr = $title->getSquidURLs();
65 return new SquidUpdate( $urlArr );
66 }
67
68 function doUpdate() {
69 SquidUpdate::purge( $this->urlArr );
70 }
71
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 */
76
77 static function purge( $urlArr ) {
78 global $wgSquidServers, $wgHTCPMulticastAddress, $wgHTCPPort;
79
80 /*if ( (@$wgSquidServers[0]) == 'echo' ) {
81 echo implode("<br />\n", $urlArr) . "<br />\n";
82 return;
83 }*/
84
85 if ( $wgHTCPMulticastAddress && $wgHTCPPort )
86 SquidUpdate::HTCPPurge( $urlArr );
87
88 $fname = 'SquidUpdate::purge';
89 wfProfileIn( $fname );
90
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] );
94 unset($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;
103 }
104 $totalsockets = count($wgSquidServers) * $sockspersq;
105 $sockets = Array();
106
107 /* this sets up the sockets and tests the first socket for each server. */
108 for ($ss=0;$ss < count($wgSquidServers);$ss++) {
109 $failed = false;
110 $so = 0;
111 while ($so < $sockspersq && !$failed) {
112 if ($so == 0) {
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);
119 #$this->debug("\n");
120 if (!$socket) {
121 $failed = true;
122 $totalsockets -= $sockspersq;
123 } else {
124 $msg = 'PURGE ' . $firsturl . " HTTP/1.0\r\n".
125 "Connection: Keep-Alive\r\n\r\n";
126 #$this->debug($msg);
127 @fputs($socket,$msg);
128 #$this->debug("...");
129 $res = @fread($socket,512);
130 #$this->debug("\n");
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) {
134 fclose($socket);
135 $failed = true;
136 $totalsockets -= $sockspersq;
137 } else {
138 @stream_set_blocking($socket,false);
139 $sockets[] = $socket;
140 }
141 }
142 } else {
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);
148 }
149 $so++;
150 }
151 }
152
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++) {
157 if($r != 0) {
158 $res = '';
159 $esc = 0;
160 while (strlen($res) < 100 && $esc < 200 ) {
161 $res .= @fread($sockets[$s],512);
162 $esc++;
163 usleep(20);
164 }
165 }
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";
170 #$this->debug($msg);
171 @fputs($sockets[$s],$msg);
172 #$this->debug("\n");
173 }
174 }
175 }
176 #$this->debug("Reading response...");
177 foreach ($sockets as $socket) {
178 $res = '';
179 $esc = 0;
180 while (strlen($res) < 100 && $esc < 200 ) {
181 $res .= @fread($socket,1024);
182 $esc++;
183 usleep(20);
184 }
185
186 @fclose($socket);
187 }
188 #$this->debug("\n");
189 wfProfileOut( $fname );
190 }
191
192 static function HTCPPurge( $urlArr ) {
193 global $wgHTCPMulticastAddress, $wgHTCPMulticastTTL, $wgHTCPPort;
194 $fname = 'SquidUpdate::HTCPPurge';
195 wfProfileIn( $fname );
196
197 $htcpOpCLR = 4; // HTCP CLR
198
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 );
204 }
205
206 // pfsockopen doesn't work because we need set_sock_opt
207 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
208 if ( $conn ) {
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 );
214
215 foreach ( $urlArr as $url ) {
216 if( !is_string( $url ) ) {
217 wfDebugDieBacktrace( 'Bad purge URL' );
218 }
219 $url = SquidUpdate::expand( $url );
220
221 // Construct a minimal HTCP request diagram
222 // as per RFC 2756
223 // Opcode 'CLR', no response desired, no auth
224 $htcpTransID = rand();
225
226 $htcpSpecifier = pack( 'na4na*na8n',
227 4, 'HEAD', strlen( $url ), $url,
228 8, 'HTTP/1.0', 0 );
229
230 $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
231 $htcpLen = 4 + $htcpDataLen + 2;
232
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);
239
240 // Send out
241 wfDebug( "Purging URL $url via HTCP\n" );
242 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
243 $wgHTCPMulticastAddress, $wgHTCPPort );
244 }
245 } else {
246 $errstr = socket_strerror( socket_last_error() );
247 wfDebug( "SquidUpdate::HTCPPurge(): Error opening UDP socket: $errstr\n" );
248 }
249 wfProfileOut( $fname );
250 }
251
252 function debug( $text ) {
253 global $wgDebugSquid;
254 if ( $wgDebugSquid ) {
255 wfDebug( $text );
256 }
257 }
258
259 /**
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.
263 *
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.
267 *
268 * Client functions should not need to call this.
269 *
270 * @return string
271 */
272 static function expand( $url ) {
273 global $wgInternalServer;
274 if( $url != '' && $url{0} == '/' ) {
275 return $wgInternalServer . $url;
276 }
277 return $url;
278 }
279 }
280 ?>