* Removed messages in English, they'll be inherited from the parent.
[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 $links = $dbr->tableName( 'links' );
36 $page = $dbr->tableName( 'page' );
37
38 $sql = "SELECT page_namespace,page_title FROM $links,$page WHERE l_to={$id} and l_from=page_id" ;
39 $res = $dbr->query( $sql, $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 newFromBrokenLinksTo( &$title ) {
55 $fname = 'SquidUpdate::newFromBrokenLinksTo';
56 wfProfileIn( $fname );
57
58 # Get a list of URLs linking to this (currently non-existent) page
59 $dbr =& wfGetDB( DB_SLAVE );
60 $brokenlinks = $dbr->tableName( 'brokenlinks' );
61 $page = $dbr->tableName( 'page' );
62 $encTitle = $dbr->addQuotes( $title->getPrefixedDBkey() );
63
64 $sql = "SELECT page_namespace,page_title FROM $brokenlinks,$cur WHERE bl_to={$encTitle} AND bl_from=page_id";
65 $res = $dbr->query( $sql, $fname );
66 $blurlArr = array();
67 if ( $dbr->numRows( $res ) <= $this->mMaxTitles ) {
68 while ( $BL = $dbr->fetchObject( $res ) )
69 {
70 $tobj = Title::makeTitle( $BL->page_namespace, $BL->page_title );
71 $blurlArr[] = $tobj->getInternalURL();
72 }
73 }
74 $dbr->freeResult( $res );
75 wfProfileOut( $fname );
76 return new SquidUpdate( $blurlArr );
77 }
78
79 /* static */ function newFromTitles( &$titles, $urlArr = array() ) {
80 foreach ( $titles as $title ) {
81 $urlArr[] = $title->getInternalURL();
82 }
83 return new SquidUpdate( $urlArr );
84 }
85
86 /* static */ function newSimplePurge( &$title ) {
87 $urlArr = $title->getSquidURLs();
88 return new SquidUpdate( $blurlArr );
89 }
90
91 function doUpdate() {
92 SquidUpdate::purge( $this->urlArr );
93 }
94
95 /* Purges a list of Squids defined in $wgSquidServers.
96 $urlArr should contain the full URLs to purge as values
97 (example: $urlArr[] = 'http://my.host/something')
98 XXX report broken Squids per mail or log */
99
100 /* static */ function purge( $urlArr ) {
101 global $wgSquidServers, $wgHTCPMulticastAddress, $wgHTCPPort;
102
103 if ( $wgSquidServers == 'echo' ) {
104 echo implode("<br />\n", $urlArr);
105 return;
106 }
107
108 if ( $wgHTCPMulticastAddress && $wgHTCPPort )
109 SquidUpdate::HTCPPurge( $urlArr );
110
111 $fname = 'SquidUpdate::purge';
112 wfProfileIn( $fname );
113
114 $maxsocketspersquid = 8; // socket cap per Squid
115 $urlspersocket = 400; // 400 seems to be a good tradeoff, opening a socket takes a while
116 $firsturl = $urlArr[0];
117 unset($urlArr[0]);
118 $urlArr = array_values($urlArr);
119 $sockspersq = max(ceil(count($urlArr) / $urlspersocket ),1);
120 if ($sockspersq == 1) {
121 /* the most common case */
122 $urlspersocket = count($urlArr);
123 } else if ($sockspersq > $maxsocketspersquid ) {
124 $urlspersocket = ceil(count($urlArr) / $maxsocketspersquid);
125 $sockspersq = $maxsocketspersquid;
126 }
127 $totalsockets = count($wgSquidServers) * $sockspersq;
128 $sockets = Array();
129
130 /* this sets up the sockets and tests the first socket for each server. */
131 for ($ss=0;$ss < count($wgSquidServers);$ss++) {
132 $failed = false;
133 $so = 0;
134 while ($so < $sockspersq && !$failed) {
135 if ($so == 0) {
136 /* first socket for this server, do the tests */
137 @list($server, $port) = explode(':', $wgSquidServers[$ss]);
138 if(!isset($port)) $port = 80;
139 #$this->debug("Opening socket to $server:$port");
140 $socket = @fsockopen($server, $port, $error, $errstr, 3);
141 #$this->debug("\n");
142 if (!$socket) {
143 $failed = true;
144 $totalsockets -= $sockspersq;
145 } else {
146 $msg = 'PURGE ' . $firsturl . " HTTP/1.0\r\n".
147 "Connection: Keep-Alive\r\n\r\n";
148 #$this->debug($msg);
149 @fputs($socket,$msg);
150 #$this->debug("...");
151 $res = @fread($socket,512);
152 #$this->debug("\n");
153 /* Squid only returns http headers with 200 or 404 status,
154 if there's more returned something's wrong */
155 if (strlen($res) > 250) {
156 fclose($socket);
157 $failed = true;
158 $totalsockets -= $sockspersq;
159 } else {
160 @stream_set_blocking($socket,false);
161 $sockets[] = $socket;
162 }
163 }
164 } else {
165 /* open the remaining sockets for this server */
166 list($server, $port) = explode(':', $wgSquidServers[$ss]);
167 if(!isset($port)) $port = 80;
168 $sockets[] = @fsockopen($server, $port, $error, $errstr, 2);
169 @stream_set_blocking($sockets[$s],false);
170 }
171 $so++;
172 }
173 }
174
175 if ($urlspersocket > 0) {
176 /* now do the heavy lifting. The fread() relies on Squid returning only the headers */
177 for ($r=0;$r < $urlspersocket;$r++) {
178 for ($s=0;$s < $totalsockets;$s++) {
179 if($r != 0) {
180 $res = '';
181 $esc = 0;
182 while (strlen($res) < 100 && $esc < 200 ) {
183 $res .= @fread($sockets[$s],512);
184 $esc++;
185 usleep(20);
186 }
187 }
188 $urindex = $r + $urlspersocket * ($s - $sockspersq * floor($s / $sockspersq));
189 $msg = 'PURGE ' . $urlArr[$urindex] . " HTTP/1.0\r\n".
190 "Connection: Keep-Alive\r\n\r\n";
191 #$this->debug($msg);
192 @fputs($sockets[$s],$msg);
193 #$this->debug("\n");
194 }
195 }
196 }
197 #$this->debug("Reading response...");
198 foreach ($sockets as $socket) {
199 $res = '';
200 $esc = 0;
201 while (strlen($res) < 100 && $esc < 200 ) {
202 $res .= @fread($socket,1024);
203 $esc++;
204 usleep(20);
205 }
206
207 @fclose($socket);
208 }
209 #$this->debug("\n");
210 wfProfileOut( $fname );
211 }
212
213 /* static */ function HTCPPurge( $urlArr ) {
214 global $wgHTCPMulticastAddress, $wgHTCPMulticastTTL, $wgHTCPPort;
215 $fname = 'SquidUpdate::HTCPPurge';
216 wfProfileIn( $fname );
217
218 $htcpOpCLR = 4; // HTCP CLR
219
220 // FIXME PHP doesn't support these socket constants (include/linux/in.h)
221 define( "IPPROTO_IP", 0 );
222 define( "IP_MULTICAST_LOOP", 34 );
223 define( "IP_MULTICAST_TTL", 33 );
224
225 // pfsockopen doesn't work because we need set_sock_opt
226 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
227 if ( $conn ) {
228 // Set socket options
229 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_LOOP, 0 );
230 if ( $wgHTCPMulticastTTL != 1 )
231 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_TTL,
232 $wgHTCPMulticastTTL );
233
234 foreach ( $urlArr as $url ) {
235 // Construct a minimal HTCP request diagram
236 // as per RFC 2756
237 // Opcode 'CLR', no response desired, no auth
238 $htcpTransID = rand();
239
240 $htcpSpecifier = pack( 'na4na*na8n',
241 4, 'NONE', strlen( $url ), $url,
242 8, 'HTTP/1.0', 0 );
243
244 $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
245 $htcpLen = 4 + $htcpDataLen + 2;
246
247 // Note! Squid gets the bit order of the first
248 // word wrong, wrt the RFC. Apparently no other
249 // implementation exists, so adapt to Squid
250 $htcpPacket = pack( 'nxxnCxNxxa*n',
251 $htcpLen, $htcpDataLen, $htcpOpCLR,
252 $htcpTransID, $htcpSpecifier, 2);
253
254 // Send out
255 wfDebug( "Purging URL $url via HTCP\n" );
256 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
257 $wgHTCPMulticastAddress, $wgHTCPPort );
258 }
259 } else {
260 $errstr = socket_strerror( socket_last_error() );
261 wfDebug( "SquidUpdate::HTCPPurge(): Error opening UDP socket: $errstr\n" );
262 }
263 wfProfileOut( $fname );
264 }
265
266 function debug( $text ) {
267 global $wgDebugSquid;
268 if ( $wgDebugSquid ) {
269 wfDebug( $text );
270 }
271 }
272 }
273 ?>