Actually fix bug 30792, despite my earlier claims. SquidUpdate::purge('//upload.wikim...
[lhc/web/wiklou.git] / includes / cache / 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 /**
29 * @param $title Title
30 *
31 * @return SquidUpdate
32 */
33 static function newFromLinksTo( &$title ) {
34 global $wgMaxSquidPurgeTitles;
35 wfProfileIn( __METHOD__ );
36
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' ),
41 array(
42 'pl_namespace' => $title->getNamespace(),
43 'pl_title' => $title->getDBkey(),
44 'pl_from=page_id' ),
45 __METHOD__ );
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();
51 }
52 }
53
54 wfProfileOut( __METHOD__ );
55 return new SquidUpdate( $blurlArr );
56 }
57
58 /**
59 * Create a SquidUpdate from an array of Title objects, or a TitleArray object
60 *
61 * @param $titles array
62 * @param $urlArr array
63 *
64 * @return SquidUpdate
65 */
66 static function newFromTitles( $titles, $urlArr = array() ) {
67 global $wgMaxSquidPurgeTitles;
68 $i = 0;
69 foreach ( $titles as $title ) {
70 $urlArr[] = $title->getInternalURL();
71 if ( $i++ > $wgMaxSquidPurgeTitles ) {
72 break;
73 }
74 }
75 return new SquidUpdate( $urlArr );
76 }
77
78 /**
79 * @param $title Title
80 *
81 * @return SquidUpdate
82 */
83 static function newSimplePurge( &$title ) {
84 $urlArr = $title->getSquidURLs();
85 return new SquidUpdate( $urlArr );
86 }
87
88 /**
89 * Purges the list of URLs passed to the constructor
90 */
91 function doUpdate() {
92 SquidUpdate::purge( $this->urlArr );
93 }
94
95 /**
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
100 *
101 * @param $urlArr array
102 * @return void
103 */
104 static function purge( $urlArr ) {
105 global $wgSquidServers, $wgHTCPMulticastAddress, $wgHTCPPort;
106
107 /*if ( (@$wgSquidServers[0]) == 'echo' ) {
108 echo implode("<br />\n", $urlArr) . "<br />\n";
109 return;
110 }*/
111
112 if( !$urlArr ) {
113 return;
114 }
115
116 if ( $wgHTCPMulticastAddress && $wgHTCPPort ) {
117 SquidUpdate::HTCPPurge( $urlArr );
118 }
119
120 wfProfileIn( __METHOD__ );
121
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;
127 }
128
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 );
136 }
137 $pool->addClient( $client );
138 }
139 }
140 $pool->run();
141
142 wfProfileOut( __METHOD__ );
143 }
144
145 /**
146 * @throws MWException
147 * @param $urlArr array
148 */
149 static function HTCPPurge( $urlArr ) {
150 global $wgHTCPMulticastAddress, $wgHTCPMulticastTTL, $wgHTCPPort;
151 wfProfileIn( __METHOD__ );
152
153 $htcpOpCLR = 4; // HTCP CLR
154
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 );
160 }
161
162 // pfsockopen doesn't work because we need set_sock_opt
163 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
164 if ( $conn ) {
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 );
170
171 foreach ( $urlArr as $url ) {
172 if( !is_string( $url ) ) {
173 throw new MWException( 'Bad purge URL' );
174 }
175 $url = SquidUpdate::expand( $url );
176
177 // Construct a minimal HTCP request diagram
178 // as per RFC 2756
179 // Opcode 'CLR', no response desired, no auth
180 $htcpTransID = rand();
181
182 $htcpSpecifier = pack( 'na4na*na8n',
183 4, 'HEAD', strlen( $url ), $url,
184 8, 'HTTP/1.0', 0 );
185
186 $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
187 $htcpLen = 4 + $htcpDataLen + 2;
188
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);
195
196 // Send out
197 wfDebug( "Purging URL $url via HTCP\n" );
198 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
199 $wgHTCPMulticastAddress, $wgHTCPPort );
200 }
201 } else {
202 $errstr = socket_strerror( socket_last_error() );
203 wfDebug( __METHOD__ . "(): Error opening UDP socket: $errstr\n" );
204 }
205 wfProfileOut( __METHOD__ );
206 }
207
208 /**
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.
212 *
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.
216 *
217 * Client functions should not need to call this.
218 *
219 * @param $url string
220 *
221 * @return string
222 */
223 static function expand( $url ) {
224 return wfExpandUrl( $url, PROTO_INTERNAL );
225 }
226 }