User:Huji/Wikimedia clone

Start with a fresh Ubuntu installation for Virtualbox.

Install all the ncessary software:

sudo su
apt-get install vim
apt-get install git
apt-get install apache2
apt-get install mysql-server
apt-get install php5 libapache2-mod-php5 php5-mysql
service apache2 restart

You will be recommended to set a password for your mysql root account. If you set a password, write it down somewhere!

Transfer your private key to ~/.ssh then make it secure using chmod 600 id_rsa and then add it using ssh-add id_rsa

Then download the MediaWiki core and store it in /var/www/w using git:

git clone ssh://<USERNAME>@gerrit.wikimedia.org:29418/mediawiki/core.git

This will look for some 300,000 objects on the remote side, but eventually you will download some 55,000 files.

Multiple Wikis

edit

Let's say you want to have two sister wikis: English Wiki and Persian Wiki.

First go into mysql (mysql -u root -p then type the password) and run the following:

CREATE DATABASE en_wiki;
CREATE DATABASE fa_wiki;

Then install MediaWiki once into the fa_wiki database. Once completed, store the LocalSettings.php into the w/ directory, but then rename it into LocalSettings_fa.php and then install MediaWiki again, this time into en_wiki database, and save this new LocalSettings.php file into w/ directory.

Short URLs

edit

The next step is to set up short URLs. Instructions are here.

In short, you add Alias /wiki /var/www/w/index.php to the file named default that you find in /etc/apache2/sites-available/ and you also edit your LocalSettings.php file where $wgScriptPath is defined, to look like this:

$wgScriptPath       = "/w";
$wgScript           = "$wgScriptPath/index.php";
$wgArticlePath      = "/wiki/$1";
$usePathInfo        = true;
$wgScriptExtension  = ".php";


Now if you restart apache using sudo apachectl graceful you should be able to browse to http://localhost/wiki/ and get to the home page of your second wiki.

Subdomains

edit

The next step is to add subdomain support, both on Apache's side and on MediaWiki's.

First edit /etc/hosts and add references to en.localhost and fa.localhost (use 127.0.0.1 as the IP). Then add ServerAlias *.localhost to the file named default that you find in /etc/apache2/sites-available/

Now move all these variables to the end of your LocalSettings.php: $wgSiteName, $wgServer, $wgDBname, $wgLanguageCode, $wgSecretKey, and $wgUpgradekey

Find the same varibles in your LocalSettings_fa.php and put them together too. Now we should move all this stuff into our LocalSettings.php file, but help MediaWiki choose which to use based on the subdomain. It is explained here and you should end up with something more like this:

if ( preg_match( '/^(.*)\.localhost$/', $server, $matches ) ) {
     $wikiname = $matches[1];
} else {
     die( "Invalid host name, can't determine wiki name" );
} 

switch ($wikiname) {
    case "fa":
        $wgSitename      = "ویکی";
        $wgServer        = "http://fa.localhost";
        $wgDBname        = "fa_wiki";
        $wgLanguageCode  = "fa";
        $wgSecretKey     = "...";
        $wgUpgradeKey    = "...";
        break;
    case "en":
        $wgSitename      = "Wiki";
        $wgServer        = "http://en.localhost";
        $wgDBname        = "en_wiki";
        $wgLanguageCode  = "en";
        $wgSecretKey     = "...";
        $wgUpgradeKey    = "...";
        break;
}

When you are copy-pasting, note that $wgServer needs to be updated in both cases, to include the subdomain.

While you are on it, also create a file named update_farm.php in the w/maintenance/ directory with the following content:

<?php
# update_farm.php script
$_SERVER['SERVER_NAME'] = $argv[1];
include("maintenance/update.php")

This way, when you update your MediaWiki, updating the DB will be as easy as running php update_farm.php en.localhost and then php update_farm.php fa.localhost

Interwiki

edit

