Manual:界面/侧边栏

From mediawiki.org
This page is a translated version of the page Manual:Interface/Sidebar and the translation is 63% complete.
边栏示例
另见:手册:移除标签页

MediaWiki:Sidebar 允许用户修改导航栏。 导航栏能提供指向 wiki 中最重要位置的链接,并为网站管理员提供一个能添加一众重要链接的地方。 例如,大多数 wiki 会链接到它们的社区讨论页和实用工具页。

Monobook 和 Vector 皮肤将导航栏与搜索栏和工具箱一起放在左上角(从右到左的语言为右上角),但是该位置可能在其他皮肤上有所不同。 此侧边栏不向移动端站点的用户显示(即由 Extension:移动前端 / Skin:MinervaNeue 生成的站点版本 ——使用 MobileMenu 钩子实现)。

自定义侧边栏

要在 wiki 上自定义 MediaWiki:Sidebar,您首先需要使用具有“editinterface”权限的用户登录。 对于管理员,这是默认启用的。[1]

你可以以以下两种方式之一访问到 MediaWiki:Sidebar:

  • 在你的浏览器访问 http://yourdomain/wiki/index.php?title=MediaWiki:Sidebar&action=edit。
  • 在搜索栏输入 MediaWiki:Sidebar,然后按 Enter 确认。
  • 在“Special:所有消息”页面中搜索“side”。

以下是在 MediaWiki:Sidebar 中为侧边栏添加的示例代码:

* navigation
** mainpage|Accueil
** Special:RecentChanges|Recent changes
* new heading
** portal-url|Forums
** https://www.mediawiki.org|MediaWiki home

本示例将产生以下侧边栏:

边栏示例
如果你需要创建一个新页面,编辑时将在编辑器中显示侧边栏的默认内容。为了成功创建MediaWiki:Sidebar页面,您需要在保存之前更改内容。

将导航栏拆分为新的部分

导航栏可以分成几个部分,每个部分都有自己的新标题。 这是通过添加一个星号来完成的。

每个部分的标题取自第一级列表元素(上例中的“导航”和“新标题”)。 如果此文本与接口消息的名称(MediaWiki名称空间中该标题的现有页面)相对应,则该页面的文本用作标签; 否则,标题标题按原样使用。 请注意,魔术字 {{int:}} 不起作用,因为它没有用:MediaWiki 会在回退到缺省消息之前自动找到一条翻译来显示。

在上面的例子中,* navigation* new heading 是章节标题。

在侧边栏创建链接

用 2 个星号创建链接(这个叫二级列表元素)。 例如:

