Extension:CategoryTree

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manualManual:Extensions
Crystal Clear action run.png
CategoryTree

Release status:Extension status stable

CategoryTreeExample.png
ImplementationTemplate:Extension#type Tag, Parser function, Special page
DescriptionTemplate:Extension#description Provides a dynamic view of the wiki's category structure as a tree.
Author(s)Template:Extension#username Daniel Kinzler (Duesentriebtalk)
Latest versionTemplate:Extension#version continuous updates
MediaWikiTemplate:Extension#mediawiki 1.23+
Database changesTemplate:Extension#needs-updatephp No
LicenseTemplate:Extension#license GNU General Public License 2.0 or later
Download
README
ParametersTemplate:Extension#parameters
  • $wgCategoryTreeMaxChildren
  • $wgCategoryTreeAllowTag
  • $wgCategoryTreeDynamicTag
  • $wgCategoryTreeDisableCache
  • $wgCategoryTreeHTTPCache
  • $wgCategoryTreeOmitNamespace
  • $wgCategoryTreeMaxDepth
  • $wgCategoryTreeDefaultMode
  • $wgCategoryTreeCategoryPageMode
Hooks usedTemplate:Extension#hook
ArticleFromTitleManual:Hooks/ArticleFromTitle
DebugManual:Hooks/Debug
MakeGlobalVariablesScriptManual:Hooks/MakeGlobalVariablesScript
OutputPageParserOutputManual:Hooks/OutputPageParserOutput
OutputPageMakeCategoryLinksManual:Hooks/OutputPageMakeCategoryLinks
ParserFirstCallInitManual:Hooks/ParserFirstCallInit
SkinTemplateOutputPageBeforeExecManual:Hooks/SkinTemplateOutputPageBeforeExec

Translate the CategoryTree extension if it is available at translatewiki.net

Check usage and version matrix.

IssuesPhabricator

Open tasks · Report a bug

The CategoryTree extension provides a dynamic view of the wiki's category structure as a tree. It uses AJAX to load parts of the tree on demand. CategoryTree was originally written by Daniel Kinzler as an external tool, but was later integrated into the MediaWiki software with the help of Tim Starling.

Usage[edit]

CategoryTree can be used in three ways:

  • directly on the category pages,
  • as a "custom tag" (or "parser function") to show a category structure in-line on a wiki page,
  • and as a special page.

The CategoryTree extension adds "expand" widgets to the subcategories listed on category pages.

The <categorytree> tag[edit]

Example. The result can be seen in the box below and on the right:

<categorytree mode=pages>Manual</categorytree>


The custom tag is called <categorytree>. For example, if you put <categorytree>Foo</categorytree> on a wiki page, it will show the contents of category Foo as a dynamic tree on that page (see example to the right). The tag accepts the following attributes, using a HTML-like syntax:

