Switch

From mw.mh370.wiki
Jump to navigation Jump to search


A Guide to Using MediaWiki in a Hosted Environment

An instructional website by the developer of mh370wiki.net - a MediaWiki site about Malaysia Airlines Flight MH370.


MediaWiki LocalSettings.php

The LocalSettings.php file contains the configuration for the MediaWiki-based website.

If you wanted to put all the namespace configuration in a separate .php file, or keep the database settings more private by putting the configuration in a specific .php file, these could be called from LocalSettings.php using lines like this:-

require_once("$IP/subdir/Namespaces.php");
require_once("$IP/subdir/Database.php");

Where both files are in /w/subdir

If you get this concept working it is only another step to using LocalSettings.php as a switch for alternate configurations or several websites.


Same Installation, Different Websites

Assume we have two MediaWiki-based websites, site1.wiki and site2.wiki. And we will use the same MediaWiki installation. The configuration files for each website will be in separate folders /w/Site1 and /w/Site2.

The /w/LocalSettings.php file may look like this:-

<?php
switch ( $_SERVER['SERVER_NAME'] ) {
            
	case 'site1.wiki':
        require_once 'Site1/Site1Settings.php';
            break;            
            
	case 'site2.wiki':
        require_once 'Site2/Site2Settings.php';
            break;             

    default:
        header( 'HTTP/1.1 404 Not Found' );
        echo 'This wiki is not available. Check configuration.';
        exit( 0 );
        }

Notes:-

  1. Every .php file must begin with <?php
  2. The first case statement is a condition - if the web request matches the domain site1.wiki then load the Site1Settings from the subdirectory /Site1
    But if the web request does not match the domain site1.wiki go to the next case statement.
  3. Each case is evaluated and if none match then the web server returns the Error 404 'Not Found' message
  4. If the web request was www.site1.wiki or www.site2.wiki the web server would return the Error 404 'Not Found' message. To avoid this, add additional case statements.
  5. The configuration for each website is separate; they just use the currently installed version of MediaWiki. So each website will use its' own database.
  6. When MediaWiki is upgraded, each site will be affected and each database must be upgraded because they are dependent on the same installation.

  7. Images for all websites will be stored by default in /w/images - so if there are multiple sites the image storage may get a bit messy.

Same Installation, Redirected Websites

A variant of the above switch could be used for this website. The domain is mh370.wiki and this website is a subdomain mw.mh370.wiki.The domain mh370.wiki could be redirected to mw.mh370.wiki in cPanel, or both domains could point to the same website folder in /public_html. The LocalSettings.php file could look like this:-

<?php

// Settings common settings to all websites can be added  before this line

switch ( $_SERVER['SERVER_NAME'] ) {
            
	case 'mh370.wiki':
        require_once 'SiteConfig/MW_Settings.php';
            
	case 'www.mh370.wiki':
        require_once 'SiteConfig/MW_Settings.php';
            break;             
            
// ####################################################           
	case 'mw.mh370.wiki':
        require_once 'SiteConfig/MW_Settings.php';
                        break;             

// ####################################################  

    default:
        header( 'HTTP/1.1 404 Not Found' );
        echo 'This wiki is not available. Check configuration.';
        exit( 0 );
        }

Notes:-

  1. A case statement for www has been included in this example.
  2. Each web request will end up at the same configuration file MW_Settings.php, and because there is only one set of values for the site name and database, the same content will be returned.
  3. In this example, all of the custom settings have been moved to a subfolder /w/SiteConfig. The path to skins and extensions will need to be altered accordingly.
  4. Settings common to all websites could be added at the beginning of the LOcalSettings.php file when it is used as a switch.
    Alternatively, create a file named CommonSettings.php and link to it from each website settings file.

Switch Benefits

The main benefit is the use of only one MediaWiki installation for multiple websites.

The concept is used in a MediaWiki 'Family', previously known as a MediaWiki Farm.

It can be used to load different versions of a website based on country or language.

It is fun to set up and get working!


Giant Switch Statement

The following section has been retrieved from the Internet Archive at the URL https://web.archive.org/web/20210817175632/https://www.mediawiki.org/wiki/Manual:Wiki_family#Giant_switch_statement from a snapshot on a random date of 17 August 2021. The examples above are based on actual switch cases that I have used, which in turn were based on this Giant Switch Statement previously published by MediaWiki

Giant switch statement

This will allow you to install more than one wiki on a single server, using the same source code checkout.

  1. Upload MediaWiki files to web folder on the webserver.
  2. Set up initial wiki as usual. For details, see Manual:Installation guide".
  3. After successful installation, move LocalSettings.php into the root directory of your wiki and rename it in such a way to make it easy to track (e.g. LocalSettings_myFirstWiki.php)
  4. Repeat step two and three above for each wiki you wish to create, creating a new LocalSettings.php file for each wiki (e.g., LocalSettings_anotherWiki.php, etc.)
  5. If two or more separately installed wikis are to be merged to operate out of files of the main wiki, then after renaming and moving each of your LocalSettings.php files to the main wiki folder, change the variable $wgScriptPath in each of the LocalSettings.php files to point to the main wiki's folder.
  6. Create a LocalSettings.php file for your global settings, then select one from the two possibilities below:
  • If you have different (sub)domains that link to one directory on your server, use this:
<?php
// Include common settings to all wikis before this line (eg. database configuration)

        switch ( $_SERVER['SERVER_NAME'] ) {
                case 'shoopz.com':
                        require_once 'LocalSettings_shoopz_com.php';
                        break;

                case 'help.shoopz.com':
                        require_once 'LocalSettings_help_shoopz_com.php';
                        break;

                case 'wiki.shoopz.net':
                        require_once 'LocalSettings_wiki_shoopz_net.php';
                        break;

                default:
                        header( 'HTTP/1.1 404 Not Found' );
                        echo 'This wiki is not available. Check configuration.';
                        exit( 0 );
        }

The second option was 'If your wikis are on the same domain but different paths (e.g. yourdomain.com/wiki1, yourdomain.com/wiki2 etc.), you can use something like this:'...

This is an uncommon configuration for an individual with hosted websites and is therefore not included here.


Articles which relate to MediaWiki Farm

Articles which relate to MediaWiki Farm are included in Category:Switch.

The CategoryTree Extension enables a listing of relevant sub-categories and pages:-



Links

Manual:Wiki family
https://www.mediawiki.org/wiki/Manual:Wiki_family
This manual used to have an example of a Giant switch statement. Older versions can be viewed on the Internet Archive.
Manual:CommonSettings.php
https://www.mediawiki.org/wiki/Manual:CommonSettings.php
Internet Archive, or Wayback Machine
https://archive.org/