Merge "Improve docs for Title::getInternalURL/getCanonicalURL"
[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 ( is_null( $options ) ) {
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 object used in getTitleInfo().
148 *
149 * Defaults to the local replica DB. Subclasses may want to override this to return a foreign
150 * database object, or null if getTitleInfo() shouldn't access the database.
151 *
152 * NOTE: This ONLY works for getTitleInfo() and isKnownEmpty(), NOT FOR ANYTHING ELSE.
153 * In particular, it doesn't work for getContent() or getScript() etc.
154 *
155 * @return IDatabase|null
156 */
157 protected function getDB() {
158 return wfGetDB( DB_REPLICA );
159 }
160
161 /**
162 * @param string $titleText
163 * @param ResourceLoaderContext|null $context (but passing null is deprecated)
164 * @return null|string
165 * @since 1.32 added the $context parameter
166 */
167 protected function getContent( $titleText, ResourceLoaderContext $context = null ) {
168 $title = Title::newFromText( $titleText );
169 if ( !$title ) {
170 return null; // Bad title
171 }
172
173 $content = $this->getContentObj( $title, $context );
174 if ( !$content ) {
175 return null; // No content found
176 }
177
178 $handler = $content->getContentHandler();
179 if ( $handler->isSupportedFormat( CONTENT_FORMAT_CSS ) ) {
180 $format = CONTENT_FORMAT_CSS;
181 } elseif ( $handler->isSupportedFormat( CONTENT_FORMAT_JAVASCRIPT ) ) {
182 $format = CONTENT_FORMAT_JAVASCRIPT;
183 } else {
184 return null; // Bad content model
185 }
186
187 return $content->serialize( $format );
188 }
189
190 /**
191 * @param Title $title
192 * @param ResourceLoaderContext|null $context (but passing null is deprecated)
193 * @param int|null $maxRedirects Maximum number of redirects to follow. If
194 * null, uses $wgMaxRedirects
195 * @return Content|null
196 * @since 1.32 added the $context and $maxRedirects parameters
197 */
198 protected function getContentObj(
199 Title $title, ResourceLoaderContext $context = null, $maxRedirects = null
200 ) {
201 if ( $context === null ) {
202 wfDeprecated( __METHOD__ . ' without a ResourceLoader context', '1.32' );
203 }
204
205 $overrideCallback = $context ? $context->getContentOverrideCallback() : null;
206 $content = $overrideCallback ? call_user_func( $overrideCallback, $title ) : null;
207 if ( $content ) {
208 if ( !$content instanceof Content ) {
209 $this->getLogger()->error(
210 'Bad content override for "{title}" in ' . __METHOD__,
211 [ 'title' => $title->getPrefixedText() ]
212 );
213 return null;
214 }
215 } else {
216 $revision = Revision::newKnownCurrent( wfGetDB( DB_REPLICA ), $title );
217 if ( !$revision ) {
218 return null;
219 }
220 $content = $revision->getContent( Revision::RAW );
221
222 if ( !$content ) {
223 $this->getLogger()->error(
224 'Failed to load content of JS/CSS page "{title}" in ' . __METHOD__,
225 [ 'title' => $title->getPrefixedText() ]
226 );
227 return null;
228 }
229 }
230
231 if ( $content && $content->isRedirect() ) {
232 if ( $maxRedirects === null ) {
233 $maxRedirects = $this->getConfig()->get( 'MaxRedirects' ) ?: 0;
234 }
235 if ( $maxRedirects > 0 ) {
236 $newTitle = $content->getRedirectTarget();
237 return $newTitle ? $this->getContentObj( $newTitle, $context, $maxRedirects - 1 ) : null;
238 }
239 }
240
241 return $content;
242 }
243
244 /**
245 * @param ResourceLoaderContext $context
246 * @return bool
247 */
248 public function shouldEmbedModule( ResourceLoaderContext $context ) {
249 $overrideCallback = $context->getContentOverrideCallback();
250 if ( $overrideCallback && $this->getSource() === 'local' ) {
251 foreach ( $this->getPages( $context ) as $page => $info ) {
252 $title = Title::newFromText( $page );
253 if ( $title && call_user_func( $overrideCallback, $title ) !== null ) {
254 return true;
255 }
256 }
257 }
258
259 return parent::shouldEmbedModule( $context );
260 }
261
262 /**
263 * @param ResourceLoaderContext $context
264 * @return string JavaScript code
265 */
266 public function getScript( ResourceLoaderContext $context ) {
267 $scripts = '';
268 foreach ( $this->getPages( $context ) as $titleText => $options ) {
269 if ( $options['type'] !== 'script' ) {
270 continue;
271 }
272 $script = $this->getContent( $titleText, $context );
273 if ( strval( $script ) !== '' ) {
274 $script = $this->validateScriptFile( $titleText, $script );
275 $scripts .= ResourceLoader::makeComment( $titleText ) . $script . "\n";
276 }
277 }
278 return $scripts;
279 }
280
281 /**
282 * @param ResourceLoaderContext $context
283 * @return array
284 */
285 public function getStyles( ResourceLoaderContext $context ) {
286 $styles = [];
287 foreach ( $this->getPages( $context ) as $titleText => $options ) {
288 if ( $options['type'] !== 'style' ) {
289 continue;
290 }
291 $media = $options['media'] ?? 'all';
292 $style = $this->getContent( $titleText, $context );
293 if ( strval( $style ) === '' ) {
294 continue;
295 }
296 if ( $this->getFlip( $context ) ) {
297 $style = CSSJanus::transform( $style, true, false );
298 }
299 $style = MemoizedCallable::call( 'CSSMin::remap',
300 [ $style, false, $this->getConfig()->get( 'ScriptPath' ), true ] );
301 if ( !isset( $styles[$media] ) ) {
302 $styles[$media] = [];
303 }
304 $style = ResourceLoader::makeComment( $titleText ) . $style;
305 $styles[$media][] = $style;
306 }
307 return $styles;
308 }
309
310 /**
311 * Disable module content versioning.
312 *
313 * This class does not support generating content outside of a module
314 * request due to foreign database support.
315 *
316 * See getDefinitionSummary() for meta-data versioning.
317 *
318 * @return bool
319 */
320 public function enableModuleContentVersion() {
321 return false;
322 }
323
324 /**
325 * @param ResourceLoaderContext $context
326 * @return array
327 */
328 public function getDefinitionSummary( ResourceLoaderContext $context ) {
329 $summary = parent::getDefinitionSummary( $context );
330 $summary[] = [
331 'pages' => $this->getPages( $context ),
332 // Includes meta data of current revisions
333 'titleInfo' => $this->getTitleInfo( $context ),
334 ];
335 return $summary;
336 }
337
338 /**
339 * @param ResourceLoaderContext $context
340 * @return bool
341 */
342 public function isKnownEmpty( ResourceLoaderContext $context ) {
343 $revisions = $this->getTitleInfo( $context );
344
345 // If a module has dependencies it cannot be empty. An empty array will be cast to false
346 if ( $this->getDependencies() ) {
347 return false;
348 }
349 // For user modules, don't needlessly load if there are no non-empty pages
350 if ( $this->getGroup() === 'user' ) {
351 foreach ( $revisions as $revision ) {
352 if ( $revision['page_len'] > 0 ) {
353 // At least one non-empty page, module should be loaded
354 return false;
355 }
356 }
357 return true;
358 }
359
360 // T70488: For other modules (i.e. ones that are called in cached html output) only check
361 // page existance. This ensures that, if some pages in a module are temporarily blanked,
362 // we don't end omit the module's script or link tag on some pages.
363 return count( $revisions ) === 0;
364 }
365
366 private function setTitleInfo( $batchKey, array $titleInfo ) {
367 $this->titleInfo[$batchKey] = $titleInfo;
368 }
369
370 private static function makeTitleKey( LinkTarget $title ) {
371 // Used for keys in titleInfo.
372 return "{$title->getNamespace()}:{$title->getDBkey()}";
373 }
374
375 /**
376 * Get the information about the wiki pages for a given context.
377 * @param ResourceLoaderContext $context
378 * @return array Keyed by page name
379 */
380 protected function getTitleInfo( ResourceLoaderContext $context ) {
381 $dbr = $this->getDB();
382 if ( !$dbr ) {
383 // We're dealing with a subclass that doesn't have a DB
384 return [];
385 }
386
387 $pageNames = array_keys( $this->getPages( $context ) );
388 sort( $pageNames );
389 $batchKey = implode( '|', $pageNames );
390 if ( !isset( $this->titleInfo[$batchKey] ) ) {
391 $this->titleInfo[$batchKey] = static::fetchTitleInfo( $dbr, $pageNames, __METHOD__ );
392 }
393
394 $titleInfo = $this->titleInfo[$batchKey];
395
396 // Override the title info from the overrides, if any
397 $overrideCallback = $context->getContentOverrideCallback();
398 if ( $overrideCallback ) {
399 foreach ( $pageNames as $page ) {
400 $title = Title::newFromText( $page );
401 $content = $title ? call_user_func( $overrideCallback, $title ) : null;
402 if ( $content !== null ) {
403 $titleInfo[$title->getPrefixedText()] = [
404 'page_len' => $content->getSize(),
405 'page_latest' => 'TBD', // None available
406 'page_touched' => wfTimestamp( TS_MW ),
407 ];
408 }
409 }
410 }
411
412 return $titleInfo;
413 }
414
415 protected static function fetchTitleInfo( IDatabase $db, array $pages, $fname = __METHOD__ ) {
416 $titleInfo = [];
417 $batch = new LinkBatch;
418 foreach ( $pages as $titleText ) {
419 $title = Title::newFromText( $titleText );
420 if ( $title ) {
421 // Page name may be invalid if user-provided (e.g. gadgets)
422 $batch->addObj( $title );
423 }
424 }
425 if ( !$batch->isEmpty() ) {
426 $res = $db->select( 'page',
427 // Include page_touched to allow purging if cache is poisoned (T117587, T113916)
428 [ 'page_namespace', 'page_title', 'page_touched', 'page_len', 'page_latest' ],
429 $batch->constructSet( 'page', $db ),
430 $fname
431 );
432 foreach ( $res as $row ) {
433 // Avoid including ids or timestamps of revision/page tables so
434 // that versions are not wasted
435 $title = new TitleValue( (int)$row->page_namespace, $row->page_title );
436 $titleInfo[self::makeTitleKey( $title )] = [
437 'page_len' => $row->page_len,
438 'page_latest' => $row->page_latest,
439 'page_touched' => $row->page_touched,
440 ];
441 }
442 }
443 return $titleInfo;
444 }
445
446 /**
447 * @since 1.28
448 * @param ResourceLoaderContext $context
449 * @param IDatabase $db
450 * @param string[] $moduleNames
451 */
452 public static function preloadTitleInfo(
453 ResourceLoaderContext $context, IDatabase $db, array $moduleNames
454 ) {
455 $rl = $context->getResourceLoader();
456 // getDB() can be overridden to point to a foreign database.
457 // For now, only preload local. In the future, we could preload by wikiID.
458 $allPages = [];
459 /** @var ResourceLoaderWikiModule[] $wikiModules */
460 $wikiModules = [];
461 foreach ( $moduleNames as $name ) {
462 $module = $rl->getModule( $name );
463 if ( $module instanceof self ) {
464 $mDB = $module->getDB();
465 // Subclasses may disable getDB and implement getTitleInfo differently
466 if ( $mDB && $mDB->getDomainID() === $db->getDomainID() ) {
467 $wikiModules[] = $module;
468 $allPages += $module->getPages( $context );
469 }
470 }
471 }
472
473 if ( !$wikiModules ) {
474 // Nothing to preload
475 return;
476 }
477
478 $pageNames = array_keys( $allPages );
479 sort( $pageNames );
480 $hash = sha1( implode( '|', $pageNames ) );
481
482 // Avoid Zend bug where "static::" does not apply LSB in the closure
483 $func = [ static::class, 'fetchTitleInfo' ];
484 $fname = __METHOD__;
485
486 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
487 $allInfo = $cache->getWithSetCallback(
488 $cache->makeGlobalKey( 'resourceloader', 'titleinfo', $db->getDomainID(), $hash ),
489 $cache::TTL_HOUR,
490 function ( $curVal, &$ttl, array &$setOpts ) use ( $func, $pageNames, $db, $fname ) {
491 $setOpts += Database::getCacheSetOptions( $db );
492
493 return call_user_func( $func, $db, $pageNames, $fname );
494 },
495 [
496 'checkKeys' => [
497 $cache->makeGlobalKey( 'resourceloader', 'titleinfo', $db->getDomainID() ) ]
498 ]
499 );
500
501 foreach ( $wikiModules as $wikiModule ) {
502 $pages = $wikiModule->getPages( $context );
503 // Before we intersect, map the names to canonical form (T145673).
504 $intersect = [];
505 foreach ( $pages as $pageName => $unused ) {
506 $title = Title::newFromText( $pageName );
507 if ( $title ) {
508 $intersect[ self::makeTitleKey( $title ) ] = 1;
509 } else {
510 // Page name may be invalid if user-provided (e.g. gadgets)
511 $rl->getLogger()->info(
512 'Invalid wiki page title "{title}" in ' . __METHOD__,
513 [ 'title' => $pageName ]
514 );
515 }
516 }
517 $info = array_intersect_key( $allInfo, $intersect );
518 $pageNames = array_keys( $pages );
519 sort( $pageNames );
520 $batchKey = implode( '|', $pageNames );
521 $wikiModule->setTitleInfo( $batchKey, $info );
522 }
523 }
524
525 /**
526 * Clear the preloadTitleInfo() cache for all wiki modules on this wiki on
527 * page change if it was a JS or CSS page
528 *
529 * @param Title $title
530 * @param Revision|null $old Prior page revision
531 * @param Revision|null $new New page revision
532 * @param string $domain Database domain ID
533 * @since 1.28
534 */
535 public static function invalidateModuleCache(
536 Title $title, Revision $old = null, Revision $new = null, $domain
537 ) {
538 static $formats = [ CONTENT_FORMAT_CSS, CONTENT_FORMAT_JAVASCRIPT ];
539
540 Assert::parameterType( 'string', $domain, '$domain' );
541
542 // TODO: MCR: differentiate between page functionality and content model!
543 // Not all pages containing CSS or JS have to be modules! [PageType]
544 if ( $old && in_array( $old->getContentFormat(), $formats ) ) {
545 $purge = true;
546 } elseif ( $new && in_array( $new->getContentFormat(), $formats ) ) {
547 $purge = true;
548 } else {
549 $purge = ( $title->isSiteConfigPage() || $title->isUserConfigPage() );
550 }
551
552 if ( $purge ) {
553 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
554 $key = $cache->makeGlobalKey( 'resourceloader', 'titleinfo', $domain );
555 $cache->touchCheckKey( $key );
556 }
557 }
558
559 /**
560 * @since 1.28
561 * @return string
562 */
563 public function getType() {
564 // Check both because subclasses don't always pass pages via the constructor,
565 // they may also override getPages() instead, in which case we should keep
566 // defaulting to LOAD_GENERAL and allow them to override getType() separately.
567 return ( $this->styles && !$this->scripts ) ? self::LOAD_STYLES : self::LOAD_GENERAL;
568 }
569 }