Jump to content

User:Mainframe98/Wiki Family/Example

From mediawiki.org

LocalSettings.php

[edit]
<?php
# Protect against web entry
if ( !defined( 'MEDIAWIKI' ) ) {
	exit;
}

# Setup $wgConf
$wgConf = new SiteConfiguration();
$wgConf->suffixes = [ 'wiki' ];

# Determine the db name
if ( defined( 'MW_DB' ) ) {
	// Command-line mode and maintenance scripts (e.g. update.php)
	$wgDBname = MW_DB;

	$wikiname = subst( MW_DB, 0, strlen( $wiki[0] ) );
} else {
	// Web server
	$server = $_SERVER['SERVER_NAME'];

	$urlComponents = explode( '.', $server );

	if ( !$urlComponents ) {
		die( "Invalid host name, can't determine wiki name" );
		// Optional: Redirect to a "No such wiki" page.
	}

	// Reverse the array so the tld is the first item in the array
	$urlComponents = array_reverse( $urlComponents );

	// If no sub domain has been given, set it to www as default
	if ( count( $urlComponents ) < 3 ) {
		$urlComponents[2] = 'www';
	}

	// Determine wiki name and remove any periods from the name
	$wikiname = implode( '', array_slice( $urlComponents, 2 ) );

	// Optional: Override database name of your "main" wiki (otherwise "wwwwiki")
	if ( $urlComponents[2] === 'www' ) {
		$wikiname = 'meta';
	}

	$wgDBname = $wikiname . $suffix[0];
}

require_once( __DIR__ . '/CommonSettings.php' );
require_once( __DIR__ . '/InitialiseSettings.php' );

$wgLocalDatabases = [];

$fgDatabaseList = file( 'all.dblist' );
# Set the language and site name for each database/wiki
foreach ( $fgDatabaseList as $wiki ) {
	$wikiDB = explode( '|', $wiki, 3 );
	list( $dbName, $siteName, $siteLang ) = array_pad( $wikiDB, 3, '' );
	$wgLocalDatabases[] = $dbName;
	$wgConf->settings['wgSitename'][$dbName] = $siteName;
	$wgConf->settings['wgLanguageCode'][$dbName] = $siteLang;
}

# Check if the obtained database name is in the list of databases
if ( !in_array ( $wgDBname, $wgLocalDatabases ) ) {
	require_once( __DIR__ . 'WikiNotFound404.php' );
	// Optional: Redirect to a "No such wiki" page on the central wiki.
}

function efGetSiteParams( SiteConfiguration $conf, $wiki ) {
	$tags = [];

	// Get a list of all the tag dblists
	$tagDbLists = glob( __DIR__ . '/dblists/*' );
	foreach ( $tagDbLists as $dblistfile ) {
		$dblist = file( $dblistfile );
		if ( in_array( $wiki, $dblist ) ) {
			// Add the tag to the list of tags, which is equal to
			// the name of the dblist, minus the file extension
			$tags[] = basename( $dblistfile, '.dblist' );
		}
	}

	if ( isset( $conf->settings['wgLanguageCode'][$wiki] ) ) {
		$lang = $conf->settings['wgLanguageCode'][$wiki];
	} else {
		$lang = $conf->settings['wgLanguageCode']['default'];
	}

	return [
		'suffix' => 'wiki',
		'lang'   => $lang,
		'tags'   => [],
		'params' => [
			'lang' => $lang,
			'site' => 'wiki',
			'wiki' => $wiki,
		],
	];
}

$wgConf->siteParamsCallback = 'efGetSiteParams';

# Set the wikis for $wgConf
$wgConf->wikis = $wgLocalDatabases;

# Extract the globals
$wgConf->extractAllGlobals( $wgDBname );

CommonSettings.php

[edit]
<?php
# Protect against web entry
if ( !defined( 'MEDIAWIKI' ) ) {
	exit;
}

## This file contains all the settings shared among all wikis.
## Ideally, you'd set this once and forget about it. As such,
## you set settings that wikis would like to override not here,
## but in InitialiseSettings.php instead.
## The URL base path to the directory containing the wiki;
## defaults for all runtime URL paths are based off of this.
$wgScriptPath = "/w";
$wgArticlePath = "/wiki/$1";

## The URL path to static resources (images, scripts, etc.)
$wgResourceBasePath = $wgScriptPath;

## To enable image uploads, make sure the 'images' directory
## is writable, then set this to true:
$wgUploadDirectory = "{$wgScriptPath}/images/$wgDBname";
$wgUploadPath = $wgUploadDirectory;

## Configuration for shared images
$wgForeignFileRepos[] = [
	'class' => 'ForeignDBRepo',
	'name' => 'mediawiki',
	'url' => "http://media.atheistpedia.org/wiki",
	'directory' => '/path/to/media',
	'hashLevels' => 2, // This must be the same for the other family member
	'dbType' => $wgDBtype,
	'dbServer' => $wgDBserver,
	'dbUser' => $wgDBuser,
	'dbPassword' => $wgDBpassword,
	'dbFlags' => DBO_DEFAULT,
	'dbName' => 'mediawiki',
	'tablePrefix' => '',
	'hasSharedCache' => false,
	'descBaseUrl' => 'http://media.atheistpedia.org/wiki/File:',
	'fetchDescription' => true
];

# Set the cookie domain to a non explicit sub domain to prevent users being logged out when
# accessing the wiki family from other sub domains.
$wgCookieDomain = '.atheistpedia.org';

# Set the Cross Site AJAX domains to allow requests across the entire family.
$wgCrossSiteAJAXdomains = [
	'*.atheistpedia.org'
];

## Set $wgCacheDirectory to a writable directory on the web server
## to make your wiki go slightly faster. The directory should not
## be publicly accessible from the web.
$wgCacheDirectory = "$IP/cache/$wgDBName";

InitialiseSettings.php

[edit]
<?php
# Protect against web entry
if ( !defined( 'MEDIAWIKI' ) ) {
	exit;
}

# All wiki settings. Remember to save any default settings here, and not in CommonSettings.php
$wgConf->settings = [
	'wgLanguageCode' => [
		'default' => 'en'
	],
	'wgLogo' => [
		'default' => "$wgScriptPath/images/$wgDBname" . '_logo.png'
	],
	'wgServer' => [
		'default' => "//$wikiname.atheistpedia.org"
	],
];

Overview

[edit]

Note: This example is brief! There are quite some settings that should still be added. For examples, see: https://github.com/mainframe98/MediaWiki-Family-Configuration-Example

  • Place the three files above in the folder where MediaWiki is installed.
  • Create a folder named dblists, and create a file named all.dblist. Its contents should be:
mediawiki|Media Wiki|en
datawiki|Data Wiki|en
enwiki|Atheistpedia|en
bnwiki|Atheistpedia in Bengali|bn
hiwiki|Atheistpedia in Hindi|hi
  • This example also contains a redirect on line 64 of LocalSettings.php.
  • File uploads are redirected to the images folder, to a specific folder for each wiki, based on their database name (i.e. if uploads are enabled on enwiki, uploads will go to $IP/images/enwiki)
  • The same applies for the cache folder, enwiki cache will go in $IP/cache/enwiki
  • Logos are, unless overridden, located in $IP/images/DATABASENAME_logo.png, so for enwiki this would be $IP/images/enwiki_logo.png.