Merge "Move up devunt's name to Developers"
[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 /**
26 * Abstraction for ResourceLoader modules which pull from wiki pages
27 *
28 * This can only be used for wiki pages in the MediaWiki and User namespaces,
29 * because of its dependence on the functionality of Title::isCssJsSubpage.
30 *
31 * This module supports being used as a placeholder for a module on a remote wiki.
32 * To do so, getDB() must be overloaded to return a foreign database object that
33 * allows local wikis to query page metadata.
34 *
35 * Safe for calls on local wikis are:
36 * - Option getters:
37 * - getGroup()
38 * - getPosition()
39 * - getPages()
40 * - Basic methods that strictly involve the foreign database
41 * - getDB()
42 * - isKnownEmpty()
43 * - getTitleInfo()
44 */
45 class ResourceLoaderWikiModule extends ResourceLoaderModule {
46 /** @var string Position on the page to load this module at */
47 protected $position = 'bottom';
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 'position':
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 $title
147 * @return null|string
148 */
149 protected function getContent( $titleText ) {
150 $title = Title::newFromText( $titleText );
151 if ( !$title ) {
152 return null;
153 }
154
155 $handler = ContentHandler::getForTitle( $title );
156 if ( $handler->isSupportedFormat( CONTENT_FORMAT_CSS ) ) {
157 $format = CONTENT_FORMAT_CSS;
158 } elseif ( $handler->isSupportedFormat( CONTENT_FORMAT_JAVASCRIPT ) ) {
159 $format = CONTENT_FORMAT_JAVASCRIPT;
160 } else {
161 return null;
162 }
163
164 $revision = Revision::newFromTitle( $title, false, Revision::READ_NORMAL );
165 if ( !$revision ) {
166 return null;
167 }
168
169 $content = $revision->getContent( Revision::RAW );
170
171 if ( !$content ) {
172 wfDebugLog( 'resourceloader', __METHOD__ . ': failed to load content of JS/CSS page!' );
173 return null;
174 }
175
176 return $content->serialize( $format );
177 }
178
179 /**
180 * @param ResourceLoaderContext $context
181 * @return string
182 */
183 public function getScript( ResourceLoaderContext $context ) {
184 $scripts = '';
185 foreach ( $this->getPages( $context ) as $titleText => $options ) {
186 if ( $options['type'] !== 'script' ) {
187 continue;
188 }
189 $script = $this->getContent( $titleText );
190 if ( strval( $script ) !== '' ) {
191 $script = $this->validateScriptFile( $titleText, $script );
192 $scripts .= ResourceLoader::makeComment( $titleText ) . $script . "\n";
193 }
194 }
195 return $scripts;
196 }
197
198 /**
199 * @param ResourceLoaderContext $context
200 * @return array
201 */
202 public function getStyles( ResourceLoaderContext $context ) {
203 $styles = [];
204 foreach ( $this->getPages( $context ) as $titleText => $options ) {
205 if ( $options['type'] !== 'style' ) {
206 continue;
207 }
208 $media = isset( $options['media'] ) ? $options['media'] : 'all';
209 $style = $this->getContent( $titleText );
210 if ( strval( $style ) === '' ) {
211 continue;
212 }
213 if ( $this->getFlip( $context ) ) {
214 $style = CSSJanus::transform( $style, true, false );
215 }
216 $style = MemoizedCallable::call( 'CSSMin::remap',
217 [ $style, false, $this->getConfig()->get( 'ScriptPath' ), true ] );
218 if ( !isset( $styles[$media] ) ) {
219 $styles[$media] = [];
220 }
221 $style = ResourceLoader::makeComment( $titleText ) . $style;
222 $styles[$media][] = $style;
223 }
224 return $styles;
225 }
226
227 /**
228 * Disable module content versioning.
229 *
230 * This class does not support generating content outside of a module
231 * request due to foreign database support.
232 *
233 * See getDefinitionSummary() for meta-data versioning.
234 *
235 * @return bool
236 */
237 public function enableModuleContentVersion() {
238 return false;
239 }
240
241 /**
242 * @param ResourceLoaderContext $context
243 * @return array
244 */
245 public function getDefinitionSummary( ResourceLoaderContext $context ) {
246 $summary = parent::getDefinitionSummary( $context );
247 $summary[] = [
248 'pages' => $this->getPages( $context ),
249 // Includes meta data of current revisions
250 'titleInfo' => $this->getTitleInfo( $context ),
251 ];
252 return $summary;
253 }
254
255 /**
256 * @param ResourceLoaderContext $context
257 * @return bool
258 */
259 public function isKnownEmpty( ResourceLoaderContext $context ) {
260 $revisions = $this->getTitleInfo( $context );
261
262 // For user modules, don't needlessly load if there are no non-empty pages
263 if ( $this->getGroup() === 'user' ) {
264 foreach ( $revisions as $revision ) {
265 if ( $revision['page_len'] > 0 ) {
266 // At least one non-empty page, module should be loaded
267 return false;
268 }
269 }
270 return true;
271 }
272
273 // Bug 68488: For other modules (i.e. ones that are called in cached html output) only check
274 // page existance. This ensures that, if some pages in a module are temporarily blanked,
275 // we don't end omit the module's script or link tag on some pages.
276 return count( $revisions ) === 0;
277 }
278
279 private function setTitleInfo( $key, array $titleInfo ) {
280 $this->titleInfo[$key] = $titleInfo;
281 }
282
283 /**
284 * Get the information about the wiki pages for a given context.
285 * @param ResourceLoaderContext $context
286 * @return array Keyed by page name
287 */
288 protected function getTitleInfo( ResourceLoaderContext $context ) {
289 $dbr = $this->getDB();
290 if ( !$dbr ) {
291 // We're dealing with a subclass that doesn't have a DB
292 return [];
293 }
294
295 $pageNames = array_keys( $this->getPages( $context ) );
296 sort( $pageNames );
297 $key = implode( '|', $pageNames );
298 if ( !isset( $this->titleInfo[$key] ) ) {
299 $this->titleInfo[$key] = static::fetchTitleInfo( $dbr, $pageNames, __METHOD__ );
300 }
301 return $this->titleInfo[$key];
302 }
303
304 protected static function fetchTitleInfo( IDatabase $db, array $pages, $fname = __METHOD__ ) {
305 $titleInfo = [];
306 $batch = new LinkBatch;
307 foreach ( $pages as $titleText ) {
308 $title = Title::newFromText( $titleText );
309 if ( $title ) {
310 // Page name may be invalid if user-provided (e.g. gadgets)
311 $batch->addObj( $title );
312 }
313 }
314 if ( !$batch->isEmpty() ) {
315 $res = $db->select( 'page',
316 // Include page_touched to allow purging if cache is poisoned (T117587, T113916)
317 [ 'page_namespace', 'page_title', 'page_touched', 'page_len', 'page_latest' ],
318 $batch->constructSet( 'page', $db ),
319 $fname
320 );
321 foreach ( $res as $row ) {
322 // Avoid including ids or timestamps of revision/page tables so
323 // that versions are not wasted
324 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
325 $titleInfo[$title->getPrefixedText()] = [
326 'page_len' => $row->page_len,
327 'page_latest' => $row->page_latest,
328 'page_touched' => $row->page_touched,
329 ];
330 }
331 }
332 return $titleInfo;
333 }
334
335 /**
336 * @since 1.28
337 * @param ResourceLoaderContext $context
338 * @param IDatabase $db
339 * @param string[] $modules
340 */
341 public static function preloadTitleInfo(
342 ResourceLoaderContext $context, IDatabase $db, array $moduleNames
343 ) {
344 $rl = $context->getResourceLoader();
345 // getDB() can be overridden to point to a foreign database.
346 // For now, only preload local. In the future, we could preload by wikiID.
347 $allPages = [];
348 $wikiModules = [];
349 foreach ( $moduleNames as $name ) {
350 $module = $rl->getModule( $name );
351 if ( $module instanceof self ) {
352 $mDB = $module->getDB();
353 // Subclasses may disable getDB and implement getTitleInfo differently
354 if ( $mDB && $mDB->getWikiID() === $db->getWikiID() ) {
355 $wikiModules[] = $module;
356 $allPages += $module->getPages( $context );
357 }
358 }
359 }
360 $allInfo = static::fetchTitleInfo( $db, array_keys( $allPages ), __METHOD__ );
361 foreach ( $wikiModules as $module ) {
362 $pages = $module->getPages( $context );
363 // Before we intersect, map the names to canonical form (T145673).
364 $intersect = [];
365 foreach ( $pages as $page => $unused ) {
366 $title = Title::newFromText( $page );
367 if ( $title ) {
368 $intersect[ $title->getPrefixedText() ] = 1;
369 } else {
370 // Page name may be invalid if user-provided (e.g. gadgets)
371 $rl->getLogger()->info(
372 'Invalid wiki page title "{title}" in ' . __METHOD__,
373 [ 'title' => $page ]
374 );
375 }
376 }
377 $info = array_intersect_key( $allInfo, $intersect );
378
379 $pageNames = array_keys( $pages );
380 sort( $pageNames );
381 $key = implode( '|', $pageNames );
382 $module->setTitleInfo( $key, $info );
383 }
384 return $allInfo;
385 }
386
387 /**
388 * @return string
389 */
390 public function getPosition() {
391 return $this->position;
392 }
393
394 /**
395 * @since 1.28
396 * @return string
397 */
398 public function getType() {
399 // Check both because subclasses don't always pass pages via the constructor,
400 // they may also override getPages() instead, in which case we should keep
401 // defaulting to LOAD_GENERAL and allow them to override getType() separately.
402 return ( $this->styles && !$this->scripts ) ? self::LOAD_STYLES : self::LOAD_GENERAL;
403 }
404 }