improve explanations of email confirmations
[mediawiki.git] / includes / cache / SquidUpdate.php
blob0ee41e52e719307fd7c4ccf38f2a50e4547493ec
1 <?php
2 /**
3 * Squid cache purging.
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.
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.
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
20 * @file
21 * @ingroup Cache
24 /**
25 * Handles purging appropriate Squid URLs given a title (or titles)
26 * @ingroup Cache
28 class SquidUpdate {
29 var $urlArr, $mMaxTitles;
31 /**
32 * @param $urlArr array
33 * @param $maxTitles bool|int
35 function __construct( $urlArr = array(), $maxTitles = false ) {
36 global $wgMaxSquidPurgeTitles;
37 if ( $maxTitles === false ) {
38 $this->mMaxTitles = $wgMaxSquidPurgeTitles;
39 } else {
40 $this->mMaxTitles = $maxTitles;
42 $urlArr = array_unique( $urlArr ); // Remove duplicates
43 if ( count( $urlArr ) > $this->mMaxTitles ) {
44 $urlArr = array_slice( $urlArr, 0, $this->mMaxTitles );
46 $this->urlArr = $urlArr;
49 /**
50 * @param $title Title
52 * @return SquidUpdate
54 static function newFromLinksTo( &$title ) {
55 global $wgMaxSquidPurgeTitles;
56 wfProfileIn( __METHOD__ );
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();
75 wfProfileOut( __METHOD__ );
76 return new SquidUpdate( $blurlArr );
79 /**
80 * Create a SquidUpdate from an array of Title objects, or a TitleArray object
82 * @param $titles array
83 * @param $urlArr array
85 * @return SquidUpdate
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;
96 return new SquidUpdate( $urlArr );
99 /**
100 * @param $title Title
102 * @return SquidUpdate
104 static function newSimplePurge( &$title ) {
105 $urlArr = $title->getSquidURLs();
106 return new SquidUpdate( $urlArr );
110 * Purges the list of URLs passed to the constructor
112 function doUpdate() {
113 SquidUpdate::purge( $this->urlArr );
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
122 * @param $urlArr array
123 * @return void
125 static function purge( $urlArr ) {
126 global $wgSquidServers, $wgHTCPMulticastRouting;
128 if ( !$urlArr ) {
129 return;
132 wfDebugLog( 'squid', __METHOD__ . ': ' . implode( ' ', $urlArr ) . "\n" );
134 if ( $wgHTCPMulticastRouting ) {
135 SquidUpdate::HTCPPurge( $urlArr );
138 wfProfileIn( __METHOD__ );
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;
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 );
156 $pool->addClient( $client );
159 $pool->run();
161 wfProfileOut( __METHOD__ );
165 * @throws MWException
166 * @param $urlArr array
168 static function HTCPPurge( $urlArr ) {
169 global $wgHTCPMulticastRouting, $wgHTCPMulticastTTL;
170 wfProfileIn( __METHOD__ );
172 $htcpOpCLR = 4; // HTCP CLR
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 );
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 );
191 $urlArr = array_unique( $urlArr ); // Remove duplicates
192 foreach ( $urlArr as $url ) {
193 if ( !is_string( $url ) ) {
194 wfProfileOut( __METHOD__ );
195 throw new MWException( 'Bad purge URL' );
197 $url = SquidUpdate::expand( $url );
198 $conf = self::getRuleForURL( $url, $wgHTCPMulticastRouting );
199 if ( !$conf ) {
200 wfDebugLog( 'squid', __METHOD__ .
201 "No HTCP rule configured for URL $url , skipping\n" );
202 continue;
204 if ( !isset( $conf['host'] ) || !isset( $conf['port'] ) ) {
205 wfProfileOut( __METHOD__ );
206 throw new MWException( "Invalid HTCP rule for URL $url\n" );
209 // Construct a minimal HTCP request diagram
210 // as per RFC 2756
211 // Opcode 'CLR', no response desired, no auth
212 $htcpTransID = rand();
214 $htcpSpecifier = pack( 'na4na*na8n',
215 4, 'HEAD', strlen( $url ), $url,
216 8, 'HTTP/1.0', 0 );
218 $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
219 $htcpLen = 4 + $htcpDataLen + 2;
221 // Note! Squid gets the bit order of the first
222 // word wrong, wrt the RFC. Apparently no other
223 // implementation exists, so adapt to Squid
224 $htcpPacket = pack( 'nxxnCxNxxa*n',
225 $htcpLen, $htcpDataLen, $htcpOpCLR,
226 $htcpTransID, $htcpSpecifier, 2 );
228 // Send out
229 wfDebugLog( 'squid', __METHOD__ .
230 "Purging URL $url via HTCP\n" );
231 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
232 $conf['host'], $conf['port'] );
234 } else {
235 $errstr = socket_strerror( socket_last_error() );
236 wfDebugLog( 'squid', __METHOD__ .
237 ": Error opening UDP socket: $errstr\n" );
239 wfProfileOut( __METHOD__ );
243 * Expand local URLs to fully-qualified URLs using the internal protocol
244 * and host defined in $wgInternalServer. Input that's already fully-
245 * qualified will be passed through unchanged.
247 * This is used to generate purge URLs that may be either local to the
248 * main wiki or include a non-native host, such as images hosted on a
249 * second internal server.
251 * Client functions should not need to call this.
253 * @param $url string
255 * @return string
257 static function expand( $url ) {
258 return wfExpandUrl( $url, PROTO_INTERNAL );
262 * Find the HTCP routing rule to use for a given URL.
263 * @param string $url URL to match
264 * @param array $rules Array of rules, see $wgHTCPMulticastRouting for format and behavior
265 * @return mixed Element of $rules that matched, or false if nothing matched
267 static function getRuleForURL( $url, $rules ) {
268 foreach ( $rules as $regex => $routing ) {
269 if ( $regex === '' || preg_match( $regex, $url ) ) {
270 return $routing;
273 return false;