contrib / mw-to-git / t / install-wiki / db_install.phpon commit remote-helpers: cleanup more global variables (641a2b5)
   1<?php
   2/**
   3 * This script generates a SQLite database for a MediaWiki version 1.19.0
   4 * You must specify the login of the admin (argument 1) and its
   5 * password (argument 2) and the folder where the database file
   6 * is located (absolute path in argument 3).
   7 * It is used by the script install-wiki.sh in order to make easy the
   8 * installation of a MediaWiki.
   9 *
  10 * In order to generate a SQLite database file, MediaWiki ask the user
  11 * to submit some forms in its web browser. This script simulates this
  12 * behavior though the functions <get> and <submit>
  13 *
  14 */
  15$argc = $_SERVER['argc'];
  16$argv = $_SERVER['argv'];
  17
  18$login = $argv[2];
  19$pass = $argv[3];
  20$tmp = $argv[4];
  21$port = $argv[5];
  22
  23$url = 'http://localhost:'.$port.'/wiki/mw-config/index.php';
  24$db_dir = urlencode($tmp);
  25$tmp_cookie = tempnam($tmp, "COOKIE_");
  26/*
  27 * Fetchs a page with cURL.
  28 */
  29function get($page_name = "") {
  30        $curl = curl_init();
  31        $page_name_add = "";
  32        if ($page_name != "") {
  33                $page_name_add = '?page='.$page_name;
  34        }
  35        $url = $GLOBALS['url'].$page_name_add;
  36        $tmp_cookie = $GLOBALS['tmp_cookie'];
  37        curl_setopt($curl, CURLOPT_COOKIEJAR, $tmp_cookie);
  38        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  39        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  40        curl_setopt($curl, CURLOPT_COOKIEFILE, $tmp_cookie);
  41        curl_setopt($curl, CURLOPT_HEADER, true);
  42        curl_setopt($curl, CURLOPT_URL, $url);
  43
  44        $page = curl_exec($curl);
  45        if (!$page) {
  46                die("Could not get page: $url\n");
  47        }
  48        curl_close($curl);
  49        return $page;
  50}
  51
  52/*
  53 * Submits a form with cURL.
  54 */
  55function submit($page_name, $option = "") {
  56        $curl = curl_init();
  57        $datapost = 'submit-continue=Continue+%E2%86%92';
  58        if ($option != "") {
  59                $datapost = $option.'&'.$datapost;
  60        }
  61        $url = $GLOBALS['url'].'?page='.$page_name;
  62        $tmp_cookie = $GLOBALS['tmp_cookie'];
  63        curl_setopt($curl, CURLOPT_URL, $url);
  64        curl_setopt($curl, CURLOPT_POST, true);
  65        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  66        curl_setopt($curl, CURLOPT_POSTFIELDS, $datapost);
  67        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  68        curl_setopt($curl, CURLOPT_COOKIEJAR, $tmp_cookie);
  69        curl_setopt($curl, CURLOPT_COOKIEFILE, $tmp_cookie);
  70
  71        $page = curl_exec($curl);
  72        if (!$page) {
  73                die("Could not get page: $url\n");
  74        }
  75        curl_close($curl);
  76        return "$page";
  77}
  78
  79/*
  80 * Here starts this script: simulates the behavior of the user
  81 * submitting forms to generates the database file.
  82 * Note this simulation was made for the MediaWiki version 1.19.0,
  83 * we can't assume it works with other versions.
  84 *
  85 */
  86
  87$page = get();
  88if (!preg_match('/input type="hidden" value="([0-9]+)" name="LanguageRequestTime"/',
  89                $page, $matches)) {
  90        echo "Unexpected content for page downloaded:\n";
  91        echo "$page";
  92        die;
  93};
  94$timestamp = $matches[1];
  95$language = "LanguageRequestTime=$timestamp&uselang=en&ContLang=en";
  96$page = submit('Language', $language);
  97
  98submit('Welcome');
  99
 100$db_config = 'DBType=sqlite';
 101$db_config = $db_config.'&sqlite_wgSQLiteDataDir='.$db_dir;
 102$db_config = $db_config.'&sqlite_wgDBname='.$argv[1];
 103submit('DBConnect', $db_config);
 104
 105$wiki_config = 'config_wgSitename=TEST';
 106$wiki_config = $wiki_config.'&config__NamespaceType=site-name';
 107$wiki_config = $wiki_config.'&config_wgMetaNamespace=MyWiki';
 108$wiki_config = $wiki_config.'&config__AdminName='.$login;
 109
 110$wiki_config = $wiki_config.'&config__AdminPassword='.$pass;
 111$wiki_config = $wiki_config.'&config__AdminPassword2='.$pass;
 112
 113$wiki_config = $wiki_config.'&wiki__configEmail=email%40email.org';
 114$wiki_config = $wiki_config.'&config__SkipOptional=skip';
 115submit('Name', $wiki_config);
 116submit('Install');
 117submit('Install');
 118
 119unlink($tmp_cookie);
 120?>