Merge "maintenance: Script to rename titles for Unicode uppercasing changes"
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderWikiModule.php
1 <?php
2 /**
3 * Abstraction for ResourceLoader modules that pull from wiki pages.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @author Trevor Parscal
22 * @author Roan Kattouw
23 */
24
25 use MediaWiki\Linker\LinkTarget;
26 use Wikimedia\Assert\Assert;
27 use Wikimedia\Rdbms\Database;
28 use Wikimedia\Rdbms\IDatabase;
29 use MediaWiki\MediaWikiServices;
30
31 /**
32 * Abstraction for ResourceLoader modules which pull from wiki pages
33 *
34 * This can only be used for wiki pages in the MediaWiki and User namespaces,
35 * because of its dependence on the functionality of Title::isUserConfigPage()
36 * and Title::isSiteConfigPage().
37 *
38 * This module supports being used as a placeholder for a module on a remote wiki.
39 * To do so, getDB() must be overloaded to return a foreign database object that
40 * allows local wikis to query page metadata.
41 *
42 * Safe for calls on local wikis are:
43 * - Option getters:
44 * - getGroup()
45 * - getPages()
46 * - Basic methods that strictly involve the foreign database
47 * - getDB()
48 * - isKnownEmpty()
49 * - getTitleInfo()
50 */
51 class ResourceLoaderWikiModule extends ResourceLoaderModule {
52
53 // Origin defaults to users with sitewide authority
54 protected $origin = self::ORIGIN_USER_SITEWIDE;
55
56 // In-process cache for title info, structured as an array
57 // [
58 // <batchKey> // Pipe-separated list of sorted keys from getPages
59 // => [
60 // <titleKey> => [ // Normalised title key
61 // 'page_len' => ..,
62 // 'page_latest' => ..,
63 // 'page_touched' => ..,
64 // ]
65 // ]
66 // ]
67 // @see self::fetchTitleInfo()
68 // @see self::makeTitleKey()
69 protected $titleInfo = [];
70
71 // List of page names that contain CSS
72 protected $styles = [];
73
74 // List of page names that contain JavaScript
75 protected $scripts = [];
76
77 // Group of module
78 protected $group;
79
80 /**
81 * @param array|null $options For back-compat, this can be omitted in favour of overwriting
82 * getPages.
83 */
84 public function __construct( array $options = null ) {
85 if ( $options === null ) {
86 return;
87 }
88
89 foreach ( $options as $member => $option ) {
90 switch ( $member ) {
91 case 'styles':
92 case 'scripts':
93 case 'group':
94 case 'targets':
95 $this->{$member} = $option;
96 break;
97 }
98 }
99 }
100
101 /**
102 * Subclasses should return an associative array of resources in the module.
103 * Keys should be the title of a page in the MediaWiki or User namespace.
104 *
105 * Values should be a nested array of options. The supported keys are 'type' and
106 * (CSS only) 'media'.
107 *
108 * For scripts, 'type' should be 'script'.
109 *
110 * For stylesheets, 'type' should be 'style'.
111 * There is an optional media key, the value of which can be the
112 * medium ('screen', 'print', etc.) of the stylesheet.
113 *
114 * @param ResourceLoaderContext $context
115 * @return array
116 */
117 protected function getPages( ResourceLoaderContext $context ) {
118 $config = $this->getConfig();
119 $pages = [];
120
121 // Filter out pages from origins not allowed by the current wiki configuration.
122 if ( $config->get( 'UseSiteJs' ) ) {
123 foreach ( $this->scripts as $script ) {
124 $pages[$script] = [ 'type' => 'script' ];
125 }
126 }
127
128 if ( $config->get( 'UseSiteCss' ) ) {
129 foreach ( $this->styles as $style ) {
130 $pages[$style] = [ 'type' => 'style' ];
131 }
132 }
133
134 return $pages;
135 }
136
137 /**
138 * Get group name
139 *
140 * @return string
141 */
142 public function getGroup() {
143 return $this->group;
144 }
145
146 /**
147 * Get the Database handle used for computing the module version.
148 *
149 * Subclasses may override this to return a foreign database, which would
150 * allow them to register a module on wiki A that fetches wiki pages from
151 * wiki B.
152 *
153 * The way this works is that the local module is a placeholder that can
154 * only computer a module version hash. The 'source' of the module must
155 * be set to the foreign wiki directly. Methods getScript() and getContent()
156 * will not use this handle and are not valid on the local wiki.
157 *
158 * @return IDatabase
159 */
160 protected function getDB() {
161 return wfGetDB( DB_REPLICA );
162 }
163
164 /**
165 * @param string $titleText
166 * @param ResourceLoaderContext|null $context (but passing null is deprecated)
167 * @return null|string
168 * @since 1.32 added the $context parameter
169 */
170 protected function getContent( $titleText, ResourceLoaderContext $context = null ) {
171 $title = Title::newFromText( $titleText );
172 if ( !$title ) {
173 return null; // Bad title
174 }
175
176 $content = $this->getContentObj( $title, $context );
177 if ( !$content ) {
178 return null; // No content found
179 }
180
181 $handler = $content->getContentHandler();
182 if ( $handler->isSupportedFormat( CONTENT_FORMAT_CSS ) ) {
183 $format = CONTENT_FORMAT_CSS;
184 } elseif ( $handler->isSupportedFormat( CONTENT_FORMAT_JAVASCRIPT ) ) {
185 $format = CONTENT_FORMAT_JAVASCRIPT;
186 } else {
187 return null; // Bad content model
188 }
189
190 return $content->serialize( $format );
191 }
192
193 /**
194 * @param Title $title
195 * @param ResourceLoaderContext|null $context (but passing null is deprecated)
196 * @param int|null $maxRedirects Maximum number of redirects to follow. If
197 * null, uses $wgMaxRedirects
198 * @return Content|null
199 * @since 1.32 added the $context and $maxRedirects parameters
200 */
201 protected function getContentObj(
202 Title $title, ResourceLoaderContext $context = null, $maxRedirects = null
203 ) {
204 if ( $context === null ) {
205 wfDeprecated( __METHOD__ . ' without a ResourceLoader context', '1.32' );
206 }
207
208 $overrideCallback = $context ? $context->getContentOverrideCallback() : null;
209 $content = $overrideCallback ? call_user_func( $overrideCallback, $title ) : null;
210 if ( $content ) {
211 if ( !$content instanceof Content ) {
212 $this->getLogger()->error(
213 'Bad content override for "{title}" in ' . __METHOD__,
214 [ 'title' => $title->getPrefixedText() ]
215 );
216 return null;
217 }
218 } else {
219 $revision = Revision::newKnownCurrent( wfGetDB( DB_REPLICA ), $title );
220 if ( !$revision ) {
221 return null;
222 }
223 $content = $revision->getContent( Revision::RAW );
224
225 if ( !$content ) {
226 $this->getLogger()->error(
227 'Failed to load content of JS/CSS page "{title}" in ' . __METHOD__,
228 [ 'title' => $title->getPrefixedText() ]
229 );
230 return null;
231 }
232 }
233
234 if ( $content && $content->isRedirect() ) {
235 if ( $maxRedirects === null ) {
236 $maxRedirects = $this->getConfig()->get( 'MaxRedirects' ) ?: 0;
237 }
238 if ( $maxRedirects > 0 ) {
239 $newTitle = $content->getRedirectTarget();
240 return $newTitle ? $this->getContentObj( $newTitle, $context, $maxRedirects - 1 ) : null;
241 }
242 }
243
244 return $content;
245 }
246
247 /**
248 * @param ResourceLoaderContext $context
249 * @return bool
250 */
251 public function shouldEmbedModule( ResourceLoaderContext $context ) {
252 $overrideCallback = $context->getContentOverrideCallback();
253 if ( $overrideCallback && $this->getSource() === 'local' ) {
254 foreach ( $this->getPages( $context ) as $page => $info ) {
255 $title = Title::newFromText( $page );
256 if ( $title && call_user_func( $overrideCallback, $title ) !== null ) {
257 return true;
258 }
259 }
260 }
261
262 return parent::shouldEmbedModule( $context );
263 }
264
265 /**
266 * @param ResourceLoaderContext $context
267 * @return string JavaScript code
268 */
269 public function getScript( ResourceLoaderContext $context ) {
270 $scripts = '';
271 foreach ( $this->getPages( $context ) as $titleText => $options ) {
272 if ( $options['type'] !== 'script' ) {
273 continue;
274 }
275 $script = $this->getContent( $titleText, $context );
276 if ( strval( $script ) !== '' ) {
277 $script = $this->validateScriptFile( $titleText, $script );
278 $scripts .= ResourceLoader::makeComment( $titleText ) . $script . "\n";
279 }
280 }
281 return $scripts;
282 }
283
284 /**
285 * @param ResourceLoaderContext $context
286 * @return array
287 */
288 public function getStyles( ResourceLoaderContext $context ) {
289 $styles = [];
290 foreach ( $this->getPages( $context ) as $titleText => $options ) {
291 if ( $options['type'] !== 'style' ) {
292 continue;
293 }
294 $media = $options['media'] ?? 'all';
295 $style = $this->getContent( $titleText, $context );
296 if ( strval( $style ) === '' ) {
297 continue;
298 }
299 if ( $this->getFlip( $context ) ) {
300 $style = CSSJanus::transform( $style, true, false );
301 }
302 $style = MemoizedCallable::call( 'CSSMin::remap',
303 [ $style, false, $this->getConfig()->get( 'ScriptPath' ), true ] );
304 if ( !isset( $styles[$media] ) ) {
305 $styles[$media] = [];
306 }
307 $style = ResourceLoader::makeComment( $titleText ) . $style;
308 $styles[$media][] = $style;
309 }
310 return $styles;
311 }
312
313 /**
314 * Disable module content versioning.
315 *
316 * This class does not support generating content outside of a module
317 * request due to foreign database support.
318 *
319 * See getDefinitionSummary() for meta-data versioning.
320 *
321 * @return bool
322 */
323 public function enableModuleContentVersion() {
324 return false;
325 }
326
327 /**
328 * @param ResourceLoaderContext $context
329 * @return array
330 */
331 public function getDefinitionSummary( ResourceLoaderContext $context ) {
332 $summary = parent::getDefinitionSummary( $context );
333 $summary[] = [
334 'pages' => $this->getPages( $context ),
335 // Includes meta data of current revisions
336 'titleInfo' => $this->getTitleInfo( $context ),
337 ];
338 return $summary;
339 }
340
341 /**
342 * @param ResourceLoaderContext $context
343 * @return bool
344 */
345 public function isKnownEmpty( ResourceLoaderContext $context ) {
346 $revisions = $this->getTitleInfo( $context );
347
348 // If a module has dependencies it cannot be empty. An empty array will be cast to false
349 if ( $this->getDependencies() ) {
350 return false;
351 }
352 // For user modules, don't needlessly load if there are no non-empty pages
353 if ( $this->getGroup() === 'user' ) {
354 foreach ( $revisions as $revision ) {
355 if ( $revision['page_len'] > 0 ) {
356 // At least one non-empty page, module should be loaded
357 return false;
358 }
359 }
360 return true;
361 }
362
363 // T70488: For other modules (i.e. ones that are called in cached html output) only check
364 // page existance. This ensures that, if some pages in a module are temporarily blanked,
365 // we don't end omit the module's script or link tag on some pages.
366 return count( $revisions ) === 0;
367 }
368
369 private function setTitleInfo( $batchKey, array $titleInfo ) {
370 $this->titleInfo[$batchKey] = $titleInfo;
371 }
372
373 private static function makeTitleKey( LinkTarget $title ) {
374 // Used for keys in titleInfo.
375 return "{$title->getNamespace()}:{$title->getDBkey()}";
376 }
377
378 /**
379 * Get the information about the wiki pages for a given context.
380 * @param ResourceLoaderContext $context
381 * @return array Keyed by page name
382 */
383 protected function getTitleInfo( ResourceLoaderContext $context ) {
384 $dbr = $this->getDB();
385
386 $pageNames = array_keys( $this->getPages( $context ) );
387 sort( $pageNames );
388 $batchKey = implode( '|', $pageNames );
389 if ( !isset( $this->titleInfo[$batchKey] ) ) {
390 $this->titleInfo[$batchKey] = static::fetchTitleInfo( $dbr, $pageNames, __METHOD__ );
391 }
392
393 $titleInfo = $this->titleInfo[$batchKey];
394
395 // Override the title info from the overrides, if any
396 $overrideCallback = $context->getContentOverrideCallback();
397 if ( $overrideCallback ) {
398 foreach ( $pageNames as $page ) {
399 $title = Title::newFromText( $page );
400 $content = $title ? call_user_func( $overrideCallback, $title ) : null;
401 if ( $content !== null ) {
402 $titleInfo[$title->getPrefixedText()] = [
403 'page_len' => $content->getSize(),
404 'page_latest' => 'TBD', // None available
405 'page_touched' => wfTimestamp( TS_MW ),
406 ];
407 }
408 }
409 }
410
411 return $titleInfo;
412 }
413
414 protected static function fetchTitleInfo( IDatabase $db, array $pages, $fname = __METHOD__ ) {
415 $titleInfo = [];
416 $batch = new LinkBatch;
417 foreach ( $pages as $titleText ) {
418 $title = Title::newFromText( $titleText );
419 if ( $title ) {
420 // Page name may be invalid if user-provided (e.g. gadgets)
421 $batch->addObj( $title );
422 }
423 }
424 if ( !$batch->isEmpty() ) {
425 $res = $db->select( 'page',
426 // Include page_touched to allow purging if cache is poisoned (T117587, T113916)
427 [ 'page_namespace', 'page_title', 'page_touched', 'page_len', 'page_latest' ],
428 $batch->constructSet( 'page', $db ),
429 $fname
430 );
431 foreach ( $res as $row ) {
432 // Avoid including ids or timestamps of revision/page tables so
433 // that versions are not wasted
434 $title = new TitleValue( (int)$row->page_namespace, $row->page_title );
435 $titleInfo[self::makeTitleKey( $title )] = [
436 'page_len' => $row->page_len,
437 'page_latest' => $row->page_latest,
438 'page_touched' => $row->page_touched,
439 ];
440 }
441 }
442 return $titleInfo;
443 }
444
445 /**
446 * @since 1.28
447 * @param ResourceLoaderContext $context
448 * @param IDatabase $db
449 * @param string[] $moduleNames
450 */
451 public static function preloadTitleInfo(
452 ResourceLoaderContext $context, IDatabase $db, array $moduleNames
453 ) {
454 $rl = $context->getResourceLoader();
455 // getDB() can be overridden to point to a foreign database.
456 // For now, only preload local. In the future, we could preload by wikiID.
457 $allPages = [];
458 /** @var ResourceLoaderWikiModule[] $wikiModules */
459 $wikiModules = [];
460 foreach ( $moduleNames as $name ) {
461 $module = $rl->getModule( $name );
462 if ( $module instanceof self ) {
463 $mDB = $module->getDB();
464 // Subclasses may implement getDB differently
465 if ( $mDB->getDomainID() === $db->getDomainID() ) {
466 $wikiModules[] = $module;
467 $allPages += $module->getPages( $context );
468 }
469 }
470 }
471
472 if ( !$wikiModules ) {
473 // Nothing to preload
474 return;
475 }
476
477 $pageNames = array_keys( $allPages );
478 sort( $pageNames );
479 $hash = sha1( implode( '|', $pageNames ) );
480
481 // Avoid Zend bug where "static::" does not apply LSB in the closure
482 $func = [ static::class, 'fetchTitleInfo' ];
483 $fname = __METHOD__;
484
485 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
486 $allInfo = $cache->getWithSetCallback(
487 $cache->makeGlobalKey( 'resourceloader-titleinfo', $db->getDomainID(), $hash ),
488 $cache::TTL_HOUR,
489 function ( $curVal, &$ttl, array &$setOpts ) use ( $func, $pageNames, $db, $fname ) {
490 $setOpts += Database::getCacheSetOptions( $db );
491
492 return call_user_func( $func, $db, $pageNames, $fname );
493 },
494 [
495 'checkKeys' => [
496 $cache->makeGlobalKey( 'resourceloader-titleinfo', $db->getDomainID() ) ]
497 ]
498 );
499
500 foreach ( $wikiModules as $wikiModule ) {
501 $pages = $wikiModule->getPages( $context );
502 // Before we intersect, map the names to canonical form (T145673).
503 $intersect = [];
504 foreach ( $pages as $pageName => $unused ) {
505 $title = Title::newFromText( $pageName );
506 if ( $title ) {
507 $intersect[ self::makeTitleKey( $title ) ] = 1;
508 } else {
509 // Page name may be invalid if user-provided (e.g. gadgets)
510 $rl->getLogger()->info(
511 'Invalid wiki page title "{title}" in ' . __METHOD__,
512 [ 'title' => $pageName ]
513 );
514 }
515 }
516 $info = array_intersect_key( $allInfo, $intersect );
517 $pageNames = array_keys( $pages );
518 sort( $pageNames );
519 $batchKey = implode( '|', $pageNames );
520 $wikiModule->setTitleInfo( $batchKey, $info );
521 }
522 }
523
524 /**
525 * Clear the preloadTitleInfo() cache for all wiki modules on this wiki on
526 * page change if it was a JS or CSS page
527 *
528 * @param Title $title
529 * @param Revision|null $old Prior page revision
530 * @param Revision|null $new New page revision
531 * @param string $domain Database domain ID
532 * @since 1.28
533 */
534 public static function invalidateModuleCache(
535 Title $title, Revision $old = null, Revision $new = null, $domain
536 ) {
537 static $formats = [ CONTENT_FORMAT_CSS, CONTENT_FORMAT_JAVASCRIPT ];
538
539 Assert::parameterType( 'string', $domain, '$domain' );
540
541 // TODO: MCR: differentiate between page functionality and content model!
542 // Not all pages containing CSS or JS have to be modules! [PageType]
543 if ( $old && in_array( $old->getContentFormat(), $formats ) ) {
544 $purge = true;
545 } elseif ( $new && in_array( $new->getContentFormat(), $formats ) ) {
546 $purge = true;
547 } else {
548 $purge = ( $title->isSiteConfigPage() || $title->isUserConfigPage() );
549 }
550
551 if ( $purge ) {
552 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
553 $key = $cache->makeGlobalKey( 'resourceloader-titleinfo', $domain );
554 $cache->touchCheckKey( $key );
555 }
556 }
557
558 /**
559 * @since 1.28
560 * @return string
561 */
562 public function getType() {
563 // Check both because subclasses don't always pass pages via the constructor,
564 // they may also override getPages() instead, in which case we should keep
565 // defaulting to LOAD_GENERAL and allow them to override getType() separately.
566 return ( $this->styles && !$this->scripts ) ? self::LOAD_STYLES : self::LOAD_GENERAL;
567 }
568 }