Extension:CategoryTree(分类树)
发行状态: 稳定版 |
|
---|---|
实现 | 标签 , 解析器函数 , 特殊页面 |
描述 | 以樹狀結構的形式提供wiki类别结构的动态檢視。 |
作者 | Daniel Kinzler (Duesentrieb留言) |
最新版本 | 持续更新 |
兼容性政策 | 快照跟随MediaWiki发布。 master分支不向后兼容。 |
MediaWiki | 1.25+ |
许可协议 | GNU通用公眾授權條款2.0或更新版本 |
下載 | README |
|
|
前往translatewiki.net翻譯CategoryTree扩展 | |
問題 | 开启的任务 · 报告错误 |
CategoryTree扩展以樹狀結構的形式提供wiki类别结构的动态檢視。它使用AJAX依據需要加载樹狀結構的某些部份。CategoryTree最初是由Daniel Kinzler作为外部工具所编写的,但后来在Tim Starling的帮助下被集成到MediaWiki软件中。
安装
- 下载文件,并解压
CategoryTree
文件夹到extensions/
目录中。
开发者和代码贡献人员应改从Git安装此扩展,输入:cd extensions/ git clone https://gerrit.wikimedia.org/r/mediawiki/extensions/CategoryTree
- 請新增下列代码到您的LocalSettings.php 文件的底部:
wfLoadExtension( 'CategoryTree' );
完成 – 請导航至您的wiki上的Special:Version,以验证此扩展已成功安装。
用法
类别树可以下列三种方式使用:
- 直接用于分类页面,
- 作为“自定义标签”(或“解析器函数”)在wiki页面中内联显示分类结构,
- 作为特殊页面。
此类别树扩展添加了一個►“展开”的小部件到分类页面上所列出的各個子分类。
<categorytree>标签
实例结果可以在下面和右侧的框中看到:
<categorytree mode="pages">Manual</categorytree>
自定义标签称为<categorytree>
。
例如,如果您将<categorytree>Foo</categorytree>添加到wiki页面上,它将在该页面上将分类Foo的内容显示为动态樹狀結構(参见右侧示例)。
该标签使用类HTML的语法接受以下属性:
- mode
- 定义在樹狀結構中显示哪些页面。这可能通过使用
namespaces
选项被覆盖。可选值有:- categories
- 只显示子分类(配置常数
0
) - pages
- 显示子分类和页面,不包括图像(配置常数
10
),最多显示200个页面 - all
- 显示所有页面、子分类和图片等。(配置常量
20
) - parents
- 列出父分类而非子分类(配置常量
100
)。这会让CategoryTree进入“反向”操作(自2008年7月的r36892起支持)。
- depth
- 确定初次显示樹狀結構的多少层。默认为1。0表示仅显示根节点。最大值取决于指定的mode和
$wgCategoryTreeMaxDepth
选项。(在2007年3月30日的r20410到r20840被引入,自2008年7月的%rev3起即便开启$wgCategoryTreeDynamicTag
也能正常运行。) onlyroot將此設定為「on」可使樹狀結構初始僅顯示「根」節點。等同於自r36864版(2008年7月)起已廢棄:請改用depth="0"
。(於2007年3月13日r20385版中引入。)depth="0"
。- hideroot
- 将此设为“on”隐藏樹狀結構的“根”节点,即
hideroot=on
。例如,这将不会显示Foo的分类名。 - hideprefix
- 決定何时应該隐藏在樹狀結構裏面的實體的命名空间前缀(自2008年7月的r36864版起)。可能的值有:
- always
- 始終隱藏前綴,此應只使用在所有頁面均來自相同命名空間時(配置的常數
10
)。 - never
- 決不隱藏命名空間的前綴、即使是分類亦然(配置的常數
0
)。 - auto
- 若僅有顯示分類,則隱藏分類的命名空間,如同
mode="categories"
(配置的常數30
)的設定。此行為等同於舊版預設的行為(2008年6月前)。 - categories
- 始終隱藏分類的命名空間、但顯示所有其他的前綴(配置的常數
20
)。此為新版預設的行為(自2008年7月r36864版起)。
- showcount
- 將此設定為「on」以顯示子分類中的實體數量,即
showcount="on"
(自2008年7月r36864版起)。 此功能預設在頁面內嵌的分類樹中停用,但在分類列表中及Special:CategoryTree頁面中預設啟用。 - notranslations
- 將此設定為「on」可隱藏所有對應於該分類相關頁面的翻譯頁面,即
notranslations="on"
(自2019年11月的Gerrit change 526234起)。 此项默认是停用。 若此選項設定為「on」,各種不同語言的翻譯(例如:頁面、頁面/en、頁面/fr、頁面/de)都不會顯示,取而代之的是,僅會顯示標準的翻譯「頁面」、且所有翻譯皆會被掩蓋。這對「showcount」並無作用,因此仍是一個bug。 - namespaces
- 應在樹狀結構中顯示頁面的命名空間的清單(自2008年7月r36864版起)。清單中的實體必須為命名空間名稱、並以空格分隔。主命名空間可使用「-」或「0」來表示。僅當清單中包含分類的命名空間時,子分類才會顯示。
- class, style, id, 等等
<categorytree>
標籤支援所有區塊元素的標準HTML屬性(自2008年7月r36864版本起;此前僅支援style
)。
'namespaces'参数的注意事项
分類是一個命名空間。
分類預設在所有模式中皆會顯示,但指定namespaces
參數即可將它們隱藏,如同任何一個未出現在清單中的其他命名空間。
若要列出特定命名空間中的某些頁面(包括子類別中的頁面),只需將「分類」加入命名空間清單即可。
例如:
- 列出在Category:Manual:中主要命名空間的頁面
<categorytree mode="pages" namespaces="Main">Manual</categorytree>
- 列出在Category:Manual:中的主命名空間頁面「以及」子分類
<categorytree mode="pages" namespaces="Main Category">Manual</categorytree>
下列情況下,mode
參數會被namespaces
覆寫:
- 若僅要求Category命名空間,此等同於
mode=categories
; - 若檔案命名空間存在於清單中,此將隱含
mode=all
; - 要不然,將使用
mode=pages
。這意味著若網站配置尚未被修改,以下情況是等效的(前兩者始終等效、第三個僅在$wgCategoryTreeDefaultMode
是預設的CategoryTreeMode::CATEGORIES
時等效):
<categorytree mode="categories">Manual</categorytree>
<categorytree namespaces="Category">Manual</categorytree>
<categorytree>Manual</categorytree>
解析器函数{{#categorytree}}
自(2007年9月13日)r25825版起,您也可以使用"解析器函数"的语法在页面上放置一個分类树。這運作起來如同是<categorytree>标签,是使用某個例如{{#categorytree:Foo}}
這種不同的语法;而各個选项則可使用例如{{#categorytree:Foo|hideroot|mode=pages}}
這樣的已命名模板的参数這種的语法來指定。
使用解析器函式語法的好處在於,它可讓您在指定要顯示的分類時使用魔法字、模板、及模板參數。以下為幾個範例:
- 使用一個魔法變數來顯示本頁的分類樹(可能在分類說明頁中會有用):
{{#categorytree:{{PAGENAME}}}}
。 - 使用一個模板來指定分類:
{{#categorytree:{{root category}}}}
--根分類的名稱將由Template:Root_category的內容來定義。 - 使用一個模板參數來指定分類,當在模板內使用CategoryTree時:可利用{{Baz|Foo}}來调用Template:Baz中的
{{#categorytree:{{{1}}}}}
。
Special:CategoryTree页面
該特殊頁面名為Special:CategoryTree;您可在其中輸入分類的名稱、然後瀏覽其內容。
配置
There are some options you can specify in your LocalSettings.php file.
0
for category mode, 10
for pages, 20
for all, 100
for parents.- $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 21600 which is 6 hours.
- $wgCategoryTreeUseCache
- enable HTTP cache for anon users. Default is false.
$wgCategoryTreeOmitNamespacedo not show the namespace name for pages and categories. Default is false, namespaces are shown unless showing categories only.Deprecated as of r36864, 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. By 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 = [10 => 2, 20 => 2, 0 => 3, 100 => 2];
. - $wgCategoryTreeDefaultMode
the default mode to use when no mode attribute is specified in a <categorytree> tag.May be0
(the default - categories),10
(pages), or20
(all).$wgCategoryTreeCategoryPageModethe mode to use when rendering trees on category pages.Deprecated as of r36864, July 2008: use$wgCategoryTreeCategoryPageOptions['mode']
instead.- $wgCategoryTreeSidebarRoot
- root category to use for integrating the category tree in the sidebar (since r36920, 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. This is currently broken for MediaWiki 1.35.1 and the vector skin, see phab:278306.
- $wgCategoryTreeDefaultOptions
- default options to apply, as an array (since r36864, July 2008). The available options are described above. Example:
$wgCategoryTreeDefaultOptions['mode'] = 'pages';
. - $wgCategoryTreeCategoryPageOptions
- options to use on category pages, as an array (since r36864, July 2008).
- $wgCategoryTreeSpecialPageOptions
- options to use on Special:CategoryTree, as an array (since r36864, July 2008).
- $wgCategoryTreeSidebarOptions
- options to use when showing the tree in the sidebar, as an array (since r36920, July 2008)
系统消息
categorytree-member-num
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:
参数 | 含义 |
---|---|
$1 | 子分类数 |
$2 | 文章数(不包括分类页面和文件页面) |
$3 | 上传文件的个数 |
$4 | 成员页面总数,包括分类页面和文件页面 |
$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 files (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. |
默认值为($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}}}})
故障排除
安装CategoryTree后wiki损坏
- 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.
LocalSettings.php中的选项无效
- Make sure you specify the options after including the extension.
<span id="Clicking_the_►_has_no_effect,_gives_a_JavaScript_error_or_just_sits_on_loading">
点击►后无效,返回JavaScript错误或处于加载中
- 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 git:mediawiki/extensions/CategoryTree
- 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.
<span id="Clicking_the_►_shows_no_sub-categories_error">
点击►显示no sub-categories错误
- Try to fix this by adding this code to LocalSettings.php:
$wgCategoryTreeDefaultOptions['namespaces'] = [ 0, NS_CATEGORY, NS_FILE ];
浏览器报告CategoryTree.js和/或CategoryTree.css的错误MIME类型
- Make sure your web server 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.
<span id="Member_counts_are_wrong,_grey_arrows_►_are_shown_instead_of_►">
成员数错误,显示为灰色箭头►而非►
Since MediaWiki 1.13, CategoryTree can show the number of members of a category, and can show a grey 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.
<span id="► ► ▼_toggles_shown_only_on_the_first_level_of_the_category_tree_view">
► ► ▼切換开关仅显示在分类树檢視的第一层级
In case you get only one level depth while visualing the tree, try to set in modules/ext.categoryTree.styles.css file display: none;
to display: inline !important;
.
The JavaScript intended to switch the tag style is somehow buggy.
机器人用户请注意
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 to fetch category listings and similar.
“Category Not Found”消息
After you implement the CategoryTree 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.
后面的200个链接在安装了CategoryTree后不起作用
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.
使用“namespaces”参数时不显示任何页面
见前文。
Hacks
在系统消息和自定义皮肤中使用CategoryTree
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;
}
在侧边栏使用CategoryTree
通过JavaScript
Since r36920 (July 2008) with MediaWiki 1.13 (r36917), 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 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: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: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 :
$wgCategoryTreeSidebarRoot = 'Category:article_name';
通过MediaWiki: 命名空间页面
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 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}}
See also
此扩展用于一个或多个维基媒体项目。 这可能意味着扩展足够稳定、运作足够良好,可以用在这样的高流量的网站上。 请在维基媒体的CommonSettings.php和InitialiseSettings.php配置文件中查找此扩展的名称以查看哪些网站安装了该扩展。 特定wiki上的已安装的扩展的完整列表位于Special:Version页面。 |
此扩展在以下wiki农场/托管网站和/或软件包中提供: 這不是一份權威名單。 即使某些wiki农场/托管网站和/或软件包未在这里列出,它们也可能提供此扩展。 请检查你的wiki农场/托管网站或软件包以确认提供情况。 |
- Stable extensions/zh
- Tag extensions/zh
- Parser function extensions/zh
- Special page extensions/zh
- GPL licensed extensions/zh
- Extensions in Wikimedia version control/zh
- CategoryViewer::doCategoryQuery extensions/zh
- CategoryViewer::generateLink extensions/zh
- MediaWikiServices extensions/zh
- OutputPageRenderCategoryLink extensions/zh
- ParserFirstCallInit extensions/zh
- SkinAfterPortlet extensions/zh
- SkinBuildSidebar extensions/zh
- SpecialTrackingCategories::generateCatLink extensions/zh
- SpecialTrackingCategories::preprocess extensions/zh
- All extensions/zh
- Pages using deprecated NoteTA template
- Extensions bundled with MediaWiki 1.31/zh
- Extensions used on Wikimedia/zh
- Extensions included in BlueSpice/zh
- Extensions included in Canasta/zh
- Extensions available as Debian packages/zh
- Extensions included in Fandom/zh
- Extensions included in Miraheze/zh
- Extensions included in MyWikis/zh
- Extensions included in ProWiki/zh
- Extensions included in semantic::core/zh
- Extensions included in ShoutWiki/zh
- Extensions included in Telepedia/zh
- Extensions included in wiki.gg/zh
- Extensions included in WikiForge/zh
- Category extensions/zh
- Tree extensions/zh
- Ajax extensions/zh