Merge "Avoid using $wgUser in TitlePermissionTest"
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderFileModule.php
1 <?php
2 /**
3 * Resource loader module based on local JavaScript/CSS files.
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 * ResourceLoader module based on local JavaScript/CSS files.
27 */
28 class ResourceLoaderFileModule extends ResourceLoaderModule {
29
30 /* Protected Members */
31
32 /** String: Local base path, see __construct() */
33 protected $localBasePath = '';
34 /** String: Remote base path, see __construct() */
35 protected $remoteBasePath = '';
36 /**
37 * Array: List of paths to JavaScript files to always include
38 * @par Usage:
39 * @code
40 * array( [file-path], [file-path], ... )
41 * @endcode
42 */
43 protected $scripts = array();
44 /**
45 * Array: List of JavaScript files to include when using a specific language
46 * @par Usage:
47 * @code
48 * array( [language-code] => array( [file-path], [file-path], ... ), ... )
49 * @endcode
50 */
51 protected $languageScripts = array();
52 /**
53 * Array: List of JavaScript files to include when using a specific skin
54 * @par Usage:
55 * @code
56 * array( [skin-name] => array( [file-path], [file-path], ... ), ... )
57 * @endcode
58 */
59 protected $skinScripts = array();
60 /**
61 * Array: List of paths to JavaScript files to include in debug mode
62 * @par Usage:
63 * @code
64 * array( [skin-name] => array( [file-path], [file-path], ... ), ... )
65 * @endcode
66 */
67 protected $debugScripts = array();
68 /**
69 * Array: List of paths to JavaScript files to include in the startup module
70 * @par Usage:
71 * @code
72 * array( [file-path], [file-path], ... )
73 * @endcode
74 */
75 protected $loaderScripts = array();
76 /**
77 * Array: List of paths to CSS files to always include
78 * @par Usage:
79 * @code
80 * array( [file-path], [file-path], ... )
81 * @endcode
82 */
83 protected $styles = array();
84 /**
85 * Array: List of paths to CSS files to include when using specific skins
86 * @par Usage:
87 * @code
88 * array( [file-path], [file-path], ... )
89 * @endcode
90 */
91 protected $skinStyles = array();
92 /**
93 * Array: List of modules this module depends on
94 * @par Usage:
95 * @code
96 * array( [file-path], [file-path], ... )
97 * @endcode
98 */
99 protected $dependencies = array();
100 /**
101 * Array: List of message keys used by this module
102 * @par Usage:
103 * @code
104 * array( [message-key], [message-key], ... )
105 * @endcode
106 */
107 protected $messages = array();
108 /** String: Name of group to load this module in */
109 protected $group;
110 /** String: Position on the page to load this module at */
111 protected $position = 'bottom';
112 /** Boolean: Link to raw files in debug mode */
113 protected $debugRaw = true;
114 /** Boolean: Whether mw.loader.state() call should be omitted */
115 protected $raw = false;
116 protected $targets = array( 'desktop' );
117
118 /**
119 * Array: Cache for mtime
120 * @par Usage:
121 * @code
122 * array( [hash] => [mtime], [hash] => [mtime], ... )
123 * @endcode
124 */
125 protected $modifiedTime = array();
126 /**
127 * Array: Place where readStyleFile() tracks file dependencies
128 * @par Usage:
129 * @code
130 * array( [file-path], [file-path], ... )
131 * @endcode
132 */
133 protected $localFileRefs = array();
134
135 /* Methods */
136
137 /**
138 * Constructs a new module from an options array.
139 *
140 * @param $options Array: List of options; if not given or empty, an empty module will be
141 * constructed
142 * @param $localBasePath String: Base path to prepend to all local paths in $options. Defaults
143 * to $IP
144 * @param $remoteBasePath String: Base path to prepend to all remote paths in $options. Defaults
145 * to $wgScriptPath
146 *
147 * Below is a description for the $options array:
148 * @throws MWException
149 * @par Construction options:
150 * @code
151 * array(
152 * // Base path to prepend to all local paths in $options. Defaults to $IP
153 * 'localBasePath' => [base path],
154 * // Base path to prepend to all remote paths in $options. Defaults to $wgScriptPath
155 * 'remoteBasePath' => [base path],
156 * // Equivalent of remoteBasePath, but relative to $wgExtensionAssetsPath
157 * 'remoteExtPath' => [base path],
158 * // Scripts to always include
159 * 'scripts' => [file path string or array of file path strings],
160 * // Scripts to include in specific language contexts
161 * 'languageScripts' => array(
162 * [language code] => [file path string or array of file path strings],
163 * ),
164 * // Scripts to include in specific skin contexts
165 * 'skinScripts' => array(
166 * [skin name] => [file path string or array of file path strings],
167 * ),
168 * // Scripts to include in debug contexts
169 * 'debugScripts' => [file path string or array of file path strings],
170 * // Scripts to include in the startup module
171 * 'loaderScripts' => [file path string or array of file path strings],
172 * // Modules which must be loaded before this module
173 * 'dependencies' => [modile name string or array of module name strings],
174 * // Styles to always load
175 * 'styles' => [file path string or array of file path strings],
176 * // Styles to include in specific skin contexts
177 * 'skinStyles' => array(
178 * [skin name] => [file path string or array of file path strings],
179 * ),
180 * // Messages to always load
181 * 'messages' => [array of message key strings],
182 * // Group which this module should be loaded together with
183 * 'group' => [group name string],
184 * // Position on the page to load this module at
185 * 'position' => ['bottom' (default) or 'top']
186 * )
187 * @endcode
188 */
189 public function __construct( $options = array(), $localBasePath = null,
190 $remoteBasePath = null )
191 {
192 global $IP, $wgScriptPath, $wgResourceBasePath;
193 $this->localBasePath = $localBasePath === null ? $IP : $localBasePath;
194 if ( $remoteBasePath !== null ) {
195 $this->remoteBasePath = $remoteBasePath;
196 } else {
197 $this->remoteBasePath = $wgResourceBasePath === null ? $wgScriptPath : $wgResourceBasePath;
198 }
199
200 if ( isset( $options['remoteExtPath'] ) ) {
201 global $wgExtensionAssetsPath;
202 $this->remoteBasePath = $wgExtensionAssetsPath . '/' . $options['remoteExtPath'];
203 }
204
205 foreach ( $options as $member => $option ) {
206 switch ( $member ) {
207 // Lists of file paths
208 case 'scripts':
209 case 'debugScripts':
210 case 'loaderScripts':
211 case 'styles':
212 $this->{$member} = (array) $option;
213 break;
214 // Collated lists of file paths
215 case 'languageScripts':
216 case 'skinScripts':
217 case 'skinStyles':
218 if ( !is_array( $option ) ) {
219 throw new MWException(
220 "Invalid collated file path list error. " .
221 "'$option' given, array expected."
222 );
223 }
224 foreach ( $option as $key => $value ) {
225 if ( !is_string( $key ) ) {
226 throw new MWException(
227 "Invalid collated file path list key error. " .
228 "'$key' given, string expected."
229 );
230 }
231 $this->{$member}[$key] = (array) $value;
232 }
233 break;
234 // Lists of strings
235 case 'dependencies':
236 case 'messages':
237 case 'targets':
238 $this->{$member} = (array) $option;
239 break;
240 // Single strings
241 case 'group':
242 case 'position':
243 case 'localBasePath':
244 case 'remoteBasePath':
245 $this->{$member} = (string) $option;
246 break;
247 // Single booleans
248 case 'debugRaw':
249 case 'raw':
250 $this->{$member} = (bool) $option;
251 break;
252 }
253 }
254 // Make sure the remote base path is a complete valid URL,
255 // but possibly protocol-relative to avoid cache pollution
256 $this->remoteBasePath = wfExpandUrl( $this->remoteBasePath, PROTO_RELATIVE );
257 }
258
259 /**
260 * Gets all scripts for a given context concatenated together.
261 *
262 * @param $context ResourceLoaderContext: Context in which to generate script
263 * @return String: JavaScript code for $context
264 */
265 public function getScript( ResourceLoaderContext $context ) {
266 $files = $this->getScriptFiles( $context );
267 return $this->readScriptFiles( $files );
268 }
269
270 /**
271 * @param $context ResourceLoaderContext
272 * @return array
273 */
274 public function getScriptURLsForDebug( ResourceLoaderContext $context ) {
275 $urls = array();
276 foreach ( $this->getScriptFiles( $context ) as $file ) {
277 $urls[] = $this->getRemotePath( $file );
278 }
279 return $urls;
280 }
281
282 /**
283 * @return bool
284 */
285 public function supportsURLLoading() {
286 return $this->debugRaw;
287 }
288
289 /**
290 * Gets loader script.
291 *
292 * @return String: JavaScript code to be added to startup module
293 */
294 public function getLoaderScript() {
295 if ( count( $this->loaderScripts ) == 0 ) {
296 return false;
297 }
298 return $this->readScriptFiles( $this->loaderScripts );
299 }
300
301 /**
302 * Gets all styles for a given context concatenated together.
303 *
304 * @param $context ResourceLoaderContext: Context in which to generate styles
305 * @return String: CSS code for $context
306 */
307 public function getStyles( ResourceLoaderContext $context ) {
308 $styles = $this->readStyleFiles(
309 $this->getStyleFiles( $context ),
310 $this->getFlip( $context )
311 );
312 // Collect referenced files
313 $this->localFileRefs = array_unique( $this->localFileRefs );
314 // If the list has been modified since last time we cached it, update the cache
315 if ( $this->localFileRefs !== $this->getFileDependencies( $context->getSkin() ) && !wfReadOnly() ) {
316 $dbw = wfGetDB( DB_MASTER );
317 $dbw->replace( 'module_deps',
318 array( array( 'md_module', 'md_skin' ) ), array(
319 'md_module' => $this->getName(),
320 'md_skin' => $context->getSkin(),
321 'md_deps' => FormatJson::encode( $this->localFileRefs ),
322 )
323 );
324 }
325 return $styles;
326 }
327
328 /**
329 * @param $context ResourceLoaderContext
330 * @return array
331 */
332 public function getStyleURLsForDebug( ResourceLoaderContext $context ) {
333 $urls = array();
334 foreach ( $this->getStyleFiles( $context ) as $mediaType => $list ) {
335 $urls[$mediaType] = array();
336 foreach ( $list as $file ) {
337 $urls[$mediaType][] = $this->getRemotePath( $file );
338 }
339 }
340 return $urls;
341 }
342
343 /**
344 * Gets list of message keys used by this module.
345 *
346 * @return Array: List of message keys
347 */
348 public function getMessages() {
349 return $this->messages;
350 }
351
352 /**
353 * Gets the name of the group this module should be loaded in.
354 *
355 * @return String: Group name
356 */
357 public function getGroup() {
358 return $this->group;
359 }
360
361 /**
362 * @return string
363 */
364 public function getPosition() {
365 return $this->position;
366 }
367
368 /**
369 * Gets list of names of modules this module depends on.
370 *
371 * @return Array: List of module names
372 */
373 public function getDependencies() {
374 return $this->dependencies;
375 }
376
377 /**
378 * @return bool
379 */
380 public function isRaw() {
381 return $this->raw;
382 }
383
384 /**
385 * Get the last modified timestamp of this module.
386 *
387 * Last modified timestamps are calculated from the highest last modified
388 * timestamp of this module's constituent files as well as the files it
389 * depends on. This function is context-sensitive, only performing
390 * calculations on files relevant to the given language, skin and debug
391 * mode.
392 *
393 * @param $context ResourceLoaderContext: Context in which to calculate
394 * the modified time
395 * @return Integer: UNIX timestamp
396 * @see ResourceLoaderModule::getFileDependencies
397 */
398 public function getModifiedTime( ResourceLoaderContext $context ) {
399 if ( isset( $this->modifiedTime[$context->getHash()] ) ) {
400 return $this->modifiedTime[$context->getHash()];
401 }
402 wfProfileIn( __METHOD__ );
403
404 $files = array();
405
406 // Flatten style files into $files
407 $styles = self::collateFilePathListByOption( $this->styles, 'media', 'all' );
408 foreach ( $styles as $styleFiles ) {
409 $files = array_merge( $files, $styleFiles );
410 }
411 $skinFiles = self::tryForKey(
412 self::collateFilePathListByOption( $this->skinStyles, 'media', 'all' ),
413 $context->getSkin(),
414 'default'
415 );
416 foreach ( $skinFiles as $styleFiles ) {
417 $files = array_merge( $files, $styleFiles );
418 }
419
420 // Final merge, this should result in a master list of dependent files
421 $files = array_merge(
422 $files,
423 $this->scripts,
424 $context->getDebug() ? $this->debugScripts : array(),
425 self::tryForKey( $this->languageScripts, $context->getLanguage() ),
426 self::tryForKey( $this->skinScripts, $context->getSkin(), 'default' ),
427 $this->loaderScripts
428 );
429 $files = array_map( array( $this, 'getLocalPath' ), $files );
430 // File deps need to be treated separately because they're already prefixed
431 $files = array_merge( $files, $this->getFileDependencies( $context->getSkin() ) );
432
433 // If a module is nothing but a list of dependencies, we need to avoid
434 // giving max() an empty array
435 if ( count( $files ) === 0 ) {
436 wfProfileOut( __METHOD__ );
437 return $this->modifiedTime[$context->getHash()] = 1;
438 }
439
440 wfProfileIn( __METHOD__.'-filemtime' );
441 $filesMtime = max( array_map( array( __CLASS__, 'safeFilemtime' ), $files ) );
442 wfProfileOut( __METHOD__.'-filemtime' );
443 $this->modifiedTime[$context->getHash()] = max(
444 $filesMtime,
445 $this->getMsgBlobMtime( $context->getLanguage() ) );
446
447 wfProfileOut( __METHOD__ );
448 return $this->modifiedTime[$context->getHash()];
449 }
450
451 /* Protected Methods */
452
453 /**
454 * @param $path string
455 * @return string
456 */
457 protected function getLocalPath( $path ) {
458 return "{$this->localBasePath}/$path";
459 }
460
461 /**
462 * @param $path string
463 * @return string
464 */
465 protected function getRemotePath( $path ) {
466 return "{$this->remoteBasePath}/$path";
467 }
468
469 /**
470 * Collates file paths by option (where provided).
471 *
472 * @param $list Array: List of file paths in any combination of index/path
473 * or path/options pairs
474 * @param $option String: option name
475 * @param $default Mixed: default value if the option isn't set
476 * @return Array: List of file paths, collated by $option
477 */
478 protected static function collateFilePathListByOption( array $list, $option, $default ) {
479 $collatedFiles = array();
480 foreach ( (array) $list as $key => $value ) {
481 if ( is_int( $key ) ) {
482 // File name as the value
483 if ( !isset( $collatedFiles[$default] ) ) {
484 $collatedFiles[$default] = array();
485 }
486 $collatedFiles[$default][] = $value;
487 } elseif ( is_array( $value ) ) {
488 // File name as the key, options array as the value
489 $optionValue = isset( $value[$option] ) ? $value[$option] : $default;
490 if ( !isset( $collatedFiles[$optionValue] ) ) {
491 $collatedFiles[$optionValue] = array();
492 }
493 $collatedFiles[$optionValue][] = $key;
494 }
495 }
496 return $collatedFiles;
497 }
498
499 /**
500 * Gets a list of element that match a key, optionally using a fallback key.
501 *
502 * @param $list Array: List of lists to select from
503 * @param $key String: Key to look for in $map
504 * @param $fallback String: Key to look for in $list if $key doesn't exist
505 * @return Array: List of elements from $map which matched $key or $fallback,
506 * or an empty list in case of no match
507 */
508 protected static function tryForKey( array $list, $key, $fallback = null ) {
509 if ( isset( $list[$key] ) && is_array( $list[$key] ) ) {
510 return $list[$key];
511 } elseif ( is_string( $fallback )
512 && isset( $list[$fallback] )
513 && is_array( $list[$fallback] ) )
514 {
515 return $list[$fallback];
516 }
517 return array();
518 }
519
520 /**
521 * Gets a list of file paths for all scripts in this module, in order of propper execution.
522 *
523 * @param $context ResourceLoaderContext: Context
524 * @return Array: List of file paths
525 */
526 protected function getScriptFiles( ResourceLoaderContext $context ) {
527 $files = array_merge(
528 $this->scripts,
529 self::tryForKey( $this->languageScripts, $context->getLanguage() ),
530 self::tryForKey( $this->skinScripts, $context->getSkin(), 'default' )
531 );
532 if ( $context->getDebug() ) {
533 $files = array_merge( $files, $this->debugScripts );
534 }
535 return $files;
536 }
537
538 /**
539 * Gets a list of file paths for all styles in this module, in order of propper inclusion.
540 *
541 * @param $context ResourceLoaderContext: Context
542 * @return Array: List of file paths
543 */
544 protected function getStyleFiles( ResourceLoaderContext $context ) {
545 return array_merge_recursive(
546 self::collateFilePathListByOption( $this->styles, 'media', 'all' ),
547 self::collateFilePathListByOption(
548 self::tryForKey( $this->skinStyles, $context->getSkin(), 'default' ), 'media', 'all'
549 )
550 );
551 }
552
553 /**
554 * Gets the contents of a list of JavaScript files.
555 *
556 * @param $scripts Array: List of file paths to scripts to read, remap and concetenate
557 * @throws MWException
558 * @return String: Concatenated and remapped JavaScript data from $scripts
559 */
560 protected function readScriptFiles( array $scripts ) {
561 global $wgResourceLoaderValidateStaticJS;
562 if ( empty( $scripts ) ) {
563 return '';
564 }
565 $js = '';
566 foreach ( array_unique( $scripts ) as $fileName ) {
567 $localPath = $this->getLocalPath( $fileName );
568 if ( !file_exists( $localPath ) ) {
569 throw new MWException( __METHOD__.": script file not found: \"$localPath\"" );
570 }
571 $contents = file_get_contents( $localPath );
572 if ( $wgResourceLoaderValidateStaticJS ) {
573 // Static files don't really need to be checked as often; unlike
574 // on-wiki module they shouldn't change unexpectedly without
575 // admin interference.
576 $contents = $this->validateScriptFile( $fileName, $contents );
577 }
578 $js .= $contents . "\n";
579 }
580 return $js;
581 }
582
583 /**
584 * Gets the contents of a list of CSS files.
585 *
586 * @param $styles Array: List of media type/list of file paths pairs, to read, remap and
587 * concetenate
588 *
589 * @param $flip bool
590 *
591 * @return Array: List of concatenated and remapped CSS data from $styles,
592 * keyed by media type
593 */
594 protected function readStyleFiles( array $styles, $flip ) {
595 if ( empty( $styles ) ) {
596 return array();
597 }
598 foreach ( $styles as $media => $files ) {
599 $uniqueFiles = array_unique( $files );
600 $styles[$media] = implode(
601 "\n",
602 array_map(
603 array( $this, 'readStyleFile' ),
604 $uniqueFiles,
605 array_fill( 0, count( $uniqueFiles ), $flip )
606 )
607 );
608 }
609 return $styles;
610 }
611
612 /**
613 * Reads a style file.
614 *
615 * This method can be used as a callback for array_map()
616 *
617 * @param $path String: File path of style file to read
618 * @param $flip bool
619 *
620 * @return String: CSS data in script file
621 * @throws MWException if the file doesn't exist
622 */
623 protected function readStyleFile( $path, $flip ) {
624 $localPath = $this->getLocalPath( $path );
625 if ( !file_exists( $localPath ) ) {
626 $msg = __METHOD__.": style file not found: \"$localPath\"";
627 wfDebugLog( 'resourceloader', $msg );
628 throw new MWException( $msg );
629 }
630 $style = file_get_contents( $localPath );
631 if ( $flip ) {
632 $style = CSSJanus::transform( $style, true, false );
633 }
634 $dirname = dirname( $path );
635 if ( $dirname == '.' ) {
636 // If $path doesn't have a directory component, don't prepend a dot
637 $dirname = '';
638 }
639 $dir = $this->getLocalPath( $dirname );
640 $remoteDir = $this->getRemotePath( $dirname );
641 // Get and register local file references
642 $this->localFileRefs = array_merge(
643 $this->localFileRefs,
644 CSSMin::getLocalFileReferences( $style, $dir )
645 );
646 return CSSMin::remap(
647 $style, $dir, $remoteDir, true
648 );
649 }
650
651 /**
652 * Safe version of filemtime(), which doesn't throw a PHP warning if the file doesn't exist
653 * but returns 1 instead.
654 * @param $filename string File name
655 * @return int UNIX timestamp, or 1 if the file doesn't exist
656 */
657 protected static function safeFilemtime( $filename ) {
658 if ( file_exists( $filename ) ) {
659 return filemtime( $filename );
660 } else {
661 // We only ever map this function on an array if we're gonna call max() after,
662 // so return our standard minimum timestamps here. This is 1, not 0, because
663 // wfTimestamp(0) == NOW
664 return 1;
665 }
666 }
667
668 /**
669 * Get whether CSS for this module should be flipped
670 * @param $context ResourceLoaderContext
671 * @return bool
672 */
673 public function getFlip( $context ) {
674 return $context->getDirection() === 'rtl';
675 }
676
677 /**
678 * Get target(s) for the module, eg ['desktop'] or ['desktop', 'mobile']
679 *
680 * @return array of strings
681 */
682 public function getTargets() {
683 return $this->targets;
684 }
685
686 }