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