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