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