* Follow-up r84610: don't assume a Parser object is attached
[lhc/web/wiklou.git] / includes / SquidUpdate.php
1 <?php
2 /**
3 * See deferred.txt
4 * @file
5 * @ingroup Cache
6 */
7
8 /**
9 * Handles purging appropriate Squid URLs given a title (or titles)
10 * @ingroup Cache
11 */
12 class SquidUpdate {
13 var $urlArr, $mMaxTitles;
14
15 function __construct( $urlArr = Array(), $maxTitles = false ) {
16 global $wgMaxSquidPurgeTitles;
17 if ( $maxTitles === false ) {
18 $this->mMaxTitles = $wgMaxSquidPurgeTitles;
19 } else {
20 $this->mMaxTitles = $maxTitles;
21 }
22 if ( count( $urlArr ) > $this->mMaxTitles ) {
23 $urlArr = array_slice( $urlArr, 0, $this->mMaxTitles );
24 }
25 $this->urlArr = $urlArr;
26 }
27
28 static function newFromLinksTo( &$title ) {
29 global $wgMaxSquidPurgeTitles;
30 wfProfileIn( __METHOD__ );
31
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' ),
36 array(
37 'pl_namespace' => $title->getNamespace(),
38 'pl_title' => $title->getDBkey(),
39 'pl_from=page_id' ),
40 __METHOD__ );
41 $blurlArr = $title->getSquidURLs();
42 if ( $dbr->numRows( $res ) <= $wgMaxSquidPurgeTitles ) {
43 foreach ( $res as $BL ) {
44 $tobj = Title::makeTitle( $BL->page_namespace, $BL->page_title ) ;
45 $blurlArr[] = $tobj->getInternalURL();
46 }
47 }
48
49 wfProfileOut( __METHOD__ );
50 return new SquidUpdate( $blurlArr );
51 }
52
53 /**
54 * Create a SquidUpdate from an array of Title objects, or a TitleArray object
55 */
56 static function newFromTitles( $titles, $urlArr = array() ) {
57 global $wgMaxSquidPurgeTitles;
58 $i = 0;
59 foreach ( $titles as $title ) {
60 $urlArr[] = $title->getInternalURL();
61 if ( $i++ > $wgMaxSquidPurgeTitles ) {
62 break;
63 }
64 }
65 return new SquidUpdate( $urlArr );
66 }
67
68 static function newSimplePurge( &$title ) {
69 $urlArr = $title->getSquidURLs();
70 return new SquidUpdate( $urlArr );
71 }
72
73 function doUpdate() {
74 SquidUpdate::purge( $this->urlArr );
75 }
76
77 /* Purges a list of Squids defined in $wgSquidServers.
78 $urlArr should contain the full URLs to purge as values
79 (example: $urlArr[] = 'http://my.host/something')
80 XXX report broken Squids per mail or log */
81
82 static function purge( $urlArr ) {
83 global $wgSquidServers, $wgHTCPMulticastAddress, $wgHTCPPort;
84
85 /*if ( (@$wgSquidServers[0]) == 'echo' ) {
86 echo implode("<br />\n", $urlArr) . "<br />\n";
87 return;
88 }*/
89
90 if( !$urlArr ) {
91 return;
92 }
93
94 if ( $wgHTCPMulticastAddress && $wgHTCPPort ) {
95 return SquidUpdate::HTCPPurge( $urlArr );
96 }
97
98 wfProfileIn( __METHOD__ );
99
100 $maxSocketsPerSquid = 8; // socket cap per Squid
101 $urlsPerSocket = 400; // 400 seems to be a good tradeoff, opening a socket takes a while
102 $socketsPerSquid = ceil( count( $urlArr ) / $urlsPerSocket );
103 if ( $socketsPerSquid > $maxSocketsPerSquid ) {
104 $socketsPerSquid = $maxSocketsPerSquid;
105 }
106
107 $pool = new SquidPurgeClientPool;
108 $chunks = array_chunk( $urlArr, ceil( count( $urlArr ) / $socketsPerSquid ) );
109 foreach ( $wgSquidServers as $server ) {
110 foreach ( $chunks as $chunk ) {
111 $client = new SquidPurgeClient( $server );
112 foreach ( $chunk as $url ) {
113 $client->queuePurge( $url );
114 }
115 $pool->addClient( $client );
116 }
117 }
118 $pool->run();
119
120 wfProfileOut( __METHOD__ );
121 }
122
123 static function HTCPPurge( $urlArr ) {
124 global $wgHTCPMulticastAddress, $wgHTCPMulticastTTL, $wgHTCPPort;
125 wfProfileIn( __METHOD__ );
126
127 $htcpOpCLR = 4; // HTCP CLR
128
129 // FIXME PHP doesn't support these socket constants (include/linux/in.h)
130 if( !defined( "IPPROTO_IP" ) ) {
131 define( "IPPROTO_IP", 0 );
132 define( "IP_MULTICAST_LOOP", 34 );
133 define( "IP_MULTICAST_TTL", 33 );
134 }
135
136 // pfsockopen doesn't work because we need set_sock_opt
137 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
138 if ( $conn ) {
139 // Set socket options
140 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_LOOP, 0 );
141 if ( $wgHTCPMulticastTTL != 1 )
142 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_TTL,
143 $wgHTCPMulticastTTL );
144
145 foreach ( $urlArr as $url ) {
146 if( !is_string( $url ) ) {
147 throw new MWException( 'Bad purge URL' );
148 }
149 $url = SquidUpdate::expand( $url );
150
151 // Construct a minimal HTCP request diagram
152 // as per RFC 2756
153 // Opcode 'CLR', no response desired, no auth
154 $htcpTransID = rand();
155
156 $htcpSpecifier = pack( 'na4na*na8n',
157 4, 'HEAD', strlen( $url ), $url,
158 8, 'HTTP/1.0', 0 );
159
160 $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
161 $htcpLen = 4 + $htcpDataLen + 2;
162
163 // Note! Squid gets the bit order of the first
164 // word wrong, wrt the RFC. Apparently no other
165 // implementation exists, so adapt to Squid
166 $htcpPacket = pack( 'nxxnCxNxxa*n',
167 $htcpLen, $htcpDataLen, $htcpOpCLR,
168 $htcpTransID, $htcpSpecifier, 2);
169
170 // Send out
171 wfDebug( "Purging URL $url via HTCP\n" );
172 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
173 $wgHTCPMulticastAddress, $wgHTCPPort );
174 }
175 } else {
176 $errstr = socket_strerror( socket_last_error() );
177 wfDebug( __METHOD__ . "(): Error opening UDP socket: $errstr\n" );
178 }
179 wfProfileOut( __METHOD__ );
180 }
181
182 /**
183 * Expand local URLs to fully-qualified URLs using the internal protocol
184 * and host defined in $wgInternalServer. Input that's already fully-
185 * qualified will be passed through unchanged.
186 *
187 * This is used to generate purge URLs that may be either local to the
188 * main wiki or include a non-native host, such as images hosted on a
189 * second internal server.
190 *
191 * Client functions should not need to call this.
192 *
193 * @return string
194 */
195 static function expand( $url ) {
196 global $wgInternalServer, $wgServer;
197 $server = $wgInternalServer !== false ? $wgInternalServer : $wgServer;
198 if( $url !== '' && $url[0] == '/' ) {
199 return $server . $url;
200 }
201 return $url;
202 }
203 }