summaryrefslogtreecommitdiff
path: root/tests/MediaWiki_TestCase.php
blob: 387fe0111fdf953ea8980bbee09c2a54db2e447f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php

abstract class MediaWiki_TestCase extends PHPUnit_Framework_TestCase {
	/**
	 * @param string $serverType
	 * @param array $tables
	 */
	protected function buildTestDatabase( $tables ) {
		global $testOptions, $wgDBprefix, $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname;
		$wgDBprefix = 'parsertest_';
		$db = new Database(
			$wgDBserver,
			$wgDBadminuser,
			$wgDBadminpassword,
			$wgDBname );
		if( $db->isOpen() ) {
			if (!(strcmp($db->getServerVersion(), '4.1') < 0 and stristr($db->getSoftwareLink(), 'MySQL'))) {
				# Database that supports CREATE TABLE ... LIKE
				foreach ($tables as $tbl) {
					$newTableName = $db->tableName( $tbl );
					#$tableName = $this->oldTableNames[$tbl];
					$tableName = $tbl;
					$db->query("CREATE TEMPORARY TABLE $newTableName (LIKE $tableName)");
				}
			} else {
				# Hack for MySQL versions < 4.1, which don't support
				# "CREATE TABLE ... LIKE". Note that
				# "CREATE TEMPORARY TABLE ... SELECT * FROM ... LIMIT 0"
				# would not create the indexes we need....
				foreach ($tables as $tbl) {
					$res = $db->query("SHOW CREATE TABLE $tbl");
					$row = $db->fetchRow($res);
					$create = $row[1];
					$create_tmp = preg_replace('/CREATE TABLE `(.*?)`/', 'CREATE TEMPORARY TABLE `'
						. $wgDBprefix . '\\1`', $create);
					if ($create === $create_tmp) {
						# Couldn't do replacement
						wfDie( "could not create temporary table $tbl" );
					}
					$db->query($create_tmp);
				}

			}
			return $db;
		} else {
			// Something amiss
			return null;
		}
	}
}