MediaWiki  1.23.14
SquidUpdate.php
Go to the documentation of this file.
1 <?php
28 class SquidUpdate {
33  protected $urlArr;
34 
39  public function __construct( $urlArr = array(), $maxTitles = false ) {
40  global $wgMaxSquidPurgeTitles;
41  if ( $maxTitles === false ) {
42  $maxTitles = $wgMaxSquidPurgeTitles;
43  }
44 
45  // Remove duplicate URLs from list
46  $urlArr = array_unique( $urlArr );
47  if ( count( $urlArr ) > $maxTitles ) {
48  // Truncate to desired maximum URL count
49  $urlArr = array_slice( $urlArr, 0, $maxTitles );
50  }
51  $this->urlArr = $urlArr;
52  }
53 
63  public static function newFromLinksTo( Title $title ) {
64  global $wgMaxSquidPurgeTitles;
65  wfProfileIn( __METHOD__ );
66 
67  # Get a list of URLs linking to this page
68  $dbr = wfGetDB( DB_SLAVE );
69  $res = $dbr->select( array( 'links', 'page' ),
70  array( 'page_namespace', 'page_title' ),
71  array(
72  'pl_namespace' => $title->getNamespace(),
73  'pl_title' => $title->getDBkey(),
74  'pl_from=page_id' ),
75  __METHOD__ );
76  $blurlArr = $title->getSquidURLs();
77  if ( $res->numRows() <= $wgMaxSquidPurgeTitles ) {
78  foreach ( $res as $BL ) {
79  $tobj = Title::makeTitle( $BL->page_namespace, $BL->page_title );
80  $blurlArr[] = $tobj->getInternalURL();
81  }
82  }
83 
84  wfProfileOut( __METHOD__ );
85 
86  return new SquidUpdate( $blurlArr );
87  }
88 
96  public static function newFromTitles( $titles, $urlArr = array() ) {
97  global $wgMaxSquidPurgeTitles;
98  $i = 0;
100  foreach ( $titles as $title ) {
101  $urlArr[] = $title->getInternalURL();
102  if ( $i++ > $wgMaxSquidPurgeTitles ) {
103  break;
104  }
105  }
106 
107  return new SquidUpdate( $urlArr );
108  }
109 
114  public static function newSimplePurge( Title $title ) {
115  $urlArr = $title->getSquidURLs();
116 
117  return new SquidUpdate( $urlArr );
118  }
119 
123  public function doUpdate() {
124  self::purge( $this->urlArr );
125  }
126 
135  public static function purge( $urlArr ) {
136  global $wgSquidServers, $wgHTCPRouting;
137 
138  if ( !$urlArr ) {
139  return;
140  }
141 
142  wfDebugLog( 'squid', __METHOD__ . ': ' . implode( ' ', $urlArr ) );
143 
144  if ( $wgHTCPRouting ) {
146  }
147 
148  wfProfileIn( __METHOD__ );
149 
150  // Remove duplicate URLs
151  $urlArr = array_unique( $urlArr );
152  // Maximum number of parallel connections per squid
153  $maxSocketsPerSquid = 8;
154  // Number of requests to send per socket
155  // 400 seems to be a good tradeoff, opening a socket takes a while
156  $urlsPerSocket = 400;
157  $socketsPerSquid = ceil( count( $urlArr ) / $urlsPerSocket );
158  if ( $socketsPerSquid > $maxSocketsPerSquid ) {
159  $socketsPerSquid = $maxSocketsPerSquid;
160  }
161 
162  $pool = new SquidPurgeClientPool;
163  $chunks = array_chunk( $urlArr, ceil( count( $urlArr ) / $socketsPerSquid ) );
164  foreach ( $wgSquidServers as $server ) {
165  foreach ( $chunks as $chunk ) {
166  $client = new SquidPurgeClient( $server );
167  foreach ( $chunk as $url ) {
168  $client->queuePurge( $url );
169  }
170  $pool->addClient( $client );
171  }
172  }
173  $pool->run();
174 
175  wfProfileOut( __METHOD__ );
176  }
177 
184  public static function HTCPPurge( $urlArr ) {
185  global $wgHTCPRouting, $wgHTCPMulticastTTL;
186  wfProfileIn( __METHOD__ );
187 
188  // HTCP CLR operation
189  $htcpOpCLR = 4;
190 
191  // @todo FIXME: PHP doesn't support these socket constants (include/linux/in.h)
192  if ( !defined( "IPPROTO_IP" ) ) {
193  define( "IPPROTO_IP", 0 );
194  define( "IP_MULTICAST_LOOP", 34 );
195  define( "IP_MULTICAST_TTL", 33 );
196  }
197 
198  // pfsockopen doesn't work because we need set_sock_opt
199  $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
200  if ( !$conn ) {
201  $errstr = socket_strerror( socket_last_error() );
202  wfDebugLog( 'squid', __METHOD__ .
203  ": Error opening UDP socket: $errstr" );
204  wfProfileOut( __METHOD__ );
205 
206  return;
207  }
208 
209  // Set socket options
210  socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_LOOP, 0 );
211  if ( $wgHTCPMulticastTTL != 1 ) {
212  // Set multicast time to live (hop count) option on socket
213  socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_TTL,
214  $wgHTCPMulticastTTL );
215  }
216 
217  // Remove duplicate URLs from collection
218  $urlArr = array_unique( $urlArr );
219  // Get sequential trx IDs for packet loss counting
221  'squidhtcppurge', 32, count( $urlArr ), UIDGenerator::QUICK_VOLATILE
222  );
223 
224  foreach ( $urlArr as $url ) {
225  if ( !is_string( $url ) ) {
226  wfProfileOut( __METHOD__ );
227  throw new MWException( 'Bad purge URL' );
228  }
229  $url = self::expand( $url );
230  $conf = self::getRuleForURL( $url, $wgHTCPRouting );
231  if ( !$conf ) {
232  wfDebugLog( 'squid', __METHOD__ .
233  "No HTCP rule configured for URL {$url} , skipping" );
234  continue;
235  }
236 
237  if ( isset( $conf['host'] ) && isset( $conf['port'] ) ) {
238  // Normalize single entries
239  $conf = array( $conf );
240  }
241  foreach ( $conf as $subconf ) {
242  if ( !isset( $subconf['host'] ) || !isset( $subconf['port'] ) ) {
243  wfProfileOut( __METHOD__ );
244  throw new MWException( "Invalid HTCP rule for URL $url\n" );
245  }
246  }
247 
248  // Construct a minimal HTCP request diagram
249  // as per RFC 2756
250  // Opcode 'CLR', no response desired, no auth
251  $htcpTransID = current( $ids );
252  next( $ids );
253 
254  $htcpSpecifier = pack( 'na4na*na8n',
255  4, 'HEAD', strlen( $url ), $url,
256  8, 'HTTP/1.0', 0 );
257 
258  $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
259  $htcpLen = 4 + $htcpDataLen + 2;
260 
261  // Note! Squid gets the bit order of the first
262  // word wrong, wrt the RFC. Apparently no other
263  // implementation exists, so adapt to Squid
264  $htcpPacket = pack( 'nxxnCxNxxa*n',
265  $htcpLen, $htcpDataLen, $htcpOpCLR,
266  $htcpTransID, $htcpSpecifier, 2 );
267 
268  wfDebugLog( 'squid', __METHOD__ .
269  "Purging URL $url via HTCP" );
270  foreach ( $conf as $subconf ) {
271  socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
272  $subconf['host'], $subconf['port'] );
273  }
274  }
275  wfProfileOut( __METHOD__ );
276  }
277 
292  public static function expand( $url ) {
293  return wfExpandUrl( $url, PROTO_INTERNAL );
294  }
295 
302  private static function getRuleForURL( $url, $rules ) {
303  foreach ( $rules as $regex => $routing ) {
304  if ( $regex === '' || preg_match( $regex, $url ) ) {
305  return $routing;
306  }
307  }
308 
309  return false;
310  }
311 }
Title\makeTitle
static & makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:398
SquidUpdate\__construct
__construct( $urlArr=array(), $maxTitles=false)
Definition: SquidUpdate.php:38
SquidUpdate\getRuleForURL
static getRuleForURL( $url, $rules)
Find the HTCP routing rule to use for a given URL.
Definition: SquidUpdate.php:301
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
PROTO_INTERNAL
const PROTO_INTERNAL
Definition: Defines.php:272
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3714
wfDebugLog
wfDebugLog( $logGroup, $text, $dest='all')
Send a line to a supplementary debug log file, if configured, or main debug log if not.
Definition: GlobalFunctions.php:1087
wfProfileIn
wfProfileIn( $functionname)
Begin profiling of a function.
Definition: Profiler.php:33
SquidUpdate\expand
static expand( $url)
Expand local URLs to fully-qualified URLs using the internal protocol and host defined in $wgInternal...
Definition: SquidUpdate.php:291
SquidPurgeClient
An HTTP 1.0 client built for the purposes of purging Squid and Varnish.
Definition: SquidPurgeClient.php:30
SquidUpdate\newSimplePurge
static newSimplePurge(Title $title)
Definition: SquidUpdate.php:113
SquidPurgeClientPool
Definition: SquidPurgeClient.php:380
$dbr
$dbr
Definition: testCompression.php:48
SquidUpdate\newFromTitles
static newFromTitles( $titles, $urlArr=array())
Create a SquidUpdate from an array of Title objects, or a TitleArray object.
Definition: SquidUpdate.php:95
SquidUpdate\HTCPPurge
static HTCPPurge( $urlArr)
Send Hyper Text Caching Protocol (HTCP) CLR requests.
Definition: SquidUpdate.php:183
MWException
MediaWiki exception.
Definition: MWException.php:26
$titles
linkcache txt The LinkCache class maintains a list of article titles and the information about whether or not the article exists in the database This is used to mark up links when displaying a page If the same link appears more than once on any page then it only has to be looked up once In most cases link lookups are done in batches with the LinkBatch class or the equivalent in so the link cache is mostly useful for short snippets of parsed and for links in the navigation areas of the skin The link cache was formerly used to track links used in a document for the purposes of updating the link tables This application is now deprecated To create a you can use the following $titles
Definition: linkcache.txt:17
SquidUpdate\purge
static purge( $urlArr)
Purges a list of Squids defined in $wgSquidServers.
Definition: SquidUpdate.php:134
wfProfileOut
wfProfileOut( $functionname='missing')
Stop profiling of a function.
Definition: Profiler.php:46
SquidUpdate\newFromLinksTo
static newFromLinksTo(Title $title)
Create a SquidUpdate from the given Title object.
Definition: SquidUpdate.php:62
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
SquidUpdate
Handles purging appropriate Squid URLs given a title (or titles)
Definition: SquidUpdate.php:28
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
SquidUpdate\$urlArr
array $urlArr
Collection of URLs to purge.
Definition: SquidUpdate.php:32
SquidUpdate\doUpdate
doUpdate()
Purges the list of URLs passed to the constructor.
Definition: SquidUpdate.php:122
DB_SLAVE
const DB_SLAVE
Definition: Defines.php:55
Title
Represents a title within MediaWiki.
Definition: Title.php:35
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
UIDGenerator\QUICK_VOLATILE
const QUICK_VOLATILE
Definition: UIDGenerator.php:43
UIDGenerator\newSequentialPerNodeIDs
static newSequentialPerNodeIDs( $bucket, $bits, $count, $flags=0)
Return IDs that are sequential only for this node and bucket.
Definition: UIDGenerator.php:248
$res
$res
Definition: database.txt:21
wfExpandUrl
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
Definition: GlobalFunctions.php:544