To set up interwikis between these two wikis, go into mysql command line and run the following:

USE en_wiki;
INSERT INTO interwiki (iw_prefix, iw_url, iw_local, iw_trans) VALUES ('fa', 'http://fa.localhost/wiki/$1', 1, 0);
USE fa_wiki;
INSERT INTO interwiki (iw_prefix, iw_url, iw_local, iw_trans) VALUES ('en', 'http://en.localhost/wiki/$1', 1, 0);

Central Auth

edit

The next step is to install and configure CentralAuth. First download it into the w/extensions directory:

git clone ssh://<USERNAME>@gerrit.wikimedia.org:29418/mediawiki/extensions/CentralAuth.git

Then go into w/extensoins/CentralAuth directory, and find the central-auth.sql file. You need to edit the file, and uncomment the lines that say CREATE DATABASE centralauth; and USE centralauth; so that you can run it from shell like this:

mysql -u root -p < central-auth.sql

Revert the changes to the central-auth.sql file. Finally, edit your LocalSettings.php and add this line:

require_once ("$IP/extensions/CentralAuth/CentralAuth.php");

Now you need to setup $wgConf for CentralAuth to work.

wgConf

edit

Define $wgConf in LocalSettings.php with at least these data in it:

$wgConf = new SiteConfiguration;
$wgConf->settings = array(

'wgCanonicalServer' => array(
    'default' => 'http://$lang.localhost',
),
 
'wgScriptPath' => array(
    'default' => '/w',
),
 
'wgArticlePath' => array(
    'default' => '/wiki/$1',
),
 
'wgSitename' => array(
    'default' => 'Wiki',
    'fa_wiki' => 'ویکی',
),
 
'wgLanguageCode' => array(
    'default' => '$lang',
),
 
'wgLocalInterwiki' => array(
    'default' => '$lang',
),
 
);

$wgConf->wikis = array( 'en_wiki', 'fa_wiki' );
$wgConf->suffixes = array( '_wiki' );
$wgConf->localVHosts = array( 'localhost' );
function efGetSiteParams( $conf, $wiki ) {
    $site = null;
    $lang = null;
    foreach( $conf->suffixes as $suffix ) {
        if ( substr( $wiki, -strlen( $suffix ) ) == $suffix ) {
            $site = substr( $suffix, 1, strlen( $suffix ) );
            $lang = substr( $wiki, 0, -strlen( $suffix ) );
            break;
        }
    }
    return array(
        'suffix' => $site,
        'lang' => $lang,
        'params' => array(
            'lang' => $lang,
            'site' => $site,
            'wiki' => $wiki,
        ),
        'tags' => array(),
    );
}
 
$wgConf->suffixes = $wgLocalDatabases;
$wgConf->siteParamsCallback = 'efGetSiteParams';
$wgConf->extractAllGlobals( $wgDBname );

Migration

edit

Now you need to run the migration scripts. Start by creating a file in /var/www/w/extensions/CentralAuth/maintenance named migratePass_farm.php with the following source:

<?php
$_SERVER['SERVER_NAME'] = $argv[1];
include("migratePass" . $argv[2] . ".php");

Now you should run these four commands on bash while you are in /var/www/w/extensions/CentralAuth/maintenance:

php migratePass_farm.php fa.localhost 0
php migratePass_farm.php fa.localhost 1

php migratePass_farm.php en.localhost 0
php migratePass_farm.php en.localhost 1

This will update the centralauth tables in the database.

Central cache

edit

Since our wikis use different databases, they need to have a shared cache to let CentralAuth's central login feature work. Start by creating a shared table in centralauth database, by running this command in mysql:

CREATE TABLE centralauth.objectcache LIKE en_wiki.objectcache

Then add these lines to the end of your LocalSettings.php after the require_once:

$wgSharedDB      = 'centralauth';
$wgSharedTables  = array( 'objectcache' );
$wgMainCacheType = CACHE_DB;