Extension:MetaDescriptionTag

An extension which gives users the ability to inject a &lt;meta&gt; description into the document header.

Note: Jim R. Wilson's excellent article Doing more with MediaWiki parser extensions contains a lengthy discussion of how this extension works.

Installation

 * 1) Create a file named MetaDescriptionTag.php containing the code below.
 * 2) Copy this file to $IP/extensions.
 * Note: $IP is your MediaWiki install dir.
 * 1) Enable the extension by adding this line to your LocalSettings.php: require_once('extensions/MetaDescriptionTag.php');

Usage
Once installed, editors of your wiki can add a &lt;meta&gt; description tag by adding the following to the article text: &lt;metadesc content="PageDescription" /&gt; Where PageDescription would be replaced by the desired meta description.

For example, the above would become: &lt;meta name="description" content="PageDescription" /&gt;

MetaDescriptionTag.php
<?php /* * MetaDescriptionTag.php - A MediaWiki tag extension for adding description to a page. * @author Joshua C. Lerner * @version 0.1 * @copyright Copyright (C) 2007 Joshua C. Lerner, Jim R. Wilson * @license The MIT License - http://www.opensource.org/licenses/mit-license.php * --- * Description: *    This is a MediaWiki extension which adds support for injecting a description tag *    into the page header. * Requirements: *    MediaWiki 1.6.x, 1.8.x, 1.9.x or higher *    PHP 4.x, 5.x or higher * Installation: *    1. Drop this script (MetaDescriptionTag.php) in $IP/extensions *        Note: $IP is your MediaWiki install dir. *    2. Enable the extension by adding this line to your LocalSettings.php: *        require_once('extensions/MetaDescriptionTag.php'); * Usage: *    Once installed, you may utilize MetaDescriptionTag by adding the tag to articles: *         * Version Notes: *    version 0.1: *        Initial release. * --- * Copyright (c) 2007 Joshua C. Lerner, Jim R. Wilson *  * 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. * --- */ if (defined('MEDIAWIKI')) { $wgExtensionCredits['parserhook'][] = array(    'name'=>'MetaDescriptionTag',     'author'=>'Joshua C. Lerner - joshua&lt;_at_&gt;busytonight.com',     'url'=>'http://www.mediawiki.org/wiki/Extension:MetaDescriptionTag',     'description'=>'Tag to inject meta description into page header.',     'version'=>'0.1' ); $wgExtensionFunctions[] = 'setupMetaDescriptionTagParserHooks'; /** * Sets up the MetaDescriptionTag Parser hook and system messages */ function setupMetaDescriptionTagParserHooks { global $wgParser, $wgMessageCache; $wgParser->setHook( 'metadesc', 'renderMetaDescriptionTag' ); $wgMessageCache->addMessage(        'metadescriptiontag-missing-content',          'Error: &lt;metadescn&gt; tag must contain a &quot;content&quot; attribute.'     ); } /** * Renders the tag. * @param String $text Incoming 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 renderMetaDescriptionTag( $text, $params = array, &$parser ) { # Short-circuit with error message if content is not specified. if (!isset($params['content'])) { return '<div class="errorbox"&gt;'. wfMsgForContent('metadescriptiontag-missing-content'). '</div&gt;'; }    # Return encoded content return ''; } $wgHooks['OutputPageBeforeHTML'][] = 'insertMetaDescription'; /** * Adds the description to document head. * Usage: $wgHooks['OutputPageBeforeHTML'][] = 'insertMetaDescription'; * @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 insertMetaDescription( $out, $text ) { # Extract meta description if (preg_match_all( '//m', $text, $matches)===false    ) return true; $data = $matches[1]; # Merge description data into OutputPage as meta tag foreach ($data AS $item) { $content = @base64_decode($item); if ($content) $out->addMeta( 'description', $content ); }    return true; } } # End MW env wrapper // ?>
 * 1) Confirm MW environment
 * 1) Credits
 * 1) Add Extension Function
 * 1) Attach post-parser hook to extract metadata and alter headers