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)
28 class SquidUpdate
implements DeferrableUpdate
{
29 /** @var string[] Collection of URLs to purge */
30 protected $urls = array();
33 * @param array $urlArr Collection of URLs to purge
35 public function __construct( array $urlArr ) {
36 // Remove duplicate URLs from list
37 $this->urls
= array_unique( $urlArr );
41 * Create a SquidUpdate from an array of Title objects, or a TitleArray object
43 * @param Traversable|array $titles
44 * @param array $urlArr
47 public static function newFromTitles( $titles, $urlArr = array() ) {
48 /** @var Title $title */
49 foreach ( $titles as $title ) {
50 $urlArr = array_merge( $urlArr, $title->getSquidURLs() );
53 return new SquidUpdate( $urlArr );
61 public static function newSimplePurge( Title
$title ) {
62 $urlArr = $title->getSquidURLs();
64 return new SquidUpdate( $urlArr );
68 * Purges the list of URLs passed to the constructor.
70 public function doUpdate() {
71 self
::purge( $this->urls
);
75 * Purges a list of Squids defined in $wgSquidServers.
76 * $urlArr should contain the full URLs to purge as values
77 * (example: $urlArr[] = 'http://my.host/something')
78 * XXX report broken Squids per mail or log
80 * @param array $urlArr List of full URLs to purge
82 public static function purge( array $urlArr ) {
83 global $wgSquidServers, $wgHTCPRouting;
89 wfDebugLog( 'squid', __METHOD__
. ': ' . implode( ' ', $urlArr ) );
91 if ( $wgHTCPRouting ) {
92 self
::HTCPPurge( $urlArr );
95 if ( $wgSquidServers ) {
96 // Remove duplicate URLs
97 $urlArr = array_unique( $urlArr );
98 // Maximum number of parallel connections per squid
99 $maxSocketsPerSquid = 8;
100 // Number of requests to send per socket
101 // 400 seems to be a good tradeoff, opening a socket takes a while
102 $urlsPerSocket = 400;
103 $socketsPerSquid = ceil( count( $urlArr ) / $urlsPerSocket );
104 if ( $socketsPerSquid > $maxSocketsPerSquid ) {
105 $socketsPerSquid = $maxSocketsPerSquid;
108 $pool = new SquidPurgeClientPool
;
109 $chunks = array_chunk( $urlArr, ceil( count( $urlArr ) / $socketsPerSquid ) );
110 foreach ( $wgSquidServers as $server ) {
111 foreach ( $chunks as $chunk ) {
112 $client = new SquidPurgeClient( $server );
113 foreach ( $chunk as $url ) {
114 $client->queuePurge( $url );
116 $pool->addClient( $client );
125 * Send Hyper Text Caching Protocol (HTCP) CLR requests.
127 * @throws MWException
128 * @param array $urlArr Collection of URLs to purge
130 protected static function HTCPPurge( $urlArr ) {
131 global $wgHTCPRouting, $wgHTCPMulticastTTL;
133 // HTCP CLR operation
136 // @todo FIXME: PHP doesn't support these socket constants (include/linux/in.h)
137 if ( !defined( "IPPROTO_IP" ) ) {
138 define( "IPPROTO_IP", 0 );
139 define( "IP_MULTICAST_LOOP", 34 );
140 define( "IP_MULTICAST_TTL", 33 );
143 // pfsockopen doesn't work because we need set_sock_opt
144 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
146 $errstr = socket_strerror( socket_last_error() );
147 wfDebugLog( 'squid', __METHOD__
.
148 ": Error opening UDP socket: $errstr" );
153 // Set socket options
154 socket_set_option( $conn, IPPROTO_IP
, IP_MULTICAST_LOOP
, 0 );
155 if ( $wgHTCPMulticastTTL != 1 ) {
156 // Set multicast time to live (hop count) option on socket
157 socket_set_option( $conn, IPPROTO_IP
, IP_MULTICAST_TTL
,
158 $wgHTCPMulticastTTL );
161 // Remove duplicate URLs from collection
162 $urlArr = array_unique( $urlArr );
163 // Get sequential trx IDs for packet loss counting
164 $ids = UIDGenerator
::newSequentialPerNodeIDs(
165 'squidhtcppurge', 32, count( $urlArr ), UIDGenerator
::QUICK_VOLATILE
168 foreach ( $urlArr as $url ) {
169 if ( !is_string( $url ) ) {
170 throw new MWException( 'Bad purge URL' );
172 $url = self
::expand( $url );
173 $conf = self
::getRuleForURL( $url, $wgHTCPRouting );
175 wfDebugLog( 'squid', __METHOD__
.
176 "No HTCP rule configured for URL {$url} , skipping" );
180 if ( isset( $conf['host'] ) && isset( $conf['port'] ) ) {
181 // Normalize single entries
182 $conf = array( $conf );
184 foreach ( $conf as $subconf ) {
185 if ( !isset( $subconf['host'] ) ||
!isset( $subconf['port'] ) ) {
186 throw new MWException( "Invalid HTCP rule for URL $url\n" );
190 // Construct a minimal HTCP request diagram
192 // Opcode 'CLR', no response desired, no auth
193 $htcpTransID = current( $ids );
196 $htcpSpecifier = pack( 'na4na*na8n',
197 4, 'HEAD', strlen( $url ), $url,
200 $htcpDataLen = 8 +
2 +
strlen( $htcpSpecifier );
201 $htcpLen = 4 +
$htcpDataLen +
2;
203 // Note! Squid gets the bit order of the first
204 // word wrong, wrt the RFC. Apparently no other
205 // implementation exists, so adapt to Squid
206 $htcpPacket = pack( 'nxxnCxNxxa*n',
207 $htcpLen, $htcpDataLen, $htcpOpCLR,
208 $htcpTransID, $htcpSpecifier, 2 );
210 wfDebugLog( 'squid', __METHOD__
.
211 "Purging URL $url via HTCP" );
212 foreach ( $conf as $subconf ) {
213 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
214 $subconf['host'], $subconf['port'] );
220 * Expand local URLs to fully-qualified URLs using the internal protocol
221 * and host defined in $wgInternalServer. Input that's already fully-
222 * qualified will be passed through unchanged.
224 * This is used to generate purge URLs that may be either local to the
225 * main wiki or include a non-native host, such as images hosted on a
226 * second internal server.
228 * Client functions should not need to call this.
233 public static function expand( $url ) {
234 return wfExpandUrl( $url, PROTO_INTERNAL
);
238 * Find the HTCP routing rule to use for a given URL.
239 * @param string $url URL to match
240 * @param array $rules Array of rules, see $wgHTCPRouting for format and behavior
241 * @return mixed Element of $rules that matched, or false if nothing matched
243 private static function getRuleForURL( $url, $rules ) {
244 foreach ( $rules as $regex => $routing ) {
245 if ( $regex === '' ||
preg_match( $regex, $url ) ) {