Made the page_count update defered, as for the site_stats update. I just removed...
[lhc/web/wiklou.git] / includes / SeleniumWebSettings.php
1 <?php
2 /*
3 * Dynamically change configuration variables based on the test suite name and a cookie value.
4 * For details on how to configure a wiki for a Selenium test, see:
5 * http://www.mediawiki.org/wiki/SeleniumFramework#Test_Wiki_configuration
6 */
7 if ( !defined( 'MEDIAWIKI' ) ) {
8 die( 1 );
9 }
10
11 require_once( "$IP/includes/GlobalFunctions.php" );
12
13 $fname = 'SeleniumWebSettings.php';
14 wfProfileIn( $fname );
15
16 $cookiePrefix = $wgSitename . "-";
17 $cookieName = $cookiePrefix . "Selenium";
18
19 // this is a fallback sql file
20 $testSqlFile = false;
21 $testImageZip = false;
22
23 //if we find a request parameter containing the test name, set a cookie with the test name
24 if ( isset( $_GET['setupTestSuite'] ) ) {
25 $setupTestSuiteName = $_GET['setupTestSuite'];
26
27 if ( preg_match( '/[^a-zA-Z0-9_-]/', $setupTestSuiteName ) || !isset( $wgSeleniumTestConfigs[$setupTestSuiteName] ) ) {
28 return;
29 }
30 if ( strlen( $setupTestSuiteName) > 0 ) {
31 $expire = time() + 600;
32 setcookie( $cookieName,
33 $setupTestSuiteName,
34 $expire,
35 $wgCookiePath,
36 $wgCookieDomain,
37 $wgCookieSecure,
38 true );
39 }
40
41 $testIncludes = array(); //array containing all the includes needed for this test
42 $testGlobalConfigs = array(); //an array containg all the global configs needed for this test
43 $testResourceFiles = array(); // an array containing all the resource files needed for this test
44 $callback = $wgSeleniumTestConfigs[$setupTestSuiteName];
45 call_user_func_array( $callback, array( &$testIncludes, &$testGlobalConfigs, &$testResourceFiles));
46
47 if ( isset( $testResourceFiles['db'] ) ) {
48 $testSqlFile = $testResourceFiles['db'];
49 }
50 if ( isset($testResourceFiles['images']) ) {
51 $testImageZip = $testResourceFiles['images'];
52 }
53
54 $testResourceName = getTestResourceNameFromTestSuiteName( $setupTestSuiteName );
55 switchToTestResources( $testResourceName, false ); // false means do not switch database yet
56 setupTestResources( $testResourceName, $testSqlFile, $testImageZip );
57 }
58
59 //clear the cookie based on a request param
60 if ( isset( $_GET['clearTestSuite'] ) ) {
61 $testSuiteName = getTestSuiteNameFromCookie( $cookieName );
62
63 $expire = time() - 600;
64 setcookie( $cookieName,
65 '',
66 $expire,
67 $wgCookiePath,
68 $wgCookieDomain,
69 $wgCookieSecure,
70 true );
71
72 $testResourceName = getTestResourceNameFromTestSuiteName( $testSuiteName );
73 teardownTestResources( $testResourceName );
74 }
75
76 //if a cookie is found, run the appropriate callback to get the config params.
77 if ( isset( $_COOKIE[$cookieName] ) ) {
78 $testSuiteName = getTestSuiteNameFromCookie( $cookieName );
79 if ( !isset( $wgSeleniumTestConfigs[$testSuiteName] ) ) {
80 return;
81 }
82
83 $testResourceName = getTestResourceNameFromTestSuiteName( $testSuiteName );
84 switchToTestResources( $testResourceName );
85
86 $testIncludes = array(); //array containing all the includes needed for this test
87 $testGlobalConfigs = array(); //an array containg all the global configs needed for this test
88 $testResourceFiles = array(); // an array containing all the resource files needed for this test
89 $callback = $wgSeleniumTestConfigs[$testSuiteName];
90 call_user_func_array( $callback, array( &$testIncludes, &$testGlobalConfigs, &$testResourceFiles));
91
92 foreach ( $testIncludes as $includeFile ) {
93 $file = $IP . '/' . $includeFile;
94 require_once( $file );
95 }
96 foreach ( $testGlobalConfigs as $key => $value ) {
97 if ( is_array( $value ) ) {
98 $GLOBALS[$key] = array_merge( $GLOBALS[$key], $value );
99
100 } else {
101 $GLOBALS[$key] = $value;
102 }
103 }
104 }
105
106 wfProfileOut( $fname );
107
108 function getTestSuiteNameFromCookie( $cookieName ) {
109 $testSuiteName = null;
110 if ( isset( $_COOKIE[$cookieName] ) ) {
111 $testSuiteName = $_COOKIE[$cookieName];
112 }
113 return $testSuiteName;
114 }
115
116 function getTestResourceNameFromTestSuiteName( $testSuiteName ) {
117 $testResourceName = null;
118 if ( isset( $testSuiteName ) ) {
119 $testResourceName = $testSuiteName;
120 }
121 return $testResourceName;
122 }
123
124 function getTestUploadPathFromResourceName( $testResourceName ) {
125 global $IP;
126 $testUploadPath = "$IP/images/$testResourceName";
127 return $testUploadPath;
128 }
129
130 function setupTestResources( $testResourceName, $testSqlFile, $testImageZip ) {
131 global $wgDBname, $wgSeleniumUseTestResources;
132
133 if ( !$wgSeleniumUseTestResources ) {
134 return false;
135 }
136 // Basic security. Do not allow to drop productive database.
137 if ( $testResouceName == $wgDBname ) {
138 die( "Cannot override productive database." );
139 }
140 if ( $testResourceName == '' ) {
141 die( "Cannot identify a test the resources should be installed for." );
142 }
143
144 //create tables
145 $dbw =& wfGetDB( DB_MASTER );
146 $dbw->query( "DROP DATABASE IF EXISTS ".$testResourceName );
147 $dbw->query( "CREATE DATABASE ".$testResourceName );
148
149 // do not set the new db name before database is setup
150 $wgDBname = $testResourceName;
151 $dbw->selectDB( $testResourceName );
152 // populate from sql file
153 if ( $testSqlFile ) {
154 $dbw->sourceFile( $testSqlFile );
155 }
156
157 // create test image dir
158 $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
159 if ( !file_exists( $testUploadPath ) ) {
160 mkdir( $testUploadPath );
161 }
162
163 if ( $testImageZip ) {
164 $zip = new ZipArchive();
165 $zip->open( $testImageZip );
166 $zip->extractTo( $testUploadPath );
167 $zip->close();
168 }
169 }
170
171 function teardownTestResources( $testResourceName ) {
172 global $wgSeleniumUseTestResources;
173
174 if ( !$wgSeleniumUseTestResources ) {
175 return false;
176 }
177 // remove test database
178 $dbw =& wfGetDB( DB_MASTER );
179 $dbw->query( "DROP DATABASE IF EXISTS ".$testResourceName );
180
181 $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
182 // remove test image dir
183 if ( file_exists( $testUploadPath ) ) {
184 wfRecursiveRemoveDir( $testUploadPath );
185 }
186 }
187
188 function switchToTestResources( $testResourceName, $switchDB = true ) {
189 global $wgDBuser, $wgDBpassword, $wgDBname, $wgDBprefix;
190 global $wgDBtestuser, $wgDBtestpassword, $wgDBtestprefix;
191 global $wgUploadPath;
192 global $wgSeleniumUseTestResources;
193
194 if ( !$wgSeleniumUseTestResources ) {
195 return false;
196 }
197
198 if ( $switchDB ) {
199 $wgDBname = $testResourceName;
200 }
201 $wgDBuser = $wgDBtestuser;
202 $wgDBpassword = $wgDBtestpassword;
203 //$wgDBprefix = $wgDBtestprefix;
204
205 $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
206 $wgUploadPath = $testUploadPath;
207 }
208
209 function wfRecursiveRemoveDir( $dir ) {
210 // taken from http://de3.php.net/manual/en/function.rmdir.php#98622
211 if ( is_dir( $dir ) ) {
212 $objects = scandir( $dir );
213 foreach ( $objects as $object ) {
214 if ( $object != "." && $object != ".." ) {
215 if ( filetype( $dir."/".$object ) == "dir" ) {
216 wfRecursiveRemoveDir( $dir."/".$object );
217 } else {
218 unlink( $dir."/".$object );
219 }
220 }
221 }
222 reset( $objects );
223 rmdir( $dir );
224 }
225 }