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;
33 static function newFromLinksTo( &$title ) {
34 global $wgMaxSquidPurgeTitles;
35 wfProfileIn( __METHOD__
);
37 # Get a list of URLs linking to this page
38 $dbr = wfGetDB( DB_SLAVE
);
39 $res = $dbr->select( array( 'links', 'page' ),
40 array( 'page_namespace', 'page_title' ),
42 'pl_namespace' => $title->getNamespace(),
43 'pl_title' => $title->getDBkey(),
46 $blurlArr = $title->getSquidURLs();
47 if ( $dbr->numRows( $res ) <= $wgMaxSquidPurgeTitles ) {
48 foreach ( $res as $BL ) {
49 $tobj = Title
::makeTitle( $BL->page_namespace
, $BL->page_title
) ;
50 $blurlArr[] = $tobj->getInternalURL();
54 wfProfileOut( __METHOD__
);
55 return new SquidUpdate( $blurlArr );
59 * Create a SquidUpdate from an array of Title objects, or a TitleArray object
61 * @param $titles array
62 * @param $urlArr array
66 static function newFromTitles( $titles, $urlArr = array() ) {
67 global $wgMaxSquidPurgeTitles;
69 foreach ( $titles as $title ) {
70 $urlArr[] = $title->getInternalURL();
71 if ( $i++
> $wgMaxSquidPurgeTitles ) {
75 return new SquidUpdate( $urlArr );
83 static function newSimplePurge( &$title ) {
84 $urlArr = $title->getSquidURLs();
85 return new SquidUpdate( $urlArr );
89 * Purges the list of URLs passed to the constructor
92 SquidUpdate
::purge( $this->urlArr
);
96 * Purges a list of Squids defined in $wgSquidServers.
97 * $urlArr should contain the full URLs to purge as values
98 * (example: $urlArr[] = 'http://my.host/something')
99 * XXX report broken Squids per mail or log
101 * @param $urlArr array
104 static function purge( $urlArr ) {
105 global $wgSquidServers, $wgHTCPMulticastAddress, $wgHTCPPort;
107 /*if ( (@$wgSquidServers[0]) == 'echo' ) {
108 echo implode("<br />\n", $urlArr) . "<br />\n";
116 if ( $wgHTCPMulticastAddress && $wgHTCPPort ) {
117 SquidUpdate
::HTCPPurge( $urlArr );
120 wfProfileIn( __METHOD__
);
122 $maxSocketsPerSquid = 8; // socket cap per Squid
123 $urlsPerSocket = 400; // 400 seems to be a good tradeoff, opening a socket takes a while
124 $socketsPerSquid = ceil( count( $urlArr ) / $urlsPerSocket );
125 if ( $socketsPerSquid > $maxSocketsPerSquid ) {
126 $socketsPerSquid = $maxSocketsPerSquid;
129 $pool = new SquidPurgeClientPool
;
130 $chunks = array_chunk( $urlArr, ceil( count( $urlArr ) / $socketsPerSquid ) );
131 foreach ( $wgSquidServers as $server ) {
132 foreach ( $chunks as $chunk ) {
133 $client = new SquidPurgeClient( $server );
134 foreach ( $chunk as $url ) {
135 $client->queuePurge( $url );
137 $pool->addClient( $client );
142 wfProfileOut( __METHOD__
);
146 * @throws MWException
147 * @param $urlArr array
149 static function HTCPPurge( $urlArr ) {
150 global $wgHTCPMulticastAddress, $wgHTCPMulticastTTL, $wgHTCPPort;
151 wfProfileIn( __METHOD__
);
153 $htcpOpCLR = 4; // HTCP CLR
155 // @todo FIXME: PHP doesn't support these socket constants (include/linux/in.h)
156 if( !defined( "IPPROTO_IP" ) ) {
157 define( "IPPROTO_IP", 0 );
158 define( "IP_MULTICAST_LOOP", 34 );
159 define( "IP_MULTICAST_TTL", 33 );
162 // pfsockopen doesn't work because we need set_sock_opt
163 $conn = socket_create( AF_INET
, SOCK_DGRAM
, SOL_UDP
);
165 // Set socket options
166 socket_set_option( $conn, IPPROTO_IP
, IP_MULTICAST_LOOP
, 0 );
167 if ( $wgHTCPMulticastTTL != 1 )
168 socket_set_option( $conn, IPPROTO_IP
, IP_MULTICAST_TTL
,
169 $wgHTCPMulticastTTL );
171 foreach ( $urlArr as $url ) {
172 if( !is_string( $url ) ) {
173 throw new MWException( 'Bad purge URL' );
175 $url = SquidUpdate
::expand( $url );
177 // Construct a minimal HTCP request diagram
179 // Opcode 'CLR', no response desired, no auth
180 $htcpTransID = rand();
182 $htcpSpecifier = pack( 'na4na*na8n',
183 4, 'HEAD', strlen( $url ), $url,
186 $htcpDataLen = 8 +
2 +
strlen( $htcpSpecifier );
187 $htcpLen = 4 +
$htcpDataLen +
2;
189 // Note! Squid gets the bit order of the first
190 // word wrong, wrt the RFC. Apparently no other
191 // implementation exists, so adapt to Squid
192 $htcpPacket = pack( 'nxxnCxNxxa*n',
193 $htcpLen, $htcpDataLen, $htcpOpCLR,
194 $htcpTransID, $htcpSpecifier, 2);
197 wfDebug( "Purging URL $url via HTCP\n" );
198 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
199 $wgHTCPMulticastAddress, $wgHTCPPort );
202 $errstr = socket_strerror( socket_last_error() );
203 wfDebug( __METHOD__
. "(): Error opening UDP socket: $errstr\n" );
205 wfProfileOut( __METHOD__
);
209 * Expand local URLs to fully-qualified URLs using the internal protocol
210 * and host defined in $wgInternalServer. Input that's already fully-
211 * qualified will be passed through unchanged.
213 * This is used to generate purge URLs that may be either local to the
214 * main wiki or include a non-native host, such as images hosted on a
215 * second internal server.
217 * Client functions should not need to call this.
223 static function expand( $url ) {
224 return wfExpandUrl( $url, PROTO_INTERNAL
);