Extension:NoTitle
From MediaWiki.org
|
Release status: beta |
|||
|---|---|---|---|
| Implementation | Extended syntax | ||
| Description | Adds a magic word that lets you hide the main title heading | ||
| Author(s) | Carlo Cabanilla | ||
| Last Version | 1.01 (5/18/2007) | ||
| MediaWiki | tested on 1.9.3 and 1.15.1 (using latest 1.13 Updated code) but probably works for others | ||
| License | No license specified | ||
| Download | see below | ||
|
|||
|
check usage (experimental) |
|||
Contents |
[edit] What can this extension do?
Adds a magic word, __NOTITLE__, that lets you hide the main title heading.
[edit] Usage
Just put __NOTITLE__ on any pages where you want to hide the title. This extension will work for any skins that puts the title heading in an h1 with class="firstHeading", including the default MonoBook skin.
[edit] Installation
Create a file in your extensions directory called notitle.php with the following code:
<?php $NoTitle = new NoTitle(); $wgHooks['MagicWordMagicWords'][] = array($NoTitle, 'addMagicWord'); $wgHooks['MagicWordwgVariableIDs'][] = array($NoTitle, 'addMagicWordId'); $wgHooks['LanguageGetMagic'][] = array($NoTitle, 'addMagicWordLanguage'); $wgHooks['ParserAfterStrip'][] = array($NoTitle, 'checkForMagicWord'); $wgHooks['BeforePageDisplay'][] = array($NoTitle, 'hideTitle'); class NoTitle { function NoTitle() {} function addMagicWord(&$magicWords) { $magicWords[] = 'MAG_NOTITLE'; return true; } function addMagicWordId(&$magicWords) { $magicWords[] = MAG_NOTITLE; return true; } function addMagicWordLanguage(&$magicWords, $langCode) { switch($langCode) { default: $magicWords[MAG_NOTITLE] = array(0, '__NOTITLE__'); } return true; } function checkForMagicWord(&$parser, &$text, &$strip_state) { $mw = MagicWord::get('MAG_NOTITLE'); if (!in_array($action, array('edit', 'submit')) && $mw->matchAndRemove($text)) { $parser->mOptions->mHideTitle = true; $parser->disableCache(); } return true; } function hideTitle(&$page) { if ($page->parserOptions()->mHideTitle) { $page->mScripts .= '<style>h1.firstHeading { display:none; } </style>'; } return true; } }
[edit] Changes to LocalSettings.php
Add the following line to your LocalSettings.php file in the wiki root:
require_once("$IP/extensions/notitle.php");
[edit] Known Problems
[edit] MediaWiki 1.10
- On some MW 1.10 servers the above code will not work, please try using the following instead in such cases:
<?php $NoTitle = new NoTitle(); $wgHooks['MagicWordMagicWords'][] = array($NoTitle, 'addMagicWord'); $wgHooks['MagicWordwgVariableIDs'][] = array($NoTitle, 'addMagicWordId'); $wgHooks['LanguageGetMagic'][] = array($NoTitle, 'addMagicWordLanguage'); $wgHooks['ParserAfterStrip'][] = array($NoTitle, 'checkForMagicWord'); class NoTitle { function NoTitle() {} function addMagicWord(&$magicWords) { $magicWords[] = 'MAG_NOTITLE'; return true; } function addMagicWordId(&$magicWords) { $magicWords[] = MAG_NOTITLE; } function addMagicWordLanguage(&$magicWords, $langCode) { switch($langCode) { default: $magicWords['MAG_NOTITLE'] = array(0, '__NOTITLE__'); } return true; } function checkForMagicWord(&$parser, &$text, &$strip_state) { global $action; $mw = MagicWord::get('MAG_NOTITLE'); if (!in_array($action, array('edit', 'submit')) && $mw->matchAndRemove($text)) { $parser->mOptions->mHideTitle = true; $this->hideTitle(); } return true; } function hideTitle() { global $wgOut; $wgOut->addScript('<style>h1.firstHeading { position : absolute; top: 0px; left : -1000px;} </style>'); return true; } }
[edit] MediaWiki 1.11
Broken on MediaWiki 1.11 due to this addition:
"Throw a showstopper exception when a hook function fails to return a value. Forgetting to give a 'true' return value is a very common error which tends to cause hard-to-track-down interactions between extensions."
You can fix this by adding "Return True;" to the end of addMagicWordId(). It will no longer spew PHP errors... However, it doesn't seem to work anymore, either.
You can remove the "Main Page" title on the front page by including the following in MediaWiki:common.js
In many cases, this bypasses the need for the extension.
/** Main Page layout fix ******************************************************* * * Description: UNDOCUMENTED * Maintainers: UNMAINTAINED * The following code was pulled from Wikipedia's http://en.wikipedia.org/wiki/MediaWiki:Common.js, and is used to * supress the title on the main page. */ var mpTitle = "Main Page"; var isMainPage = (/(title=|\/wiki\/)([Tt]alk:|)[Mm]ain[ _][Pp]age/.test(document.location)); var isMainPageFront = (document.title.substr(0, document.title.lastIndexOf(" - ")) == mpTitle); var isDiff = (document.location.search && (document.location.search.indexOf("diff=") != -1 || document.location.search.indexOf("oldid=") != -1)); if (isMainPageFront && !isDiff) { document.write('<style type="text/css">/*<![CDATA[*/ #lastmod, #siteSub, #contentSub, h1.firstHeading { display: none !important; } /*]]>*/</style>'); addOnloadHook( appendOtherLanguageLink ); } /**/
[edit] MediaWiki 1.13
This code is a slight modification so that it works with MediaWiki 1.13. It fixes mainly the no return true bug in the 1.10 version, the not-a-constant bug in the original, and also the variable-not-defined in the original in the hideTitle() function. The check for $action not being 'edit' or 'submit' was removed.
<?php // ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### #### // File: NoTitle.php // Belongs-To: MediaWiki NoTitle Extension // Version: 1.0-live // Authors: Carlo Cabanilla / Andrew Dodd // Email: andrewdodd13@gmail.com [Expires 31/12/2009] // Purpose: Hide title on pages with __NOTITLE__ magic word // Reqs: None // ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### #### $NoTitle = new NoTitle(); $wgHooks['MagicWordMagicWords'][] = array($NoTitle, 'addMagicWord'); $wgHooks['MagicWordwgVariableIDs'][] = array($NoTitle, 'addMagicWordId'); $wgHooks['LanguageGetMagic'][] = array($NoTitle, 'addMagicWordLanguage'); $wgHooks['ParserAfterStrip'][] = array($NoTitle, 'checkForMagicWord'); $wgHooks['BeforePageDisplay'][] = array($NoTitle, 'hideTitle'); class NoTitle { function NoTitle() {} function addMagicWord(&$magicWords) { $magicWords[] = 'MAG_NOTITLE'; return true; } function addMagicWordId(&$magicWords) { $magicWords[] = MAG_NOTITLE; return true; } function addMagicWordLanguage(&$magicWords, $langCode) { switch($langCode) { default: $magicWords['MAG_NOTITLE'] = array(0, '__NOTITLE__'); } return true; } function checkForMagicWord(&$parser, &$text, &$strip_state) { global $action; $mw = MagicWord::get('MAG_NOTITLE'); if ($mw->matchAndRemove($text)) { $parser->mOptions->mHideTitle = true; $parser->disableCache(); } return true; } function hideTitle(&$page) { if (isset($page->parserOptions()->mHideTitle) && $page->parserOptions()->mHideTitle) { $page->mScripts .= "<style type='text/css'>h1.firstHeading { display:none; } </style>"; } return true; } }