Prevent blocked users from changing page protection levels
[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 = $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[] = @fsockopen($server, $port, $error, $errstr, 2);
147 @stream_set_blocking($sockets[$s],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 $msg = 'PURGE ' . $urlArr[$urindex] . " HTTP/1.0\r\n".
168 "Connection: Keep-Alive\r\n\r\n";
169 #$this->debug($msg);
170 @fputs($sockets[$s],$msg);
171 #$this->debug("\n");
172 }
173 }
174 }
175 #$this->debug("Reading response...");
176 foreach ($sockets as $socket) {
177 $res = '';
178 $esc = 0;
179 while (strlen($res) < 100 && $esc < 200 ) {
180 $res .= @fread($socket,1024);
181 $esc++;
182 usleep(20);
183 }
184
185 @fclose($socket);
186 }
187 #$this->debug("\n");
188 wfProfileOut( $fname );
189 }
190
191 /* static */ function HTCPPurge( $urlArr ) {
192 global $wgHTCPMulticastAddress, $wgHTCPMulticastTTL, $wgHTCPPort;
193 $fname = 'SquidUpdate::HTCPPurge';
194 wfProfileIn( $fname );
195
196 $htcpOpCLR = 4; // HTCP CLR
197
198 // FIXME PHP doesn't support these socket constants (include/linux/in.h)
199 define( "IPPROTO_IP", 0 );
200 define( "IP_MULTICAST_LOOP", 34 );
201 define( "IP_MULTICAST_TTL", 33 );
202
203 // pfsockopen doesn't work because we need set_sock_opt
204 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
205 if ( $conn ) {
206 // Set socket options
207 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_LOOP, 0 );
208 if ( $wgHTCPMulticastTTL != 1 )
209 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_TTL,
210 $wgHTCPMulticastTTL );
211
212 foreach ( $urlArr as $url ) {
213 // Construct a minimal HTCP request diagram
214 // as per RFC 2756
215 // Opcode 'CLR', no response desired, no auth
216 $htcpTransID = rand();
217
218 $htcpSpecifier = pack( 'na4na*na8n',
219 4, 'NONE', strlen( $url ), $url,
220 8, 'HTTP/1.0', 0 );
221
222 $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
223 $htcpLen = 4 + $htcpDataLen + 2;
224
225 // Note! Squid gets the bit order of the first
226 // word wrong, wrt the RFC. Apparently no other
227 // implementation exists, so adapt to Squid
228 $htcpPacket = pack( 'nxxnCxNxxa*n',
229 $htcpLen, $htcpDataLen, $htcpOpCLR,
230 $htcpTransID, $htcpSpecifier, 2);
231
232 // Send out
233 wfDebug( "Purging URL $url via HTCP\n" );
234 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
235 $wgHTCPMulticastAddress, $wgHTCPPort );
236 }
237 } else {
238 $errstr = socket_strerror( socket_last_error() );
239 wfDebug( "SquidUpdate::HTCPPurge(): Error opening UDP socket: $errstr\n" );
240 }
241 wfProfileOut( $fname );
242 }
243
244 function debug( $text ) {
245 global $wgDebugSquid;
246 if ( $wgDebugSquid ) {
247 wfDebug( $text );
248 }
249 }
250 }
251 ?>