* (bug 6243) Fix email for usernames containing dots when using PEAR::Mail
[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 foreach ( $titles as $title ) {
58 $urlArr[] = $title->getInternalURL();
59 }
60 return new SquidUpdate( $urlArr );
61 }
62
63 /* static */ function newSimplePurge( &$title ) {
64 $urlArr = $title->getSquidURLs();
65 return new SquidUpdate( $urlArr );
66 }
67
68 function doUpdate() {
69 SquidUpdate::purge( $this->urlArr );
70 }
71
72 /* Purges a list of Squids defined in $wgSquidServers.
73 $urlArr should contain the full URLs to purge as values
74 (example: $urlArr[] = 'http://my.host/something')
75 XXX report broken Squids per mail or log */
76
77 /* static */ function purge( $urlArr ) {
78 global $wgSquidServers, $wgHTCPMulticastAddress, $wgHTCPPort;
79
80 if ( $wgSquidServers == 'echo' ) {
81 echo implode("<br />\n", $urlArr);
82 return;
83 }
84
85 if ( $wgHTCPMulticastAddress && $wgHTCPPort )
86 SquidUpdate::HTCPPurge( $urlArr );
87
88 $fname = 'SquidUpdate::purge';
89 wfProfileIn( $fname );
90
91 $maxsocketspersquid = 8; // socket cap per Squid
92 $urlspersocket = 400; // 400 seems to be a good tradeoff, opening a socket takes a while
93 $firsturl = SquidUpdate::expand( $urlArr[0] );
94 unset($urlArr[0]);
95 $urlArr = array_values($urlArr);
96 $sockspersq = max(ceil(count($urlArr) / $urlspersocket ),1);
97 if ($sockspersq == 1) {
98 /* the most common case */
99 $urlspersocket = count($urlArr);
100 } else if ($sockspersq > $maxsocketspersquid ) {
101 $urlspersocket = ceil(count($urlArr) / $maxsocketspersquid);
102 $sockspersq = $maxsocketspersquid;
103 }
104 $totalsockets = count($wgSquidServers) * $sockspersq;
105 $sockets = Array();
106
107 /* this sets up the sockets and tests the first socket for each server. */
108 for ($ss=0;$ss < count($wgSquidServers);$ss++) {
109 $failed = false;
110 $so = 0;
111 while ($so < $sockspersq && !$failed) {
112 if ($so == 0) {
113 /* first socket for this server, do the tests */
114 @list($server, $port) = explode(':', $wgSquidServers[$ss]);
115 if(!isset($port)) $port = 80;
116 #$this->debug("Opening socket to $server:$port");
117 $error = $errstr = false;
118 $socket = @fsockopen($server, $port, $error, $errstr, 3);
119 #$this->debug("\n");
120 if (!$socket) {
121 $failed = true;
122 $totalsockets -= $sockspersq;
123 } else {
124 $msg = 'PURGE ' . $firsturl . " HTTP/1.0\r\n".
125 "Connection: Keep-Alive\r\n\r\n";
126 #$this->debug($msg);
127 @fputs($socket,$msg);
128 #$this->debug("...");
129 $res = @fread($socket,512);
130 #$this->debug("\n");
131 /* Squid only returns http headers with 200 or 404 status,
132 if there's more returned something's wrong */
133 if (strlen($res) > 250) {
134 fclose($socket);
135 $failed = true;
136 $totalsockets -= $sockspersq;
137 } else {
138 @stream_set_blocking($socket,false);
139 $sockets[] = $socket;
140 }
141 }
142 } else {
143 /* open the remaining sockets for this server */
144 list($server, $port) = explode(':', $wgSquidServers[$ss]);
145 if(!isset($port)) $port = 80;
146 $sockets[$so+1] = @fsockopen($server, $port, $error, $errstr, 2);
147 @stream_set_blocking($sockets[$so+1],false);
148 }
149 $so++;
150 }
151 }
152
153 if ($urlspersocket > 0) {
154 /* now do the heavy lifting. The fread() relies on Squid returning only the headers */
155 for ($r=0;$r < $urlspersocket;$r++) {
156 for ($s=0;$s < $totalsockets;$s++) {
157 if($r != 0) {
158 $res = '';
159 $esc = 0;
160 while (strlen($res) < 100 && $esc < 200 ) {
161 $res .= @fread($sockets[$s],512);
162 $esc++;
163 usleep(20);
164 }
165 }
166 $urindex = $r + $urlspersocket * ($s - $sockspersq * floor($s / $sockspersq));
167 $url = SquidUpdate::expand( $urlArr[$urindex] );
168 $msg = 'PURGE ' . $url . " 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 $url = SquidUpdate::expand( $url );
215
216 // Construct a minimal HTCP request diagram
217 // as per RFC 2756
218 // Opcode 'CLR', no response desired, no auth
219 $htcpTransID = rand();
220
221 $htcpSpecifier = pack( 'na4na*na8n',
222 4, 'NONE', strlen( $url ), $url,
223 8, 'HTTP/1.0', 0 );
224
225 $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
226 $htcpLen = 4 + $htcpDataLen + 2;
227
228 // Note! Squid gets the bit order of the first
229 // word wrong, wrt the RFC. Apparently no other
230 // implementation exists, so adapt to Squid
231 $htcpPacket = pack( 'nxxnCxNxxa*n',
232 $htcpLen, $htcpDataLen, $htcpOpCLR,
233 $htcpTransID, $htcpSpecifier, 2);
234
235 // Send out
236 wfDebug( "Purging URL $url via HTCP\n" );
237 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
238 $wgHTCPMulticastAddress, $wgHTCPPort );
239 }
240 } else {
241 $errstr = socket_strerror( socket_last_error() );
242 wfDebug( "SquidUpdate::HTCPPurge(): Error opening UDP socket: $errstr\n" );
243 }
244 wfProfileOut( $fname );
245 }
246
247 function debug( $text ) {
248 global $wgDebugSquid;
249 if ( $wgDebugSquid ) {
250 wfDebug( $text );
251 }
252 }
253
254 /**
255 * Expand local URLs to fully-qualified URLs using the internal protocol
256 * and host defined in $wgInternalServer. Input that's already fully-
257 * qualified will be passed through unchanged.
258 *
259 * This is used to generate purge URLs that may be either local to the
260 * main wiki or include a non-native host, such as images hosted on a
261 * second internal server.
262 *
263 * Client functions should not need to call this.
264 *
265 * @return string
266 */
267 static function expand( $url ) {
268 global $wgInternalServer;
269 if( $url != '' && $url{0} == '/' ) {
270 return $wgInternalServer . $url;
271 }
272 return $url;
273 }
274 }
275 ?>