mode
determines which pages are shown in the tree. This may be overridden by the use of the namespaces option. Possible values:
categories
show subcategories only (configuration constant CT_MODE_CATEGORIES)
pages
show subcategories and pages, except images (configuration constant CT_MODE_PAGES), limited to 200 pages
all
show all pages, subcategories, images, etc (configuration constant CT_MODE_ALL)
parents
list parent categories instead of subcategories (configuration constant CT_MODE_PARENTS). This puts CategoryTree into "inverse" operation (supported since rev:36892, July 2008).
depth
determines how many levels of the tree are shown initially. Default is 1. 0 means only the root node. The maximum value depends on the mode specified, and on the $wgCategoryTreeMaxDepth option. (introduced between rev:r20410 and rev:20840, Mar 30 2007; fully functional even with $wgCategoryTreeDynamicTag since rev:36864, July 2008)
onlyroot
set this to "on" to only show the "root" node of the tree initially. Equivalent to depth="0". (introduced in rev:20385, March 13 2007) Deprecated as of rev:36864, July 2008: use depth="0" instead.
hideroot
set this to "on" to hide the "root" node of the tree, i.e. hideroot=on. For example, it will not display the category name Foo.
hideprefix
determines when the namespace prefix should be hidden from entries in the tree (since rev:36864, July 2008). Possible values are:
always
always hide the prefix. Should be used only if all pages come from the same namespace (configuration constant CT_MODE_CT_HIDEPREFIX_ALWAYS).
never
never hide the namespace prefix, not even for categories (configuration constant CT_MODE_CT_HIDEPREFIX_NEVER).
auto
hide the namespace of categories, if only categories are shown, as with mode="categories" (configuration constant CT_MODE_CT_HIDEPREFIX_AUTO); This is equivalent to the old default behavior (before Jun 2008).
categories
always hide the namespace of categories, but show all other prefixes (configuration constant CT_MODE_CT_HIDEPREFIX_CATEGORIES). This is the new default behavior (since rev:36864, July 2008).
showcount
set this to "on" to show the number of entries in subcategories, i.e. showcount="on" (since rev:36864, July 2008). This is disabled per default for category trees inlined on pages, but enabled per default in category listings and on Special:CategoryTree.
namespaces
a list of namespaces from which pages should be shown in the tree (since rev:36864, July 2008). The entries in the list must be namespace names, separated by spaces. The main namespaces can be represented by "-" or "0". This overrides the mode option.
class, style, id, etc
The <categorytree> tag supports all standard HTML attributes for block elements (since rev:36864, July 2008. Before that, only style was supported).

