Extension:CustomTitle

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

Release status: unmaintained

Implementation Parser function
Description Allows to modify the default page title and header
Author(s) Wiktor Walc (Wwalctalk)
Latest version 1.0 (2008-01-30)
MediaWiki 1.11+
Database changes No
License GNU General Public License
Download See the code section
Hooks used
LanguageGetMagic

Translate the CustomTitle extension if it is available at translatewiki.net

Check usage and version matrix; code metrics

The CustomTitle extension allows to modify the page header and title.

Installation[edit | edit source]

  • Copy the code into a file and extract the tarball in your extensions/ folder. It should generate a new folder called CustomTitle directly inside your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php:
require_once "$IP/extensions/CustomTitle/CustomTitle.php";
  • Done! Navigate to "Special:Version" on your wiki to verify that the extension is successfully installed.

Code[edit | edit source]

CustomTitle.php
<?php

# Not a valid entry point, skip unless MEDIAWIKI is defined
if (!defined('MEDIAWIKI')) {
    echo <<<HEREDOC
To install this extension, put the following line in LocalSettings.php:
require_once( "\$IP/extensions/CustomTitle/CustomTitle.php" );
HEREDOC
;
    exit( 1 );
}

$wgExtensionCredits['other'][] = array(
"name" => "CustomTitle",
"author" => "Wiktor Walc",
"version" => '1.0',
"url" => "https://www.mediawiki.org/wiki/Extension:CustomTitle",
"description" => "Allows to modify the page header and title"
);

$oCustomTitleExtension = new CustomTitle_MediaWiki();
$oCustomTitleExtension->registerHooks();

$wgExtensionFunctions[] = array($oCustomTitleExtension, "setupExtensionFunction");
$wgHooks['LanguageGetMagic'][] = array($oCustomTitleExtension, "addMagicWord");


class CustomTitle_MediaWiki
{
    protected $customTitle;
    protected $customPageTitle;
   
    function setupExtensionFunction()
    {
        global $wgParser;
       
        $wgParser->setFunctionHook( 'customtitle', array($this, "substitute"));        
    }
   
    function substitute(&$parser, $param1 = '', $param2 = '')
    {
        $ret = "";
        if ($param1 !== '')
            $ret .= "xxx-CustomTitleStart-xxx". $param1 ."xxx-CustomTitleEnd-xxx";
        if ($param2 !== '')
            $ret .= "xxx-CustomPageTitleStart-xxx". $param2 ."xxx-CustomPageTitleEnd-xxx";
        return $ret;        
    }
   
    function addMagicWord(&$magicWords, $langCode)
    {
        $magicWords['customtitle'] = array( 0, 'customtitle' );
       
        return true;
    }

    function onSkinTemplateOutputPageBeforeExec(&$m_skinTemplate, &$m_tpl)
    {
        if (isset($this->customTitle))
            $m_tpl->set('title', $this->customTitle);
        if (isset($this->customPageTitle))
            $m_tpl->set('pagetitle', $this->customPageTitle);

        return true;
    }

    function onOutputPageBeforeHTML(&$out, &$text)
    {
        if (($found = strpos($text, 'xxx-CustomTitleStart-xxx')) !== false) {
            if (preg_match("/xxx-CustomTitleStart-xxx(.*?)xxx-CustomTitleEnd-xxx/", $text, $matches)) {
                $this->customTitle = $matches[1];
                $text = str_replace($matches[0], "", $text);
            }
        }

        if (($found = strpos($text, 'xxx-CustomPageTitleStart-xxx')) !== false) {
            if (preg_match("/xxx-CustomPageTitleStart-xxx(.*?)xxx-CustomPageTitleEnd-xxx/", $text, $matches)) {
                $this->customPageTitle = $matches[1];
                $text = str_replace($matches[0], "", $text);
            }
        }

        return true;
    }

    public function registerHooks() {
        global $wgHooks;

        $wgHooks['SkinTemplateOutputPageBeforeExec'][] = array($this, 'onSkinTemplateOutputPageBeforeExec');
        $wgHooks['OutputPageBeforeHTML'][] = array($this, 'onOutputPageBeforeHTML');
    }
}

Usage[edit | edit source]

  • To change the page header and title
   {{#customtitle:My header|My Title}}
  • To change header only:
   {{#customtitle:My header}}

Important Notes[edit | edit source]

This extension modifies template variables and nothing more. You have to remember that although different title is being displayed, you still have to link to that page using the real page name.

Example[edit | edit source]

Let's say we have a page named "Example". Even if you change the title to "My own title" with {{#customtitle:My own title}}, to create a link to that page, you still have to use [[Example]].

See Also[edit | edit source]