(bug 17602) fix Monobook action tabs not quite touching the page body
[lhc/web/wiklou.git] / includes / cache / SquidUpdate.php
1 <?php
2 /**
3 * Squid cache purging.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Cache
22 */
23
24 /**
25 * Handles purging appropriate Squid URLs given a title (or titles)
26 * @ingroup Cache
27 */
28 class SquidUpdate {
29 var $urlArr, $mMaxTitles;
30
31 /**
32 * @param $urlArr array
33 * @param $maxTitles bool|int
34 */
35 function __construct( $urlArr = array(), $maxTitles = false ) {
36 global $wgMaxSquidPurgeTitles;
37 if ( $maxTitles === false ) {
38 $this->mMaxTitles = $wgMaxSquidPurgeTitles;
39 } else {
40 $this->mMaxTitles = $maxTitles;
41 }
42 $urlArr = array_unique( $urlArr ); // Remove duplicates
43 if ( count( $urlArr ) > $this->mMaxTitles ) {
44 $urlArr = array_slice( $urlArr, 0, $this->mMaxTitles );
45 }
46 $this->urlArr = $urlArr;
47 }
48
49 /**
50 * @param $title Title
51 *
52 * @return SquidUpdate
53 */
54 static function newFromLinksTo( &$title ) {
55 global $wgMaxSquidPurgeTitles;
56 wfProfileIn( __METHOD__ );
57
58 # Get a list of URLs linking to this page
59 $dbr = wfGetDB( DB_SLAVE );
60 $res = $dbr->select( array( 'links', 'page' ),
61 array( 'page_namespace', 'page_title' ),
62 array(
63 'pl_namespace' => $title->getNamespace(),
64 'pl_title' => $title->getDBkey(),
65 'pl_from=page_id' ),
66 __METHOD__ );
67 $blurlArr = $title->getSquidURLs();
68 if ( $res->numRows() <= $wgMaxSquidPurgeTitles ) {
69 foreach ( $res as $BL ) {
70 $tobj = Title::makeTitle( $BL->page_namespace, $BL->page_title );
71 $blurlArr[] = $tobj->getInternalURL();
72 }
73 }
74
75 wfProfileOut( __METHOD__ );
76 return new SquidUpdate( $blurlArr );
77 }
78
79 /**
80 * Create a SquidUpdate from an array of Title objects, or a TitleArray object
81 *
82 * @param $titles array
83 * @param $urlArr array
84 *
85 * @return SquidUpdate
86 */
87 static function newFromTitles( $titles, $urlArr = array() ) {
88 global $wgMaxSquidPurgeTitles;
89 $i = 0;
90 foreach ( $titles as $title ) {
91 $urlArr[] = $title->getInternalURL();
92 if ( $i++ > $wgMaxSquidPurgeTitles ) {
93 break;
94 }
95 }
96 return new SquidUpdate( $urlArr );
97 }
98
99 /**
100 * @param $title Title
101 *
102 * @return SquidUpdate
103 */
104 static function newSimplePurge( &$title ) {
105 $urlArr = $title->getSquidURLs();
106 return new SquidUpdate( $urlArr );
107 }
108
109 /**
110 * Purges the list of URLs passed to the constructor
111 */
112 function doUpdate() {
113 SquidUpdate::purge( $this->urlArr );
114 }
115
116 /**
117 * Purges a list of Squids defined in $wgSquidServers.
118 * $urlArr should contain the full URLs to purge as values
119 * (example: $urlArr[] = 'http://my.host/something')
120 * XXX report broken Squids per mail or log
121 *
122 * @param $urlArr array
123 * @return void
124 */
125 static function purge( $urlArr ) {
126 global $wgSquidServers, $wgHTCPMulticastRouting;
127
128 if( !$urlArr ) {
129 return;
130 }
131
132 wfDebug( "Squid purge: " . implode( ' ', $urlArr ) . "\n" );
133
134 if ( $wgHTCPMulticastRouting ) {
135 SquidUpdate::HTCPPurge( $urlArr );
136 }
137
138 wfProfileIn( __METHOD__ );
139
140 $urlArr = array_unique( $urlArr ); // Remove duplicates
141 $maxSocketsPerSquid = 8; // socket cap per Squid
142 $urlsPerSocket = 400; // 400 seems to be a good tradeoff, opening a socket takes a while
143 $socketsPerSquid = ceil( count( $urlArr ) / $urlsPerSocket );
144 if ( $socketsPerSquid > $maxSocketsPerSquid ) {
145 $socketsPerSquid = $maxSocketsPerSquid;
146 }
147
148 $pool = new SquidPurgeClientPool;
149 $chunks = array_chunk( $urlArr, ceil( count( $urlArr ) / $socketsPerSquid ) );
150 foreach ( $wgSquidServers as $server ) {
151 foreach ( $chunks as $chunk ) {
152 $client = new SquidPurgeClient( $server );
153 foreach ( $chunk as $url ) {
154 $client->queuePurge( $url );
155 }
156 $pool->addClient( $client );
157 }
158 }
159 $pool->run();
160
161 wfProfileOut( __METHOD__ );
162 }
163
164 /**
165 * @throws MWException
166 * @param $urlArr array
167 */
168 static function HTCPPurge( $urlArr ) {
169 global $wgHTCPMulticastRouting, $wgHTCPMulticastTTL;
170 wfProfileIn( __METHOD__ );
171
172 $htcpOpCLR = 4; // HTCP CLR
173
174 // @todo FIXME: PHP doesn't support these socket constants (include/linux/in.h)
175 if( !defined( "IPPROTO_IP" ) ) {
176 define( "IPPROTO_IP", 0 );
177 define( "IP_MULTICAST_LOOP", 34 );
178 define( "IP_MULTICAST_TTL", 33 );
179 }
180
181 // pfsockopen doesn't work because we need set_sock_opt
182 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
183 if ( $conn ) {
184 // Set socket options
185 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_LOOP, 0 );
186 if ( $wgHTCPMulticastTTL != 1 )
187 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_TTL,
188 $wgHTCPMulticastTTL );
189
190 $urlArr = array_unique( $urlArr ); // Remove duplicates
191 foreach ( $urlArr as $url ) {
192 if( !is_string( $url ) ) {
193 wfProfileOut( __METHOD__ );
194 throw new MWException( 'Bad purge URL' );
195 }
196 $url = SquidUpdate::expand( $url );
197 $conf = self::getRuleForURL( $url, $wgHTCPMulticastRouting );
198 if ( !$conf ) {
199 wfDebug( "No HTCP rule configured for URL $url , skipping\n" );
200 continue;
201 }
202 if ( !isset( $conf['host'] ) || !isset( $conf['port'] ) ) {
203 wfProfileOut( __METHOD__ );
204 throw new MWException( "Invalid HTCP rule for URL $url\n" );
205 }
206
207 // Construct a minimal HTCP request diagram
208 // as per RFC 2756
209 // Opcode 'CLR', no response desired, no auth
210 $htcpTransID = rand();
211
212 $htcpSpecifier = pack( 'na4na*na8n',
213 4, 'HEAD', strlen( $url ), $url,
214 8, 'HTTP/1.0', 0 );
215
216 $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
217 $htcpLen = 4 + $htcpDataLen + 2;
218
219 // Note! Squid gets the bit order of the first
220 // word wrong, wrt the RFC. Apparently no other
221 // implementation exists, so adapt to Squid
222 $htcpPacket = pack( 'nxxnCxNxxa*n',
223 $htcpLen, $htcpDataLen, $htcpOpCLR,
224 $htcpTransID, $htcpSpecifier, 2 );
225
226 // Send out
227 wfDebug( "Purging URL $url via HTCP\n" );
228 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
229 $conf['host'], $conf['port'] );
230 }
231 } else {
232 $errstr = socket_strerror( socket_last_error() );
233 wfDebug( __METHOD__ . "(): Error opening UDP socket: $errstr\n" );
234 }
235 wfProfileOut( __METHOD__ );
236 }
237
238 /**
239 * Expand local URLs to fully-qualified URLs using the internal protocol
240 * and host defined in $wgInternalServer. Input that's already fully-
241 * qualified will be passed through unchanged.
242 *
243 * This is used to generate purge URLs that may be either local to the
244 * main wiki or include a non-native host, such as images hosted on a
245 * second internal server.
246 *
247 * Client functions should not need to call this.
248 *
249 * @param $url string
250 *
251 * @return string
252 */
253 static function expand( $url ) {
254 return wfExpandUrl( $url, PROTO_INTERNAL );
255 }
256
257 /**
258 * Find the HTCP routing rule to use for a given URL.
259 * @param string $url URL to match
260 * @param array $rules Array of rules, see $wgHTCPMulticastRouting for format and behavior
261 * @return mixed Element of $rules that matched, or false if nothing matched
262 */
263 static function getRuleForURL( $url, $rules ) {
264 foreach ( $rules as $regex => $routing ) {
265 if ( $regex === '' || preg_match( $regex, $url ) ) {
266 return $routing;
267 }
268 }
269 return false;
270 }
271 }