The {{#categorytree}} parser function[edit]

Since rev:25825 (2007-9-13), you can also use parser function syntax to place a category tree on a page. This works just like the <categorytree> tag, using a different syntax, e.g. {{#categorytree:Foo}}; options can be specified using the syntax for named template parameters, e.g. {{#categorytree:Foo|hideroot|mode=pages}}.

Using the parser function syntax has the advantage that it allows you to use magic words, templates and template parameters when specifying the category to show. Some examples:

  • Using a magic variable to show "this pages" category tree (may be useful on category description pages): {{#categorytree:{{PAGENAME}}}}.
  • Using a template to specify the category: {{#categorytree:{{root category}}}} - the name of the root category would be defined by the contents of Template:Root_category.
  • Using a template parameter to specify the category, when using CategoryTree inside a template: {{#categorytree:{{{1}}}}} in Template:Baz can be called using {{Baz|Foo}}

The Special:CategoryTree page[edit]

The special page is called Special:CategoryTree; there you can enter the name of a category and then browse its content.

Installation[edit]

  • Download and place the file(s) in a directory called CategoryTree in your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php:
require_once "$IP/extensions/CategoryTree/CategoryTree.php";
  • YesY Done - Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Note Note: If you are using a version of MediaWiki pre-1.10, then you must also include $wgUseAjax = true to enable the AJAX framework in MediaWiki which is required by the CategoryTree extension. AJAX is a term relating to the use of JavaScript to load parts of a page on demand. It is supported by all recent graphic web browsers.

Configuration[edit]

There are some options you can specify in your LocalSettings.php file:

$wgCategoryTreeMaxChildren
maximum number of children shown in a tree node. Default is 200.
$wgCategoryTreeAllowTag
enable <categorytree> tag. Default is true.
$wgCategoryTreeDynamicTag
loads the first level of the tree in a <categorytree> dynamically. This way, the cache does not need to be disabled. Default is false.
$wgCategoryTreeDisableCache
disables the parser cache for pages with a <categorytree> tag or provides max cache time in seconds. Default is 6 hours.
$wgCategoryTreeUseCache
enable HTTP cache for anon users. Default is false.
$wgCategoryTreeOmitNamespace
do not show the namespace name for pages and categories. Default is false, namespaces are shown unless showing categories only. Deprecated as of rev:36864, July 2008: use $wgCategoryTreeDefaultOptions['hideprefix'] instead.
$wgCategoryTreeMaxDepth
an array defining the maximum depth for each mode, to which the tree may be expanded initially using the depth parameter in a <category> tag. Per default, this is set to 2 for the "categories" mode, and 1 for other modes. To set it to 3 for categories and 2 for other modes, for example, you can use $wgCategoryTreeMaxDepth = array(CT_MODE_PAGES => 2, CT_MODE_ALL => 2, CT_MODE_CATEGORIES => 3);.
$wgCategoryTreeDefaultMode
the default mode to use when no mode attribute is specified in a <categorytree> tag. May be CT_MODE_CATEGORIES (the default), CT_MODE_PAGES, or CT_MODE_ALL.
$wgCategoryTreeCategoryPageMode
the mode to use when rendering trees on category pages. Deprecated as of rev:36864, July 2008: use $wgCategoryTreeCategoryPageOptions['mode'] instead.
$wgCategoryTreeForceHeaders
if true, forces the scripts needed by CategoryTree on every page, instead of on-demand. This may be needed if a CategoryTree is embedded in an unusual way, for example in a custom skin or in a system message (since rev:36888, July 2008).
$wgCategoryTreeSidebarRoot
root category to use for integrating the category tree in the sidebar (since rev:36920, July 2008). If not set, no tree is integrated (this is the default). Integrating a CategoryTree into the sidebar only works with skins based upon SkinTemplate, that is, Modern, Monobook, Chick, Simple, and MySkin, but not Standard, Nostalgia or CologneBlue.
$wgCategoryTreeDefaultOptions
default options to apply, as an array (since rev:36864, July 2008). The available options are described above. Example: $wgCategoryTreeDefaultOptions['mode'] = 'pages';
$wgCategoryTreeCategoryPageOptions
options to use on category pages, as an array (since rev:36864, July 2008).
$wgCategoryTreeSpecialPageOptions
options to use on Special:CategoryTree, as an array (since rev:36864, July 2008).
$wgCategoryTreeSidebarOptions
options to use when showing the tree in the sidebar, as an array (since rev:36920, July 2008).

System messages[edit]

categorytree-member-num[edit]

By default, when listing subcategories on a category page, each subcategory is followed by an integer, indicating how many subcategories lie beneath it, how many pages it contains, and how many files are in the category (if applicable):

My category name (5 C, 2 P, 1 F)

You can change this behavior by overriding the system message MediaWiki:Categorytree-member-num. It takes five parameters:

Parameter Meaning
$1 Number of subcategories
$2 Number of articles (not including category pages or file pages)
$3 Number of uploaded files
$4 Total number of member pages, including category pages and file pages
$5 Something close to ($1 C, $2 P, $3 F) but with anything that is 0 omitted.

More precisely, a comma separated list of the number of pages (mediawiki:categorytree-num-pages), subcategories (mediawiki:categorytree-num-categories), and images (mediawiki:categorytree-num-files) in the category, or empty (mediawiki:categorytree-num-empty) in the case the category has no members. In versions prior to those around 1.19 this value was different. See older version of these docs.

The default value is ($5).

Tip: Use the ParserFunctions extension to provide if/then/else logic to display different parts conditionally. For example:

{{#ifeq:$1|0||$1 categories}}

A configuration to manage commas and spaces when outputting all info:

Abbreviation version:

({{#ifeq:$4|0|empty|{{#ifeq:$1|0||$1 C{{#ifeq:{{#expr:$3+$2}}|0||,}}}} {{#ifeq:$2|0||$2 P{{#ifeq:$3|0||,}}}} {{#ifeq:$3|0||$3 F}}}})

Full name version:

({{#ifeq:$4|0|empty|{{#ifeq:$1|0||$1 categories{{#ifeq:{{#expr:$3+$2}}|0||,}}}} {{#ifeq:$2|0||$2 pages{{#ifeq:$3|0||,}}}} {{#ifeq:$3|0||$3 files}}}})

Troubleshooting[edit]

Wiki is broken after installing CategoryTree[edit]

  • Turn on error reporting
  • Make sure you have all files installed
  • Make sure all of the CategoryTree extension files have mode 644, and the CategoryTree directory has mode 755.

Options in LocalSettings.php have no effect[edit]

  • Make sure you specify the options after including the extension.

Clicking the has no effect, gives a JavaScript error or just sits on loading[edit]

  • Note that using the <categorytree> tag in system messages (MediaWiki-Namespace) may not work as expected - this is a known caveat. For a workaround, see #Using CategoryTree in the Sidebar below.
  • Make sure you have $wgUseAjax = true; in your LocalSettings.php before including the extension.
  • Make sure you are using the latest version of the extension - the bleeding edge is at [1]
  • If you are using rewrite rules or aliasing (e.g. to have "very short URLs" - i.e. wiki pages in the document root or perhaps redirecting to a www-version of the requested URL), make sure you have an exception in place for the /extensions directory, just like you would for the /skins directory. Also see below.

Clicking the shows no sub-categories error[edit]

  • Try to fix this by adding this code to LocalSettings.php: $wgCategoryTreeDefaultOptions['namespaces'] = array( 0, NS_CATEGORY, NS_FILE );

Browser reports a bad MIME type for CategoryTree.js and/or CategoryTree.css[edit]

  • Make sure your Webserver uses the correct mime types for .js and .css files
  • Enter the URLs directly into the browser and see if it is serving the pages at all, and not some error page; especially:
  • If you are using rewrite rules or aliasing (e.g. to have "very short URLs" - i.e. wiki pages in the document root), make sure you have an exception in place for the /extensions directory, just like you would for the /skins directory.

Member counts are wrong, gray arrows are shown instead of [edit]

Since MediaWiki 1.13, CategoryTree can show the number of members of a category, and can show a gray arrow for tree nodes that do not have any children based on that number. This information is based on the new category table. However, populating that table with the current values does not always work when upgrading to 1.13. In that case, run the populateCategory.php maintenance script, if necessary with the --force option.

toggles shown only on the first level of the category tree view[edit]

In case you get only one level depth while visualing the tree, try to set in CategoryTreeFunctions.php file the following line

$linkattr['style'] = 'display: none;'; // Unhidden by JS

to

$linkattr['style'] = 'display: inline !important;'; // Unhidden by JS

The javascript intended to switch the tag style is somehow buggy.

Note to bot users[edit]

Bots and scripts that parse the HTML of category pages can use the notree URL parameter to force the traditional output. However, HTML output is generally not stable, so it is recommended to change your script to use the api.php to fetch category listings and similar.

"Category Not Found" message[edit]

After you implement the Category Tree plugin and add the code to show the tree, if you see a message that says Category Not Found, you need to add some content in your category pages. The category pages can't be red pages, basically. Click your category names, add some text in the category page, and then your tree should work fine.

Next 200 links do not work with CategoryTree installed[edit]

Make sure the right version of this extension is installed, relative to the version of MediaWiki you have installed. In particular, versions of this extension made for versions of MediaWiki prior to 1.17 are not compatible with MediaWiki 1.17 and greater, and will cause issues where all the paging links on category pages are broken.

Hacks[edit]

Using CategoryTree in system messages and custom skins[edit]

To use the CategoryTree in system messages or the sidebar, that is, to use it as part of the user interface instead of page content, you have to set wgCategoryTreeForceHeaders to true, so that the support scripts are linked on every page (normally, they are only linked "on demand", but that detection does not work for system messages). This works since rev:36888 (July 2008). If you try this without forcing the scripts to be included, you will find that Clicking the ► has no effect, gives a JavaScript error or just sits on loading.

In older versions of CategoryTree, you have to modify the code to make this work: find the efCategoryTreeParserOutput function in the file CategoryTree.php, and remove the condition; i.e. it should then look like this:

function efCategoryTreeParserOutput( &$outputPage, &$parserOutput )  {
    CategoryTree::setHeaders( $outputPage );
    return true;
}

Using CategoryTree in the Sidebar[edit]

Using JavaScript[edit]

Since rev:36920 (July 2008) with MediaWiki 1.13 (rev:36917), you can integrate a CategoryTree in the sidebar simply by setting $wgCategoryTreeSidebarRoot to the category you want to use as a root. This only works with skins based upon SkinTemplate however, that is, Modern, Monobook, Chick, Simple, and MySkin, but not Standard, Nostalgia or CologneBlue.

If you use $wgHooks['MonoBookTemplateToolboxEnd'][] = 'wfSomeFunc'; in LocalSettings.php to drop your own wikitext article into the Sidebar, and then add CategoryTree items to it, you'll need to set $wgCategoryTreeForceHeaders = true; to ensure that every page works.

If you want the CategoryTree on a different position in the sidebar, you can reference it on [[MediaWiki::Sidebar]] as "categorytree-portlet".

For earlier versions of CategoryTree and/or MediaWiki, you'll need to install a mod to MonoBook.php to enable normal wikitext in the sidebar, see Wikitext in side bar --DJ 19:26, 24 January 2008 (UTC)

To append a link "CategoryTree" to your toolbox (sidebar) add following code to the javascript-page (e.g. monobook.js) of your skin and customize the URL "w:en:Special:CategoryTree".

function AppendCategoryTreeToSidebar() {
    try {
        var node = document.getElementById( "p-tb" )
                           .getElementsByTagName('div')[0]
                           .getElementsByTagName('ul')[0];
 
        var aNode = document.createElement( 'a' );
        var liNode = document.createElement( 'li' );
 
        aNode.appendChild( document.createTextNode( 'CategoryTree' ) );
        aNode.setAttribute( 'href' , '[[w:en:Special:CategoryTree]]' );
        liNode.appendChild( aNode );
        liNode.className = 'plainlinks';
        node.appendChild( liNode );
    } catch(e) {
        // lets just ignore what's happened
        return;
    }
}
 
addOnloadHook( AppendCategoryTreeToSidebar );

A more generic version of this code can be found at Manual:Interface/Sidebar#content of sidebar and toolbox (javascript).

Step by step if works easy:

  • Add these lines to LocalSettings.php:
    1. $wgCategoryTreeSidebarRoot = 'Category:article_name';
    2. $wgCategoryTreeForceHeaders = true;

Using MediaWiki: namespace pages[edit]

If you want to create a link that goes to the generic categorytree (displayed as Category tree) when you're on a non-category page, and to a particular categorytree (displayed as View as category tree, with foo being the name of the category you're viewing) when you're on a category page:

  • Add to the your MediaWiki:Sidebar (right above the SEARCH is a good place) a line, ** sidebarcategorytree-url|sidebarcategorytree
  • Create a MediaWiki:Sidebarcategorytree with, say: {{#ifeq: {{NAMESPACE}}|Category|View as category tree|Category tree}}
  • Create a MediaWiki:Sidebarcategorytree-url with, say: {{#ifeq: {{NAMESPACE}}|Category|Special:CategoryTree/{{BASEPAGENAME}}|Special:CategoryTree/Categories}}

Parameters to request[edit]

It is possible to override the default site configuration of "mode" by providing it to as URL parameter. For example

  • //en.wikipedia.org/wiki/Category:MediaWiki - use default mode
  • //en.wikipedia.org/wiki/Category:MediaWiki?mode=0 - use categories mode
  • //en.wikipedia.org/wiki/Category:MediaWiki?mode=10 - use page mode
  • //en.wikipedia.org/wiki/Category:MediaWiki?mode=20 - use all mode
  • //en.wikipedia.org/wiki/Category:MediaWiki?mode=100 - use parent mode


Language: English  • français • 日本語 • português do Brasil • русский