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
24 use Wikimedia\Assert\Assert
;
27 * Handles purging appropriate Squid URLs given a title (or titles)
30 class CdnCacheUpdate
implements DeferrableUpdate
, MergeableUpdate
{
31 /** @var string[] Collection of URLs to purge */
32 protected $urls = array();
35 * @param string[] $urlArr Collection of URLs to purge
37 public function __construct( array $urlArr ) {
38 $this->urls
= $urlArr;
42 * Create an update object from an array of Title objects, or a TitleArray object
44 * @param Traversable|array $titles
45 * @param string[] $urlArr
46 * @return CdnCacheUpdate
48 public static function newFromTitles( $titles, $urlArr = array() ) {
49 /** @var Title $title */
50 foreach ( $titles as $title ) {
51 $urlArr = array_merge( $urlArr, $title->getSquidURLs() );
54 return new CdnCacheUpdate( $urlArr );
59 * @return CdnCacheUpdate
62 public static function newSimplePurge( Title
$title ) {
63 return new CdnCacheUpdate( $title->getSquidURLs() );
67 * Purges the list of URLs passed to the constructor.
69 public function doUpdate() {
70 self
::purge( $this->urls
);
73 public function merge( MergeableUpdate
$update ) {
74 /** @var CdnCacheUpdate $update */
75 Assert
::parameterType( __CLASS__
, $update, '$update' );
77 $this->urls
= array_merge( $this->urls
, $update->urls
);
81 * Purges a list of Squids defined in $wgSquidServers.
82 * $urlArr should contain the full URLs to purge as values
83 * (example: $urlArr[] = 'http://my.host/something')
84 * XXX report broken Squids per mail or log
86 * @param string[] $urlArr List of full URLs to purge
88 public static function purge( array $urlArr ) {
89 global $wgSquidServers, $wgHTCPRouting;
95 // Remove duplicate URLs from list
96 $urlArr = array_unique( $urlArr );
98 wfDebugLog( 'squid', __METHOD__
. ': ' . implode( ' ', $urlArr ) );
100 if ( $wgHTCPRouting ) {
101 self
::HTCPPurge( $urlArr );
104 if ( $wgSquidServers ) {
105 // Maximum number of parallel connections per squid
106 $maxSocketsPerSquid = 8;
107 // Number of requests to send per socket
108 // 400 seems to be a good tradeoff, opening a socket takes a while
109 $urlsPerSocket = 400;
110 $socketsPerSquid = ceil( count( $urlArr ) / $urlsPerSocket );
111 if ( $socketsPerSquid > $maxSocketsPerSquid ) {
112 $socketsPerSquid = $maxSocketsPerSquid;
115 $pool = new SquidPurgeClientPool
;
116 $chunks = array_chunk( $urlArr, ceil( count( $urlArr ) / $socketsPerSquid ) );
117 foreach ( $wgSquidServers as $server ) {
118 foreach ( $chunks as $chunk ) {
119 $client = new SquidPurgeClient( $server );
120 foreach ( $chunk as $url ) {
121 $client->queuePurge( $url );
123 $pool->addClient( $client );
132 * Send Hyper Text Caching Protocol (HTCP) CLR requests.
134 * @throws MWException
135 * @param string[] $urlArr Collection of URLs to purge
137 private static function HTCPPurge( array $urlArr ) {
138 global $wgHTCPRouting, $wgHTCPMulticastTTL;
140 // HTCP CLR operation
143 // @todo FIXME: PHP doesn't support these socket constants (include/linux/in.h)
144 if ( !defined( "IPPROTO_IP" ) ) {
145 define( "IPPROTO_IP", 0 );
146 define( "IP_MULTICAST_LOOP", 34 );
147 define( "IP_MULTICAST_TTL", 33 );
150 // pfsockopen doesn't work because we need set_sock_opt
151 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
153 $errstr = socket_strerror( socket_last_error() );
154 wfDebugLog( 'squid', __METHOD__
.
155 ": Error opening UDP socket: $errstr" );
160 // Set socket options
161 socket_set_option( $conn, IPPROTO_IP
, IP_MULTICAST_LOOP
, 0 );
162 if ( $wgHTCPMulticastTTL != 1 ) {
163 // Set multicast time to live (hop count) option on socket
164 socket_set_option( $conn, IPPROTO_IP
, IP_MULTICAST_TTL
,
165 $wgHTCPMulticastTTL );
168 // Get sequential trx IDs for packet loss counting
169 $ids = UIDGenerator
::newSequentialPerNodeIDs(
170 'squidhtcppurge', 32, count( $urlArr ), UIDGenerator
::QUICK_VOLATILE
173 foreach ( $urlArr as $url ) {
174 if ( !is_string( $url ) ) {
175 throw new MWException( 'Bad purge URL' );
177 $url = self
::expand( $url );
178 $conf = self
::getRuleForURL( $url, $wgHTCPRouting );
180 wfDebugLog( 'squid', __METHOD__
.
181 "No HTCP rule configured for URL {$url} , skipping" );
185 if ( isset( $conf['host'] ) && isset( $conf['port'] ) ) {
186 // Normalize single entries
187 $conf = array( $conf );
189 foreach ( $conf as $subconf ) {
190 if ( !isset( $subconf['host'] ) ||
!isset( $subconf['port'] ) ) {
191 throw new MWException( "Invalid HTCP rule for URL $url\n" );
195 // Construct a minimal HTCP request diagram
197 // Opcode 'CLR', no response desired, no auth
198 $htcpTransID = current( $ids );
201 $htcpSpecifier = pack( 'na4na*na8n',
202 4, 'HEAD', strlen( $url ), $url,
205 $htcpDataLen = 8 +
2 +
strlen( $htcpSpecifier );
206 $htcpLen = 4 +
$htcpDataLen +
2;
208 // Note! Squid gets the bit order of the first
209 // word wrong, wrt the RFC. Apparently no other
210 // implementation exists, so adapt to Squid
211 $htcpPacket = pack( 'nxxnCxNxxa*n',
212 $htcpLen, $htcpDataLen, $htcpOpCLR,
213 $htcpTransID, $htcpSpecifier, 2 );
215 wfDebugLog( 'squid', __METHOD__
.
216 "Purging URL $url via HTCP" );
217 foreach ( $conf as $subconf ) {
218 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
219 $subconf['host'], $subconf['port'] );
225 * Expand local URLs to fully-qualified URLs using the internal protocol
226 * and host defined in $wgInternalServer. Input that's already fully-
227 * qualified will be passed through unchanged.
229 * This is used to generate purge URLs that may be either local to the
230 * main wiki or include a non-native host, such as images hosted on a
231 * second internal server.
233 * Client functions should not need to call this.
238 public static function expand( $url ) {
239 return wfExpandUrl( $url, PROTO_INTERNAL
);
243 * Find the HTCP routing rule to use for a given URL.
244 * @param string $url URL to match
245 * @param array $rules Array of rules, see $wgHTCPRouting for format and behavior
246 * @return mixed Element of $rules that matched, or false if nothing matched
248 private static function getRuleForURL( $url, $rules ) {
249 foreach ( $rules as $regex => $routing ) {
250 if ( $regex === '' ||
preg_match( $regex, $url ) ) {
260 * @deprecated since 1.27
262 class SquidUpdate
extends CdnCacheUpdate
{
263 // Keep class name for b/c