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
25 * Handles purging appropriate Squid URLs given a title (or titles)
30 * Collection of URLs to purge.
36 * @param array $urlArr Collection of URLs to purge
37 * @param bool|int $maxTitles Maximum number of unique URLs to purge
39 public function __construct( $urlArr = array(), $maxTitles = false ) {
40 global $wgMaxSquidPurgeTitles;
41 if ( $maxTitles === false ) {
42 $maxTitles = $wgMaxSquidPurgeTitles;
45 // Remove duplicate URLs from list
46 $urlArr = array_unique( $urlArr );
47 if ( count( $urlArr ) > $maxTitles ) {
48 // Truncate to desired maximum URL count
49 $urlArr = array_slice( $urlArr, 0, $maxTitles );
51 $this->urlArr
= $urlArr;
55 * Create a SquidUpdate from the given Title object.
57 * The resulting SquidUpdate will purge the given Title's URLs as well as
58 * the pages that link to it. Capped at $wgMaxSquidPurgeTitles total URLs.
63 public static function newFromLinksTo( Title
$title ) {
64 global $wgMaxSquidPurgeTitles;
65 wfProfileIn( __METHOD__
);
67 # Get a list of URLs linking to this page
68 $dbr = wfGetDB( DB_SLAVE
);
69 $res = $dbr->select( array( 'links', 'page' ),
70 array( 'page_namespace', 'page_title' ),
72 'pl_namespace' => $title->getNamespace(),
73 'pl_title' => $title->getDBkey(),
76 $blurlArr = $title->getSquidURLs();
77 if ( $res->numRows() <= $wgMaxSquidPurgeTitles ) {
78 foreach ( $res as $BL ) {
79 $tobj = Title
::makeTitle( $BL->page_namespace
, $BL->page_title
);
80 $blurlArr[] = $tobj->getInternalURL();
84 wfProfileOut( __METHOD__
);
86 return new SquidUpdate( $blurlArr );
90 * Create a SquidUpdate from an array of Title objects, or a TitleArray object
92 * @param array $titles
93 * @param array $urlArr
96 public static function newFromTitles( $titles, $urlArr = array() ) {
97 global $wgMaxSquidPurgeTitles;
99 /** @var Title $title */
100 foreach ( $titles as $title ) {
101 $urlArr[] = $title->getInternalURL();
102 if ( $i++
> $wgMaxSquidPurgeTitles ) {
107 return new SquidUpdate( $urlArr );
111 * @param Title $title
112 * @return SquidUpdate
114 public static function newSimplePurge( Title
$title ) {
115 $urlArr = $title->getSquidURLs();
117 return new SquidUpdate( $urlArr );
121 * Purges the list of URLs passed to the constructor.
123 public function doUpdate() {
124 self
::purge( $this->urlArr
);
128 * Purges a list of Squids defined in $wgSquidServers.
129 * $urlArr should contain the full URLs to purge as values
130 * (example: $urlArr[] = 'http://my.host/something')
131 * XXX report broken Squids per mail or log
133 * @param array $urlArr List of full URLs to purge
135 public static function purge( $urlArr ) {
136 global $wgSquidServers, $wgHTCPRouting;
142 wfDebugLog( 'squid', __METHOD__
. ': ' . implode( ' ', $urlArr ) );
144 if ( $wgHTCPRouting ) {
145 self
::HTCPPurge( $urlArr );
148 wfProfileIn( __METHOD__
);
150 // Remove duplicate URLs
151 $urlArr = array_unique( $urlArr );
152 // Maximum number of parallel connections per squid
153 $maxSocketsPerSquid = 8;
154 // Number of requests to send per socket
155 // 400 seems to be a good tradeoff, opening a socket takes a while
156 $urlsPerSocket = 400;
157 $socketsPerSquid = ceil( count( $urlArr ) / $urlsPerSocket );
158 if ( $socketsPerSquid > $maxSocketsPerSquid ) {
159 $socketsPerSquid = $maxSocketsPerSquid;
162 $pool = new SquidPurgeClientPool
;
163 $chunks = array_chunk( $urlArr, ceil( count( $urlArr ) / $socketsPerSquid ) );
164 foreach ( $wgSquidServers as $server ) {
165 foreach ( $chunks as $chunk ) {
166 $client = new SquidPurgeClient( $server );
167 foreach ( $chunk as $url ) {
168 $client->queuePurge( $url );
170 $pool->addClient( $client );
175 wfProfileOut( __METHOD__
);
179 * Send Hyper Text Caching Protocol (HTCP) CLR requests.
181 * @throws MWException
182 * @param array $urlArr Collection of URLs to purge
184 public static function HTCPPurge( $urlArr ) {
185 global $wgHTCPRouting, $wgHTCPMulticastTTL;
186 wfProfileIn( __METHOD__
);
188 // HTCP CLR operation
191 // @todo FIXME: PHP doesn't support these socket constants (include/linux/in.h)
192 if ( !defined( "IPPROTO_IP" ) ) {
193 define( "IPPROTO_IP", 0 );
194 define( "IP_MULTICAST_LOOP", 34 );
195 define( "IP_MULTICAST_TTL", 33 );
198 // pfsockopen doesn't work because we need set_sock_opt
199 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
201 $errstr = socket_strerror( socket_last_error() );
202 wfDebugLog( 'squid', __METHOD__
.
203 ": Error opening UDP socket: $errstr" );
204 wfProfileOut( __METHOD__
);
209 // Set socket options
210 socket_set_option( $conn, IPPROTO_IP
, IP_MULTICAST_LOOP
, 0 );
211 if ( $wgHTCPMulticastTTL != 1 ) {
212 // Set multicast time to live (hop count) option on socket
213 socket_set_option( $conn, IPPROTO_IP
, IP_MULTICAST_TTL
,
214 $wgHTCPMulticastTTL );
217 // Remove duplicate URLs from collection
218 $urlArr = array_unique( $urlArr );
219 // Get sequential trx IDs for packet loss counting
220 $ids = UIDGenerator
::newSequentialPerNodeIDs(
221 'squidhtcppurge', 32, count( $urlArr ), UIDGenerator
::QUICK_VOLATILE
224 foreach ( $urlArr as $url ) {
225 if ( !is_string( $url ) ) {
226 wfProfileOut( __METHOD__
);
227 throw new MWException( 'Bad purge URL' );
229 $url = self
::expand( $url );
230 $conf = self
::getRuleForURL( $url, $wgHTCPRouting );
232 wfDebugLog( 'squid', __METHOD__
.
233 "No HTCP rule configured for URL {$url} , skipping" );
237 if ( isset( $conf['host'] ) && isset( $conf['port'] ) ) {
238 // Normalize single entries
239 $conf = array( $conf );
241 foreach ( $conf as $subconf ) {
242 if ( !isset( $subconf['host'] ) ||
!isset( $subconf['port'] ) ) {
243 wfProfileOut( __METHOD__
);
244 throw new MWException( "Invalid HTCP rule for URL $url\n" );
248 // Construct a minimal HTCP request diagram
250 // Opcode 'CLR', no response desired, no auth
251 $htcpTransID = current( $ids );
254 $htcpSpecifier = pack( 'na4na*na8n',
255 4, 'HEAD', strlen( $url ), $url,
258 $htcpDataLen = 8 +
2 +
strlen( $htcpSpecifier );
259 $htcpLen = 4 +
$htcpDataLen +
2;
261 // Note! Squid gets the bit order of the first
262 // word wrong, wrt the RFC. Apparently no other
263 // implementation exists, so adapt to Squid
264 $htcpPacket = pack( 'nxxnCxNxxa*n',
265 $htcpLen, $htcpDataLen, $htcpOpCLR,
266 $htcpTransID, $htcpSpecifier, 2 );
268 wfDebugLog( 'squid', __METHOD__
.
269 "Purging URL $url via HTCP" );
270 foreach ( $conf as $subconf ) {
271 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
272 $subconf['host'], $subconf['port'] );
275 wfProfileOut( __METHOD__
);
279 * Expand local URLs to fully-qualified URLs using the internal protocol
280 * and host defined in $wgInternalServer. Input that's already fully-
281 * qualified will be passed through unchanged.
283 * This is used to generate purge URLs that may be either local to the
284 * main wiki or include a non-native host, such as images hosted on a
285 * second internal server.
287 * Client functions should not need to call this.
292 public static function expand( $url ) {
293 return wfExpandUrl( $url, PROTO_INTERNAL
);
297 * Find the HTCP routing rule to use for a given URL.
298 * @param string $url URL to match
299 * @param array $rules Array of rules, see $wgHTCPRouting for format and behavior
300 * @return mixed Element of $rules that matched, or false if nothing matched
302 private static function getRuleForURL( $url, $rules ) {
303 foreach ( $rules as $regex => $routing ) {
304 if ( $regex === '' ||
preg_match( $regex, $url ) ) {