1 | <?php |
||
31 | class CdnCacheUpdate implements DeferrableUpdate, MergeableUpdate { |
||
32 | /** @var string[] Collection of URLs to purge */ |
||
33 | protected $urls = []; |
||
34 | |||
35 | /** |
||
36 | * @param string[] $urlArr Collection of URLs to purge |
||
37 | */ |
||
38 | public function __construct( array $urlArr ) { |
||
41 | |||
42 | public function merge( MergeableUpdate $update ) { |
||
48 | |||
49 | /** |
||
50 | * Create an update object from an array of Title objects, or a TitleArray object |
||
51 | * |
||
52 | * @param Traversable|array $titles |
||
53 | * @param string[] $urlArr |
||
54 | * @return CdnCacheUpdate |
||
55 | */ |
||
56 | public static function newFromTitles( $titles, $urlArr = [] ) { |
||
64 | |||
65 | /** |
||
66 | * @param Title $title |
||
67 | * @return CdnCacheUpdate |
||
68 | * @deprecated since 1.27 |
||
69 | */ |
||
70 | public static function newSimplePurge( Title $title ) { |
||
73 | |||
74 | /** |
||
75 | * Purges the list of URLs passed to the constructor. |
||
76 | */ |
||
77 | public function doUpdate() { |
||
92 | |||
93 | /** |
||
94 | * Purges a list of CDN nodes defined in $wgSquidServers. |
||
95 | * $urlArr should contain the full URLs to purge as values |
||
96 | * (example: $urlArr[] = 'http://my.host/something') |
||
97 | * |
||
98 | * @param string[] $urlArr List of full URLs to purge |
||
99 | */ |
||
100 | public static function purge( array $urlArr ) { |
||
101 | global $wgSquidServers, $wgHTCPRouting; |
||
102 | |||
103 | if ( !$urlArr ) { |
||
104 | return; |
||
105 | } |
||
106 | |||
107 | // Remove duplicate URLs from list |
||
108 | $urlArr = array_unique( $urlArr ); |
||
109 | |||
110 | wfDebugLog( 'squid', __METHOD__ . ': ' . implode( ' ', $urlArr ) ); |
||
111 | |||
112 | // Reliably broadcast the purge to all edge nodes |
||
113 | $relayer = MediaWikiServices::getInstance()->getEventRelayerGroup() |
||
114 | ->getRelayer( 'cdn-url-purges' ); |
||
115 | $ts = microtime( true ); |
||
116 | $relayer->notifyMulti( |
||
117 | 'cdn-url-purges', |
||
118 | array_map( |
||
119 | function ( $url ) use ( $ts ) { |
||
120 | return [ |
||
121 | 'url' => $url, |
||
122 | 'timestamp' => $ts, |
||
123 | ]; |
||
124 | }, |
||
125 | $urlArr |
||
126 | ) |
||
127 | ); |
||
128 | |||
129 | // Send lossy UDP broadcasting if enabled |
||
130 | if ( $wgHTCPRouting ) { |
||
131 | self::HTCPPurge( $urlArr ); |
||
132 | } |
||
133 | |||
134 | // Do direct server purges if enabled (this does not scale very well) |
||
135 | if ( $wgSquidServers ) { |
||
136 | // Maximum number of parallel connections per squid |
||
137 | $maxSocketsPerSquid = 8; |
||
138 | // Number of requests to send per socket |
||
139 | // 400 seems to be a good tradeoff, opening a socket takes a while |
||
140 | $urlsPerSocket = 400; |
||
141 | $socketsPerSquid = ceil( count( $urlArr ) / $urlsPerSocket ); |
||
142 | if ( $socketsPerSquid > $maxSocketsPerSquid ) { |
||
143 | $socketsPerSquid = $maxSocketsPerSquid; |
||
144 | } |
||
145 | |||
146 | $pool = new SquidPurgeClientPool; |
||
147 | $chunks = array_chunk( $urlArr, ceil( count( $urlArr ) / $socketsPerSquid ) ); |
||
148 | foreach ( $wgSquidServers as $server ) { |
||
149 | foreach ( $chunks as $chunk ) { |
||
150 | $client = new SquidPurgeClient( $server ); |
||
151 | foreach ( $chunk as $url ) { |
||
152 | $client->queuePurge( $url ); |
||
153 | } |
||
154 | $pool->addClient( $client ); |
||
155 | } |
||
156 | } |
||
157 | |||
158 | $pool->run(); |
||
159 | } |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Send Hyper Text Caching Protocol (HTCP) CLR requests. |
||
164 | * |
||
165 | * @throws MWException |
||
166 | * @param string[] $urlArr Collection of URLs to purge |
||
167 | */ |
||
168 | private static function HTCPPurge( array $urlArr ) { |
||
254 | |||
255 | /** |
||
256 | * Expand local URLs to fully-qualified URLs using the internal protocol |
||
257 | * and host defined in $wgInternalServer. Input that's already fully- |
||
258 | * qualified will be passed through unchanged. |
||
259 | * |
||
260 | * This is used to generate purge URLs that may be either local to the |
||
261 | * main wiki or include a non-native host, such as images hosted on a |
||
262 | * second internal server. |
||
263 | * |
||
264 | * Client functions should not need to call this. |
||
265 | * |
||
266 | * @param string $url |
||
267 | * @return string |
||
268 | */ |
||
269 | public static function expand( $url ) { |
||
272 | |||
273 | /** |
||
274 | * Find the HTCP routing rule to use for a given URL. |
||
275 | * @param string $url URL to match |
||
276 | * @param array $rules Array of rules, see $wgHTCPRouting for format and behavior |
||
277 | * @return mixed Element of $rules that matched, or false if nothing matched |
||
278 | */ |
||
279 | private static function getRuleForURL( $url, $rules ) { |
||
288 | } |
||
289 | |||
296 |
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.