<?php
/**
* MetaTag.php - A MediaWiki tag extension for adding <meta> tags to a page.
* @author Peter Georgeson
* @version 0.2
* @copyright Copyright (C) 2007 Peter Georgeson
* @license The MIT License - http://www.opensource.org/licenses/mit-license.php
* -----------------------------------------------------------------------
* Description:
* This is a MediaWiki extension which adds support for injecting <meta> keyword tags
* into the page header.
* Heavily based on Jim R. Wilson's meta keywords extension
* Requirements:
* MediaWiki 1.17 or higher
* 5.x or higher
* Installation:
* 1. Drop this script (MetaTag.php) and the i18n file in $IP/extensions/MetaTag
* Note: $IP is your MediaWiki install dir.
* 2. Enable the extension by adding this line to your LocalSettings.php:
* require_once($IP . 'extensions/MetaTag/MetaTag.php');
* Usage:
* Once installed, you may utilize MetaTag by adding the <meta> tag to articles:
* <meta name="keywords" content="example, data" />
* Change the title of the page with the command:
* <meta name="title" content="new title"/>
* Version Notes:
* version 0.2:
* Modernized not to break newer MediaWikis.
* version 0.1:
* Initial release.
* -----------------------------------------------------------------------
* Copyright (c) 2008 Peter Georgeson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
* -----------------------------------------------------------------------
*/
# Confirm MW environment
if ( !defined( 'MEDIAWIKI' ) ) {
die( 'This is not a valid entry point to MediaWiki.' );
}
// Extension credits that will show up on Special:Version
$wgExtensionCredits['parserhook'][] = array(
'name' => 'MetaTag',
'version' => '0.2',
'author' => array( 'Jim Wilson', 'Peter Georgeson' ),
'url' => 'http://jimbojw.com/wiki/index.php?title=MetaKeywordsTag',
'description' => 'Tag to inject meta tags into page header',
);
// Set up i18n
$wgExtensionMessagesFiles['MetaTag'] = dirname( __FILE__ ) . '/MetaTag.i18n.php';
// Register the <meta> tag with MediaWiki's Parser
$wgHooks['ParserFirstCallInit'][] = 'setupMetaTagParserHook';
/**
* Register the <meta> tag with MediaWiki's Parser
*/
function setupMetaTagParserHook( &$parser ) {
$parser->setHook( 'meta', 'renderMetaTag' );
return true;
}
/**
* Renders the <meta> tag.
* @param String $text Incomming text - should always be null or empty (passed by value).
* @param Array $params Attributes specified for tag - must contain 'content' (passed by value).
* @param Parser $parser Reference to currently running parser (passed by reference).
* @return String Always empty.
*/
function renderMetaTag( $text, $params = array() ) {
return encodeTag( $params['name'], $params['content'] );
}
function encodeTag( $name, $content ) {
# Short-circuit with error message if content is not specified.
if ( !isset( $content ) ) {
return
'<div class="errorbox">' .
wfMessage( 'metatag-missing-content' )->text() .
'</div>';
}
if ( !isset( $name ) ) {
return
'<div class="errorbox">' .
wfMessage( 'metatag-missing-name' )->text() .
'</div>';
}
# Return encoded content
return '<!-- META_' . $name . ' ' . base64_encode( $content ) . ' -->';
}
# Attach post-parser hook to extract metadata and alter headers
$wgHooks['OutputPageBeforeHTML'][] = 'insertMetaTags';
/**
* Adds the <meta> keywords to document head.
* Usage: $wgHooks['OutputPageBeforeHTML'][] = 'insertMetaKeywords';
* @param OutputPage $out Handle to an OutputPage object - presumably $wgOut (passed by reference).
* @param String $text Output text.
* @return Boolean Always true to allow other extensions to continue processing.
*/
function insertMetaTags( $out, $text ) {
# Extract meta keywords
if ( preg_match_all(
'/<!-- META_([a-zA-Z]+) ([0-9a-zA-Z\\-\\+\\/]+=*) -->/m',
$text,
$matches ) === false
)
{
return true;
}
$name = $matches[1];
$data = $matches[2];
# Merge keyword data into OutputPage as meta tags
for ( $i = 0; $i < sizeof( $name ); $i++ ) {
wfSuppressWarnings();
$content = base64_decode( $data[$i] );
wfRestoreWarnings();
if ( $content ) {
if ( $name[$i] == 'title' ) {
$out->setHTMLTitle( $content );
} else {
$out->addMeta( $name[$i], $content );
}
}
}
return true;
}