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;
31 function __construct( $urlArr = Array(), $maxTitles = false ) {
32 global $wgMaxSquidPurgeTitles;
33 if ( $maxTitles === false ) {
34 $this->mMaxTitles
= $wgMaxSquidPurgeTitles;
36 $this->mMaxTitles
= $maxTitles;
38 $urlArr = array_unique( $urlArr ); // Remove duplicates
39 if ( count( $urlArr ) > $this->mMaxTitles
) {
40 $urlArr = array_slice( $urlArr, 0, $this->mMaxTitles
);
42 $this->urlArr
= $urlArr;
50 static function newFromLinksTo( &$title ) {
51 global $wgMaxSquidPurgeTitles;
52 wfProfileIn( __METHOD__
);
54 # Get a list of URLs linking to this page
55 $dbr = wfGetDB( DB_SLAVE
);
56 $res = $dbr->select( array( 'links', 'page' ),
57 array( 'page_namespace', 'page_title' ),
59 'pl_namespace' => $title->getNamespace(),
60 'pl_title' => $title->getDBkey(),
63 $blurlArr = $title->getSquidURLs();
64 if ( $dbr->numRows( $res ) <= $wgMaxSquidPurgeTitles ) {
65 foreach ( $res as $BL ) {
66 $tobj = Title
::makeTitle( $BL->page_namespace
, $BL->page_title
) ;
67 $blurlArr[] = $tobj->getInternalURL();
71 wfProfileOut( __METHOD__
);
72 return new SquidUpdate( $blurlArr );
76 * Create a SquidUpdate from an array of Title objects, or a TitleArray object
78 * @param $titles array
79 * @param $urlArr array
83 static function newFromTitles( $titles, $urlArr = array() ) {
84 global $wgMaxSquidPurgeTitles;
86 foreach ( $titles as $title ) {
87 $urlArr[] = $title->getInternalURL();
88 if ( $i++
> $wgMaxSquidPurgeTitles ) {
92 return new SquidUpdate( $urlArr );
100 static function newSimplePurge( &$title ) {
101 $urlArr = $title->getSquidURLs();
102 return new SquidUpdate( $urlArr );
106 * Purges the list of URLs passed to the constructor
108 function doUpdate() {
109 SquidUpdate
::purge( $this->urlArr
);
113 * Purges a list of Squids defined in $wgSquidServers.
114 * $urlArr should contain the full URLs to purge as values
115 * (example: $urlArr[] = 'http://my.host/something')
116 * XXX report broken Squids per mail or log
118 * @param $urlArr array
121 static function purge( $urlArr ) {
122 global $wgSquidServers, $wgHTCPMulticastRouting;
124 /*if ( (@$wgSquidServers[0]) == 'echo' ) {
125 echo implode("<br />\n", $urlArr) . "<br />\n";
133 if ( $wgHTCPMulticastRouting ) {
134 SquidUpdate
::HTCPPurge( $urlArr );
137 wfProfileIn( __METHOD__
);
139 $urlArr = array_unique( $urlArr ); // Remove duplicates
140 $maxSocketsPerSquid = 8; // socket cap per Squid
141 $urlsPerSocket = 400; // 400 seems to be a good tradeoff, opening a socket takes a while
142 $socketsPerSquid = ceil( count( $urlArr ) / $urlsPerSocket );
143 if ( $socketsPerSquid > $maxSocketsPerSquid ) {
144 $socketsPerSquid = $maxSocketsPerSquid;
147 $pool = new SquidPurgeClientPool
;
148 $chunks = array_chunk( $urlArr, ceil( count( $urlArr ) / $socketsPerSquid ) );
149 foreach ( $wgSquidServers as $server ) {
150 foreach ( $chunks as $chunk ) {
151 $client = new SquidPurgeClient( $server );
152 foreach ( $chunk as $url ) {
153 $client->queuePurge( $url );
155 $pool->addClient( $client );
160 wfProfileOut( __METHOD__
);
164 * @throws MWException
165 * @param $urlArr array
167 static function HTCPPurge( $urlArr ) {
168 global $wgHTCPMulticastRouting, $wgHTCPMulticastTTL;
169 wfProfileIn( __METHOD__
);
171 $htcpOpCLR = 4; // HTCP CLR
173 // @todo FIXME: PHP doesn't support these socket constants (include/linux/in.h)
174 if( !defined( "IPPROTO_IP" ) ) {
175 define( "IPPROTO_IP", 0 );
176 define( "IP_MULTICAST_LOOP", 34 );
177 define( "IP_MULTICAST_TTL", 33 );
180 // pfsockopen doesn't work because we need set_sock_opt
181 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
183 // Set socket options
184 socket_set_option( $conn, IPPROTO_IP
, IP_MULTICAST_LOOP
, 0 );
185 if ( $wgHTCPMulticastTTL != 1 )
186 socket_set_option( $conn, IPPROTO_IP
, IP_MULTICAST_TTL
,
187 $wgHTCPMulticastTTL );
189 $urlArr = array_unique( $urlArr ); // Remove duplicates
190 foreach ( $urlArr as $url ) {
191 if( !is_string( $url ) ) {
192 throw new MWException( 'Bad purge URL' );
194 $url = SquidUpdate
::expand( $url );
195 $conf = self
::getRuleForURL( $url, $wgHTCPMulticastRouting );
197 wfDebug( "No HTCP rule configured for URL $url , skipping\n" );
200 if ( !isset( $conf['host'] ) ||
!isset( $conf['port'] ) ) {
201 throw new MWException( "Invalid HTCP rule for URL $url\n" );
204 // Construct a minimal HTCP request diagram
206 // Opcode 'CLR', no response desired, no auth
207 $htcpTransID = rand();
209 $htcpSpecifier = pack( 'na4na*na8n',
210 4, 'HEAD', strlen( $url ), $url,
213 $htcpDataLen = 8 +
2 +
strlen( $htcpSpecifier );
214 $htcpLen = 4 +
$htcpDataLen +
2;
216 // Note! Squid gets the bit order of the first
217 // word wrong, wrt the RFC. Apparently no other
218 // implementation exists, so adapt to Squid
219 $htcpPacket = pack( 'nxxnCxNxxa*n',
220 $htcpLen, $htcpDataLen, $htcpOpCLR,
221 $htcpTransID, $htcpSpecifier, 2);
224 wfDebug( "Purging URL $url via HTCP\n" );
225 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
226 $conf['host'], $conf['port'] );
229 $errstr = socket_strerror( socket_last_error() );
230 wfDebug( __METHOD__
. "(): Error opening UDP socket: $errstr\n" );
232 wfProfileOut( __METHOD__
);
236 * Expand local URLs to fully-qualified URLs using the internal protocol
237 * and host defined in $wgInternalServer. Input that's already fully-
238 * qualified will be passed through unchanged.
240 * This is used to generate purge URLs that may be either local to the
241 * main wiki or include a non-native host, such as images hosted on a
242 * second internal server.
244 * Client functions should not need to call this.
250 static function expand( $url ) {
251 return wfExpandUrl( $url, PROTO_INTERNAL
);
255 * Find the HTCP routing rule to use for a given URL.
256 * @param $url string URL to match
257 * @param $rules array Array of rules, see $wgHTCPMulticastRouting for format and behavior
258 * @return mixed Element of $rules that matched, or false if nothing matched
260 static function getRuleForURL( $url, $rules ) {
261 foreach ( $rules as $regex => $routing ) {
262 if ( $regex === '' ||
preg_match( $regex, $url ) ) {