Revert revert of setSingleton(), unrelated to broken, accidentally committed code...
[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( empty( $urlArr ) ) {
86 return;
87 }
88
89 if ( $wgHTCPMulticastAddress && $wgHTCPPort ) {
90 return SquidUpdate::HTCPPurge( $urlArr );
91 }
92
93 $fname = 'SquidUpdate::purge';
94 wfProfileIn( $fname );
95
96 $maxsocketspersquid = 8; // socket cap per Squid
97 $urlspersocket = 400; // 400 seems to be a good tradeoff, opening a socket takes a while
98 $firsturl = SquidUpdate::expand( $urlArr[0] );
99 unset($urlArr[0]);
100 $urlArr = array_values($urlArr);
101 $sockspersq = max(ceil(count($urlArr) / $urlspersocket ),1);
102 if ($sockspersq == 1) {
103 /* the most common case */
104 $urlspersocket = count($urlArr);
105 } else if ($sockspersq > $maxsocketspersquid ) {
106 $urlspersocket = ceil(count($urlArr) / $maxsocketspersquid);
107 $sockspersq = $maxsocketspersquid;
108 }
109 $totalsockets = count($wgSquidServers) * $sockspersq;
110 $sockets = Array();
111
112 /* this sets up the sockets and tests the first socket for each server. */
113 for ($ss=0;$ss < count($wgSquidServers);$ss++) {
114 $failed = false;
115 $so = 0;
116 while ($so < $sockspersq && !$failed) {
117 if ($so == 0) {
118 /* first socket for this server, do the tests */
119 @list($server, $port) = explode(':', $wgSquidServers[$ss]);
120 if(!isset($port)) $port = 80;
121 #$this->debug("Opening socket to $server:$port");
122 $error = $errstr = false;
123 $socket = @fsockopen($server, $port, $error, $errstr, 3);
124 #$this->debug("\n");
125 if (!$socket) {
126 $failed = true;
127 $totalsockets -= $sockspersq;
128 } else {
129 $msg = 'PURGE ' . $firsturl . " HTTP/1.0\r\n".
130 "Connection: Keep-Alive\r\n\r\n";
131 #$this->debug($msg);
132 @fputs($socket,$msg);
133 #$this->debug("...");
134 $res = @fread($socket,512);
135 #$this->debug("\n");
136 /* Squid only returns http headers with 200 or 404 status,
137 if there's more returned something's wrong */
138 if (strlen($res) > 250) {
139 fclose($socket);
140 $failed = true;
141 $totalsockets -= $sockspersq;
142 } else {
143 @stream_set_blocking($socket,false);
144 $sockets[] = $socket;
145 }
146 }
147 } else {
148 /* open the remaining sockets for this server */
149 list($server, $port) = explode(':', $wgSquidServers[$ss]);
150 if(!isset($port)) $port = 80;
151 $sockets[$so+1] = @fsockopen($server, $port, $error, $errstr, 2);
152 @stream_set_blocking($sockets[$so+1],false);
153 }
154 $so++;
155 }
156 }
157
158 if ($urlspersocket > 0) {
159 /* now do the heavy lifting. The fread() relies on Squid returning only the headers */
160 for ($r=0;$r < $urlspersocket;$r++) {
161 for ($s=0;$s < $totalsockets;$s++) {
162 if($r != 0) {
163 $res = '';
164 $esc = 0;
165 while (strlen($res) < 100 && $esc < 200 ) {
166 $res .= @fread($sockets[$s],512);
167 $esc++;
168 usleep(20);
169 }
170 }
171 $urindex = $r + $urlspersocket * ($s - $sockspersq * floor($s / $sockspersq));
172 $url = SquidUpdate::expand( $urlArr[$urindex] );
173 $msg = 'PURGE ' . $url . " HTTP/1.0\r\n".
174 "Connection: Keep-Alive\r\n\r\n";
175 #$this->debug($msg);
176 @fputs($sockets[$s],$msg);
177 #$this->debug("\n");
178 }
179 }
180 }
181 #$this->debug("Reading response...");
182 foreach ($sockets as $socket) {
183 $res = '';
184 $esc = 0;
185 while (strlen($res) < 100 && $esc < 200 ) {
186 $res .= @fread($socket,1024);
187 $esc++;
188 usleep(20);
189 }
190
191 @fclose($socket);
192 }
193 #$this->debug("\n");
194 wfProfileOut( $fname );
195 }
196
197 static function HTCPPurge( $urlArr ) {
198 global $wgHTCPMulticastAddress, $wgHTCPMulticastTTL, $wgHTCPPort;
199 $fname = 'SquidUpdate::HTCPPurge';
200 wfProfileIn( $fname );
201
202 $htcpOpCLR = 4; // HTCP CLR
203
204 // FIXME PHP doesn't support these socket constants (include/linux/in.h)
205 if( !defined( "IPPROTO_IP" ) ) {
206 define( "IPPROTO_IP", 0 );
207 define( "IP_MULTICAST_LOOP", 34 );
208 define( "IP_MULTICAST_TTL", 33 );
209 }
210
211 // pfsockopen doesn't work because we need set_sock_opt
212 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
213 if ( $conn ) {
214 // Set socket options
215 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_LOOP, 0 );
216 if ( $wgHTCPMulticastTTL != 1 )
217 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_TTL,
218 $wgHTCPMulticastTTL );
219
220 foreach ( $urlArr as $url ) {
221 if( !is_string( $url ) ) {
222 wfDebugDieBacktrace( 'Bad purge URL' );
223 }
224 $url = SquidUpdate::expand( $url );
225
226 // Construct a minimal HTCP request diagram
227 // as per RFC 2756
228 // Opcode 'CLR', no response desired, no auth
229 $htcpTransID = rand();
230
231 $htcpSpecifier = pack( 'na4na*na8n',
232 4, 'HEAD', strlen( $url ), $url,
233 8, 'HTTP/1.0', 0 );
234
235 $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
236 $htcpLen = 4 + $htcpDataLen + 2;
237
238 // Note! Squid gets the bit order of the first
239 // word wrong, wrt the RFC. Apparently no other
240 // implementation exists, so adapt to Squid
241 $htcpPacket = pack( 'nxxnCxNxxa*n',
242 $htcpLen, $htcpDataLen, $htcpOpCLR,
243 $htcpTransID, $htcpSpecifier, 2);
244
245 // Send out
246 wfDebug( "Purging URL $url via HTCP\n" );
247 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
248 $wgHTCPMulticastAddress, $wgHTCPPort );
249 }
250 } else {
251 $errstr = socket_strerror( socket_last_error() );
252 wfDebug( "SquidUpdate::HTCPPurge(): Error opening UDP socket: $errstr\n" );
253 }
254 wfProfileOut( $fname );
255 }
256
257 function debug( $text ) {
258 global $wgDebugSquid;
259 if ( $wgDebugSquid ) {
260 wfDebug( $text );
261 }
262 }
263
264 /**
265 * Expand local URLs to fully-qualified URLs using the internal protocol
266 * and host defined in $wgInternalServer. Input that's already fully-
267 * qualified will be passed through unchanged.
268 *
269 * This is used to generate purge URLs that may be either local to the
270 * main wiki or include a non-native host, such as images hosted on a
271 * second internal server.
272 *
273 * Client functions should not need to call this.
274 *
275 * @return string
276 */
277 static function expand( $url ) {
278 global $wgInternalServer;
279 if( $url != '' && $url{0} == '/' ) {
280 return $wgInternalServer . $url;
281 }
282 return $url;
283 }
284 }
285