]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/resourceloader/ResourceLoaderWikiModule.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.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 Wikimedia\Rdbms\Database;
26 use Wikimedia\Rdbms\IDatabase;
27
28 /**
29  * Abstraction for ResourceLoader modules which pull from wiki pages
30  *
31  * This can only be used for wiki pages in the MediaWiki and User namespaces,
32  * because of its dependence on the functionality of Title::isCssJsSubpage
33  * and Title::isCssOrJsPage().
34  *
35  * This module supports being used as a placeholder for a module on a remote wiki.
36  * To do so, getDB() must be overloaded to return a foreign database object that
37  * allows local wikis to query page metadata.
38  *
39  * Safe for calls on local wikis are:
40  * - Option getters:
41  *   - getGroup()
42  *   - getPages()
43  * - Basic methods that strictly involve the foreign database
44  *   - getDB()
45  *   - isKnownEmpty()
46  *   - getTitleInfo()
47  */
48 class ResourceLoaderWikiModule extends ResourceLoaderModule {
49
50         // Origin defaults to users with sitewide authority
51         protected $origin = self::ORIGIN_USER_SITEWIDE;
52
53         // In-process cache for title info
54         protected $titleInfo = [];
55
56         // List of page names that contain CSS
57         protected $styles = [];
58
59         // List of page names that contain JavaScript
60         protected $scripts = [];
61
62         // Group of module
63         protected $group;
64
65         /**
66          * @param array $options For back-compat, this can be omitted in favour of overwriting getPages.
67          */
68         public function __construct( array $options = null ) {
69                 if ( is_null( $options ) ) {
70                         return;
71                 }
72
73                 foreach ( $options as $member => $option ) {
74                         switch ( $member ) {
75                                 case 'styles':
76                                 case 'scripts':
77                                 case 'group':
78                                 case 'targets':
79                                         $this->{$member} = $option;
80                                         break;
81                         }
82                 }
83         }
84
85         /**
86          * Subclasses should return an associative array of resources in the module.
87          * Keys should be the title of a page in the MediaWiki or User namespace.
88          *
89          * Values should be a nested array of options.  The supported keys are 'type' and
90          * (CSS only) 'media'.
91          *
92          * For scripts, 'type' should be 'script'.
93          *
94          * For stylesheets, 'type' should be 'style'.
95          * There is an optional media key, the value of which can be the
96          * medium ('screen', 'print', etc.) of the stylesheet.
97          *
98          * @param ResourceLoaderContext $context
99          * @return array
100          */
101         protected function getPages( ResourceLoaderContext $context ) {
102                 $config = $this->getConfig();
103                 $pages = [];
104
105                 // Filter out pages from origins not allowed by the current wiki configuration.
106                 if ( $config->get( 'UseSiteJs' ) ) {
107                         foreach ( $this->scripts as $script ) {
108                                 $pages[$script] = [ 'type' => 'script' ];
109                         }
110                 }
111
112                 if ( $config->get( 'UseSiteCss' ) ) {
113                         foreach ( $this->styles as $style ) {
114                                 $pages[$style] = [ 'type' => 'style' ];
115                         }
116                 }
117
118                 return $pages;
119         }
120
121         /**
122          * Get group name
123          *
124          * @return string
125          */
126         public function getGroup() {
127                 return $this->group;
128         }
129
130         /**
131          * Get the Database object used in getTitleInfo().
132          *
133          * Defaults to the local replica DB. Subclasses may want to override this to return a foreign
134          * database object, or null if getTitleInfo() shouldn't access the database.
135          *
136          * NOTE: This ONLY works for getTitleInfo() and isKnownEmpty(), NOT FOR ANYTHING ELSE.
137          * In particular, it doesn't work for getContent() or getScript() etc.
138          *
139          * @return IDatabase|null
140          */
141         protected function getDB() {
142                 return wfGetDB( DB_REPLICA );
143         }
144
145         /**
146          * @param string $titleText
147          * @return null|string
148          */
149         protected function getContent( $titleText ) {
150                 $title = Title::newFromText( $titleText );
151                 if ( !$title ) {
152                         return null; // Bad title
153                 }
154
155                 // If the page is a redirect, follow the redirect.
156                 if ( $title->isRedirect() ) {
157                         $content = $this->getContentObj( $title );
158                         $title = $content ? $content->getUltimateRedirectTarget() : null;
159                         if ( !$title ) {
160                                 return null; // Dead redirect
161                         }
162                 }
163
164                 $handler = ContentHandler::getForTitle( $title );
165                 if ( $handler->isSupportedFormat( CONTENT_FORMAT_CSS ) ) {
166                         $format = CONTENT_FORMAT_CSS;
167                 } elseif ( $handler->isSupportedFormat( CONTENT_FORMAT_JAVASCRIPT ) ) {
168                         $format = CONTENT_FORMAT_JAVASCRIPT;
169                 } else {
170                         return null; // Bad content model
171                 }
172
173                 $content = $this->getContentObj( $title );
174                 if ( !$content ) {
175                         return null; // No content found
176                 }
177
178                 return $content->serialize( $format );
179         }
180
181         /**
182          * @param Title $title
183          * @return Content|null
184          */
185         protected function getContentObj( Title $title ) {
186                 $revision = Revision::newKnownCurrent( wfGetDB( DB_REPLICA ), $title->getArticleID(),
187                         $title->getLatestRevID() );
188                 if ( !$revision ) {
189                         return null;
190                 }
191                 $revision->setTitle( $title );
192                 $content = $revision->getContent( Revision::RAW );
193                 if ( !$content ) {
194                         wfDebugLog( 'resourceloader', __METHOD__ . ': failed to load content of JS/CSS page!' );
195                         return null;
196                 }
197                 return $content;
198         }
199
200         /**
201          * @param ResourceLoaderContext $context
202          * @return string JavaScript code
203          */
204         public function getScript( ResourceLoaderContext $context ) {
205                 $scripts = '';
206                 foreach ( $this->getPages( $context ) as $titleText => $options ) {
207                         if ( $options['type'] !== 'script' ) {
208                                 continue;
209                         }
210                         $script = $this->getContent( $titleText );
211                         if ( strval( $script ) !== '' ) {
212                                 $script = $this->validateScriptFile( $titleText, $script );
213                                 $scripts .= ResourceLoader::makeComment( $titleText ) . $script . "\n";
214                         }
215                 }
216                 return $scripts;
217         }
218
219         /**
220          * @param ResourceLoaderContext $context
221          * @return array
222          */
223         public function getStyles( ResourceLoaderContext $context ) {
224                 $styles = [];
225                 foreach ( $this->getPages( $context ) as $titleText => $options ) {
226                         if ( $options['type'] !== 'style' ) {
227                                 continue;
228                         }
229                         $media = isset( $options['media'] ) ? $options['media'] : 'all';
230                         $style = $this->getContent( $titleText );
231                         if ( strval( $style ) === '' ) {
232                                 continue;
233                         }
234                         if ( $this->getFlip( $context ) ) {
235                                 $style = CSSJanus::transform( $style, true, false );
236                         }
237                         $style = MemoizedCallable::call( 'CSSMin::remap',
238                                 [ $style, false, $this->getConfig()->get( 'ScriptPath' ), true ] );
239                         if ( !isset( $styles[$media] ) ) {
240                                 $styles[$media] = [];
241                         }
242                         $style = ResourceLoader::makeComment( $titleText ) . $style;
243                         $styles[$media][] = $style;
244                 }
245                 return $styles;
246         }
247
248         /**
249          * Disable module content versioning.
250          *
251          * This class does not support generating content outside of a module
252          * request due to foreign database support.
253          *
254          * See getDefinitionSummary() for meta-data versioning.
255          *
256          * @return bool
257          */
258         public function enableModuleContentVersion() {
259                 return false;
260         }
261
262         /**
263          * @param ResourceLoaderContext $context
264          * @return array
265          */
266         public function getDefinitionSummary( ResourceLoaderContext $context ) {
267                 $summary = parent::getDefinitionSummary( $context );
268                 $summary[] = [
269                         'pages' => $this->getPages( $context ),
270                         // Includes meta data of current revisions
271                         'titleInfo' => $this->getTitleInfo( $context ),
272                 ];
273                 return $summary;
274         }
275
276         /**
277          * @param ResourceLoaderContext $context
278          * @return bool
279          */
280         public function isKnownEmpty( ResourceLoaderContext $context ) {
281                 $revisions = $this->getTitleInfo( $context );
282
283                 // For user modules, don't needlessly load if there are no non-empty pages
284                 if ( $this->getGroup() === 'user' ) {
285                         foreach ( $revisions as $revision ) {
286                                 if ( $revision['page_len'] > 0 ) {
287                                         // At least one non-empty page, module should be loaded
288                                         return false;
289                                 }
290                         }
291                         return true;
292                 }
293
294                 // T70488: For other modules (i.e. ones that are called in cached html output) only check
295                 // page existance. This ensures that, if some pages in a module are temporarily blanked,
296                 // we don't end omit the module's script or link tag on some pages.
297                 return count( $revisions ) === 0;
298         }
299
300         private function setTitleInfo( $key, array $titleInfo ) {
301                 $this->titleInfo[$key] = $titleInfo;
302         }
303
304         /**
305          * Get the information about the wiki pages for a given context.
306          * @param ResourceLoaderContext $context
307          * @return array Keyed by page name
308          */
309         protected function getTitleInfo( ResourceLoaderContext $context ) {
310                 $dbr = $this->getDB();
311                 if ( !$dbr ) {
312                         // We're dealing with a subclass that doesn't have a DB
313                         return [];
314                 }
315
316                 $pageNames = array_keys( $this->getPages( $context ) );
317                 sort( $pageNames );
318                 $key = implode( '|', $pageNames );
319                 if ( !isset( $this->titleInfo[$key] ) ) {
320                         $this->titleInfo[$key] = static::fetchTitleInfo( $dbr, $pageNames, __METHOD__ );
321                 }
322                 return $this->titleInfo[$key];
323         }
324
325         protected static function fetchTitleInfo( IDatabase $db, array $pages, $fname = __METHOD__ ) {
326                 $titleInfo = [];
327                 $batch = new LinkBatch;
328                 foreach ( $pages as $titleText ) {
329                         $title = Title::newFromText( $titleText );
330                         if ( $title ) {
331                                 // Page name may be invalid if user-provided (e.g. gadgets)
332                                 $batch->addObj( $title );
333                         }
334                 }
335                 if ( !$batch->isEmpty() ) {
336                         $res = $db->select( 'page',
337                                 // Include page_touched to allow purging if cache is poisoned (T117587, T113916)
338                                 [ 'page_namespace', 'page_title', 'page_touched', 'page_len', 'page_latest' ],
339                                 $batch->constructSet( 'page', $db ),
340                                 $fname
341                         );
342                         foreach ( $res as $row ) {
343                                 // Avoid including ids or timestamps of revision/page tables so
344                                 // that versions are not wasted
345                                 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
346                                 $titleInfo[$title->getPrefixedText()] = [
347                                         'page_len' => $row->page_len,
348                                         'page_latest' => $row->page_latest,
349                                         'page_touched' => $row->page_touched,
350                                 ];
351                         }
352                 }
353                 return $titleInfo;
354         }
355
356         /**
357          * @since 1.28
358          * @param ResourceLoaderContext $context
359          * @param IDatabase $db
360          * @param string[] $moduleNames
361          */
362         public static function preloadTitleInfo(
363                 ResourceLoaderContext $context, IDatabase $db, array $moduleNames
364         ) {
365                 $rl = $context->getResourceLoader();
366                 // getDB() can be overridden to point to a foreign database.
367                 // For now, only preload local. In the future, we could preload by wikiID.
368                 $allPages = [];
369                 /** @var ResourceLoaderWikiModule[] $wikiModules */
370                 $wikiModules = [];
371                 foreach ( $moduleNames as $name ) {
372                         $module = $rl->getModule( $name );
373                         if ( $module instanceof self ) {
374                                 $mDB = $module->getDB();
375                                 // Subclasses may disable getDB and implement getTitleInfo differently
376                                 if ( $mDB && $mDB->getDomainID() === $db->getDomainID() ) {
377                                         $wikiModules[] = $module;
378                                         $allPages += $module->getPages( $context );
379                                 }
380                         }
381                 }
382
383                 if ( !$wikiModules ) {
384                         // Nothing to preload
385                         return;
386                 }
387
388                 $pageNames = array_keys( $allPages );
389                 sort( $pageNames );
390                 $hash = sha1( implode( '|', $pageNames ) );
391
392                 // Avoid Zend bug where "static::" does not apply LSB in the closure
393                 $func = [ static::class, 'fetchTitleInfo' ];
394                 $fname = __METHOD__;
395
396                 $cache = ObjectCache::getMainWANInstance();
397                 $allInfo = $cache->getWithSetCallback(
398                         $cache->makeGlobalKey( 'resourceloader', 'titleinfo', $db->getDomainID(), $hash ),
399                         $cache::TTL_HOUR,
400                         function ( $curVal, &$ttl, array &$setOpts ) use ( $func, $pageNames, $db, $fname ) {
401                                 $setOpts += Database::getCacheSetOptions( $db );
402
403                                 return call_user_func( $func, $db, $pageNames, $fname );
404                         },
405                         [
406                                 'checkKeys' => [
407                                         $cache->makeGlobalKey( 'resourceloader', 'titleinfo', $db->getDomainID() ) ]
408                         ]
409                 );
410
411                 foreach ( $wikiModules as $wikiModule ) {
412                         $pages = $wikiModule->getPages( $context );
413                         // Before we intersect, map the names to canonical form (T145673).
414                         $intersect = [];
415                         foreach ( $pages as $page => $unused ) {
416                                 $title = Title::newFromText( $page );
417                                 if ( $title ) {
418                                         $intersect[ $title->getPrefixedText() ] = 1;
419                                 } else {
420                                         // Page name may be invalid if user-provided (e.g. gadgets)
421                                         $rl->getLogger()->info(
422                                                 'Invalid wiki page title "{title}" in ' . __METHOD__,
423                                                 [ 'title' => $page ]
424                                         );
425                                 }
426                         }
427                         $info = array_intersect_key( $allInfo, $intersect );
428                         $pageNames = array_keys( $pages );
429                         sort( $pageNames );
430                         $key = implode( '|', $pageNames );
431                         $wikiModule->setTitleInfo( $key, $info );
432                 }
433         }
434
435         /**
436          * Clear the preloadTitleInfo() cache for all wiki modules on this wiki on
437          * page change if it was a JS or CSS page
438          *
439          * @param Title $title
440          * @param Revision|null $old Prior page revision
441          * @param Revision|null $new New page revision
442          * @param string $wikiId
443          * @since 1.28
444          */
445         public static function invalidateModuleCache(
446                 Title $title, Revision $old = null, Revision $new = null, $wikiId
447         ) {
448                 static $formats = [ CONTENT_FORMAT_CSS, CONTENT_FORMAT_JAVASCRIPT ];
449
450                 if ( $old && in_array( $old->getContentFormat(), $formats ) ) {
451                         $purge = true;
452                 } elseif ( $new && in_array( $new->getContentFormat(), $formats ) ) {
453                         $purge = true;
454                 } else {
455                         $purge = ( $title->isCssOrJsPage() || $title->isCssJsSubpage() );
456                 }
457
458                 if ( $purge ) {
459                         $cache = ObjectCache::getMainWANInstance();
460                         $key = $cache->makeGlobalKey( 'resourceloader', 'titleinfo', $wikiId );
461                         $cache->touchCheckKey( $key );
462                 }
463         }
464
465         /**
466          * @since 1.28
467          * @return string
468          */
469         public function getType() {
470                 // Check both because subclasses don't always pass pages via the constructor,
471                 // they may also override getPages() instead, in which case we should keep
472                 // defaulting to LOAD_GENERAL and allow them to override getType() separately.
473                 return ( $this->styles && !$this->scripts ) ? self::LOAD_STYLES : self::LOAD_GENERAL;
474         }
475 }