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