mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/CategoryTree
synced 2024-11-14 19:35:11 +00:00
Add depth argument to allow auto-expanding tree (to small, configurable limit)
This commit is contained in:
parent
06647afe4a
commit
93882a1cc4
|
@ -34,7 +34,13 @@ define('CT_MODE_ALL', 20);
|
|||
* $wgCategoryTreeUseCache - enable HTTP cache for anon users. Default is false.
|
||||
* $wgCategoryTreeUnifiedView - use unified view on category pages, instead of "tree" or "traditional list". Default is true.
|
||||
* $wgCategoryTreeOmitNamespace - never show namespace prefix. Default is false
|
||||
|
||||
* $wgCategoryMaxDepth - maximum value for depth argument; can be an
|
||||
* integer, or an array of two integers. The first element is the maximum
|
||||
* depth for the "pages" and "all" modes; the second is for the categories
|
||||
* mode. Ignored if $wgCategoryTreeDynamicTag is true.
|
||||
*/
|
||||
|
||||
$wgCategoryTreeMaxChildren = 200;
|
||||
$wgCategoryTreeAllowTag = true;
|
||||
$wgCategoryTreeDisableCache = true;
|
||||
|
@ -42,6 +48,7 @@ $wgCategoryTreeDynamicTag = false;
|
|||
$wgCategoryTreeHTTPCache = false;
|
||||
$wgCategoryTreeUnifiedView = true;
|
||||
$wgCategoryTreeOmitNamespace = false;
|
||||
$wgCategoryMaxDepth = array(1,2);
|
||||
|
||||
/**
|
||||
* Register extension setup hook and credits
|
||||
|
@ -111,6 +118,45 @@ function efCategoryTreeAjaxWrapper( $category, $mode = CT_MODE_CATEGORIES ) {
|
|||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal function to cap depth
|
||||
*/
|
||||
|
||||
function efCategoryTreeCapDepth( $mode, $depth )
|
||||
{
|
||||
|
||||
if (is_numeric($depth))
|
||||
$depth = intval($depth);
|
||||
else
|
||||
$depth = 1;
|
||||
|
||||
|
||||
global $wgCategoryMaxDepth;
|
||||
if (is_array($wgCategoryMaxDepth)) {
|
||||
switch($mode) {
|
||||
case CT_MODE_PAGES:
|
||||
case CT_MODE_ALL:
|
||||
$max = isset($wgCategoryMaxDepth[0])?$wgCategoryMaxDepth[0]:1;
|
||||
break;
|
||||
case CT_MODE_CATEGORIES:
|
||||
default:
|
||||
$max = isset($wgCategoryMaxDepth[1])?$wgCategoryMaxDepth[1]:1;
|
||||
break;
|
||||
}
|
||||
} elseif (is_numeric($wgCategoryMaxDepth)) {
|
||||
$max = $wgCategoryMaxDepth;
|
||||
} else {
|
||||
wfDebug( 'efCategoryTreeCapDepth: $wgCategoryMaxDepth is invalid.' );
|
||||
$max = 1;
|
||||
}
|
||||
|
||||
//echo "mode $mode:max is $max\n";
|
||||
if ($depth>$max)
|
||||
$depth = $max;
|
||||
|
||||
return $depth;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to convert a string to a boolean value.
|
||||
* Perhaps make this a global function in MediaWiki proper
|
||||
|
@ -157,12 +203,15 @@ function efCategoryTreeParserHook( $cat, $argv, &$parser ) {
|
|||
$hideroot = isset( $argv[ 'hideroot' ] ) ? efCategoryTreeAsBool( $argv[ 'hideroot' ] ) : null;
|
||||
$onlyroot = isset( $argv[ 'onlyroot' ] ) ? efCategoryTreeAsBool( $argv[ 'onlyroot' ] ) : null;
|
||||
|
||||
$depth = efCategoryTreeCapDepth($mode,@$argv[ 'depth' ]);
|
||||
|
||||
if ( $onlyroot ) $display = 'onlyroot';
|
||||
else if ( $hideroot ) $display = 'hideroot';
|
||||
else $display = 'expandroot';
|
||||
|
||||
$ct = new CategoryTree;
|
||||
return $ct->getTag( $parser, $cat, $mode, $display, $style );
|
||||
return $ct->getTag( $parser, $cat, $mode, $hideroot, $style, $depth );
|
||||
return $ct->getTag( $parser, $cat, $mode, $display, $style, $depth );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -113,7 +113,7 @@ class CategoryTree {
|
|||
* Custom tag implementation. This is called by efCategoryTreeParserHook, which is used to
|
||||
* load CategoryTreeFunctions.php on demand.
|
||||
*/
|
||||
function getTag( &$parser, $category, $mode, $display = 'expandroot', $style = '' ) {
|
||||
function getTag( &$parser, $category, $mode, $display = 'expandroot', $style = '', $depth=1 ) {
|
||||
global $wgCategoryTreeDisableCache, $wgCategoryTreeDynamicTag;
|
||||
static $uniq = 0;
|
||||
|
||||
|
@ -138,8 +138,8 @@ class CategoryTree {
|
|||
$html .= wfCloseElement( 'span' );
|
||||
}
|
||||
else {
|
||||
if ( $display != 'hideroot' ) $html .= CategoryTree::renderNode( $title, $mode, $display != 'onlyroot', $wgCategoryTreeDynamicTag );
|
||||
else if ( !$wgCategoryTreeDynamicTag ) $html .= $this->renderChildren( $title, $mode );
|
||||
if ( $display != 'hideroot' ) $html .= CategoryTree::renderNode( $title, $mode, $depth>0, $wgCategoryTreeDynamicTag, $depth-1 );
|
||||
else if ( !$wgCategoryTreeDynamicTag ) $html .= $this->renderChildren( $title, $mode, $depth-1 );
|
||||
else {
|
||||
$uniq += 1;
|
||||
$load = 'ct-' . $uniq . '-' . mt_rand( 1, 100000 );
|
||||
|
@ -160,7 +160,7 @@ class CategoryTree {
|
|||
* Returns a string with an HTML representation of the children of the given category.
|
||||
* $title must be a Title object
|
||||
*/
|
||||
function renderChildren( &$title, $mode = CT_MODE_CATEGORIES ) {
|
||||
function renderChildren( &$title, $mode = CT_MODE_CATEGORIES, $depth=0 ) {
|
||||
global $wgCategoryTreeMaxChildren;
|
||||
|
||||
$dbr =& wfGetDB( DB_SLAVE );
|
||||
|
@ -198,7 +198,7 @@ class CategoryTree {
|
|||
while ( $row = $dbr->fetchRow( $res ) ) {
|
||||
#TODO: translation support; ideally added to Title object
|
||||
$t = Title::makeTitle( $row['page_namespace'], $row['page_title'] );
|
||||
$s .= $this->renderNode( $t, $mode, false );
|
||||
$s .= $this->renderNode( $t, $mode, $depth>0, false, $depth-1 );
|
||||
$s .= "\n\t\t";
|
||||
}
|
||||
|
||||
|
@ -267,7 +267,7 @@ class CategoryTree {
|
|||
* Returns a string with a HTML represenation of the given page.
|
||||
* $title must be a Title object
|
||||
*/
|
||||
function renderNode( &$title, $mode = CT_MODE_CATEGORIES, $children = false, $loadchildren = false ) {
|
||||
function renderNode( &$title, $mode = CT_MODE_CATEGORIES, $children = false, $loadchildren = false, $depth = 1 ) {
|
||||
global $wgCategoryTreeOmitNamespace;
|
||||
static $uniq = 0;
|
||||
|
||||
|
@ -343,7 +343,8 @@ class CategoryTree {
|
|||
$s .= wfCloseElement( 'div' );
|
||||
$s .= "\n\t\t";
|
||||
$s .= wfOpenElement( 'div', array( 'class' => 'CategoryTreeChildren', 'style' => $children ? "display:block" : "display:none" ) );
|
||||
if ( $children ) $s .= $this->renderChildren( $title, $mode );
|
||||
//HACK here?
|
||||
if ( $children ) $s .= $this->renderChildren( $title, $mode, $depth );
|
||||
$s .= wfCloseElement( 'div' );
|
||||
$s .= wfCloseElement( 'div' );
|
||||
|
||||
|
|
7
README
7
README
|
@ -93,5 +93,12 @@ $wgCategoryTreeOmitNamespace - never show namespace prefix. Default is
|
|||
false. Patch contributed by Manuel Schneider
|
||||
<manuel.schneider@wikimedia.ch>, Bug 8011
|
||||
|
||||
$wgCategoryMaxDepth - maximum value for depth argument; can be an integer,
|
||||
or an array of two integers. The first element is the
|
||||
maximum depth for the "pages" and "all" modes; the
|
||||
second is for the categories mode. Ignored if
|
||||
$wgCategoryTreeDynamicTag is true. Patch contributed by
|
||||
Steve Sanbeg.
|
||||
|
||||
--------------------------------------------------------------------------
|
||||
EOF
|
Loading…
Reference in a new issue