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