User:Ilmari Karonen/MetaKeywords

<?php

$wgExtensionCredits['other'][] = array(
	'path' => __FILE__,
	'name' => "MetaKeywords",
	'description' => "Adds back the automatically generated meta keywords removed in r53482.",
	'version' => 1.0,
	'author' => "Ilmari Karonen",
	'url' => "http://www.mediawiki.org/wiki/User:Ilmari_Karonen/MetaKeywords",
);

$wgHooks['OutputPageParserOutput'][] = 'addMetaKeywords';

/**
 * This function takes the title (first item of mGoodLinks), categories,
 * existing and broken links for the page
 * and uses the first 10 of them for META keywords
 *
 * It's based on the code Brion removed in r53482, converted into an
 * OutputPageParserOutput hook by replacing $this with $out.  See bug
 * 19761 on bugzilla for details.
 *
 * @param OutputPage $out
 * @param ParserOutput $parserOutput
 */
function addMetaKeywords( $out, $parserOutput ) {
	global $wgContLang;
	// Get an array of keywords if there are more than one
	// variant of the site language
	$text = $wgContLang->autoConvertToAllVariants( $out->getTitle()->getPrefixedText());
	// array_values: We needn't to merge variant's code name
	// into $out->mKeywords;
	// array_unique: We should insert a keyword just for once
	if( is_array( $text ))
		$text = array_unique( array_values( $text ));
	$out->addKeyword( $text );
	$count = 1;
	$links2d =& $parserOutput->getLinks();
	if ( !is_array( $links2d ) ) {
		return;
	}
	foreach ( $links2d as $dbkeys ) {
		foreach( $dbkeys as $dbkey => $unused ) {
			$dbkey = $wgContLang->autoConvertToAllVariants( $dbkey );
			if( is_array( $dbkey ))
				$dbkey = array_unique( array_values( $dbkey ));
			$out>addKeyword( $dbkey );
			if ( ++$count > 10 ) {
				break 2;
			}
		}
	}
}