Open main menu

Extension:CategoryMagicWords/CategoryMagicWords.php

< Extension:CategoryMagicWords
<?php
/**
 * Extension CategoryMagicWords - Adds magic words for changing the behaviour of category pages.
 * @version 0.1.3 - 2012/12/29
 *
 * @link https://www.mediawiki.org/wiki/Extension:SemanticExtraSpecialProperties Documentation
 *
 * @file CategoryMagicWords.php
 * @ingroup Extensions
 * @package MediaWiki
 * @author Leo Wallentin (Rotsee)
 * @license http://www.opensource.org/licenses/BSD-2-Clause BSD
 */

// Check to see if we are being called as an extension or directly
if( !defined( 'MEDIAWIKI' ) ) {
        die( 'This file is an extension to MediaWiki and thus not a valid entry point.' );
}

// Register this extension on Special:Version
$wgExtensionCredits['parserhook'][] = array(
	'name' => 'CategoryMagicWords',
	'descriptionmsg' => 'cmw-desc',
	'author' => array( '[http://xn--ssongsmat-v2a.nu Leo Wallentin]' ),
	'url' => 'https://www.mediawiki.org/wiki/Extension:CategoryMagicWords', 
	'version' => '0.1.3',
	'path' => __FILE__,
);

// Tell the whereabouts of files
$dir = __DIR__;

// Allow translations for this extension
$wgExtensionMessagesFiles['CategoryMagicWords'] = $dir . '/CategoryMagicWords.i18n.php';

// Define parameter for passing found magic words around
global $wgCMWoptions;
$wgCMWoptions = 0;

define( 'CATEGORYNOPAGES', 1 );
define( 'CATEGORYNOSUBCATS', 2 );

// Add hooks for magic word
$wgHooks['ArticleFromTitle'][] = 'efCMW';
$wgHooks['LanguageGetMagic'][] = 'efCMW_Magic';
$wgHooks['ParserBeforeStrip'][] = 'efCMW_ParserBeforeStrip';

// Add hooks for parser functions
$wgHooks['ParserFirstCallInit'][] = 'efCMW_Initialize';
$wgHooks['LanguageGetMagic'][] = 'efCMW_RegisterMagicWords';

// Register parser functions
function efCMW_Initialize( &$parser ) {
 	$parser->setFunctionHook('parentcategories', 'efCMW_Parentcategories' );
	return true; 
}

// Add magic words for parser functions
function efCMW_RegisterMagicWords( &$magicWords, $langCode ) {
	$magicWords['parentcategories'] = array(0, 'parentcategories');
	return true;
}

function efCMW_Parentcategories( $parser, $param1 = null, $lcfirst ) {
	if ( !$param1 ) {
		$t = $parser->getTitle();
	} else {
		$t = Title::newFromText( $param1 );
	}

	$s = '';
	if ( $t->exists() ) {
		$c = array_keys( $t->getParentCategories() );
		foreach ( $c as &$parent ) {
			$ct = Title::newFromText( $parent );
			$ctt = $ct->getText();
			if ($lcfirst)
				$ctt = lcfirst( $ctt );
			$parent = "[[:$parent|$ctt]]";
		}
		$s = implode ( $c, ',' );
	}
	return $s;
}

// Setup magic words
function efCMW_Magic ( &$magicWords, $langCode ) {
	$magicWords['CATEGORYNOPAGES']    = array( 1, '__CATEGORYNOPAGES__' );
	$magicWords['CATEGORYNOSUBCATS']    = array( 1, '__CATEGORYNOSUBCATS__' );
	return true;
}

// Detect magic words in text
function efCMW_ParserBeforeStrip ( $parser, $text, $stripState ) {

	global $wgCMWoptions;
        if ( MagicWord::get( 'CATEGORYNOPAGES' )->matchAndRemove( $text ) ) {
		$wgCMWoptions |= CATEGORYNOPAGES;
	}

        if ( MagicWord::get( 'CATEGORYNOSUBCATS' )->matchAndRemove( $text ) ) {
		$wgCMWoptions |= CATEGORYNOSUBCATS;
	}
        return true;

}

// Extend CategoryPage (extends Article) to setup our own CategoryViewer
class CategoryPageCMW extends CategoryPage {
	protected $mCategoryViewerClass = 'CategoryViewerCMW';
}

class CategoryViewerCMW extends CategoryViewer { 

	function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
		global $wgCMWoptions;
		if ( $wgCMWoptions & CATEGORYNOPAGES )
			return;
		parent::addPage( $title, $sortkey, $pageLength, $isRedirect );
	}

	function addSubcategoryObject( $cat, $sortkey, $pageLength ) {
		global $wgCMWoptions;
		if ( $wgCMWoptions & CATEGORYNOSUBCATS )
			return;

		parent::addSubcategoryObject( $cat, $sortkey, $pageLength );
	}
}

function efCMW(&$title, &$article) {

	//Create our own article object if this is a category
	if ( NS_CATEGORY == $title->getNamespace() ) { //TODO: ->inNamespace fr. 1.19 //TODO Also support SMW concepts?
		$article = new CategoryPageCMW( $title );
	}

	return true;
}