Documentation, stylize, remove some global dependencies

This commit is contained in:
Sam Reed 2012-02-09 01:23:31 +00:00
parent 4383d6d598
commit 079968308b
Notes: Sam Reed 2012-02-09 01:23:31 +00:00
4 changed files with 292 additions and 110 deletions

View file

@ -7,11 +7,21 @@ class CategoryTreeCategoryPage extends CategoryPage {
class CategoryTreeCategoryViewer extends CategoryViewer { class CategoryTreeCategoryViewer extends CategoryViewer {
var $child_cats; var $child_cats;
/**
* @var CategoryTree
*/
var $categorytree;
/**
* @return CategoryTree
*/
function getCategoryTree() { function getCategoryTree() {
global $wgOut, $wgCategoryTreeCategoryPageOptions, $wgCategoryTreeForceHeaders; global $wgOut, $wgCategoryTreeCategoryPageOptions, $wgCategoryTreeForceHeaders;
if ( ! isset( $this->categorytree ) ) { if ( !isset( $this->categorytree ) ) {
if ( !$wgCategoryTreeForceHeaders ) CategoryTree::setHeaders( $wgOut ); if ( !$wgCategoryTreeForceHeaders ) {
CategoryTree::setHeaders( $wgOut );
}
$this->categorytree = new CategoryTree( $wgCategoryTreeCategoryPageOptions ); $this->categorytree = new CategoryTree( $wgCategoryTreeCategoryPageOptions );
} }
@ -21,6 +31,10 @@ class CategoryTreeCategoryViewer extends CategoryViewer {
/** /**
* Add a subcategory to the internal lists * Add a subcategory to the internal lists
* @param $cat Category
* @param $sortkey
* @param $pageLength
* @return
*/ */
function addSubcategoryObject( Category $cat, $sortkey, $pageLength ) { function addSubcategoryObject( Category $cat, $sortkey, $pageLength ) {
global $wgRequest; global $wgRequest;
@ -28,7 +42,8 @@ class CategoryTreeCategoryViewer extends CategoryViewer {
$title = $cat->getTitle(); $title = $cat->getTitle();
if ( $wgRequest->getCheck( 'notree' ) ) { if ( $wgRequest->getCheck( 'notree' ) ) {
return parent::addSubcategoryObject( $cat, $sortkey, $pageLength ); parent::addSubcategoryObject( $cat, $sortkey, $pageLength );
return;
} }
$tree = $this->getCategoryTree(); $tree = $this->getCategoryTree();

View file

@ -217,6 +217,10 @@ function efCategoryTree() {
$wgHooks['ResourceLoaderGetConfigVars'][] = 'efCategoryTreeGetConfigVars'; $wgHooks['ResourceLoaderGetConfigVars'][] = 'efCategoryTreeGetConfigVars';
} }
/**
* @param $parser Parser
* @return bool
*/
function efCategoryTreeSetHooks( $parser ) { function efCategoryTreeSetHooks( $parser ) {
$parser->setHook( 'categorytree' , 'efCategoryTreeParserHook' ); $parser->setHook( 'categorytree' , 'efCategoryTreeParserHook' );
$parser->setFunctionHook( 'categorytree' , 'efCategoryTreeParserFunction' ); $parser->setFunctionHook( 'categorytree' , 'efCategoryTreeParserFunction' );
@ -229,6 +233,10 @@ function efCategoryTreeSetHooks( $parser ) {
* If $enc is not given, '' is asumed, which simulates the old call interface, * If $enc is not given, '' is asumed, which simulates the old call interface,
* namely, only providing the mode name or number. * namely, only providing the mode name or number.
* This loads CategoryTreeFunctions.php and calls CategoryTree::ajax() * This loads CategoryTreeFunctions.php and calls CategoryTree::ajax()
* @param $category
* @param $options array
* @param $enc string
* @return AjaxResponse|bool
*/ */
function efCategoryTreeAjaxWrapper( $category, $options = array(), $enc = '' ) { function efCategoryTreeAjaxWrapper( $category, $options = array(), $enc = '' ) {
global $wgCategoryTreeHTTPCache, $wgSquidMaxage, $wgUseSquid; global $wgCategoryTreeHTTPCache, $wgSquidMaxage, $wgUseSquid;
@ -254,8 +262,10 @@ function efCategoryTreeAjaxWrapper( $category, $options = array(), $enc = '' ) {
/** /**
* Internal function to cap depth * Internal function to cap depth
* @param $mode
* @param $depth
* @return int|mixed
*/ */
function efCategoryTreeCapDepth( $mode, $depth ) { function efCategoryTreeCapDepth( $mode, $depth ) {
global $wgCategoryTreeMaxDepth; global $wgCategoryTreeMaxDepth;
@ -280,6 +290,8 @@ function efCategoryTreeCapDepth( $mode, $depth ) {
/** /**
* Entry point for the {{#categorytree}} tag parser function. * Entry point for the {{#categorytree}} tag parser function.
* This is a wrapper around efCategoryTreeParserHook * This is a wrapper around efCategoryTreeParserHook
* @param $parser Parser
* @return array|string
*/ */
function efCategoryTreeParserFunction( $parser ) { function efCategoryTreeParserFunction( $parser ) {
$params = func_get_args(); $params = func_get_args();
@ -297,8 +309,7 @@ function efCategoryTreeParserFunction( $parser ) {
if ( preg_match( '/^\s*(\S.*?)\s*=\s*(.*?)\s*$/', $p, $m ) ) { if ( preg_match( '/^\s*(\S.*?)\s*=\s*(.*?)\s*$/', $p, $m ) ) {
$k = $m[1]; $k = $m[1];
$v = preg_replace( '/^"\s*(.*?)\s*"$/', '$1', $m[2] ); // strip any quotes enclusing the value $v = preg_replace( '/^"\s*(.*?)\s*"$/', '$1', $m[2] ); // strip any quotes enclusing the value
} } else {
else {
$k = trim( $p ); $k = trim( $p );
$v = true; $v = true;
} }
@ -314,13 +325,16 @@ function efCategoryTreeParserFunction( $parser ) {
/** /**
* Hook implementation for injecting a category tree into the sidebar. * Hook implementation for injecting a category tree into the sidebar.
* Registered automatically if $wgCategoryTreeSidebarRoot is set to a category name. * Registered automatically if $wgCategoryTreeSidebarRoot is set to a category name.
* @param $skin
* @param $tpl SkinTemplate
* @return bool
*/ */
function efCategoryTreeSkinTemplateOutputPageBeforeExec( $skin, $tpl ) { function efCategoryTreeSkinTemplateOutputPageBeforeExec( $skin, $tpl ) {
global $wgCategoryTreeSidebarRoot, $wgCategoryTreeSidebarOptions; global $wgCategoryTreeSidebarRoot, $wgCategoryTreeSidebarOptions;
$html = efCategoryTreeParserHook( $wgCategoryTreeSidebarRoot, $wgCategoryTreeSidebarOptions ); $html = efCategoryTreeParserHook( $wgCategoryTreeSidebarRoot, $wgCategoryTreeSidebarOptions );
if ( $html ) { if ( $html ) {
$tpl->data['sidebar']['categorytree-portlet'] = $html; // requires MW 1.13, r36917 $tpl->data['sidebar']['categorytree-portlet'] = $html;
} }
return true; return true;
@ -329,6 +343,11 @@ function efCategoryTreeSkinTemplateOutputPageBeforeExec( $skin, $tpl ) {
/** /**
* Entry point for the <categorytree> tag parser hook. * Entry point for the <categorytree> tag parser hook.
* This loads CategoryTreeFunctions.php and calls CategoryTree::getTag() * This loads CategoryTreeFunctions.php and calls CategoryTree::getTag()
* @param $cat
* @param $argv
* @param $parser Parser
* @param $allowMissing bool
* @return bool|string
*/ */
function efCategoryTreeParserHook( $cat, $argv, $parser = null, $allowMissing = false ) { function efCategoryTreeParserHook( $cat, $argv, $parser = null, $allowMissing = false ) {
global $wgOut; global $wgOut;
@ -356,9 +375,12 @@ function efCategoryTreeParserHook( $cat, $argv, $parser = null, $allowMissing =
} }
/** /**
* Hook callback that injects messages and things into the <head> tag * Hook callback that injects messages and things into the <head> tag
* Does nothing if $parserOutput->mCategoryTreeTag is not set * Does nothing if $parserOutput->mCategoryTreeTag is not set
*/ * @param $outputPage OutputPage
* @param $parserOutput ParserOutput
* @return bool
*/
function efCategoryTreeParserOutput( $outputPage, $parserOutput ) { function efCategoryTreeParserOutput( $outputPage, $parserOutput ) {
if ( !empty( $parserOutput->mCategoryTreeTag ) ) { if ( !empty( $parserOutput->mCategoryTreeTag ) ) {
CategoryTree::setHeaders( $outputPage ); CategoryTree::setHeaders( $outputPage );
@ -371,6 +393,7 @@ function efCategoryTreeParserOutput( $outputPage, $parserOutput ) {
* *
* @param $title Title * @param $title Title
* @param $article Article * @param $article Article
* @return bool
*/ */
function efCategoryTreeArticleFromTitle( $title, &$article ) { function efCategoryTreeArticleFromTitle( $title, &$article ) {
if ( $title->getNamespace() == NS_CATEGORY ) { if ( $title->getNamespace() == NS_CATEGORY ) {
@ -381,6 +404,10 @@ function efCategoryTreeArticleFromTitle( $title, &$article ) {
/** /**
* OutputPageMakeCategoryLinks hook, override category links * OutputPageMakeCategoryLinks hook, override category links
* @param $out
* @param $categories
* @param $links
* @return bool
*/ */
function efCategoryTreeOutputPageMakeCategoryLinks( $out, &$categories, &$links ) { function efCategoryTreeOutputPageMakeCategoryLinks( $out, &$categories, &$links ) {
global $wgCategoryTreePageCategoryOptions; global $wgCategoryTreePageCategoryOptions;
@ -392,6 +419,12 @@ function efCategoryTreeOutputPageMakeCategoryLinks( $out, &$categories, &$links
return false; return false;
} }
/**
* @param $skin
* @param $links
* @param $result
* @return bool
*/
function efCategoryTreeSkinJoinCategoryLinks( $skin, &$links, &$result ) { function efCategoryTreeSkinJoinCategoryLinks( $skin, &$links, &$result ) {
$embed = '<div class="CategoryTreeCategoryBarItem">'; $embed = '<div class="CategoryTreeCategoryBarItem">';
$pop = '</div>'; $pop = '</div>';
@ -402,6 +435,10 @@ function efCategoryTreeSkinJoinCategoryLinks( $skin, &$links, &$result ) {
return false; return false;
} }
/**
* @param $vars
* @return bool
*/
function efCategoryTreeGetConfigVars( &$vars ) { function efCategoryTreeGetConfigVars( &$vars ) {
global $wgCategoryTreeCategoryPageOptions; global $wgCategoryTreeCategoryPageOptions;
@ -409,4 +446,4 @@ function efCategoryTreeGetConfigVars( &$vars ) {
$ct = new CategoryTree( $wgCategoryTreeCategoryPageOptions ); $ct = new CategoryTree( $wgCategoryTreeCategoryPageOptions );
$vars['wgCategoryTreePageCategoryOptions'] = $ct->getOptionsAsJsStructure(); $vars['wgCategoryTreePageCategoryOptions'] = $ct->getOptionsAsJsStructure();
return true; return true;
} }

View file

@ -19,6 +19,10 @@ class CategoryTree {
var $mIsAjaxRequest = false; var $mIsAjaxRequest = false;
var $mOptions = array(); var $mOptions = array();
/**
* @param $options array
* @param $ajax bool
*/
function __construct( $options, $ajax = false ) { function __construct( $options, $ajax = false ) {
global $wgCategoryTreeDefaultOptions; global $wgCategoryTreeDefaultOptions;
@ -56,38 +60,56 @@ class CategoryTree {
} }
} }
/**
* @param $name string
* @return mixed
*/
function getOption( $name ) { function getOption( $name ) {
return $this->mOptions[$name]; return $this->mOptions[$name];
} }
/**
* @return bool
*/
function isInverse( ) { function isInverse( ) {
return $this->getOption( 'mode' ) == CT_MODE_PARENTS; return $this->getOption( 'mode' ) == CT_MODE_PARENTS;
} }
/**
* @param $nn
* @return array|bool
*/
static function decodeNamespaces( $nn ) { static function decodeNamespaces( $nn ) {
global $wgContLang; global $wgContLang;
if ( !$nn ) if ( !$nn ) {
return false; return false;
}
if ( !is_array( $nn ) ) if ( !is_array( $nn ) ) {
$nn = preg_split( '![\s#:|]+!', $nn ); $nn = preg_split( '![\s#:|]+!', $nn );
}
$namespaces = array(); $namespaces = array();
foreach ( $nn as $n ) { foreach ( $nn as $n ) {
if ( is_int( $n ) ) { if ( is_int( $n ) ) {
$ns = $n; $ns = $n;
} } else {
else {
$n = trim( $n ); $n = trim( $n );
if ( $n === '' ) continue; if ( $n === '' ) {
continue;
}
$lower = strtolower( $n ); $lower = strtolower( $n );
if ( is_numeric( $n ) ) $ns = (int)$n; if ( is_numeric( $n ) ) {
elseif ( $n == '-' || $n == '_' || $n == '*' || $lower == 'main' ) $ns = NS_MAIN; $ns = (int)$n;
else $ns = $wgContLang->getNsIndex( $n ); } elseif ( $n == '-' || $n == '_' || $n == '*' || $lower == 'main' ) {
$ns = NS_MAIN;
} else {
$ns = $wgContLang->getNsIndex( $n );
}
} }
if ( is_int( $ns ) ) { if ( is_int( $ns ) ) {
@ -99,61 +121,111 @@ class CategoryTree {
return $namespaces; return $namespaces;
} }
/**
* @param $mode
* @return int|string
*/
static function decodeMode( $mode ) { static function decodeMode( $mode ) {
global $wgCategoryTreeDefaultOptions; global $wgCategoryTreeDefaultOptions;
if ( is_null( $mode ) ) return $wgCategoryTreeDefaultOptions['mode']; if ( is_null( $mode ) ) {
if ( is_int( $mode ) ) return $mode; return $wgCategoryTreeDefaultOptions['mode'];
}
if ( is_int( $mode ) ) {
return $mode;
}
$mode = trim( strtolower( $mode ) ); $mode = trim( strtolower( $mode ) );
if ( is_numeric( $mode ) ) return (int)$mode; if ( is_numeric( $mode ) ) {
return (int)$mode;
}
if ( $mode == 'all' ) $mode = CT_MODE_ALL; if ( $mode == 'all' ) {
elseif ( $mode == 'pages' ) $mode = CT_MODE_PAGES; $mode = CT_MODE_ALL;
elseif ( $mode == 'categories' || $mode == 'sub' ) $mode = CT_MODE_CATEGORIES; } elseif ( $mode == 'pages' ) {
elseif ( $mode == 'parents' || $mode == 'super' || $mode == 'inverse' ) $mode = CT_MODE_PARENTS; $mode = CT_MODE_PAGES;
elseif ( $mode == 'default' ) $mode = $wgCategoryTreeDefaultOptions['mode']; } elseif ( $mode == 'categories' || $mode == 'sub' ) {
$mode = CT_MODE_CATEGORIES;
} elseif ( $mode == 'parents' || $mode == 'super' || $mode == 'inverse' ) {
$mode = CT_MODE_PARENTS;
} elseif ( $mode == 'default' ) {
$mode = $wgCategoryTreeDefaultOptions['mode'];
}
return (int)$mode; return (int)$mode;
} }
/** /**
* Helper function to convert a string to a boolean value. * Helper function to convert a string to a boolean value.
* Perhaps make this a global function in MediaWiki proper * Perhaps make this a global function in MediaWiki proper
*/ * @param $value
* @return bool|null|string
*/
static function decodeBoolean( $value ) { static function decodeBoolean( $value ) {
if ( is_null( $value ) ) return null; if ( is_null( $value ) ) {
if ( is_bool( $value ) ) return $value; return null;
if ( is_int( $value ) ) return ( $value > 0 ); }
if ( is_bool( $value ) ) {
return $value;
}
if ( is_int( $value ) ) {
return ( $value > 0 );
}
$value = trim( strtolower( $value ) ); $value = trim( strtolower( $value ) );
if ( is_numeric( $value ) ) return ( (int)$value > 0 ); if ( is_numeric( $value ) ) {
return ( (int)$value > 0 );
}
if ( $value == 'yes' || $value == 'y' || $value == 'true' || $value == 't' || $value == 'on' ) return true; if ( $value == 'yes' || $value == 'y' || $value == 'true' || $value == 't' || $value == 'on' ) {
elseif ( $value == 'no' || $value == 'n' || $value == 'false' || $value == 'f' || $value == 'off' ) return false; return true;
elseif ( $value == 'null' || $value == 'default' || $value == 'none' || $value == 'x' ) return null; } elseif ( $value == 'no' || $value == 'n' || $value == 'false' || $value == 'f' || $value == 'off' ) {
else return false; return false;
} elseif ( $value == 'null' || $value == 'default' || $value == 'none' || $value == 'x' ) {
return null;
} else {
return false;
}
} }
/**
* @param $value
* @return int|string
*/
static function decodeHidePrefix( $value ) { static function decodeHidePrefix( $value ) {
global $wgCategoryTreeDefaultOptions; global $wgCategoryTreeDefaultOptions;
if ( is_null( $value ) ) return $wgCategoryTreeDefaultOptions['hideprefix']; if ( is_null( $value ) ) {
if ( is_int( $value ) ) return $value; return $wgCategoryTreeDefaultOptions['hideprefix'];
if ( $value === true ) return CT_HIDEPREFIX_ALWAYS; }
if ( $value === false ) return CT_HIDEPREFIX_NEVER; if ( is_int( $value ) ) {
return $value;
}
if ( $value === true ) {
return CT_HIDEPREFIX_ALWAYS;
}
if ( $value === false ) {
return CT_HIDEPREFIX_NEVER;
}
$value = trim( strtolower( $value ) ); $value = trim( strtolower( $value ) );
if ( $value == 'yes' || $value == 'y' || $value == 'true' || $value == 't' || $value == 'on' ) return CT_HIDEPREFIX_ALWAYS; if ( $value == 'yes' || $value == 'y' || $value == 'true' || $value == 't' || $value == 'on' ) {
elseif ( $value == 'no' || $value == 'n' || $value == 'false' || $value == 'f' || $value == 'off' ) return CT_HIDEPREFIX_NEVER; return CT_HIDEPREFIX_ALWAYS;
// elseif ( $value == 'null' || $value == 'default' || $value == 'none' || $value == 'x' ) return $wgCategoryTreeDefaultOptions['hideprefix']; } elseif ( $value == 'no' || $value == 'n' || $value == 'false' || $value == 'f' || $value == 'off' ) {
elseif ( $value == 'always' ) return CT_HIDEPREFIX_ALWAYS; return CT_HIDEPREFIX_NEVER;
elseif ( $value == 'never' ) return CT_HIDEPREFIX_NEVER; } elseif ( $value == 'always' ) {
elseif ( $value == 'auto' ) return CT_HIDEPREFIX_AUTO; return CT_HIDEPREFIX_ALWAYS;
elseif ( $value == 'categories' || $value == 'category' || $value == 'smart' ) return CT_HIDEPREFIX_CATEGORIES; } elseif ( $value == 'never' ) {
else return $wgCategoryTreeDefaultOptions['hideprefix']; return CT_HIDEPREFIX_NEVER;
} elseif ( $value == 'auto' ) {
return CT_HIDEPREFIX_AUTO;
} elseif ( $value == 'categories' || $value == 'category' || $value == 'smart' ) {
return CT_HIDEPREFIX_CATEGORIES;
} else {
return $wgCategoryTreeDefaultOptions['hideprefix'];
}
} }
/** /**
@ -179,6 +251,12 @@ class CategoryTree {
return $json; return $json;
} }
/**
* @param $options
* @param $enc
* @return mixed
* @throws MWException
*/
static function encodeOptions( $options, $enc ) { static function encodeOptions( $options, $enc ) {
if ( $enc == 'mode' || $enc == '' ) { if ( $enc == 'mode' || $enc == '' ) {
$opt = $options['mode']; $opt = $options['mode'];
@ -192,6 +270,12 @@ class CategoryTree {
return $opt; return $opt;
} }
/**
* @param $options
* @param $enc
* @return array|mixed
* @throws MWException
*/
static function decodeOptions( $options, $enc ) { static function decodeOptions( $options, $enc ) {
if ( $enc == 'mode' || $enc == '' ) { if ( $enc == 'mode' || $enc == '' ) {
$opt = array( "mode" => $options ); $opt = array( "mode" => $options );
@ -206,6 +290,10 @@ class CategoryTree {
return $opt; return $opt;
} }
/**
* @param $depth null
* @return string
*/
function getOptionsAsCacheKey( $depth = null ) { function getOptionsAsCacheKey( $depth = null ) {
$key = ""; $key = "";
@ -214,10 +302,16 @@ class CategoryTree {
$key .= $k . ':' . $v . ';'; $key .= $k . ':' . $v . ';';
} }
if ( !is_null( $depth ) ) $key .= ";depth=" . $depth; if ( !is_null( $depth ) ) {
$key .= ";depth=" . $depth;
}
return $key; return $key;
} }
/**
* @param $depthnull
* @return mixed
*/
function getOptionsAsJsStructure( $depth = null ) { function getOptionsAsJsStructure( $depth = null ) {
if ( !is_null( $depth ) ) { if ( !is_null( $depth ) ) {
$opt = $this->mOptions; $opt = $this->mOptions;
@ -230,10 +324,17 @@ class CategoryTree {
return $s; return $s;
} }
function getOptionsAsJsString( $depth = NULL ) { /**
* @param $depth null
* @return String
*/
function getOptionsAsJsString( $depth = null ) {
return Xml::escapeJsString( $this->getOptionsAsJsStructure( $depth ) ); return Xml::escapeJsString( $this->getOptionsAsJsStructure( $depth ) );
} }
/**
* @return string
*/
function getOptionsAsUrlParameters() { function getOptionsAsUrlParameters() {
$u = ''; $u = '';
@ -246,9 +347,12 @@ class CategoryTree {
} }
/** /**
* Ajax call. This is called by efCategoryTreeAjaxWrapper, which is used to * Ajax call. This is called by efCategoryTreeAjaxWrapper, which is used to
* load CategoryTreeFunctions.php on demand. * load CategoryTreeFunctions.php on demand.
*/ * @param $category
* @param $depth int
* @return AjaxResponse|bool
*/
function ajax( $category, $depth = 1 ) { function ajax( $category, $depth = 1 ) {
global $wgLang, $wgContLang, $wgRenderHashAppend; global $wgLang, $wgContLang, $wgRenderHashAppend;
$title = self::makeTitle( $category ); $title = self::makeTitle( $category );
@ -266,7 +370,12 @@ class CategoryTree {
'page_title' => $dbkey, 'page_title' => $dbkey,
), __METHOD__ ); ), __METHOD__ );
$mckey = wfMemcKey( "categorytree(" . $this->getOptionsAsCacheKey( $depth ) . ")", $dbkey, $wgLang->getCode(), $wgContLang->getExtraHashOptions(), $wgRenderHashAppend ); $mckey = wfMemcKey(
"categorytree(" . $this->getOptionsAsCacheKey( $depth ) . ")",
$dbkey, $wgLang->getCode(),
$wgContLang->getExtraHashOptions(),
$wgRenderHashAppend
);
$response = new AjaxResponse(); $response = new AjaxResponse();
@ -368,9 +477,11 @@ class CategoryTree {
} }
/** /**
* Returns a string with an HTML representation of the children of the given category. * Returns a string with an HTML representation of the children of the given category.
* @param $title Title * @param $title Title
*/ * @param $depth int
* @return string
*/
function renderChildren( $title, $depth = 1 ) { function renderChildren( $title, $depth = 1 ) {
global $wgCategoryTreeMaxChildren, $wgCategoryTreeUseCategoryTable; global $wgCategoryTreeMaxChildren, $wgCategoryTreeUseCategoryTable;
@ -459,9 +570,10 @@ class CategoryTree {
} }
/** /**
* Returns a string with an HTML representation of the parents of the given category. * Returns a string with an HTML representation of the parents of the given category.
* @var $title Title * @param $title Title
*/ * @return string
*/
function renderParents( $title ) { function renderParents( $title ) {
global $wgCategoryTreeMaxChildren; global $wgCategoryTreeMaxChildren;
@ -513,9 +625,12 @@ class CategoryTree {
} }
/** /**
* Returns a string with a HTML represenation of the given page. * Returns a string with a HTML represenation of the given page.
* $title must be a Title object * @param $title Title
*/ * @param int $children
* @param bool $loadchildren
* @return string
*/
function renderNode( $title, $children = 0, $loadchildren = false ) { function renderNode( $title, $children = 0, $loadchildren = false ) {
global $wgCategoryTreeUseCategoryTable; global $wgCategoryTreeUseCategoryTable;
@ -529,9 +644,14 @@ class CategoryTree {
} }
/** /**
* Returns a string with a HTML represenation of the given page. * Returns a string with a HTML represenation of the given page.
* $info must be an associative array, containing at least a Title object under the 'title' key. * $info must be an associative array, containing at least a Title object under the 'title' key.
*/ * @param $title Title
* @param $cat Category
* @param $children int
* @param $loadchildren bool
* @return string
*/
function renderNodeInfo( $title, $cat, $children = 0, $loadchildren = false ) { function renderNodeInfo( $title, $cat, $children = 0, $loadchildren = false ) {
static $uniq = 0; static $uniq = 0;
@ -739,8 +859,10 @@ class CategoryTree {
} }
/** /**
* Creates a Title object from a user provided (and thus unsafe) string * Creates a Title object from a user provided (and thus unsafe) string
*/ * @param $title string
* @return null|Title
*/
static function makeTitle( $title ) { static function makeTitle( $title ) {
$title = trim( $title ); $title = trim( $title );

View file

@ -19,13 +19,14 @@ class CategoryTreePage extends SpecialPage {
var $target = ''; var $target = '';
var $tree = null; var $tree = null;
/**
* Constructor
*/
function __construct() { function __construct() {
parent::__construct( 'CategoryTree', '', true ); parent::__construct( 'CategoryTree', '', true );
} }
/**
* @param $name
* @return mixed
*/
function getOption( $name ) { function getOption( $name ) {
global $wgCategoryTreeDefaultOptions; global $wgCategoryTreeDefaultOptions;
@ -38,15 +39,18 @@ class CategoryTreePage extends SpecialPage {
/** /**
* Main execution function * Main execution function
* @param $par Parameters passed to the page * @param $par array Parameters passed to the page
*/ */
function execute( $par ) { function execute( $par ) {
global $wgRequest, $wgOut, $wgCategoryTreeDefaultOptions, $wgCategoryTreeSpecialPageOptions, $wgCategoryTreeForceHeaders; global $wgCategoryTreeDefaultOptions, $wgCategoryTreeSpecialPageOptions, $wgCategoryTreeForceHeaders;
$this->setHeaders(); $this->setHeaders();
$request = $this->getRequest();
if ( $par ) $this->target = $par; if ( $par ) {
else $this->target = $wgRequest->getVal( 'target', wfMsg( 'rootcategory' ) ); $this->target = $par;
} else {
$this->target = $request->getVal( 'target', wfMsg( 'rootcategory' ) );
}
$this->target = trim( $this->target ); $this->target = trim( $this->target );
@ -59,46 +63,49 @@ class CategoryTreePage extends SpecialPage {
# grab all known options from the request. Normalization is done by the CategoryTree class # grab all known options from the request. Normalization is done by the CategoryTree class
foreach ( $wgCategoryTreeDefaultOptions as $option => $default ) { foreach ( $wgCategoryTreeDefaultOptions as $option => $default ) {
if ( isset( $wgCategoryTreeSpecialPageOptions[$option] ) ) if ( isset( $wgCategoryTreeSpecialPageOptions[$option] ) ) {
$default = $wgCategoryTreeSpecialPageOptions[$option]; $default = $wgCategoryTreeSpecialPageOptions[$option];
}
$options[$option] = $wgRequest->getVal( $option, $default ); $options[$option] = $request->getVal( $option, $default );
} }
$this->tree = new CategoryTree( $options ); $this->tree = new CategoryTree( $options );
$wgOut->addWikiMsg( 'categorytree-header' ); $output = $this->getOutput();
$output->addWikiMsg( 'categorytree-header' );
$this->executeInputForm(); $this->executeInputForm();
if ( $this->target !== '' && $this->target !== null ) { if ( $this->target !== '' && $this->target !== null ) {
if ( !$wgCategoryTreeForceHeaders ) CategoryTree::setHeaders( $wgOut ); if ( !$wgCategoryTreeForceHeaders ) {
CategoryTree::setHeaders( $output );
}
$title = CategoryTree::makeTitle( $this->target ); $title = CategoryTree::makeTitle( $this->target );
if ( $title && $title->getArticleID() ) { if ( $title && $title->getArticleID() ) {
$wgOut->addHTML( Xml::openElement( 'div', array( 'class' => 'CategoryTreeParents' ) ) ); $output->addHTML( Xml::openElement( 'div', array( 'class' => 'CategoryTreeParents' ) ) );
$wgOut->addHTML( wfMsgExt( 'categorytree-parents', 'parseinline' ) ); $output->addHTML( wfMsgExt( 'categorytree-parents', 'parseinline' ) );
$wgOut->addHTML( wfMsg( 'colon-separator' ) ); $output->addHTML( wfMsg( 'colon-separator' ) );
$parents = $this->tree->renderParents( $title ); $parents = $this->tree->renderParents( $title );
if ( $parents == '' ) { if ( $parents == '' ) {
$wgOut->addHTML( wfMsgExt( 'categorytree-no-parent-categories', 'parseinline' ) ); $output->addHTML( wfMsgExt( 'categorytree-no-parent-categories', 'parseinline' ) );
} else { } else {
$wgOut->addHTML( $parents ); $output->addHTML( $parents );
} }
$wgOut->addHTML( Xml::closeElement( 'div' ) ); $output->addHTML( Xml::closeElement( 'div' ) );
$wgOut->addHTML( Xml::openElement( 'div', array( 'class' => 'CategoryTreeResult' ) ) ); $output->addHTML( Xml::openElement( 'div', array( 'class' => 'CategoryTreeResult' ) ) );
$wgOut->addHTML( $this->tree->renderNode( $title, 1 ) ); $output->addHTML( $this->tree->renderNode( $title, 1 ) );
$wgOut->addHTML( Xml::closeElement( 'div' ) ); $output->addHTML( Xml::closeElement( 'div' ) );
} } else {
else { $output->addHTML( Xml::openElement( 'div', array( 'class' => 'CategoryTreeNotice' ) ) );
$wgOut->addHTML( Xml::openElement( 'div', array( 'class' => 'CategoryTreeNotice' ) ) ); $output->addHTML( wfMsgExt( 'categorytree-not-found', 'parseinline' , $this->target ) );
$wgOut->addHTML( wfMsgExt( 'categorytree-not-found', 'parseinline' , $this->target ) ); $output->addHTML( Xml::closeElement( 'div' ) );
$wgOut->addHTML( Xml::closeElement( 'div' ) );
} }
} }
} }
@ -107,22 +114,23 @@ class CategoryTreePage extends SpecialPage {
* Input form for entering a category * Input form for entering a category
*/ */
function executeInputForm() { function executeInputForm() {
global $wgScript, $wgOut; global $wgScript;
$thisTitle = SpecialPage::getTitleFor( $this->getName() ); $thisTitle = SpecialPage::getTitleFor( $this->getName() );
$mode = $this->getOption( 'mode' ); $mode = $this->getOption( 'mode' );
$wgOut->addHTML( Xml::openElement( 'form', array( 'name' => 'categorytree', 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-categorytree-form' ) ) ); $output = $this->getOutput();
$wgOut->addHTML( Xml::openElement( 'fieldset' ) ); $output->addHTML( Xml::openElement( 'form', array( 'name' => 'categorytree', 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-categorytree-form' ) ) );
$wgOut->addHTML( Xml::element( 'legend', null, wfMsgNoTrans( 'categorytree-legend' ) ) ); $output->addHTML( Xml::openElement( 'fieldset' ) );
$wgOut->addHTML( Html::Hidden( 'title', $thisTitle->getPrefixedDbKey() ) ); $output->addHTML( Xml::element( 'legend', null, wfMsgNoTrans( 'categorytree-legend' ) ) );
$wgOut->addHTML( Xml::inputLabel( wfMsgNoTrans( 'categorytree-category' ), 'target', 'target', 20, $this->target ) . ' ' ); $output->addHTML( Html::Hidden( 'title', $thisTitle->getPrefixedDbKey() ) );
$wgOut->addHTML( Xml::openElement( 'select', array( 'name' => 'mode' ) ) ); $output->addHTML( Xml::inputLabel( wfMsgNoTrans( 'categorytree-category' ), 'target', 'target', 20, $this->target ) . ' ' );
$wgOut->addHTML( Xml::option( wfMsgNoTrans( 'categorytree-mode-categories' ), 'categories', $mode == CT_MODE_CATEGORIES ? true : false ) ); $output->addHTML( Xml::openElement( 'select', array( 'name' => 'mode' ) ) );
$wgOut->addHTML( Xml::option( wfMsgNoTrans( 'categorytree-mode-pages' ), 'pages', $mode == CT_MODE_PAGES ? true : false ) ); $output->addHTML( Xml::option( wfMsgNoTrans( 'categorytree-mode-categories' ), 'categories', $mode == CT_MODE_CATEGORIES ) );
$wgOut->addHTML( Xml::option( wfMsgNoTrans( 'categorytree-mode-all' ), 'all', $mode == CT_MODE_ALL ? true : false ) ); $output->addHTML( Xml::option( wfMsgNoTrans( 'categorytree-mode-pages' ), 'pages', $mode == CT_MODE_PAGES ) );
$wgOut->addHTML( Xml::closeElement( 'select' ) . ' ' ); $output->addHTML( Xml::option( wfMsgNoTrans( 'categorytree-mode-all' ), 'all', $mode == CT_MODE_ALL ) );
$wgOut->addHTML( Xml::submitButton( wfMsgNoTrans( 'categorytree-go' ), array( 'name' => 'dotree' ) ) ); $output->addHTML( Xml::closeElement( 'select' ) . ' ' );
$wgOut->addHTML( Xml::closeElement( 'fieldset' ) ); $output->addHTML( Xml::submitButton( wfMsgNoTrans( 'categorytree-go' ), array( 'name' => 'dotree' ) ) );
$wgOut->addHTML( Xml::closeElement( 'form' ) ); $output->addHTML( Xml::closeElement( 'fieldset' ) );
$output->addHTML( Xml::closeElement( 'form' ) );
} }
} }