Difference between revisions of "PersonalisedHomepage"
m (Fix case error) |
(Added {{ExtensionCheck}}) |
||
Line 1: | Line 1: | ||
+ | {{ExtensionCheck}} | ||
{{On MediaWiki.org}} | {{On MediaWiki.org}} | ||
This extension allows users to personalise the home page by creating a new page in their user space that will be used instead. | This extension allows users to personalise the home page by creating a new page in their user space that will be used instead. |
Revision as of 21:01, 18 October 2011
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
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
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: 2024-01-27 12:32:18
<?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 * * @author Mark Clements <mclements at kennel17 dot co dot uk> * @copyright Copyright © 2007-2024, Mark Clements * @license http://creativecommons.org/licenses/by-sa/2.5/ cc-by-sa 2.5 or later * @version $Rev: 2439 $ */ ///////////////////////////////////////////////// // CREDITS // Setup version number $pMCExt_Version = '$Rev: 2439 $'; $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); ///////////////////////////////////////////////// // EXTENSION CONFIGURATION $wgExtensionFunctions[] = "wfPersonalHomepage"; function wfPersonalHomepage() { global $wgHooks; // Setup parser hook to replace the standard home page content with the user's // personal home page, if one is defined. // For this hook, we want to run as early as possible in the parsing process. // Before MW 1.14, ParserBeforeStrip was the earliest point. From 1.14, onwards, // both hooks are equivalent. As ParserBeforeStrip was removed in MW 1.36, we // therefore use ParserBeforeInternalParse on all versions after MW 1.14. // @back-compat MW < 1.14 if (version_compare(MW_VERSION, '1.14.0', '<')) $wgHooks['ParserBeforeStrip'][] = "wfPERSHOM_DoIt"; else $wgHooks['ParserBeforeInternalParse'][] = "wfPERSHOM_DoIt"; } ///////////////////////////////////////////////// // EXTENSION FUNCTIONALITY function wfPERSHOM_DoIt(&$Parser, &$Text, &$StripState) { global $wgOut, $wgRequest; $objUser = PERSHOM_GetCurrentUser(); if (isset($wgOut) && isset($objUser) && isset($wgRequest)) { if ($wgRequest->getVal('action', 'view') == "view") { $MainPage = PERSHOM_Msg("mainpage"); $Title = PERSHOM_GetTitleFromParser($Parser); if ($Title->getNamespace() == NS_MAIN && $Title->getText() == $MainPage) { $objContentLanguage = PERSHOM_GetContentLanguage(); $ToPage = $objContentLanguage->getNsText(NS_USER) . ":" . $objUser->getName() . "/" . $MainPage; $NewTitle = Title::newFromText($ToPage); if ($NewTitle->exists()) $wgOut->redirect($NewTitle->getFullURL()); } } } return true; } ///////////////////////////////////////////////// // HELPER FUNCTIONS // 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); } } // PERSHOM_GetCurrentUser() // Returns the User object for the current context. // The global $wgUser variable was deprecated in MediaWiki 1.35 and will, ultimately, // be removed (though probably not for quite a while). In modern MediaWiki versions // you should instead get the User object from the current context. For code where // a context is available, you should pull it from there instead of calling this // function. However, for situations where no obvious alternative context source is // available you should use this function to get the current user. function PERSHOM_GetCurrentUser() { // In modern MediaWiki versions, the RequestContext class can be used to get // the current User object. We get the main context and return the User it // refers to. // For older MediaWiki versions, we get the user via the global $wgUser variable. // @back-compat MW < 1.18 if (class_exists("RequestContext")) { $objContext = RequestContext::getMain(); return $objContext->getUser(); } else { // $wgUser is already deprecated and will ultimately be removed so, to // avoid confusion, we put the global declaration in this // backwards-compatibility code branch, not at the top of the function // (as best-practice would normally dictate). global $wgUser; return $wgUser; } } // PERSHOM_GetContentLanguage() // Returns the content language for the wiki. // This requires several lines of code on modern MediaWiki, hence the wrapper // function. // The function also handles backwards-compatibility, for older MediaWiki versions // where this was accessed differently, via a global variable. function PERSHOM_GetContentLanguage() { // 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")); return $MWServices->getContentLanguage(); } // On earlier versions, $wgContLang is available, so we simply return that. // @back-compat MW < 1.32 else { // $wgContLang was removed in MW 1.36 so, to avoid confusion, we put the // global declaration in this backwards-compatibility code branch, not at // the top of the function (as best-practice would normally dictate). global $wgContLang; return $wgContLang; } } // PERSHOM_GetTitleFromParser() // Wrapper function to handle the fact that the way you retrieve a Title object // from a Parser object has changed over time. In addition, it looks like it will // be changing further in the future, so there may be further development of this // function required. function PERSHOM_GetTitleFromParser($objParser) { // Older versions of MediaWiki use Parser::getTitle(). As of MW 1.37, this // function is deprecated and Parser::getPage() should be used instead. // Currently, this function still returns a Title object and so is fully // compatible with the previous implementation (just the name has changed). // However, this may well change in the future. I will need to keep an eye on // this! // @back-compat MW < 1.37 if (method_exists($objParser, "getPage")) { return $objParser->getPage(); } else { return $objParser->getTitle(); } }