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