Extension:NoTitle

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
NoTitle

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 (12/1/2011)
MediaWiki 1.9.x - 1.18
License No license specified
Download see below
Hooks used
ParserBeforeStrip

BeforePageDisplay

Check usage (experimental)

Adds a magic word, __NOTITLE__, that lets you hide the main title heading on any page.

Contents

[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:

[edit] MediaWiki 1.16, 1.18

Note: The following modified code works for 1.18. The original code by the author does not work for 1.18 anymore.

Modifications from original code:

  • Works with MW 1.16 and 1.17 (and 1.18, tested)
  • Doesn't disable the parser cache
  • Doesn't search for the magic word itself, since the parser already does that
  • hides the subhead (redirected from notice etc.)
  • works better with the other included skins (it doesn't hide the title on modern, because that would look weird)
<?php
 
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'No title',
        'author' => '[http://www.mediawiki.org/wiki/User:Nx Nx]',
        'description' => 'Adds a magic word to hide the title heading.'
);
 
$wgHooks['LanguageGetMagic'][] = 'NoTitle::addMagicWordLanguage';
$wgHooks['ParserBeforeTidy'][] = 'NoTitle::checkForMagicWord';
 
 
class NoTitle
{
 
  static function addMagicWordLanguage(&$magicWords, $langCode) {
    switch($langCode) {
    default:
      $magicWords['notitle'] = array(0, '__NOTITLE__');
    }
    MagicWord::$mDoubleUnderscoreIDs[] = 'notitle';
    return true;
  }
 
  static function checkForMagicWord(&$parser, &$text) {
    if ( isset( $parser->mDoubleUnderscores['notitle'] ) ) {
      $parser->mOutput->addHeadItem('<style type="text/css">/*<![CDATA[*/ .firstHeading, .subtitle, #siteSub, #contentSub, .pagetitle { 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] GuMax Fix

The following is needed if you're using GuMax skins:

Replace

      $parser->mOutput->addHeadItem('<style type="text/css">/*<![CDATA[*/ .firstHeading, .subtitle, #siteSub, #contentSub, .pagetitle { display:none; } /*]]>*/</style>');

with

      $parser->mOutput->addHeadItem('<style type="text/css">/*<![CDATA[*/ .gumax-firstHeading, .firstHeading, .subtitle, #siteSub, #contentSub, .pagetitle { display:none; } /*]]>*/</style>');
      $parser->mOutput->addHeadItem('<style type="text/css">/*<![CDATA[*/  #content {border-top:none;} /*]]>*/</style>');


[edit] Other versions

This method (original code by author) does not work for 1.18 (see above for that version):

<?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] 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 - 1.15

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;
    }
}

[edit] See also

Language: English  • 日本語
Personal tools
Namespaces
Variants
Actions
Site
Support
Download
Development
Communication
Print/export
Toolbox