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