User:Ilmari Karonen/canonical

From NetHackWiki
Jump to: navigation, search

Quick and dirty MediaWiki hook to add rel=canonical links to all normal page views:

$wgHooks['ArticleViewHeader'][] = 'addCanonicalLinks';
function addCanonicalLinks ( &$article ) {
	if ( $article->getOldID() ) return true;  // permalink to old version or diff
	if ( $article->getPage()->getID() == 0 ) return true;  // redlink, shared image, etc.
 
	$context = $article->getContext();
	if ( $context->getRequest()->getVal( 'redirect' ) == 'no' ) return true;  // redirect
 
	// XXX: MediaWiki core only calls setCanonicalUrl() for shared images and redirects,
	// and for the latter case it sets the exact same canonical URL as we set here.
	$context->getOutput()->setCanonicalUrl( $article->getTitle()->getLocalURL() );
	return true;
}