Kill "* @return void"
[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 */
103 static function purge( $urlArr ) {
104 global $wgSquidServers, $wgHTCPMulticastAddress, $wgHTCPPort;
105
106 /*if ( (@$wgSquidServers[0]) == 'echo' ) {
107 echo implode("<br />\n", $urlArr) . "<br />\n";
108 return;
109 }*/
110
111 if( !$urlArr ) {
112 return;
113 }
114
115 if ( $wgHTCPMulticastAddress && $wgHTCPPort ) {
116 SquidUpdate::HTCPPurge( $urlArr );
117 }
118
119 wfProfileIn( __METHOD__ );
120
121 $maxSocketsPerSquid = 8; // socket cap per Squid
122 $urlsPerSocket = 400; // 400 seems to be a good tradeoff, opening a socket takes a while
123 $socketsPerSquid = ceil( count( $urlArr ) / $urlsPerSocket );
124 if ( $socketsPerSquid > $maxSocketsPerSquid ) {
125 $socketsPerSquid = $maxSocketsPerSquid;
126 }
127
128 $pool = new SquidPurgeClientPool;
129 $chunks = array_chunk( $urlArr, ceil( count( $urlArr ) / $socketsPerSquid ) );
130 foreach ( $wgSquidServers as $server ) {
131 foreach ( $chunks as $chunk ) {
132 $client = new SquidPurgeClient( $server );
133 foreach ( $chunk as $url ) {
134 $client->queuePurge( $url );
135 }
136 $pool->addClient( $client );
137 }
138 }
139 $pool->run();
140
141 wfProfileOut( __METHOD__ );
142 }
143
144 /**
145 * @throws MWException
146 * @param $urlArr array
147 */
148 static function HTCPPurge( $urlArr ) {
149 global $wgHTCPMulticastAddress, $wgHTCPMulticastTTL, $wgHTCPPort;
150 wfProfileIn( __METHOD__ );
151
152 $htcpOpCLR = 4; // HTCP CLR
153
154 // @todo FIXME: PHP doesn't support these socket constants (include/linux/in.h)
155 if( !defined( "IPPROTO_IP" ) ) {
156 define( "IPPROTO_IP", 0 );
157 define( "IP_MULTICAST_LOOP", 34 );
158 define( "IP_MULTICAST_TTL", 33 );
159 }
160
161 // pfsockopen doesn't work because we need set_sock_opt
162 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
163 if ( $conn ) {
164 // Set socket options
165 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_LOOP, 0 );
166 if ( $wgHTCPMulticastTTL != 1 )
167 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_TTL,
168 $wgHTCPMulticastTTL );
169
170 foreach ( $urlArr as $url ) {
171 if( !is_string( $url ) ) {
172 throw new MWException( 'Bad purge URL' );
173 }
174 $url = SquidUpdate::expand( $url );
175
176 // Construct a minimal HTCP request diagram
177 // as per RFC 2756
178 // Opcode 'CLR', no response desired, no auth
179 $htcpTransID = rand();
180
181 $htcpSpecifier = pack( 'na4na*na8n',
182 4, 'HEAD', strlen( $url ), $url,
183 8, 'HTTP/1.0', 0 );
184
185 $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
186 $htcpLen = 4 + $htcpDataLen + 2;
187
188 // Note! Squid gets the bit order of the first
189 // word wrong, wrt the RFC. Apparently no other
190 // implementation exists, so adapt to Squid
191 $htcpPacket = pack( 'nxxnCxNxxa*n',
192 $htcpLen, $htcpDataLen, $htcpOpCLR,
193 $htcpTransID, $htcpSpecifier, 2);
194
195 // Send out
196 wfDebug( "Purging URL $url via HTCP\n" );
197 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
198 $wgHTCPMulticastAddress, $wgHTCPPort );
199 }
200 } else {
201 $errstr = socket_strerror( socket_last_error() );
202 wfDebug( __METHOD__ . "(): Error opening UDP socket: $errstr\n" );
203 }
204 wfProfileOut( __METHOD__ );
205 }
206
207 /**
208 * Expand local URLs to fully-qualified URLs using the internal protocol
209 * and host defined in $wgInternalServer. Input that's already fully-
210 * qualified will be passed through unchanged.
211 *
212 * This is used to generate purge URLs that may be either local to the
213 * main wiki or include a non-native host, such as images hosted on a
214 * second internal server.
215 *
216 * Client functions should not need to call this.
217 *
218 * @param $url string
219 *
220 * @return string
221 */
222 static function expand( $url ) {
223 return wfExpandUrl( $url, PROTO_INTERNAL );
224 }
225 }