** 目标页面|链接文本
目标页面
链接目标可以是界面消息(MediaWiki 命名空间中的页面)或维基页面的名称,也可以是外部链接。 在任一情况下,链接可以是内部的、跨维基的或外部的。[2]
按以下步骤操作以确定目标:
  1. 获取目标文本。
  2. 如果存在具有该名称的现有或默认界面消息,请使用该消息的内容而不是目标文本。
  3. 如果上一步的输出是有效的 URL(以 http:// 或其他 URL 协议 开头),则链接将指向该 URL。
  4. 否则,它会将其视为 wikilink 的链接目标(链接到该页面名称或 interwiki)。
  5. 如果它最终会链接到“-”,则整个条目将从侧边栏中删除。 (这对于通过更改包含链接的消息来删除所有语言的条目很有用)。
示例:
  • "** portal-url|portal" 使用 MediaWiki:Portal-url 的文本(其中包含“Project:Community portal”)。
  • "** https://www.mediawiki.org|MediaWiki home" 链接到 https://www.mediawiki.org 因为这是个有效的 URL。
  • "** Special:RecentChanges|Recent changes" 链接到 [[Special:RecentChanges|Special:最近更改]],因为没有该名称的界面消息。
  • "** w:Foo|Some interwiki page" 出于同样的原因,链接到 w:Foo
链接文本
链接文本可以是界面消息的名称(MediaWiki 命名空间中的页面)或纯文本。
  • 如果链接文本是现有或默认界面消息的名称,则将使用该消息的内容。 MediaWiki 将检查本地化版本; 例如,如果当前语言是fr(法语),并且链接文本是界面消息“forum”,则在使用“forum”之前会检查“forum/fr”。
  • 否则,链接文本按原样用作目标。
  • 请注意,链接文本不是可选的,就像在普通的 wiki 链接中一样。 如果链接文本丢失,该项目将被忽略
示例:
  • ** 主页|mainpage 使用了 MediaWiki:Mainpage(内容为“MediaWiki”)。
  • ** Special:最近更改|最近更改 使用“最近更改”,因为没有名为“最近更改”的界面消息。

章节顺序(元素)

某些皮肤允许自定义某些特殊元素的顺序,比如搜索框、工具箱以及语言列表。 以标题语法的形式向 MediaWiki:Sidebar 添加 SEARCH、TOOLBOX 和 LANGUAGE 关键词,即可排放对应的元素。

但是,MonoBook 等一些皮肤不再支持定义搜索元素的位置,而是将搜索元素放置在侧边栏的第一部分之后。 在这种情况下,要将搜索元素移动到顶部,解决方法是定义一个空的前导部分。 例如:

* empty section
* navigation
** mainpage|mainpage
** Special:Recentchanges|Recent changed
* new heading
** portal-url|portal
** https://www.mediawiki.org|MediaWiki home

请注意,某些皮肤可能会以不同方式显示第一部分,或者在第一部分和第二部分(如果有)之间放置一些元素。

工具提示和快捷键

工具提示 (tooltip) 是当光标位于图形用户界面中的图标、图像、超链接或其他元素上时出现的消息。

如果您将一个项目添加到侧边栏,它将没有“工具提示”。但是您可以通过执行以下操作来创建工具提示:[3]

  • 创建一个名为“MediaWiki:Tooltip-n-<项目 ID>”的维基页面
  • 并将工具提示放在该页面中。

快捷键 (access key/accesskey) 允许计算机用户通过键盘立即跳转到网页的特定部分。

也可以通过执行以下操作来创建快捷键:

  • 创建一个名为“MediaWiki:Accesskey-n-<项目 ID>”的维基页面
  • 并将快捷键放在该页面中。

例如:

翻译

您可以通过编辑 MediaWiki 命名空间中的相应页面来翻译您使用的字符串。

示例:替换“mainpage”项目的字符串取自 MediaWiki:Mainpage。要为用中文显示您的维基的用户设置/更改此文本,您可以将相应的文本放在页面 MediaWiki:Mainpage/zh 上。

这样您就可以通过 MediaWiki 界面轻松翻译这些文本。

请注意,MediaWiki 将默认使用您 wiki 的默认语言显示侧边栏页面内容。所以,如果你的维基使用中文作为其默认语言,编辑 MediaWiki:Mainpage/zh 以便向所有未更改语言设置的用户和所有匿名用户显示修改后的侧边栏。

有关更多翻译的信息,参见 帮助:扩展:翻译/未结构化元素翻译

Advanced customization

The sidebar can be fully customized by implementing JavaScript or Cascading Style Sheets, or by editing the PHP files directly. Before using these methods, note that:

  • JavaScript is fragile: it will not work for users with JavaScript disabled, and scripts frequently fail in different browsers or skins.
  • Editing the PHP files can easily cause unexpected errors, and your changes will be lost in the next update unless you manually change the new files.

Remove the Sidebar and main logo at MediaWiki:Sidebar

To completely remove the sidebar and logo:

Blank the MediaWiki:Sidebar page and save.

Then add:

#column-content { margin: 0 0 1em 0; }
#content { margin: 0 0 0 0; }
#p-cactions { left: .1em; }

This moves the main text of your wiki to the left.

Completely remove the sidebar, tabs and search bar

See also: Manual:Remove Tabs

Deletes the logo, and the entire sidebar:

#p-logo, .generated-sidebar, #p-lang, #p-tb  { display:none; }

Deletes the search bar:

#p-search { display:none; }

Deletes the talk tab:

#ca-talk { display:none!important; }

Deletes the Page tab:

#ca-nstab-main { display:none!important; }

Force UI-messages to follow content

Some pages should sometimes follow the content language, especially for multilingual sites. This can be controlled with the setting $wgForceUIMsgAsContentMsg . Each message overridden in this way must be explicitly given, for example to let the sidebar link to versions given by the content language for the main page and the portal page add the following to

LocalSettings.php

code
$wgForceUIMsgAsContentMsg = [ 'mainpage', 'portal-url' ];

用 JavaScript 添加或移除工具部分

The toolbox appears on the left under MediaWiki:Sidebar. The toolbox is a dynamic element requiring programmatic methods such as skin extensions, PHP (see skins/MonoBook.php and creating a new skin) or JavaScript.

The JavaScript solution uses User:{username}/common.js / MediaWiki:Common.js, available for MediaWiki 1.9+.

Now simply configure which link should appear in which section. You also can remove some links if you want to.

code

function ModifySidebar( action, section, name, link ) {
	try {
		switch ( section ) {
			case 'languages':
				var target = 'p-lang';
				break;
			case 'toolbox':
				var target = 'p-tb';
				break;
			case 'navigation':
				var target = 'p-navigation';
				break;
			default:
				var target = 'p-' + section;
				break;
		}

		if ( action == 'add' ) {
			var node = document.getElementById( target )
							   .getElementsByTagName( 'div' )[0]
							   .getElementsByTagName( 'ul' )[0];

			var aNode = document.createElement( 'a' );
			var liNode = document.createElement( 'li' );

			aNode.appendChild( document.createTextNode( name ) );
			aNode.setAttribute( 'href', link );
			liNode.appendChild( aNode );
			liNode.className = 'plainlinks';
			node.appendChild( liNode );
		}

		if ( action == 'remove' ) {
			var list = document.getElementById( target )
							   .getElementsByTagName( 'div' )[0]
							   .getElementsByTagName( 'ul' )[0];

			var listelements = list.getElementsByTagName( 'li' );

			for ( var i = 0; i < listelements.length; i++ ) {
				if (
					listelements[i].getElementsByTagName( 'a' )[0].innerHTML == name ||
					listelements[i].getElementsByTagName( 'a' )[0].href == link
				)
				{
					list.removeChild( listelements[i] );
				}
			}
		}


	} catch( e ) {
		// let's just ignore what's happened
		return;
	}
}

function CustomizeModificationsOfSidebar() {
	// adds [[Special:CategoryTree|Special:CategoryTree]] to toolbox
	ModifySidebar( 'add', 'toolbox', 'CategoryTree', 'https://en.wikipedia.org/wiki/Special:CategoryTree' );
	// removes [[Special:Upload|Special:Upload]] from toolbox
	ModifySidebar( 'remove', 'toolbox', 'Upload file', 'https://en.wikipedia.org/wiki/Special:Upload' );
}

jQuery( CustomizeModificationsOfSidebar );
用法
function CustomizeModificationsOfSidebar() has to be customized for adding or removing links in specific sections:
ModifySidebar( "action", "section", "name", "link" );
参数
action add to add a link; remove to remove a link
section navigation, toolbox, languages but also any other existing customized section; the given link will be added to or removed from this section
name contains the text of the link
link contains the URL of the link

Restricting modifications to specific usergroups

If you want to restrict the modification of the links to a specific user group (e.g. bureaucrat), change:

jQuery( CustomizeModificationsOfSidebar );

to

if ( mw.config.get( 'wgUserGroups', [] ).indexOf( 'bureaucrat' ) !== -1 ) {
	jQuery( CustomizeModificationsOfSidebar );
}

For restricting the modifications to IPs instead of a specific user group, use

if ( mw.config.get( 'wgUserGroups', [] ).indexOf( 'user' ) === -1 ) {
	jQuery( CustomizeModificationsOfSidebar );
}

Add or remove toolbox sections (PHP)

You can add in your file LocalSettings.php the hook described on this page. With this, you can modify via MediaWiki:Sidebar the links in the toolbox panel (remove some links, change their name) and add custom links (e.g. add the "Recent changes" link there instead of the navigation panel).

Make expanding/collapsing sidebar for all users (Monobook skin only)

If you use the Monobook skin and don't want to miss the expandable menu bar from e.g. Vector skin, paste the following code into MediaWiki:Monobook.js of your wiki.

/////////////////////////////////////////////////////////
// Code snippet to make your sidebar items expandable. 
// Use this code ONLY for the Monobook skin. 
/////////////////////////////////////////////////////////

$( document ).ready( function() {
	// Set the default expanded items by their headline
	var defaultExpandItems = ['Navigation', 'Orga'];
	// Set the basic-name for the cookies, which save the current state of expanding
	var expandCookieName = 'disdance_project_wiki_nav_expanded_';

var maxHeights = [];
	var expandeds = [];
	var labels = [];
	initNav();
});

function initNav() {
	$( '#p-logo' ).css({'position': 'relative', 'display': 'block'});
	$( '.generated-sidebar h5,#p-tb h5 ').each( function( i ) {
		var id = $( this ).parent().attr( 'id' );
		maxHeights[id] = $( this ).next( 'div' ).height();
		var str = $( this ).html();
		labels[id] = str;

if ( $.cookie( expandCookieName + id ) == 'false' ) {
			expandeds[id] = false;
			minimize( $( this ) );
		} else if ( $.cookie( expandCookieName + id ) == 'true' ) {
			expandeds[id] = true;
			maximize( $( this ) );
		} else if ( defaultExpandItems.indexOf( str ) == -1 ) {
			expandeds[id] = false;
			minimize( $( this ) );
		} else {
			expandeds[id] = true;
			maximize( $( this ) );
		}
		$( this ).css({'cursor': 'pointer'});
		$( this ).click( toggleNav );
	} );
}

function minimize( target ) {
	var id = $( target ).parent().attr( 'id' );
	// You can change the expires parameter to save the cookie longer/shorter than 7 days like in this code
	$.cookie( expandCookieName + id, 'false', { expires: 7} );
	var str = labels[id] + '  ►';
	$( target ).next( 'div' ).animate({'height': '0px'});
	$( target ).html( str );
}

function maximize( target ) {
	var id = $( target ).parent().attr( 'id' );
	// You can change the expires parameter to save the cookie longer/shorter than 7 days like in this code
	$.cookie( expandCookieName + id, 'true', { expires: 7} );
	var str = labels[id] + '  ▼';
	var newHeight = maxHeights[id];
	$( target ).next( 'div' ).animate({'height': newHeight + 'px'});
	$( target ).html( str );
}

function toggleNav( e ) {
	var id = $(e.target ).parent().attr( 'id' );
	expandeds[id] = !expandeds[id];
	if( expandeds[id] == true ) {
		maximize( e.target );
	} else {
		minimize( e.target );
	}
}

///////////////////////////////////////////////////////
///////////////////////////////////////////////////////

Sidebar width in Monobook skin (CSS)

You can change the width of your sidebar by adding the following CSS rules to your MediaWiki:Monobook.css

note that this is an article, not a file. This changes the width to 15em, the actions position and portlet width should be an em or so less, so I've set them to 14em in this example.

/* increase sidebar width */
#column-content { margin-left: -15em; }
#column-content #content { margin-left: 15em; }
#p-logo a, #p-logo a:hover { width: 15em; }
#p-cactions { left: 14.5em; }
.portlet { width: 14em; }
#column-content { margin-left: -14em; }
#content.mw-body { margin-left: 14em; }

Change sidebar content when logged in (PHP)

Install and configure the DynamicSidebar extension.

Parser functions in sidebar

Although it is not exactly recommended, the sidebar does support parser functions (such as ParserFunctions ), provided the ** is outside the parser function, and the parser function does not span multiple lines. For example:

*Heading
**{{#ifeq:{{NAMESPACE}}|User|Page-to-link-to{{!}}text-to-show-on-user-page}}
**{{#ifeq:{{NAMESPACE}}|Talk|Another-Page-to-link-to{{!}}text-to-show-on-talk-page}}

works. However the following would not work since the parser function spans multiple lines:

*Heading
{{#ifeq:1|1|
**foo{{!}}
bar
|
**baz{{!}}fred
}}

故障排除

更改未显示

如果可能,MediaWiki 会主动缓存内容,这通常会导致导航栏在更改后仍然存在。 清除受影响的页面的缓存应该可以纠正这种情况。 要一次对所有缓存执行批量清除,touch LocalSettings.php 文件或 truncate 数据库中的 objectcache 表(您可能必须同时执行这两项操作)。

您还需要将 $wgUseDatabaseMessages 设置为 true。 此外,如果您的链接中没有 |,它们将不会显示,因为 Skin.php 中有这行代码: if ( strpos( $line, '|' ) !== false ) {

如果您是用户或普通 wiki 管理员,您将无法执行上述高级操作来显示更改。如果清除也不起作用,您有时仍然可以在预览中立即看到 CSS 更改(尤其是在更改您的个人 CSS 时)。例如:更改侧边栏宽度、填充或边距。不过,更改可能需要一段时间才能在保存后显示出来。如果一段时间后没有任何显示,您可能需要联系整个 wiki,或 wiki 农场的管理员。

小写链接标签

如果您的链接未按预期大写,请尝试在 | 字符周围添加空格。例如:

** http://www.example.com/download.php?file=44555 | Download

部分内容消失或显示意外内容

如果某个部分不显示或显示意外内容,请通过搜索 Special:AllMessages 检查标题文本是否不是接口消息的名称。如果是,请使用不同的标题文本,或创建新的界面消息并使用它。

例如,要使用“Sidebar”作为标题文本,请创建仅包含“Sidebar”的界面消息“MediaWiki:Sidebar-header”。 然后,使用 * sidebar-header 作为标头。

如果没有 ** target | link text 形式的链接,则不会显示部分(例如,您忘记链接目标时)。

更新 Special:Version 后链接失效

例如,每当您的站点语言的侧边栏项目有更新的翻译时,就会发生这种情况。每次都可以添加新的重定向页面,但更好的解决方案是使用自己的侧边栏项目名称,而不是试图跟踪当前的 MediaWiki 翻译。

参见

References

  1. (有关为组或用户分配权限的信息,请参阅 手册:用户权限 。) 然后,使用你的 wiki 上的一个页面链接,但是要把地址栏/URL里你的 wiki 页面标题替换成 MediaWiki:Sidebar(如 首页)然后加载那个页面 - 现在你应该看得到侧边栏当前的内容。(如果你使用长 URL,可能需要使用语法 //yourdomain/yourwiki/index.php?title=MediaWiki:Sidebar&action=edit) 如果侧边栏的内容已被修改,只需点击“编辑”或“编辑源代码”链接,否则你可能需要通过点击“创建”或“创建源代码”链接先创建页面。
  2. 在某些情况下(例如带有“&”字符的链接)需要一个界面消息。链接目标不能依赖于首选项中设置的界面语言。
  3. http://www.kaarebmikkelsen.dk/?p=174