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