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