9 * Handles purging appropriate Squid URLs given a title (or titles)
13 var $urlArr, $mMaxTitles;
15 function __construct( $urlArr = Array(), $maxTitles = false ) {
16 global $wgMaxSquidPurgeTitles;
17 if ( $maxTitles === false ) {
18 $this->mMaxTitles
= $wgMaxSquidPurgeTitles;
20 $this->mMaxTitles
= $maxTitles;
22 if ( count( $urlArr ) > $this->mMaxTitles
) {
23 $urlArr = array_slice( $urlArr, 0, $this->mMaxTitles
);
25 $this->urlArr
= $urlArr;
28 static function newFromLinksTo( &$title ) {
29 global $wgMaxSquidPurgeTitles;
30 wfProfileIn( __METHOD__
);
32 # Get a list of URLs linking to this page
33 $dbr = wfGetDB( DB_SLAVE
);
34 $res = $dbr->select( array( 'links', 'page' ),
35 array( 'page_namespace', 'page_title' ),
37 'pl_namespace' => $title->getNamespace(),
38 'pl_title' => $title->getDBkey(),
41 $blurlArr = $title->getSquidURLs();
42 if ( $dbr->numRows( $res ) <= $wgMaxSquidPurgeTitles ) {
43 while ( $BL = $dbr->fetchObject ( $res ) )
45 $tobj = Title
::makeTitle( $BL->page_namespace
, $BL->page_title
) ;
46 $blurlArr[] = $tobj->getInternalURL();
49 $dbr->freeResult ( $res ) ;
51 wfProfileOut( __METHOD__
);
52 return new SquidUpdate( $blurlArr );
56 * Create a SquidUpdate from an array of Title objects, or a TitleArray object
58 static function newFromTitles( $titles, $urlArr = array() ) {
59 global $wgMaxSquidPurgeTitles;
61 foreach ( $titles as $title ) {
62 $urlArr[] = $title->getInternalURL();
63 if ( $i++
> $wgMaxSquidPurgeTitles ) {
67 return new SquidUpdate( $urlArr );
70 static function newSimplePurge( &$title ) {
71 $urlArr = $title->getSquidURLs();
72 return new SquidUpdate( $urlArr );
76 SquidUpdate
::purge( $this->urlArr
);
79 /* Purges a list of Squids defined in $wgSquidServers.
80 $urlArr should contain the full URLs to purge as values
81 (example: $urlArr[] = 'http://my.host/something')
82 XXX report broken Squids per mail or log */
84 static function purge( $urlArr ) {
85 global $wgSquidServers, $wgHTCPMulticastAddress, $wgHTCPPort;
87 /*if ( (@$wgSquidServers[0]) == 'echo' ) {
88 echo implode("<br />\n", $urlArr) . "<br />\n";
96 if ( $wgHTCPMulticastAddress && $wgHTCPPort ) {
97 return SquidUpdate
::HTCPPurge( $urlArr );
100 wfProfileIn( __METHOD__
);
102 $maxSocketsPerSquid = 8; // socket cap per Squid
103 $urlsPerSocket = 400; // 400 seems to be a good tradeoff, opening a socket takes a while
104 $socketsPerSquid = ceil( count( $urlArr ) / $urlsPerSocket );
105 if ( $socketsPerSquid > $maxSocketsPerSquid ) {
106 $socketsPerSquid = $maxSocketsPerSquid;
109 $pool = new SquidPurgeClientPool
;
110 $chunks = array_chunk( $urlArr, ceil( count( $urlArr ) / $socketsPerSquid ) );
111 foreach ( $wgSquidServers as $server ) {
112 foreach ( $chunks as $chunk ) {
113 $client = new SquidPurgeClient( $server );
114 foreach ( $chunk as $url ) {
115 $client->queuePurge( $url );
117 $pool->addClient( $client );
122 wfProfileOut( __METHOD__
);
125 static function HTCPPurge( $urlArr ) {
126 global $wgHTCPMulticastAddress, $wgHTCPMulticastTTL, $wgHTCPPort;
127 wfProfileIn( __METHOD__
);
129 $htcpOpCLR = 4; // HTCP CLR
131 // FIXME PHP doesn't support these socket constants (include/linux/in.h)
132 if( !defined( "IPPROTO_IP" ) ) {
133 define( "IPPROTO_IP", 0 );
134 define( "IP_MULTICAST_LOOP", 34 );
135 define( "IP_MULTICAST_TTL", 33 );
138 // pfsockopen doesn't work because we need set_sock_opt
139 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
141 // Set socket options
142 socket_set_option( $conn, IPPROTO_IP
, IP_MULTICAST_LOOP
, 0 );
143 if ( $wgHTCPMulticastTTL != 1 )
144 socket_set_option( $conn, IPPROTO_IP
, IP_MULTICAST_TTL
,
145 $wgHTCPMulticastTTL );
147 foreach ( $urlArr as $url ) {
148 if( !is_string( $url ) ) {
149 throw new MWException( 'Bad purge URL' );
151 $url = SquidUpdate
::expand( $url );
153 // Construct a minimal HTCP request diagram
155 // Opcode 'CLR', no response desired, no auth
156 $htcpTransID = rand();
158 $htcpSpecifier = pack( 'na4na*na8n',
159 4, 'HEAD', strlen( $url ), $url,
162 $htcpDataLen = 8 +
2 +
strlen( $htcpSpecifier );
163 $htcpLen = 4 +
$htcpDataLen +
2;
165 // Note! Squid gets the bit order of the first
166 // word wrong, wrt the RFC. Apparently no other
167 // implementation exists, so adapt to Squid
168 $htcpPacket = pack( 'nxxnCxNxxa*n',
169 $htcpLen, $htcpDataLen, $htcpOpCLR,
170 $htcpTransID, $htcpSpecifier, 2);
173 wfDebug( "Purging URL $url via HTCP\n" );
174 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
175 $wgHTCPMulticastAddress, $wgHTCPPort );
178 $errstr = socket_strerror( socket_last_error() );
179 wfDebug( __METHOD__
. "(): Error opening UDP socket: $errstr\n" );
181 wfProfileOut( __METHOD__
);
185 * Expand local URLs to fully-qualified URLs using the internal protocol
186 * and host defined in $wgInternalServer. Input that's already fully-
187 * qualified will be passed through unchanged.
189 * This is used to generate purge URLs that may be either local to the
190 * main wiki or include a non-native host, such as images hosted on a
191 * second internal server.
193 * Client functions should not need to call this.
197 static function expand( $url ) {
198 global $wgInternalServer;
199 if( $url != '' && $url{0} == '/' ) {
200 return $wgInternalServer . $url;