Fixed spacing in db/debug/diff/externalstore/objectcache folder
[mediawiki.git] / includes / cache / SquidUpdate.php
blobdd7180173a0a5686112a2b2f43bfd3f756e20e85
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 wfDebug( "Squid purge: " . 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 wfDebug( "No HTCP rule configured for URL $url , skipping\n" );
201 continue;
203 if ( !isset( $conf['host'] ) || !isset( $conf['port'] ) ) {
204 wfProfileOut( __METHOD__ );
205 throw new MWException( "Invalid HTCP rule for URL $url\n" );
208 // Construct a minimal HTCP request diagram
209 // as per RFC 2756
210 // Opcode 'CLR', no response desired, no auth
211 $htcpTransID = rand();
213 $htcpSpecifier = pack( 'na4na*na8n',
214 4, 'HEAD', strlen( $url ), $url,
215 8, 'HTTP/1.0', 0 );
217 $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
218 $htcpLen = 4 + $htcpDataLen + 2;
220 // Note! Squid gets the bit order of the first
221 // word wrong, wrt the RFC. Apparently no other
222 // implementation exists, so adapt to Squid
223 $htcpPacket = pack( 'nxxnCxNxxa*n',
224 $htcpLen, $htcpDataLen, $htcpOpCLR,
225 $htcpTransID, $htcpSpecifier, 2 );
227 // Send out
228 wfDebug( "Purging URL $url via HTCP\n" );
229 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
230 $conf['host'], $conf['port'] );
232 } else {
233 $errstr = socket_strerror( socket_last_error() );
234 wfDebug( __METHOD__ . "(): Error opening UDP socket: $errstr\n" );
236 wfProfileOut( __METHOD__ );
240 * Expand local URLs to fully-qualified URLs using the internal protocol
241 * and host defined in $wgInternalServer. Input that's already fully-
242 * qualified will be passed through unchanged.
244 * This is used to generate purge URLs that may be either local to the
245 * main wiki or include a non-native host, such as images hosted on a
246 * second internal server.
248 * Client functions should not need to call this.
250 * @param $url string
252 * @return string
254 static function expand( $url ) {
255 return wfExpandUrl( $url, PROTO_INTERNAL );
259 * Find the HTCP routing rule to use for a given URL.
260 * @param string $url URL to match
261 * @param array $rules Array of rules, see $wgHTCPMulticastRouting for format and behavior
262 * @return mixed Element of $rules that matched, or false if nothing matched
264 static function getRuleForURL( $url, $rules ) {
265 foreach ( $rules as $regex => $routing ) {
266 if ( $regex === '' || preg_match( $regex, $url ) ) {
267 return $routing;
270 return false;