summaryrefslogtreecommitdiff
path: root/contrib/mw-to-git/t/install-wiki/db_install.php
blob: b033849800bc2cbb2052ca57a501bc2d54304530 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
 * This script generates a SQLite database for a MediaWiki version 1.19.0
 * You must specify the login of the admin (argument 1) and its
 * password (argument 2) and the folder where the database file
 * is located (absolute path in argument 3).
 * It is used by the script install-wiki.sh in order to make easy the
 * installation of a MediaWiki.
 *
 * In order to generate a SQLite database file, MediaWiki ask the user
 * to submit some forms in its web browser. This script simulates this
 * behavior though the functions <get> and <submit>
 *
 */
$argc = $_SERVER['argc'];
$argv = $_SERVER['argv'];

$login = $argv[2];
$pass = $argv[3];
$tmp = $argv[4];
$port = $argv[5];

$url = 'http://localhost:'.$port.'/wiki/mw-config/index.php';
$db_dir = urlencode($tmp);
$tmp_cookie = tempnam($tmp, "COOKIE_");
/*
 * Fetches a page with cURL.
 */
function get($page_name = "") {
	$curl = curl_init();
	$page_name_add = "";
	if ($page_name != "") {
		$page_name_add = '?page='.$page_name;
	}
	$url = $GLOBALS['url'].$page_name_add;
	$tmp_cookie = $GLOBALS['tmp_cookie'];
	curl_setopt($curl, CURLOPT_COOKIEJAR, $tmp_cookie);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
	curl_setopt($curl, CURLOPT_COOKIEFILE, $tmp_cookie);
	curl_setopt($curl, CURLOPT_HEADER, true);
	curl_setopt($curl, CURLOPT_URL, $url);

	$page = curl_exec($curl);
	if (!$page) {
		die("Could not get page: $url\n");
	}
	curl_close($curl);
	return $page;
}

/*
 * Submits a form with cURL.
 */
function submit($page_name, $option = "") {
	$curl = curl_init();
	$datapost = 'submit-continue=Continue+%E2%86%92';
	if ($option != "") {
		$datapost = $option.'&'.$datapost;
	}
	$url = $GLOBALS['url'].'?page='.$page_name;
	$tmp_cookie = $GLOBALS['tmp_cookie'];
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl, CURLOPT_POST, true);
	curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
	curl_setopt($curl, CURLOPT_POSTFIELDS, $datapost);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_COOKIEJAR, $tmp_cookie);
	curl_setopt($curl, CURLOPT_COOKIEFILE, $tmp_cookie);

	$page = curl_exec($curl);
	if (!$page) {
		die("Could not get page: $url\n");
	}
	curl_close($curl);
	return "$page";
}

/*
 * Here starts this script: simulates the behavior of the user
 * submitting forms to generates the database file.
 * Note this simulation was made for the MediaWiki version 1.19.0,
 * we can't assume it works with other versions.
 *
 */

$page = get();
if (!preg_match('/input type="hidden" value="([0-9]+)" name="LanguageRequestTime"/',
		$page, $matches)) {
	echo "Unexpected content for page downloaded:\n";
	echo "$page";
	die;
};
$timestamp = $matches[1];
$language = "LanguageRequestTime=$timestamp&uselang=en&ContLang=en";
$page = submit('Language', $language);

submit('Welcome');

$db_config = 'DBType=sqlite';
$db_config = $db_config.'&sqlite_wgSQLiteDataDir='.$db_dir;
$db_config = $db_config.'&sqlite_wgDBname='.$argv[1];
submit('DBConnect', $db_config);

$wiki_config = 'config_wgSitename=TEST';
$wiki_config = $wiki_config.'&config__NamespaceType=site-name';
$wiki_config = $wiki_config.'&config_wgMetaNamespace=MyWiki';
$wiki_config = $wiki_config.'&config__AdminName='.$login;

$wiki_config = $wiki_config.'&config__AdminPassword='.$pass;
$wiki_config = $wiki_config.'&config__AdminPassword2='.$pass;

$wiki_config = $wiki_config.'&wiki__configEmail=email%40email.org';
$wiki_config = $wiki_config.'&config__SkipOptional=skip';
submit('Name', $wiki_config);
submit('Install');
submit('Install');

unlink($tmp_cookie);
?>