2004-02-18 02:15:00 +00:00
|
|
|
<?php
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2012-05-08 12:51:21 +00:00
|
|
|
* Squid cache purging.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @file
|
|
|
|
|
* @ingroup Cache
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-01-30 16:06:22 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2008-10-06 15:31:03 +00:00
|
|
|
* Handles purging appropriate Squid URLs given a title (or titles)
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Cache
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-01-30 16:06:22 +00:00
|
|
|
class SquidUpdate {
|
2006-05-11 22:40:38 +00:00
|
|
|
var $urlArr, $mMaxTitles;
|
2004-03-20 15:03:26 +00:00
|
|
|
|
2007-01-20 13:34:31 +00:00
|
|
|
function __construct( $urlArr = Array(), $maxTitles = false ) {
|
2004-06-30 03:39:48 +00:00
|
|
|
global $wgMaxSquidPurgeTitles;
|
|
|
|
|
if ( $maxTitles === false ) {
|
|
|
|
|
$this->mMaxTitles = $wgMaxSquidPurgeTitles;
|
|
|
|
|
} else {
|
|
|
|
|
$this->mMaxTitles = $maxTitles;
|
|
|
|
|
}
|
2012-04-06 18:20:04 +00:00
|
|
|
$urlArr = array_unique( $urlArr ); // Remove duplicates
|
2004-06-30 03:39:48 +00:00
|
|
|
if ( count( $urlArr ) > $this->mMaxTitles ) {
|
|
|
|
|
$urlArr = array_slice( $urlArr, 0, $this->mMaxTitles );
|
|
|
|
|
}
|
2004-03-08 09:43:36 +00:00
|
|
|
$this->urlArr = $urlArr;
|
|
|
|
|
}
|
2004-01-30 16:06:22 +00:00
|
|
|
|
2011-04-25 22:41:54 +00:00
|
|
|
/**
|
|
|
|
|
* @param $title Title
|
2011-07-12 19:59:35 +00:00
|
|
|
*
|
2011-04-25 22:41:54 +00:00
|
|
|
* @return SquidUpdate
|
|
|
|
|
*/
|
2007-05-02 15:36:17 +00:00
|
|
|
static function newFromLinksTo( &$title ) {
|
2010-07-26 20:51:33 +00:00
|
|
|
global $wgMaxSquidPurgeTitles;
|
2009-11-03 17:49:49 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2004-07-18 08:48:43 +00:00
|
|
|
|
2004-03-20 15:03:26 +00:00
|
|
|
# Get a list of URLs linking to this page
|
2007-01-22 23:50:42 +00:00
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
2005-05-26 10:23:36 +00:00
|
|
|
$res = $dbr->select( array( 'links', 'page' ),
|
|
|
|
|
array( 'page_namespace', 'page_title' ),
|
|
|
|
|
array(
|
|
|
|
|
'pl_namespace' => $title->getNamespace(),
|
2008-01-14 09:26:36 +00:00
|
|
|
'pl_title' => $title->getDBkey(),
|
2005-05-26 10:23:36 +00:00
|
|
|
'pl_from=page_id' ),
|
2009-11-03 17:49:49 +00:00
|
|
|
__METHOD__ );
|
2004-03-20 15:03:26 +00:00
|
|
|
$blurlArr = $title->getSquidURLs();
|
2010-07-26 20:51:33 +00:00
|
|
|
if ( $dbr->numRows( $res ) <= $wgMaxSquidPurgeTitles ) {
|
2010-10-13 23:11:40 +00:00
|
|
|
foreach ( $res as $BL ) {
|
2006-01-07 13:09:30 +00:00
|
|
|
$tobj = Title::makeTitle( $BL->page_namespace, $BL->page_title ) ;
|
2004-06-30 03:39:48 +00:00
|
|
|
$blurlArr[] = $tobj->getInternalURL();
|
|
|
|
|
}
|
2004-03-20 15:03:26 +00:00
|
|
|
}
|
2004-07-18 08:48:43 +00:00
|
|
|
|
2009-11-03 17:49:49 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2004-03-20 15:03:26 +00:00
|
|
|
return new SquidUpdate( $blurlArr );
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-16 14:26:34 +00:00
|
|
|
/**
|
|
|
|
|
* Create a SquidUpdate from an array of Title objects, or a TitleArray object
|
2011-04-25 22:41:54 +00:00
|
|
|
*
|
|
|
|
|
* @param $titles array
|
|
|
|
|
* @param $urlArr array
|
|
|
|
|
*
|
|
|
|
|
* @return SquidUpdate
|
2009-02-16 14:26:34 +00:00
|
|
|
*/
|
|
|
|
|
static function newFromTitles( $titles, $urlArr = array() ) {
|
2006-06-18 12:42:16 +00:00
|
|
|
global $wgMaxSquidPurgeTitles;
|
2009-02-16 14:26:34 +00:00
|
|
|
$i = 0;
|
2005-04-17 08:30:15 +00:00
|
|
|
foreach ( $titles as $title ) {
|
|
|
|
|
$urlArr[] = $title->getInternalURL();
|
2009-02-16 14:26:34 +00:00
|
|
|
if ( $i++ > $wgMaxSquidPurgeTitles ) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2005-04-17 08:30:15 +00:00
|
|
|
}
|
|
|
|
|
return new SquidUpdate( $urlArr );
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-25 22:41:54 +00:00
|
|
|
/**
|
|
|
|
|
* @param $title Title
|
|
|
|
|
*
|
|
|
|
|
* @return SquidUpdate
|
|
|
|
|
*/
|
2007-05-02 15:36:17 +00:00
|
|
|
static function newSimplePurge( &$title ) {
|
2004-03-20 15:03:26 +00:00
|
|
|
$urlArr = $title->getSquidURLs();
|
2005-11-07 04:10:02 +00:00
|
|
|
return new SquidUpdate( $urlArr );
|
2004-03-20 15:03:26 +00:00
|
|
|
}
|
2004-01-30 16:06:22 +00:00
|
|
|
|
2011-07-12 19:59:35 +00:00
|
|
|
/**
|
|
|
|
|
* Purges the list of URLs passed to the constructor
|
|
|
|
|
*/
|
2004-03-11 09:06:13 +00:00
|
|
|
function doUpdate() {
|
2004-03-20 15:03:26 +00:00
|
|
|
SquidUpdate::purge( $this->urlArr );
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-25 22:41:54 +00:00
|
|
|
/**
|
|
|
|
|
* Purges a list of Squids defined in $wgSquidServers.
|
|
|
|
|
* $urlArr should contain the full URLs to purge as values
|
|
|
|
|
* (example: $urlArr[] = 'http://my.host/something')
|
|
|
|
|
* XXX report broken Squids per mail or log
|
|
|
|
|
*
|
|
|
|
|
* @param $urlArr array
|
2012-01-12 19:41:18 +00:00
|
|
|
* @return void
|
2011-04-25 22:41:54 +00:00
|
|
|
*/
|
2007-05-02 15:36:17 +00:00
|
|
|
static function purge( $urlArr ) {
|
2012-04-06 20:27:10 +00:00
|
|
|
global $wgSquidServers, $wgHTCPMulticastRouting;
|
2004-03-20 15:03:26 +00:00
|
|
|
|
2006-06-18 12:46:50 +00:00
|
|
|
/*if ( (@$wgSquidServers[0]) == 'echo' ) {
|
2006-06-18 12:42:16 +00:00
|
|
|
echo implode("<br />\n", $urlArr) . "<br />\n";
|
2004-03-20 15:03:26 +00:00
|
|
|
return;
|
2006-06-18 12:46:50 +00:00
|
|
|
}*/
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2010-02-05 03:36:04 +00:00
|
|
|
if( !$urlArr ) {
|
2007-06-19 21:13:17 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2004-03-20 15:03:26 +00:00
|
|
|
|
2012-04-06 20:27:10 +00:00
|
|
|
if ( $wgHTCPMulticastRouting ) {
|
2011-07-12 19:59:35 +00:00
|
|
|
SquidUpdate::HTCPPurge( $urlArr );
|
2007-06-19 21:13:17 +00:00
|
|
|
}
|
2005-04-06 17:14:43 +00:00
|
|
|
|
2009-11-03 17:49:49 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2012-04-06 18:20:04 +00:00
|
|
|
$urlArr = array_unique( $urlArr ); // Remove duplicates
|
2010-02-05 03:36:04 +00:00
|
|
|
$maxSocketsPerSquid = 8; // socket cap per Squid
|
|
|
|
|
$urlsPerSocket = 400; // 400 seems to be a good tradeoff, opening a socket takes a while
|
|
|
|
|
$socketsPerSquid = ceil( count( $urlArr ) / $urlsPerSocket );
|
|
|
|
|
if ( $socketsPerSquid > $maxSocketsPerSquid ) {
|
|
|
|
|
$socketsPerSquid = $maxSocketsPerSquid;
|
2004-03-20 15:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
2010-02-05 03:36:04 +00:00
|
|
|
$pool = new SquidPurgeClientPool;
|
|
|
|
|
$chunks = array_chunk( $urlArr, ceil( count( $urlArr ) / $socketsPerSquid ) );
|
|
|
|
|
foreach ( $wgSquidServers as $server ) {
|
|
|
|
|
foreach ( $chunks as $chunk ) {
|
|
|
|
|
$client = new SquidPurgeClient( $server );
|
|
|
|
|
foreach ( $chunk as $url ) {
|
|
|
|
|
$client->queuePurge( $url );
|
2004-03-20 15:03:26 +00:00
|
|
|
}
|
2010-02-05 03:36:04 +00:00
|
|
|
$pool->addClient( $client );
|
2004-03-08 09:43:36 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-02-05 03:36:04 +00:00
|
|
|
$pool->run();
|
2004-01-30 16:06:22 +00:00
|
|
|
|
2009-11-03 17:49:49 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2004-06-30 02:02:43 +00:00
|
|
|
}
|
|
|
|
|
|
2011-07-12 19:59:35 +00:00
|
|
|
/**
|
|
|
|
|
* @throws MWException
|
|
|
|
|
* @param $urlArr array
|
|
|
|
|
*/
|
2007-05-02 15:36:17 +00:00
|
|
|
static function HTCPPurge( $urlArr ) {
|
2012-04-06 20:27:10 +00:00
|
|
|
global $wgHTCPMulticastRouting, $wgHTCPMulticastTTL;
|
2009-11-03 17:49:49 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2005-04-06 17:14:43 +00:00
|
|
|
|
2011-04-25 22:41:54 +00:00
|
|
|
$htcpOpCLR = 4; // HTCP CLR
|
2005-04-06 17:14:43 +00:00
|
|
|
|
2011-05-17 22:03:20 +00:00
|
|
|
// @todo FIXME: PHP doesn't support these socket constants (include/linux/in.h)
|
2006-10-16 19:12:56 +00:00
|
|
|
if( !defined( "IPPROTO_IP" ) ) {
|
|
|
|
|
define( "IPPROTO_IP", 0 );
|
|
|
|
|
define( "IP_MULTICAST_LOOP", 34 );
|
|
|
|
|
define( "IP_MULTICAST_TTL", 33 );
|
|
|
|
|
}
|
2005-04-06 17:14:43 +00:00
|
|
|
|
|
|
|
|
// pfsockopen doesn't work because we need set_sock_opt
|
2009-11-03 17:49:49 +00:00
|
|
|
$conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
|
2005-04-06 17:14:43 +00:00
|
|
|
if ( $conn ) {
|
|
|
|
|
// Set socket options
|
|
|
|
|
socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_LOOP, 0 );
|
|
|
|
|
if ( $wgHTCPMulticastTTL != 1 )
|
|
|
|
|
socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_TTL,
|
|
|
|
|
$wgHTCPMulticastTTL );
|
|
|
|
|
|
2012-04-06 18:20:04 +00:00
|
|
|
$urlArr = array_unique( $urlArr ); // Remove duplicates
|
2005-04-06 17:14:43 +00:00
|
|
|
foreach ( $urlArr as $url ) {
|
2006-10-16 19:12:56 +00:00
|
|
|
if( !is_string( $url ) ) {
|
2008-03-24 15:04:55 +00:00
|
|
|
throw new MWException( 'Bad purge URL' );
|
2006-10-16 19:12:56 +00:00
|
|
|
}
|
2006-05-24 21:57:25 +00:00
|
|
|
$url = SquidUpdate::expand( $url );
|
2012-04-06 20:27:10 +00:00
|
|
|
$conf = self::getRuleForURL( $url, $wgHTCPMulticastRouting );
|
|
|
|
|
if ( !$conf ) {
|
|
|
|
|
wfDebug( "No HTCP rule configured for URL $url , skipping\n" );
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if ( !isset( $conf['host'] ) || !isset( $conf['port'] ) ) {
|
|
|
|
|
throw new MWException( "Invalid HTCP rule for URL $url\n" );
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2005-04-06 17:14:43 +00:00
|
|
|
// Construct a minimal HTCP request diagram
|
|
|
|
|
// as per RFC 2756
|
|
|
|
|
// Opcode 'CLR', no response desired, no auth
|
2012-04-30 20:57:56 +00:00
|
|
|
$htcpTransID = rand();
|
2005-04-06 17:14:43 +00:00
|
|
|
|
2006-10-16 01:26:14 +00:00
|
|
|
$htcpSpecifier = pack( 'na4na*na8n',
|
|
|
|
|
4, 'HEAD', strlen( $url ), $url,
|
2005-04-06 17:14:43 +00:00
|
|
|
8, 'HTTP/1.0', 0 );
|
|
|
|
|
|
|
|
|
|
$htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
|
|
|
|
|
$htcpLen = 4 + $htcpDataLen + 2;
|
|
|
|
|
|
|
|
|
|
// Note! Squid gets the bit order of the first
|
|
|
|
|
// word wrong, wrt the RFC. Apparently no other
|
|
|
|
|
// implementation exists, so adapt to Squid
|
|
|
|
|
$htcpPacket = pack( 'nxxnCxNxxa*n',
|
|
|
|
|
$htcpLen, $htcpDataLen, $htcpOpCLR,
|
|
|
|
|
$htcpTransID, $htcpSpecifier, 2);
|
|
|
|
|
|
|
|
|
|
// Send out
|
2005-04-06 17:37:55 +00:00
|
|
|
wfDebug( "Purging URL $url via HTCP\n" );
|
2005-04-06 17:14:43 +00:00
|
|
|
socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
|
2012-04-06 20:27:10 +00:00
|
|
|
$conf['host'], $conf['port'] );
|
2005-04-06 17:14:43 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$errstr = socket_strerror( socket_last_error() );
|
2009-11-03 17:49:49 +00:00
|
|
|
wfDebug( __METHOD__ . "(): Error opening UDP socket: $errstr\n" );
|
2005-04-06 17:14:43 +00:00
|
|
|
}
|
2009-11-03 17:49:49 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2005-04-06 17:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
2006-05-24 21:57:25 +00:00
|
|
|
/**
|
|
|
|
|
* Expand local URLs to fully-qualified URLs using the internal protocol
|
|
|
|
|
* and host defined in $wgInternalServer. Input that's already fully-
|
|
|
|
|
* qualified will be passed through unchanged.
|
|
|
|
|
*
|
|
|
|
|
* This is used to generate purge URLs that may be either local to the
|
|
|
|
|
* main wiki or include a non-native host, such as images hosted on a
|
|
|
|
|
* second internal server.
|
|
|
|
|
*
|
|
|
|
|
* Client functions should not need to call this.
|
|
|
|
|
*
|
2011-04-25 22:41:54 +00:00
|
|
|
* @param $url string
|
|
|
|
|
*
|
2006-05-24 21:57:25 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
static function expand( $url ) {
|
2011-09-07 14:15:03 +00:00
|
|
|
return wfExpandUrl( $url, PROTO_INTERNAL );
|
2006-05-24 21:57:25 +00:00
|
|
|
}
|
2012-04-06 20:27:10 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Find the HTCP routing rule to use for a given URL.
|
|
|
|
|
* @param $url string URL to match
|
|
|
|
|
* @param $rules array Array of rules, see $wgHTCPMulticastRouting for format and behavior
|
|
|
|
|
* @return mixed Element of $rules that matched, or false if nothing matched
|
|
|
|
|
*/
|
|
|
|
|
static function getRuleForURL( $url, $rules ) {
|
|
|
|
|
foreach ( $rules as $regex => $routing ) {
|
|
|
|
|
if ( $regex === '' || preg_match( $regex, $url ) ) {
|
|
|
|
|
return $routing;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2004-01-30 16:06:22 +00:00
|
|
|
}
|