Merge "LogFormatter: Fail softer when trying to link an invalid titles"
[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
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 class ResourceLoaderWikiModule extends ResourceLoaderModule {
51
52 // Origin defaults to users with sitewide authority
53 protected $origin = self::ORIGIN_USER_SITEWIDE;
54
55 // In-process cache for title info, structured as an array
56 // [
57 // <batchKey> // Pipe-separated list of sorted keys from getPages
58 // => [
59 // <titleKey> => [ // Normalised title key
60 // 'page_len' => ..,
61 // 'page_latest' => ..,
62 // 'page_touched' => ..,
63 // ]
64 // ]
65 // ]
66 // @see self::fetchTitleInfo()
67 // @see self::makeTitleKey()
68 protected $titleInfo = [];
69
70 // List of page names that contain CSS
71 protected $styles = [];
72
73 // List of page names that contain JavaScript
74 protected $scripts = [];
75
76 // Group of module
77 protected $group;
78
79 /**
80 * @param array|null $options For back-compat, this can be omitted in favour of overwriting
81 * getPages.
82 */
83 public function __construct( array $options = null ) {
84 if ( is_null( $options ) ) {
85 return;
86 }
87
88 foreach ( $options as $member => $option ) {
89 switch ( $member ) {
90 case 'styles':
91 case 'scripts':
92 case 'group':
93 case 'targets':
94 $this->{$member} = $option;
95 break;
96 }
97 }
98 }
99
100 /**
101 * Subclasses should return an associative array of resources in the module.
102 * Keys should be the title of a page in the MediaWiki or User namespace.
103 *
104 * Values should be a nested array of options. The supported keys are 'type' and
105 * (CSS only) 'media'.
106 *
107 * For scripts, 'type' should be 'script'.
108 *
109 * For stylesheets, 'type' should be 'style'.
110 * There is an optional media key, the value of which can be the
111 * medium ('screen', 'print', etc.) of the stylesheet.
112 *
113 * @param ResourceLoaderContext $context
114 * @return array
115 */
116 protected function getPages( ResourceLoaderContext $context ) {
117 $config = $this->getConfig();
118 $pages = [];
119
120 // Filter out pages from origins not allowed by the current wiki configuration.
121 if ( $config->get( 'UseSiteJs' ) ) {
122 foreach ( $this->scripts as $script ) {
123 $pages[$script] = [ 'type' => 'script' ];
124 }
125 }
126
127 if ( $config->get( 'UseSiteCss' ) ) {
128 foreach ( $this->styles as $style ) {
129 $pages[$style] = [ 'type' => 'style' ];
130 }
131 }
132
133 return $pages;
134 }
135
136 /**
137 * Get group name
138 *
139 * @return string
140 */
141 public function getGroup() {
142 return $this->group;
143 }
144
145 /**
146 * Get the Database object used in getTitleInfo().
147 *
148 * Defaults to the local replica DB. Subclasses may want to override this to return a foreign
149 * database object, or null if getTitleInfo() shouldn't access the database.
150 *
151 * NOTE: This ONLY works for getTitleInfo() and isKnownEmpty(), NOT FOR ANYTHING ELSE.
152 * In particular, it doesn't work for getContent() or getScript() etc.
153 *
154 * @return IDatabase|null
155 */
156 protected function getDB() {
157 return wfGetDB( DB_REPLICA );
158 }
159
160 /**
161 * @param string $titleText
162 * @param ResourceLoaderContext|null $context (but passing null is deprecated)
163 * @return null|string
164 * @since 1.32 added the $context parameter
165 */
166 protected function getContent( $titleText, ResourceLoaderContext $context = null ) {
167 $title = Title::newFromText( $titleText );
168 if ( !$title ) {
169 return null; // Bad title
170 }
171
172 $content = $this->getContentObj( $title, $context );
173 if ( !$content ) {
174 return null; // No content found
175 }
176
177 $handler = $content->getContentHandler();
178 if ( $handler->isSupportedFormat( CONTENT_FORMAT_CSS ) ) {
179 $format = CONTENT_FORMAT_CSS;
180 } elseif ( $handler->isSupportedFormat( CONTENT_FORMAT_JAVASCRIPT ) ) {
181 $format = CONTENT_FORMAT_JAVASCRIPT;
182 } else {
183 return null; // Bad content model
184 }
185
186 return $content->serialize( $format );
187 }
188
189 /**
190 * @param Title $title
191 * @param ResourceLoaderContext|null $context (but passing null is deprecated)
192 * @param int|null $maxRedirects Maximum number of redirects to follow. If
193 * null, uses $wgMaxRedirects
194 * @return Content|null
195 * @since 1.32 added the $context and $maxRedirects parameters
196 */
197 protected function getContentObj(
198 Title $title, ResourceLoaderContext $context = null, $maxRedirects = null
199 ) {
200 if ( $context === null ) {
201 wfDeprecated( __METHOD__ . ' without a ResourceLoader context', '1.32' );
202 }
203
204 $overrideCallback = $context ? $context->getContentOverrideCallback() : null;
205 $content = $overrideCallback ? call_user_func( $overrideCallback, $title ) : null;
206 if ( $content ) {
207 if ( !$content instanceof Content ) {
208 $this->getLogger()->error(
209 'Bad content override for "{title}" in ' . __METHOD__,
210 [ 'title' => $title->getPrefixedText() ]
211 );
212 return null;
213 }
214 } else {
215 $revision = Revision::newKnownCurrent( wfGetDB( DB_REPLICA ), $title );
216 if ( !$revision ) {
217 return null;
218 }
219 $content = $revision->getContent( Revision::RAW );
220
221 if ( !$content ) {
222 $this->getLogger()->error(
223 'Failed to load content of JS/CSS page "{title}" in ' . __METHOD__,
224 [ 'title' => $title->getPrefixedText() ]
225 );
226 return null;
227 }
228 }
229
230 if ( $content && $content->isRedirect() ) {
231 if ( $maxRedirects === null ) {
232 $maxRedirects = $this->getConfig()->get( 'MaxRedirects' ) ?: 0;
233 }
234 if ( $maxRedirects > 0 ) {
235 $newTitle = $content->getRedirectTarget();
236 return $newTitle ? $this->getContentObj( $newTitle, $context, $maxRedirects - 1 ) : null;
237 }
238 }
239
240 return $content;
241 }
242
243 /**
244 * @param ResourceLoaderContext $context
245 * @return bool
246 */
247 public function shouldEmbedModule( ResourceLoaderContext $context ) {
248 $overrideCallback = $context->getContentOverrideCallback();
249 if ( $overrideCallback && $this->getSource() === 'local' ) {
250 foreach ( $this->getPages( $context ) as $page => $info ) {
251 $title = Title::newFromText( $page );
252 if ( $title && call_user_func( $overrideCallback, $title ) !== null ) {
253 return true;
254 }
255 }
256 }
257
258 return parent::shouldEmbedModule( $context );
259 }
260
261 /**
262 * @param ResourceLoaderContext $context
263 * @return string JavaScript code
264 */
265 public function getScript( ResourceLoaderContext $context ) {
266 $scripts = '';
267 foreach ( $this->getPages( $context ) as $titleText => $options ) {
268 if ( $options['type'] !== 'script' ) {
269 continue;
270 }
271 $script = $this->getContent( $titleText, $context );
272 if ( strval( $script ) !== '' ) {
273 $script = $this->validateScriptFile( $titleText, $script );
274 $scripts .= ResourceLoader::makeComment( $titleText ) . $script . "\n";
275 }
276 }
277 return $scripts;
278 }
279
280 /**
281 * @param ResourceLoaderContext $context
282 * @return array
283 */
284 public function getStyles( ResourceLoaderContext $context ) {
285 $styles = [];
286 foreach ( $this->getPages( $context ) as $titleText => $options ) {
287 if ( $options['type'] !== 'style' ) {
288 continue;
289 }
290 $media = $options['media'] ?? 'all';
291 $style = $this->getContent( $titleText, $context );
292 if ( strval( $style ) === '' ) {
293 continue;
294 }
295 if ( $this->getFlip( $context ) ) {
296 $style = CSSJanus::transform( $style, true, false );
297 }
298 $style = MemoizedCallable::call( 'CSSMin::remap',
299 [ $style, false, $this->getConfig()->get( 'ScriptPath' ), true ] );
300 if ( !isset( $styles[$media] ) ) {
301 $styles[$media] = [];
302 }
303 $style = ResourceLoader::makeComment( $titleText ) . $style;
304 $styles[$media][] = $style;
305 }
306 return $styles;
307 }
308
309 /**
310 * Disable module content versioning.
311 *
312 * This class does not support generating content outside of a module
313 * request due to foreign database support.
314 *
315 * See getDefinitionSummary() for meta-data versioning.
316 *
317 * @return bool
318 */
319 public function enableModuleContentVersion() {
320 return false;
321 }
322
323 /**
324 * @param ResourceLoaderContext $context
325 * @return array
326 */
327 public function getDefinitionSummary( ResourceLoaderContext $context ) {
328 $summary = parent::getDefinitionSummary( $context );
329 $summary[] = [
330 'pages' => $this->getPages( $context ),
331 // Includes meta data of current revisions
332 'titleInfo' => $this->getTitleInfo( $context ),
333 ];
334 return $summary;
335 }
336
337 /**
338 * @param ResourceLoaderContext $context
339 * @return bool
340 */
341 public function isKnownEmpty( ResourceLoaderContext $context ) {
342 $revisions = $this->getTitleInfo( $context );
343
344 // If a module has dependencies it cannot be empty. An empty array will be cast to false
345 if ( $this->getDependencies() ) {
346 return false;
347 }
348 // For user modules, don't needlessly load if there are no non-empty pages
349 if ( $this->getGroup() === 'user' ) {
350 foreach ( $revisions as $revision ) {
351 if ( $revision['page_len'] > 0 ) {
352 // At least one non-empty page, module should be loaded
353 return false;
354 }
355 }
356 return true;
357 }
358
359 // T70488: For other modules (i.e. ones that are called in cached html output) only check
360 // page existance. This ensures that, if some pages in a module are temporarily blanked,
361 // we don't end omit the module's script or link tag on some pages.
362 return count( $revisions ) === 0;
363 }
364
365 private function setTitleInfo( $batchKey, array $titleInfo ) {
366 $this->titleInfo[$batchKey] = $titleInfo;
367 }
368
369 private static function makeTitleKey( LinkTarget $title ) {
370 // Used for keys in titleInfo.
371 return "{$title->getNamespace()}:{$title->getDBkey()}";
372 }
373
374 /**
375 * Get the information about the wiki pages for a given context.
376 * @param ResourceLoaderContext $context
377 * @return array Keyed by page name
378 */
379 protected function getTitleInfo( ResourceLoaderContext $context ) {
380 $dbr = $this->getDB();
381 if ( !$dbr ) {
382 // We're dealing with a subclass that doesn't have a DB
383 return [];
384 }
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 disable getDB and implement getTitleInfo differently
465 if ( $mDB && $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 = ObjectCache::getMainWANInstance();
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 $wikiId
532 * @since 1.28
533 */
534 public static function invalidateModuleCache(
535 Title $title, Revision $old = null, Revision $new = null, $wikiId
536 ) {
537 static $formats = [ CONTENT_FORMAT_CSS, CONTENT_FORMAT_JAVASCRIPT ];
538
539 Assert::parameterType( 'string', $wikiId, '$wikiId' );
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 = ObjectCache::getMainWANInstance();
553 $key = $cache->makeGlobalKey( 'resourceloader', 'titleinfo', $wikiId );
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 }