KLMeta

Jump to: navigation, search

This extension allows the addition of HEAD meta tags through the use of the magic word {{#metatags:}}.

Usage

Simply insert the keyword and a pipe-delimited list of name-value pairs. See Help:KLMeta for examples.

Installation

To install:

  • Copy the code from the code block at the end of this page to the file $IP/extensions/KLMeta.php
  • Add this line to LocalSettings.php: require_once("$IP/extensions/KLMeta.php");

Code

<?php
/**
 * KLMeta.php
 * This extension inserts a form mailer into a page
 * written by Kennylucius http://kennylucius.com/
 * LocalSettings.php file:
 * require_once("$IP/extensions/KLMeta.php");
 */

if(! defined( 'MEDIAWIKI' ) ) {
    die("This is not a valid entry point.\n");
}

$wgExtensionCredits['parserhook'][] = array(
	 'name' => 'KLMeta',
	 'version' => '0.1',
	 'author' =>'Kenny Lucius', 
	 'url' => 'http://kennylucius.com/KLMeta',
	 'description' => 'Allows insertion of HEAD meta tags.'
	 );

// function to call to initialize extension
$wgExtensionFunctions[] = 'wfKLMeta_Extension';

// Add a hook to initialise the magic word
$wgHooks['LanguageGetMagic'][] = 'wfKLMeta_Magic';

//this hook actually inserts the accumulated metas
$wgHooks['BeforePageDisplay'][] = 'KLMeta_BeforePageDisplay';

function wfKLMeta_Extension() {
	global $wgParser;
	$wgParser->setFunctionHook( 'metatags', "KLMeta_Parse");
}
function wfKLMeta_Magic(&$magicWords, $langCode) {
	$magicWords['metatags'] = array( 0, 'metatags' );
	return true;
}

//metas are stored in this array
$KLMetatagArray = array();

# Callback function that accumulates metas and returns empty string
function KLMeta_Parse() {
	global $KLMetatagArray;
	$args = func_get_args();
	$parser = array_shift($args);
	foreach($args as $arg) {
		$fld = $val = '';
		//decode first to prevent double encoding when cleaning input
		$a =  explode('=', html_entity_decode($arg,ENT_QUOTES), 2);
		if(! isset($a[0]) || ! $a[0]) continue;
		if(! isset($a[1])) {
			$val = '';
		}
		else {
			$val = $a[1]; 
		}
		$fld = $a[0];
		
		//tags that accumulate
		if($fld == 'keywords') {
			$KLMetatagArray[$fld][] =  KLMeta_clean($val);
		}
		else {
			$KLMetatagArray[$fld] =  KLMeta_clean($val);
		}
	}
	//return print_r($args, true) . print_r($KLMetatagArray, true) .'KLMeta_Parse';
	return '';
}

//inserts the accumulated metas
function KLMeta_BeforePageDisplay($out) {
	global $KLMetatagArray;
//	print_r($KLMetatagArray);
	foreach($KLMetatagArray as $name=>$val) {
		if($name == 'keywords') {
			$out->mKeywords = $val; 
		}
		elseif(is_array($val)) {
			$v = implode(',',$val); 
			$out->addMeta( $name, $v );
		}
		else {
			$out->addMeta( $name, $val);
		}
	}
	return true;
}

function KLMeta_clean($text) {
	$ret = htmlspecialchars(str_replace( '"',  "''", $text), ENT_COMPAT,'UTF-8') ;
	return $ret;
}
 
Personal tools