PersonalisedHomepage

From TestWiki
Jump to: navigation, search
This extension is currently installed on this wiki. Feel free to experiment with it!
MediaWiki logo.png
This extension is also documented at
Extension:PersonalisedHomepage
on MediaWiki.org.

If the pages are not in sync. then this version on my test wiki is always the most up-to-date.

This extension allows users to personalise the home page by creating a new page in their user space that will be used instead.

How to customise[edit]

Create a sub-page of your user page which has the same name as the main page of your wiki. By default this is Main Page, but this is configurable on a per-wiki basis so it may differ in your case. You can find the correct name by checking the contents of MediaWiki:Mainpage for the wiki in question. "Main Page" should be replaced by the appropriate value for your wiki throughout the remainder of these instructions.

If your username was "Sir Dennis" you would therefore create your personalised home page at "User:Sir Dennis/Main Page". You can edit the page to contain whatever you want. If you want to include the contents of the non-customised main page then add {{:Main Page}} at the point you want it to appear.

Source[edit]

Copy the source code to a file called PersonalisedHomepage.php in your extensions directory, and add the following line to the bottom of your LocalSettings.php file:

include_once("extensions/PersonalisedHomepage.php");

Live sourcecode viewer: PersonalisedHomepage.php
Last modified: 2019-03-06 11:07:28

<?php
if (!defined('MEDIAWIKI'))
	die("MediaWiki extensions cannot be run directly.");
/**
 * An extension to allow users to have their own customised main page.
 *
 * THIS EXTENSION WAS DEVELOPED AND TESTED ON MEDIAWIKI 1.6.10
 * OTHER VERSIONS ARE UNTESTED, BUT SHOULD BE OK
 *
 * @package MediaWiki
 * @subpackage Extensions
 *
 * @author Mark Clements <mclements at kennel17 dot co dot uk>
 * @copyright copyright  2007-2019, Mark Clements
 * @license http://creativecommons.org/licenses/by-sa/2.5/ cc-by-sa 2.5 or later
 * @version $Rev: 1538 $
 */
 
// Setup version number
	$pMCExt_Version = '$Rev: 1538 $';
	$pMCExt_Version = substr($pMCExt_Version, 6, -2);
// Setup extension credits
	$wgExtensionCredits['other'][] = array(
		'name' => "Personal Homepage",
		'version' => "r" . $pMCExt_Version,
		'author' => "Mark Clements",
		'description' => "Allows users to customise their Main Page",
		'url' => "http://www.kennel17.co.uk/testwiki/PersonalisedHomepage",
	);
// Tidy up
	unset($pMCExt_Version);
 
	$wgExtensionFunctions[] = "wfPersonalHomepage";
 
/////////////////////////////////////////////////
 
	function wfPersonalHomepage() {
		global $wgHooks;
 
	// Setup parser hooks
		$wgHooks['ParserBeforeStrip'][] = "wfPERSHOM_DoIt";
	}
 
	function wfPERSHOM_DoIt(&$Parser, &$Text, &$StripState) {
		global $wgOut, $wgUser, $wgContLang, $wgRequest;
 
	// If the getContentLanguage() function exists on the MediaWikiServices class
	// then use this to get the content Language object.
	// We need to check for the method as the class was introduced in MW 1.27, but
	// the method was only added in MW 1.32.
		if (method_exists("MediaWiki\\MediaWikiServices", "getContentLanguage")) {
		// To ensure the code remains syntax-compatible with all supported versions
		// of PHP, we can't use a literal namespace.  Nor can we use
		// $ClassName::Method().
		// We therefore use a string variable and call_user_func().
		// @back-compat PHP < 5.3 (MW < 1.20)
			$ClassName = "MediaWiki\\MediaWikiServices";
			$MWServices = call_user_func(array($ClassName, "getInstance"));
			$objContentLanguage = $MWServices->getContentLanguage();
		}
	// On earlier versions, $wgContLang is available, so we simply return that.
	// @back-compat MW < 1.32
		else {
			$objContentLanguage = $wgContLang;
		}
 
		if (isset($wgOut) && isset($wgUser) && isset($wgRequest)) {
			if ($wgRequest->getVal('action', 'view') == "view") {
				$MainPage = PERSHOM_Msg("mainpage");
				$Title = $Parser->getTitle();
				if ($Title->getNamespace() == NS_MAIN
					&& $Title->getText() == $MainPage)
				{
					$ToPage = $objContentLanguage->getNsText(NS_USER) . ":"
							. $wgUser->getName() . "/" . $MainPage;
					$NewTitle = Title::newFromText($ToPage);
					if ($NewTitle->exists())
						$wgOut->redirect($NewTitle->getFullURL());
				}
			}
		}
 
		return true;
	}
 
// PERSHOM_Msg()
// Returns the text of the specified system message, using any additional arguments
// for variable replacement.
// Functionally equivalent to wfMsg(), which is the original way of handling this
// in MediaWiki.  However, this was deprecated in favour of wfMessage() and the new
// Message class in MW 1.17 and removed altogether in MW 1.27.
// This wrapper function calls wfMessage() if available, otherwise wfMsg(), and
// in both cases returns the expanded string (not a Message object).  As well as
// ensuring compatibility across MediaWiki versions, using this function simplifies
// our code as it doesn't need to explicitly call text() to get the output string.
// Note that the only difference between this and wfMsg() is that it benefits from
// some bug fixes that were applied to the wfMessage() implementation, namely that
// variable substitution now happens before template expansion, so constructs such
// as {{PLURAL:$1|egg|eggs}} now work as expected.
	function PERSHOM_Msg($Key) {
		$Args = func_get_args();
 
		if (function_exists("wfMessage")) {
			$objMessage = call_user_func_array("wfMessage", $Args);
			return $objMessage->text();
		}
		else {
			return call_user_func_array("wfMsg", $Args);
		}
	}