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: 2017-03-18 12:33:12

<?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-2017, Mark Clements
 * @license http://creativecommons.org/licenses/by-sa/2.5/ cc-by-sa 2.5 or later
 * @version $Rev: 1341 $
 */
 
// Setup version number
	$pMCExt_Version = '$Rev: 1341 $';
	$pMCExt_Version = substr($pMCExt_Version, 6, -2);
// Setup extension credits
	$wgExtensionCredits['other'][] = array(
		'name' => "Personal Homepage",
		'version' => "r" . $pMCExt_Version,
		'author' => "Mark Clements",
		'description' => "an extension to allow users to customise the 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 (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 = $wgContLang->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);
		}
	}