Merge "(bug 19195) Make user IDs more readily available with the API"
[lhc/web/wiklou.git] / includes / cache / SquidUpdate.php
1 <?php
2 /**
3 * Squid cache purging.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Cache
22 */
23
24 /**
25 * Handles purging appropriate Squid URLs given a title (or titles)
26 * @ingroup Cache
27 */
28 class SquidUpdate {
29 var $urlArr, $mMaxTitles;
30
31 function __construct( $urlArr = Array(), $maxTitles = false ) {
32 global $wgMaxSquidPurgeTitles;
33 if ( $maxTitles === false ) {
34 $this->mMaxTitles = $wgMaxSquidPurgeTitles;
35 } else {
36 $this->mMaxTitles = $maxTitles;
37 }
38 $urlArr = array_unique( $urlArr ); // Remove duplicates
39 if ( count( $urlArr ) > $this->mMaxTitles ) {
40 $urlArr = array_slice( $urlArr, 0, $this->mMaxTitles );
41 }
42 $this->urlArr = $urlArr;
43 }
44
45 /**
46 * @param $title Title
47 *
48 * @return SquidUpdate
49 */
50 static function newFromLinksTo( &$title ) {
51 global $wgMaxSquidPurgeTitles;
52 wfProfileIn( __METHOD__ );
53
54 # Get a list of URLs linking to this page
55 $dbr = wfGetDB( DB_SLAVE );
56 $res = $dbr->select( array( 'links', 'page' ),
57 array( 'page_namespace', 'page_title' ),
58 array(
59 'pl_namespace' => $title->getNamespace(),
60 'pl_title' => $title->getDBkey(),
61 'pl_from=page_id' ),
62 __METHOD__ );
63 $blurlArr = $title->getSquidURLs();
64 if ( $dbr->numRows( $res ) <= $wgMaxSquidPurgeTitles ) {
65 foreach ( $res as $BL ) {
66 $tobj = Title::makeTitle( $BL->page_namespace, $BL->page_title ) ;
67 $blurlArr[] = $tobj->getInternalURL();
68 }
69 }
70
71 wfProfileOut( __METHOD__ );
72 return new SquidUpdate( $blurlArr );
73 }
74
75 /**
76 * Create a SquidUpdate from an array of Title objects, or a TitleArray object
77 *
78 * @param $titles array
79 * @param $urlArr array
80 *
81 * @return SquidUpdate
82 */
83 static function newFromTitles( $titles, $urlArr = array() ) {
84 global $wgMaxSquidPurgeTitles;
85 $i = 0;
86 foreach ( $titles as $title ) {
87 $urlArr[] = $title->getInternalURL();
88 if ( $i++ > $wgMaxSquidPurgeTitles ) {
89 break;
90 }
91 }
92 return new SquidUpdate( $urlArr );
93 }
94
95 /**
96 * @param $title Title
97 *
98 * @return SquidUpdate
99 */
100 static function newSimplePurge( &$title ) {
101 $urlArr = $title->getSquidURLs();
102 return new SquidUpdate( $urlArr );
103 }
104
105 /**
106 * Purges the list of URLs passed to the constructor
107 */
108 function doUpdate() {
109 SquidUpdate::purge( $this->urlArr );
110 }
111
112 /**
113 * Purges a list of Squids defined in $wgSquidServers.
114 * $urlArr should contain the full URLs to purge as values
115 * (example: $urlArr[] = 'http://my.host/something')
116 * XXX report broken Squids per mail or log
117 *
118 * @param $urlArr array
119 * @return void
120 */
121 static function purge( $urlArr ) {
122 global $wgSquidServers, $wgHTCPMulticastRouting;
123
124 /*if ( (@$wgSquidServers[0]) == 'echo' ) {
125 echo implode("<br />\n", $urlArr) . "<br />\n";
126 return;
127 }*/
128
129 if( !$urlArr ) {
130 return;
131 }
132
133 if ( $wgHTCPMulticastRouting ) {
134 SquidUpdate::HTCPPurge( $urlArr );
135 }
136
137 wfProfileIn( __METHOD__ );
138
139 $urlArr = array_unique( $urlArr ); // Remove duplicates
140 $maxSocketsPerSquid = 8; // socket cap per Squid
141 $urlsPerSocket = 400; // 400 seems to be a good tradeoff, opening a socket takes a while
142 $socketsPerSquid = ceil( count( $urlArr ) / $urlsPerSocket );
143 if ( $socketsPerSquid > $maxSocketsPerSquid ) {
144 $socketsPerSquid = $maxSocketsPerSquid;
145 }
146
147 $pool = new SquidPurgeClientPool;
148 $chunks = array_chunk( $urlArr, ceil( count( $urlArr ) / $socketsPerSquid ) );
149 foreach ( $wgSquidServers as $server ) {
150 foreach ( $chunks as $chunk ) {
151 $client = new SquidPurgeClient( $server );
152 foreach ( $chunk as $url ) {
153 $client->queuePurge( $url );
154 }
155 $pool->addClient( $client );
156 }
157 }
158 $pool->run();
159
160 wfProfileOut( __METHOD__ );
161 }
162
163 /**
164 * @throws MWException
165 * @param $urlArr array
166 */
167 static function HTCPPurge( $urlArr ) {
168 global $wgHTCPMulticastRouting, $wgHTCPMulticastTTL;
169 wfProfileIn( __METHOD__ );
170
171 $htcpOpCLR = 4; // HTCP CLR
172
173 // @todo FIXME: PHP doesn't support these socket constants (include/linux/in.h)
174 if( !defined( "IPPROTO_IP" ) ) {
175 define( "IPPROTO_IP", 0 );
176 define( "IP_MULTICAST_LOOP", 34 );
177 define( "IP_MULTICAST_TTL", 33 );
178 }
179
180 // pfsockopen doesn't work because we need set_sock_opt
181 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
182 if ( $conn ) {
183 // Set socket options
184 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_LOOP, 0 );
185 if ( $wgHTCPMulticastTTL != 1 )
186 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_TTL,
187 $wgHTCPMulticastTTL );
188
189 $urlArr = array_unique( $urlArr ); // Remove duplicates
190 foreach ( $urlArr as $url ) {
191 if( !is_string( $url ) ) {
192 throw new MWException( 'Bad purge URL' );
193 }
194 $url = SquidUpdate::expand( $url );
195 $conf = self::getRuleForURL( $url, $wgHTCPMulticastRouting );
196 if ( !$conf ) {
197 wfDebug( "No HTCP rule configured for URL $url , skipping\n" );
198 continue;
199 }
200 if ( !isset( $conf['host'] ) || !isset( $conf['port'] ) ) {
201 throw new MWException( "Invalid HTCP rule for URL $url\n" );
202 }
203
204 // Construct a minimal HTCP request diagram
205 // as per RFC 2756
206 // Opcode 'CLR', no response desired, no auth
207 $htcpTransID = rand();
208
209 $htcpSpecifier = pack( 'na4na*na8n',
210 4, 'HEAD', strlen( $url ), $url,
211 8, 'HTTP/1.0', 0 );
212
213 $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
214 $htcpLen = 4 + $htcpDataLen + 2;
215
216 // Note! Squid gets the bit order of the first
217 // word wrong, wrt the RFC. Apparently no other
218 // implementation exists, so adapt to Squid
219 $htcpPacket = pack( 'nxxnCxNxxa*n',
220 $htcpLen, $htcpDataLen, $htcpOpCLR,
221 $htcpTransID, $htcpSpecifier, 2);
222
223 // Send out
224 wfDebug( "Purging URL $url via HTCP\n" );
225 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
226 $conf['host'], $conf['port'] );
227 }
228 } else {
229 $errstr = socket_strerror( socket_last_error() );
230 wfDebug( __METHOD__ . "(): Error opening UDP socket: $errstr\n" );
231 }
232 wfProfileOut( __METHOD__ );
233 }
234
235 /**
236 * Expand local URLs to fully-qualified URLs using the internal protocol
237 * and host defined in $wgInternalServer. Input that's already fully-
238 * qualified will be passed through unchanged.
239 *
240 * This is used to generate purge URLs that may be either local to the
241 * main wiki or include a non-native host, such as images hosted on a
242 * second internal server.
243 *
244 * Client functions should not need to call this.
245 *
246 * @param $url string
247 *
248 * @return string
249 */
250 static function expand( $url ) {
251 return wfExpandUrl( $url, PROTO_INTERNAL );
252 }
253
254 /**
255 * Find the HTCP routing rule to use for a given URL.
256 * @param $url string URL to match
257 * @param $rules array Array of rules, see $wgHTCPMulticastRouting for format and behavior
258 * @return mixed Element of $rules that matched, or false if nothing matched
259 */
260 static function getRuleForURL( $url, $rules ) {
261 foreach ( $rules as $regex => $routing ) {
262 if ( $regex === '' || preg_match( $regex, $url ) ) {
263 return $routing;
264 }
265 }
266 return false;
267 }
268
269 }