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)
29 var $urlArr, $mMaxTitles;
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;
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;
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' ),
63 'pl_namespace' => $title->getNamespace(),
64 'pl_title' => $title->getDBkey(),
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 );
80 * Create a SquidUpdate from an array of Title objects, or a TitleArray object
82 * @param $titles array
83 * @param $urlArr array
87 static function newFromTitles( $titles, $urlArr = array() ) {
88 global $wgMaxSquidPurgeTitles;
90 foreach ( $titles as $title ) {
91 $urlArr[] = $title->getInternalURL();
92 if ( $i++
> $wgMaxSquidPurgeTitles ) {
96 return new SquidUpdate( $urlArr );
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
125 static function purge( $urlArr ) {
126 global $wgSquidServers, $wgHTCPRouting;
132 wfDebugLog( 'squid', __METHOD__
. ': ' . implode( ' ', $urlArr ) . "\n" );
134 if ( $wgHTCPRouting ) {
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 );
161 wfProfileOut( __METHOD__
);
165 * @throws MWException
166 * @param $urlArr array
168 static function HTCPPurge( $urlArr ) {
169 global $wgHTCPRouting, $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
);
184 $errstr = socket_strerror( socket_last_error() );
185 wfDebugLog( 'squid', __METHOD__
.
186 ": Error opening UDP socket: $errstr\n" );
187 wfProfileOut( __METHOD__
);
191 // Set socket options
192 socket_set_option( $conn, IPPROTO_IP
, IP_MULTICAST_LOOP
, 0 );
193 if ( $wgHTCPMulticastTTL != 1 ) {
194 socket_set_option( $conn, IPPROTO_IP
, IP_MULTICAST_TTL
,
195 $wgHTCPMulticastTTL );
198 $urlArr = array_unique( $urlArr ); // Remove duplicates
199 foreach ( $urlArr as $url ) {
200 if ( !is_string( $url ) ) {
201 wfProfileOut( __METHOD__
);
202 throw new MWException( 'Bad purge URL' );
204 $url = SquidUpdate
::expand( $url );
205 $conf = self
::getRuleForURL( $url, $wgHTCPRouting );
207 wfDebugLog( 'squid', __METHOD__
.
208 "No HTCP rule configured for URL $url , skipping\n" );
212 if( isset( $conf['host'] ) && isset( $conf['port'] ) ) {
213 // Normalize single entries
214 $conf = array( $conf );
216 foreach( $conf as $subconf ) {
217 if ( !isset( $subconf['host'] ) ||
!isset( $subconf['port'] ) ) {
218 wfProfileOut( __METHOD__
);
219 throw new MWException( "Invalid HTCP rule for URL $url\n" );
223 // Construct a minimal HTCP request diagram
225 // Opcode 'CLR', no response desired, no auth
226 $htcpTransID = rand();
228 $htcpSpecifier = pack( 'na4na*na8n',
229 4, 'HEAD', strlen( $url ), $url,
232 $htcpDataLen = 8 +
2 +
strlen( $htcpSpecifier );
233 $htcpLen = 4 +
$htcpDataLen +
2;
235 // Note! Squid gets the bit order of the first
236 // word wrong, wrt the RFC. Apparently no other
237 // implementation exists, so adapt to Squid
238 $htcpPacket = pack( 'nxxnCxNxxa*n',
239 $htcpLen, $htcpDataLen, $htcpOpCLR,
240 $htcpTransID, $htcpSpecifier, 2 );
242 wfDebugLog( 'squid', __METHOD__
.
243 "Purging URL $url via HTCP\n" );
244 foreach( $conf as $subconf ) {
245 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
246 $subconf['host'], $subconf['port'] );
249 wfProfileOut( __METHOD__
);
253 * Expand local URLs to fully-qualified URLs using the internal protocol
254 * and host defined in $wgInternalServer. Input that's already fully-
255 * qualified will be passed through unchanged.
257 * This is used to generate purge URLs that may be either local to the
258 * main wiki or include a non-native host, such as images hosted on a
259 * second internal server.
261 * Client functions should not need to call this.
267 static function expand( $url ) {
268 return wfExpandUrl( $url, PROTO_INTERNAL
);
272 * Find the HTCP routing rule to use for a given URL.
273 * @param string $url URL to match
274 * @param array $rules Array of rules, see $wgHTCPRouting for format and behavior
275 * @return mixed Element of $rules that matched, or false if nothing matched
277 static function getRuleForURL( $url, $rules ) {
278 foreach ( $rules as $regex => $routing ) {
279 if ( $regex === '' ||
preg_match( $regex, $url ) ) {