680b0f418c042c54c9ff209dbfd92927e763e397
[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 public $urlArr; // FIXME : is it really public ?
13 private $mMaxTitles;
14
15 function SquidUpdate( $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 $id = $title->getArticleID();
34
35 $dbr =& wfGetDB( DB_SLAVE );
36 $res = $dbr->select( array( 'links', 'page' ),
37 array( 'page_namespace', 'page_title' ),
38 array(
39 'pl_namespace' => $title->getNamespace(),
40 'pl_title' => $title->getDbKey(),
41 'pl_from=page_id' ),
42 $fname );
43 $blurlArr = $title->getSquidURLs();
44 if ( $dbr->numRows( $res ) <= $this->mMaxTitles ) {
45 while ( $BL = $dbr->fetchObject ( $res ) )
46 {
47 $tobj = Title::makeTitle( $BL->page_namespace, $BL->page_title ) ;
48 $blurlArr[] = $tobj->getInternalURL();
49 }
50 }
51 $dbr->freeResult ( $res ) ;
52
53 wfProfileOut( $fname );
54 return new SquidUpdate( $blurlArr );
55 }
56
57 /* static */ function newFromTitles( &$titles, $urlArr = array() ) {
58 foreach ( $titles as $title ) {
59 $urlArr[] = $title->getInternalURL();
60 }
61 return new SquidUpdate( $urlArr );
62 }
63
64 /* static */ function newSimplePurge( &$title ) {
65 $urlArr = $title->getSquidURLs();
66 return new SquidUpdate( $urlArr );
67 }
68
69 function doUpdate() {
70 SquidUpdate::purge( $this->urlArr );
71 }
72
73 /* Purges a list of Squids defined in $wgSquidServers.
74 $urlArr should contain the full URLs to purge as values
75 (example: $urlArr[] = 'http://my.host/something')
76 XXX report broken Squids per mail or log */
77
78 /* static */ function purge( $urlArr ) {
79 global $wgSquidServers, $wgHTCPMulticastAddress, $wgHTCPPort;
80
81 if ( $wgSquidServers == 'echo' ) {
82 echo implode("<br />\n", $urlArr);
83 return;
84 }
85
86 if ( $wgHTCPMulticastAddress && $wgHTCPPort )
87 SquidUpdate::HTCPPurge( $urlArr );
88
89 $fname = 'SquidUpdate::purge';
90 wfProfileIn( $fname );
91
92 $maxsocketspersquid = 8; // socket cap per Squid
93 $urlspersocket = 400; // 400 seems to be a good tradeoff, opening a socket takes a while
94 $firsturl = $urlArr[0];
95 unset($urlArr[0]);
96 $urlArr = array_values($urlArr);
97 $sockspersq = max(ceil(count($urlArr) / $urlspersocket ),1);
98 if ($sockspersq == 1) {
99 /* the most common case */
100 $urlspersocket = count($urlArr);
101 } else if ($sockspersq > $maxsocketspersquid ) {
102 $urlspersocket = ceil(count($urlArr) / $maxsocketspersquid);
103 $sockspersq = $maxsocketspersquid;
104 }
105 $totalsockets = count($wgSquidServers) * $sockspersq;
106 $sockets = Array();
107
108 /* this sets up the sockets and tests the first socket for each server. */
109 for ($ss=0;$ss < count($wgSquidServers);$ss++) {
110 $failed = false;
111 $so = 0;
112 while ($so < $sockspersq && !$failed) {
113 if ($so == 0) {
114 /* first socket for this server, do the tests */
115 @list($server, $port) = explode(':', $wgSquidServers[$ss]);
116 if(!isset($port)) $port = 80;
117 #$this->debug("Opening socket to $server:$port");
118 $error = $errstr = false;
119 $socket = @fsockopen($server, $port, $error, $errstr, 3);
120 #$this->debug("\n");
121 if (!$socket) {
122 $failed = true;
123 $totalsockets -= $sockspersq;
124 } else {
125 $msg = 'PURGE ' . $firsturl . " HTTP/1.0\r\n".
126 "Connection: Keep-Alive\r\n\r\n";
127 #$this->debug($msg);
128 @fputs($socket,$msg);
129 #$this->debug("...");
130 $res = @fread($socket,512);
131 #$this->debug("\n");
132 /* Squid only returns http headers with 200 or 404 status,
133 if there's more returned something's wrong */
134 if (strlen($res) > 250) {
135 fclose($socket);
136 $failed = true;
137 $totalsockets -= $sockspersq;
138 } else {
139 @stream_set_blocking($socket,false);
140 $sockets[] = $socket;
141 }
142 }
143 } else {
144 /* open the remaining sockets for this server */
145 list($server, $port) = explode(':', $wgSquidServers[$ss]);
146 if(!isset($port)) $port = 80;
147 $sockets[$so+1] = @fsockopen($server, $port, $error, $errstr, 2);
148 @stream_set_blocking($sockets[$so+1],false);
149 }
150 $so++;
151 }
152 }
153
154 if ($urlspersocket > 0) {
155 /* now do the heavy lifting. The fread() relies on Squid returning only the headers */
156 for ($r=0;$r < $urlspersocket;$r++) {
157 for ($s=0;$s < $totalsockets;$s++) {
158 if($r != 0) {
159 $res = '';
160 $esc = 0;
161 while (strlen($res) < 100 && $esc < 200 ) {
162 $res .= @fread($sockets[$s],512);
163 $esc++;
164 usleep(20);
165 }
166 }
167 $urindex = $r + $urlspersocket * ($s - $sockspersq * floor($s / $sockspersq));
168 $msg = 'PURGE ' . $urlArr[$urindex] . " 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 define( "IPPROTO_IP", 0 );
201 define( "IP_MULTICAST_LOOP", 34 );
202 define( "IP_MULTICAST_TTL", 33 );
203
204 // pfsockopen doesn't work because we need set_sock_opt
205 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
206 if ( $conn ) {
207 // Set socket options
208 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_LOOP, 0 );
209 if ( $wgHTCPMulticastTTL != 1 )
210 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_TTL,
211 $wgHTCPMulticastTTL );
212
213 foreach ( $urlArr as $url ) {
214 // Construct a minimal HTCP request diagram
215 // as per RFC 2756
216 // Opcode 'CLR', no response desired, no auth
217 $htcpTransID = rand();
218
219 $htcpSpecifier = pack( 'na4na*na8n',
220 4, 'NONE', strlen( $url ), $url,
221 8, 'HTTP/1.0', 0 );
222
223 $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
224 $htcpLen = 4 + $htcpDataLen + 2;
225
226 // Note! Squid gets the bit order of the first
227 // word wrong, wrt the RFC. Apparently no other
228 // implementation exists, so adapt to Squid
229 $htcpPacket = pack( 'nxxnCxNxxa*n',
230 $htcpLen, $htcpDataLen, $htcpOpCLR,
231 $htcpTransID, $htcpSpecifier, 2);
232
233 // Send out
234 wfDebug( "Purging URL $url via HTCP\n" );
235 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
236 $wgHTCPMulticastAddress, $wgHTCPPort );
237 }
238 } else {
239 $errstr = socket_strerror( socket_last_error() );
240 wfDebug( "SquidUpdate::HTCPPurge(): Error opening UDP socket: $errstr\n" );
241 }
242 wfProfileOut( $fname );
243 }
244
245 function debug( $text ) {
246 global $wgDebugSquid;
247 if ( $wgDebugSquid ) {
248 wfDebug( $text );
249 }
250 }
251 }
252 ?>