Add AJAX category management system. Includes suggestion system, dialogs for setting...
[lhc/web/wiklou.git] / includes / SquidUpdate.php
1 <?php
2 /**
3 * See deferred.txt
4 * @file
5 * @ingroup Cache
6 */
7
8 /**
9 * Handles purging appropriate Squid URLs given a title (or titles)
10 * @ingroup Cache
11 */
12 class SquidUpdate {
13 var $urlArr, $mMaxTitles;
14
15 function __construct( $urlArr = Array(), $maxTitles = false ) {
16 global $wgMaxSquidPurgeTitles;
17 if ( $maxTitles === false ) {
18 $this->mMaxTitles = $wgMaxSquidPurgeTitles;
19 } else {
20 $this->mMaxTitles = $maxTitles;
21 }
22 if ( count( $urlArr ) > $this->mMaxTitles ) {
23 $urlArr = array_slice( $urlArr, 0, $this->mMaxTitles );
24 }
25 $this->urlArr = $urlArr;
26 }
27
28 static function newFromLinksTo( &$title ) {
29 $fname = 'SquidUpdate::newFromLinksTo';
30 wfProfileIn( $fname );
31
32 # Get a list of URLs linking to this page
33 $dbr = wfGetDB( DB_SLAVE );
34 $res = $dbr->select( array( 'links', 'page' ),
35 array( 'page_namespace', 'page_title' ),
36 array(
37 'pl_namespace' => $title->getNamespace(),
38 'pl_title' => $title->getDBkey(),
39 'pl_from=page_id' ),
40 $fname );
41 $blurlArr = $title->getSquidURLs();
42 if ( $dbr->numRows( $res ) <= $this->mMaxTitles ) {
43 while ( $BL = $dbr->fetchObject ( $res ) )
44 {
45 $tobj = Title::makeTitle( $BL->page_namespace, $BL->page_title ) ;
46 $blurlArr[] = $tobj->getInternalURL();
47 }
48 }
49 $dbr->freeResult ( $res ) ;
50
51 wfProfileOut( $fname );
52 return new SquidUpdate( $blurlArr );
53 }
54
55 /**
56 * Create a SquidUpdate from an array of Title objects, or a TitleArray object
57 */
58 static function newFromTitles( $titles, $urlArr = array() ) {
59 global $wgMaxSquidPurgeTitles;
60 $i = 0;
61 foreach ( $titles as $title ) {
62 $urlArr[] = $title->getInternalURL();
63 if ( $i++ > $wgMaxSquidPurgeTitles ) {
64 break;
65 }
66 }
67 return new SquidUpdate( $urlArr );
68 }
69
70 static function newSimplePurge( &$title ) {
71 $urlArr = $title->getSquidURLs();
72 return new SquidUpdate( $urlArr );
73 }
74
75 function doUpdate() {
76 SquidUpdate::purge( $this->urlArr );
77 }
78
79 /* Purges a list of Squids defined in $wgSquidServers.
80 $urlArr should contain the full URLs to purge as values
81 (example: $urlArr[] = 'http://my.host/something')
82 XXX report broken Squids per mail or log */
83
84 static function purge( $urlArr ) {
85 global $wgSquidServers, $wgHTCPMulticastAddress, $wgHTCPPort;
86
87 /*if ( (@$wgSquidServers[0]) == 'echo' ) {
88 echo implode("<br />\n", $urlArr) . "<br />\n";
89 return;
90 }*/
91
92 if( empty( $urlArr ) ) {
93 return;
94 }
95
96 if ( $wgHTCPMulticastAddress && $wgHTCPPort ) {
97 return SquidUpdate::HTCPPurge( $urlArr );
98 }
99
100 $fname = 'SquidUpdate::purge';
101 wfProfileIn( $fname );
102
103 $maxsocketspersquid = 8; // socket cap per Squid
104 $urlspersocket = 400; // 400 seems to be a good tradeoff, opening a socket takes a while
105 $firsturl = SquidUpdate::expand( $urlArr[0] );
106 unset($urlArr[0]);
107 $urlArr = array_values($urlArr);
108 $sockspersq = max(ceil(count($urlArr) / $urlspersocket ),1);
109 if ($sockspersq == 1) {
110 /* the most common case */
111 $urlspersocket = count($urlArr);
112 } else if ($sockspersq > $maxsocketspersquid ) {
113 $urlspersocket = ceil(count($urlArr) / $maxsocketspersquid);
114 $sockspersq = $maxsocketspersquid;
115 }
116 $totalsockets = count($wgSquidServers) * $sockspersq;
117 $sockets = Array();
118
119 /* this sets up the sockets and tests the first socket for each server. */
120 for ($ss=0;$ss < count($wgSquidServers);$ss++) {
121 $failed = false;
122 $so = 0;
123 while ($so < $sockspersq && !$failed) {
124 if ($so == 0) {
125 /* first socket for this server, do the tests */
126 @list($server, $port) = explode(':', $wgSquidServers[$ss]);
127 if(!isset($port)) $port = 80;
128 #$this->debug("Opening socket to $server:$port");
129 $error = $errstr = false;
130 $socket = @fsockopen($server, $port, $error, $errstr, 3);
131 #$this->debug("\n");
132 if (!$socket) {
133 $failed = true;
134 $totalsockets -= $sockspersq;
135 } else {
136 $msg = 'PURGE ' . $firsturl . " HTTP/1.0\r\n".
137 "Connection: Keep-Alive\r\n\r\n";
138 #$this->debug($msg);
139 @fputs($socket,$msg);
140 #$this->debug("...");
141 $res = @fread($socket,512);
142 #$this->debug("\n");
143 /* Squid only returns http headers with 200 or 404 status,
144 if there's more returned something's wrong */
145 if (strlen($res) > 250) {
146 fclose($socket);
147 $failed = true;
148 $totalsockets -= $sockspersq;
149 } else {
150 @stream_set_blocking($socket,false);
151 $sockets[] = $socket;
152 }
153 }
154 } else {
155 /* open the remaining sockets for this server */
156 list($server, $port) = explode(':', $wgSquidServers[$ss]);
157 if(!isset($port)) $port = 80;
158 $socket = @fsockopen($server, $port, $error, $errstr, 2);
159 @stream_set_blocking($socket,false);
160 $sockets[] = $socket;
161 }
162 $so++;
163 }
164 }
165
166 if ($urlspersocket > 0) {
167 /* now do the heavy lifting. The fread() relies on Squid returning only the headers */
168 for ($r=0;$r < $urlspersocket;$r++) {
169 for ($s=0;$s < $totalsockets;$s++) {
170 if($r != 0) {
171 $res = '';
172 $esc = 0;
173 while (strlen($res) < 100 && $esc < 200 ) {
174 $res .= @fread($sockets[$s],512);
175 $esc++;
176 usleep(20);
177 }
178 }
179 $urindex = $r + $urlspersocket * ($s - $sockspersq * floor($s / $sockspersq));
180 $url = SquidUpdate::expand( $urlArr[$urindex] );
181 $msg = 'PURGE ' . $url . " HTTP/1.0\r\n".
182 "Connection: Keep-Alive\r\n\r\n";
183 #$this->debug($msg);
184 @fputs($sockets[$s],$msg);
185 #$this->debug("\n");
186 }
187 }
188 }
189 #$this->debug("Reading response...");
190 foreach ($sockets as $socket) {
191 $res = '';
192 $esc = 0;
193 while (strlen($res) < 100 && $esc < 200 ) {
194 $res .= @fread($socket,1024);
195 $esc++;
196 usleep(20);
197 }
198
199 @fclose($socket);
200 }
201 #$this->debug("\n");
202 wfProfileOut( $fname );
203 }
204
205 static function HTCPPurge( $urlArr ) {
206 global $wgHTCPMulticastAddress, $wgHTCPMulticastTTL, $wgHTCPPort;
207 $fname = 'SquidUpdate::HTCPPurge';
208 wfProfileIn( $fname );
209
210 $htcpOpCLR = 4; // HTCP CLR
211
212 // FIXME PHP doesn't support these socket constants (include/linux/in.h)
213 if( !defined( "IPPROTO_IP" ) ) {
214 define( "IPPROTO_IP", 0 );
215 define( "IP_MULTICAST_LOOP", 34 );
216 define( "IP_MULTICAST_TTL", 33 );
217 }
218
219 // pfsockopen doesn't work because we need set_sock_opt
220 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
221 if ( $conn ) {
222 // Set socket options
223 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_LOOP, 0 );
224 if ( $wgHTCPMulticastTTL != 1 )
225 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_TTL,
226 $wgHTCPMulticastTTL );
227
228 foreach ( $urlArr as $url ) {
229 if( !is_string( $url ) ) {
230 throw new MWException( 'Bad purge URL' );
231 }
232 $url = SquidUpdate::expand( $url );
233
234 // Construct a minimal HTCP request diagram
235 // as per RFC 2756
236 // Opcode 'CLR', no response desired, no auth
237 $htcpTransID = rand();
238
239 $htcpSpecifier = pack( 'na4na*na8n',
240 4, 'HEAD', strlen( $url ), $url,
241 8, 'HTTP/1.0', 0 );
242
243 $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
244 $htcpLen = 4 + $htcpDataLen + 2;
245
246 // Note! Squid gets the bit order of the first
247 // word wrong, wrt the RFC. Apparently no other
248 // implementation exists, so adapt to Squid
249 $htcpPacket = pack( 'nxxnCxNxxa*n',
250 $htcpLen, $htcpDataLen, $htcpOpCLR,
251 $htcpTransID, $htcpSpecifier, 2);
252
253 // Send out
254 wfDebug( "Purging URL $url via HTCP\n" );
255 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
256 $wgHTCPMulticastAddress, $wgHTCPPort );
257 }
258 } else {
259 $errstr = socket_strerror( socket_last_error() );
260 wfDebug( "SquidUpdate::HTCPPurge(): Error opening UDP socket: $errstr\n" );
261 }
262 wfProfileOut( $fname );
263 }
264
265 function debug( $text ) {
266 global $wgDebugSquid;
267 if ( $wgDebugSquid ) {
268 wfDebug( $text );
269 }
270 }
271
272 /**
273 * Expand local URLs to fully-qualified URLs using the internal protocol
274 * and host defined in $wgInternalServer. Input that's already fully-
275 * qualified will be passed through unchanged.
276 *
277 * This is used to generate purge URLs that may be either local to the
278 * main wiki or include a non-native host, such as images hosted on a
279 * second internal server.
280 *
281 * Client functions should not need to call this.
282 *
283 * @return string
284 */
285 static function expand( $url ) {
286 global $wgInternalServer;
287 if( $url != '' && $url{0} == '/' ) {
288 return $wgInternalServer . $url;
289 }
290 return $url;
291 }
292 }