Merge "(bug 35240) Fix mw.loader state machine."
[lhc/web/wiklou.git] / tests / qunit / data / testloader.php
1 <?php
2 /**
3 * ResourceLoader stub working with pre-defined test modules.
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 * @package MediaWiki
22 * @author Lupo
23 * @since 1.20
24 */
25 header( 'Content-Type: text/javascript; charset=utf-8' );
26
27 require_once "../../../includes/Xml.php";
28
29 $modules = array (
30 'test.use_missing' => array (
31 'src' => 'mw.loader.implement("test.use_missing", function () {start(); ok(false, "Module test.use_missing should not run.");}, {}, {});',
32 'deps' => array ( 'test.missing' )
33 ),
34 'test.use_missing2' => array (
35 'src' => 'mw.loader.implement("test.use_missing2", function () {start(); ok(false, "Module test.use_missing2 should not run.");}, {}, {});',
36 'deps' => array ( 'test.missing2' )
37 )
38 );
39
40 // Copied from ResourceLoaderContext
41 function expandModuleNames( $modules ) {
42 $retval = array();
43 // For backwards compatibility with an earlier hack, replace ! with .
44 $modules = str_replace( '!', '.', $modules );
45 $exploded = explode( '|', $modules );
46 foreach ( $exploded as $group ) {
47 if ( strpos( $group, ',' ) === false ) {
48 // This is not a set of modules in foo.bar,baz notation
49 // but a single module
50 $retval[] = $group;
51 } else {
52 // This is a set of modules in foo.bar,baz notation
53 $pos = strrpos( $group, '.' );
54 if ( $pos === false ) {
55 // Prefixless modules, i.e. without dots
56 $retval = explode( ',', $group );
57 } else {
58 // We have a prefix and a bunch of suffixes
59 $prefix = substr( $group, 0, $pos ); // 'foo'
60 $suffixes = explode( ',', substr( $group, $pos + 1 ) ); // array( 'bar', 'baz' )
61 foreach ( $suffixes as $suffix ) {
62 $retval[] = "$prefix.$suffix";
63 }
64 }
65 }
66 }
67 return $retval;
68 }
69
70 function addModule ( $moduleName, &$gotten ) {
71 global $modules;
72
73 $result = "";
74 if ( isset( $gotten[$moduleName] ) ) {
75 return $result;
76 }
77 $gotten[$moduleName] = true;
78 if ( isset( $modules[$moduleName] ) ) {
79 $deps = $modules[$moduleName]['deps'];
80 foreach ( $deps as $depName ) {
81 $result .= addModule( $depName, $gotten ) . "\n";
82 }
83 $result .= $modules[$moduleName]['src'] . "\n";
84 } else {
85 $result .= 'mw.loader.state( ' . Xml::encodeJsVar( $moduleName ) . ', "missing" );' . "\n";
86 }
87 return $result . "\n";
88 }
89
90 $result = "";
91
92 if ( isset( $_GET['modules'] ) ) {
93 $toGet = expandModuleNames( $_GET['modules'] );
94 $gotten = array();
95 foreach ( $toGet as $moduleName ) {
96 $result .= addModule( $moduleName, $gotten );
97 }
98 }
99
100 echo $result;