手册:LocalSettings.php

This page is a translated version of the page Manual:LocalSettings.php and the translation is 60% complete.
Outdated translations are marked like this.
警告 警告: 不要使用文档编辑器(例如1809及之前版本的Windows 10上的记事本、TextEdit或其他将字节顺序标记添加至文件的文本编辑器)编辑LocalSettings.php。 这会破坏您wiki的PHP运行时。 请改用代码编辑器,例如VimNotepad++。 它们会正确处理文件编码。 它们也可以用于修复之前被文档编辑器破坏的文件。

LocalSettings.php文件提供了MediaWiki安装的基本配置设置(基于DefaultSettings.php 文件)。 您应该花些时间查看文件中的设置。 该文件一般是由基于Web的MediaWiki安装程序生成,但您可以调整参数,与Apache配合使用。 若将MediaWiki作为维基农场配置,可能会使用一个被称为CommonSettings.php 的文件。

服务器上的位置

LocalSettings.php文件不是一个Wiki页面且您不能够通过网页浏览器访问它。 相反,它是服务器文件系统中的一个文件。 它的内容是在wiki的初始设置过程中生成的,生成的文件必须手动复制到服务器上。 此文件必须位于您安装MediaWiki的目录中,与文件夹includes/skins/和文件api.php在同一级目录。 若该文件不在这里,这个Wiki将完全不会工作;若这个Wiki在工作,那么该文件就在这里。 如果您不知道这个文件在哪里,您可以在终端窗口中输入例如命令来查找它,例如:find / -iname LocalSettings.php -print

(如果你正在使用 Vagrant,另见 MediaWiki-Vagrant#MediaWiki_settings。)

安全

LocalSettings.php通常包含敏感数据比如数据库登录信息。 这个数据永远不应向公众透露。 由于服务器上某处存在安全漏洞,可能会发生其他用户可以查看文件内容。 为了提高数据的安全性,您应该相应地为此文件设置UNIX权限:服务器用户必须能够访问这个文件。 若与拥有该文件的帐户相同,则可以将权限设置为600。 有时服务器用户不是这个文件的所有者,而在所有者的UNIX用户组中。 这时权限应被设置为640。为了提高安全性,您应尽可能使用最小权限。

此外,您可以创建一个仅在Wiki上使用MySQL用户并在LocalSettings.php中提供相关的凭据。此外,您还可以配置您的MySQL服务器仅接受来自于本地的连接,这将降低从外部访问使凭据泄露的情况。

另请参阅 手册:保护数据库密码 以了解将 LocalSettings.php 的敏感部分移动到不同目录中的不同文件的方法。

文件内容

当您在编辑LocalSettings.php文件时,请确保以正确的编码下保存。您应该使用“ANSI as UTF-8”编码(即无字节顺序标记的UTF-8编码)。

LocalSettings.php文件中包含了PHP代码,主要是变量和其值的定义。更改设置通常意味着更改PHP变量的值。将文件保存到服务器后,您的更改将立即生效:不需要手动“重启”任何东西。尽管在某些情况下,您可能需要清除浏览器的缓存才能看到您所做的更改。

许多默认值includes/DefaultSettings.php中被设定,您不应该去改动它。如果您要更改的变量未在LocalSettings.php中提及,请从DefaultSettings.php中复制相应的行到LocalSettings.php中并做些相应的更改。LocalSettings.php中,您可以在末尾添加新行。 Within LocalSettings.php you can add new lines at the end.

在该文件中,将有几行内容指向扩展,例如:“require_once "$IP/extensions/extension.php";”这几行允许在Wiki中启用相应的扩展。这些扩展可能需要在LocalSettings.php中设定更多变量的值, 有关进一步的说明,请参阅相应扩展的文档。 These lines enable the according extension in the wiki. Those extensions may require setting the values of more variables in LocalSettings.php; check the documentation of the according extension for further instructions.

像MediaWiki中的大多数PHP文件一样, LocalSettings.php文件并不以关闭的PHP标签?>结束。这样可以防止管理员意外地在此标签之后添加新信息。PHP没有关闭标签工作正常。

倘若您有一个在维基农场上的MediaWiki,您可能并没有编辑(可能也没有阅读的权限)LocalSettings.php文件的权限(例如参见[1])。Wiki Farm的公司可能愿意或不愿做出您所期望的更改。也许他们希望在维基农场的所有维基上保持多数相同的配置。

出于安全原因,这个文件不在网络服务器上自动写入。它作为下载内容提供,您必须将它上传到您的服务器上以使您的Wiki工作。获取更为完整的解释,参阅这个页面

可用参数概览

请参阅配置设置索引以及设置文件中包含的说明,以获取所有相关变量的帮助。以下列出了最重要的变量以及多数需要的功能的简短列表。

标准设置

安裝路徑

$IP (安装路径)变量保存了你的 wiki 基本安装的本地文件路径。 从 MediaWiki 1.18 开始,系统会自动设置 $IP。 不再需要在 $IP 中手动设置 LocalSettings.php$IP 将默认为当前工作目录,无需手动定义即可使用。 Attempting to set $IP (or MW_INSTALL_PATH) in LocalSettings.php may produce unexpected results.

DefaultSettings.php 文件从 IP 变量指定的目录中加载。

网站名称

$wgSitename 保存您安装wiki时设置的名称。这个名字在整个系统中被多次包含如通过MediaWiki:Pagetitle。例如,维基百科的标语“维基百科,自由的百科全书”就使用了这个设置。

网站名称的变格

界面的一些翻译已准备好用于站点名称的变化。 您可以在变量 $wgGrammarForms 中设置正确的单词形式(例如,请参阅 $wgSitename 文档页面)。

网站语言

$LanguageCode用于控制您wiki的界面语言。 尽管用户可以通过在参数设置中改变他们看到的语言,但此变量设置了所有匿名用户和大多数注册用户看到的默认语言。

脚本路径

$wgScriptPath 是访问主要 MediaWiki 脚本的 URL 路径前缀,该脚本是 MediaWiki 的核心代理代码。 这一设置应该与Apache的设置相符合,尤其是如果您使用了Apache的URL重写规则。

服务器名称

$wgServer 包含服务器的基本 URL,包括协议,但没有尾部斜杠,也没有子目录(如果有)。 当 wiki 从它运行的本地主机、Intranet 或 Internet 访问时,电子邮件通知和一些其他计算消息通常会通过不同的 URL 传送。

从 MediaWiki 1.34 开始,$wgServer 必须设置为 LocalSettings.php。 在此之前,MediaWiki 尝试自动检测服务器的名称,$wgServer 是可选的,以覆盖自动检测。

如果您wiki服务器在互联网上的地址是www.example.com ,那么请加入类似如下的一行:

$wgServer = 'http://www.example.com';

magic word 变量 {{SERVER}} 可用于维基页面; 它等于 $wgServer 的值; 然而,在维基媒体系列中,使用相对网址的维基,它不会完全展开,例如这里显示//www.mediawiki.org,既不能点击也不能复制粘贴到地址栏 照原样。

您还可以使用协议相对 URL,如下所示。 When using a protocol relative URL, be sure to set $wgCanonicalServer. $wgCanonicalServer is needed for some places in the code where an origin with a protocol and hostname is needed.

$wgServer = '//www.example.com';
$wgCanonicalServer = 'https://www.example.com';

脚本名称

$wgScript 是主要(索引)MediaWiki PHP 脚本的名称,默认名为 index.php。 更改脚本名称可能不是一个好主意。 但是,如果您有这样做的冲动,那么这里就是您进行更改的地方。 确保你知道你在做什么。

文章路径

$wgArticlePath 是访问 MediaWiki 页面时使用的路径。 该路径应包含主脚本的路径(通常使用 $wgScript)并使用 $1 占位符作为文章名称。 The path should contain the path to the main script (usually making use of $wgScript) and use the $1 placeholder for the article name.

如果您使用 Apache 重写规则来创建漂亮且简短的 URL,您可能需要调整 $wgArticlePath 以找到正确的路径。 请注意, wgArticlePath 用于从 MediaWiki 中构建 URL。 如果您在这里犯了错误,内部链接将不正确显示,而您仍然可以通过手动指定正确的 URL 来访问主页。 有关 URL 配置的更多信息,请参阅 手册:短链接

典型值为:

"$wgScript/$1" 用分隔符“/”传递文章名称
"$wgScript?title=$1" 将文章名称作为参数传值(旧风格)
"/mypath/$1" 自定义路径。使用 Apache 重写规则将“mypath”转换为访问主脚本的正确路径

样式表位置

使用 $wgStylePath 将 URL 路径设置为 MediaWiki 安装的样式表 (CSS) 所在的位置。 $wgStyleDirectory 应该指向同一个地方,但请注意,这是一个本地文件系统路径,用于访问文件系统的内部脚本。

上传位置

上传目录是存放用户上传文件的地方。 $wgUploadPath 指定 URL 路径,$wgUploadDirectory 指向本地文件系统路径。

标志

$wgLogo 指定在所有 MediaWiki 页面的左上角显示哪个图形标志。 这些步骤替换 /wiki/skins/common/images/ 目录中的默认标志,将 /wiki 替换为您安装 MediaWiki 软件的目录的路径。

首先,将您想要的任何标志复制到 /wiki/skins/common/images/ 目录中(将 /wiki 替换为您安装 MediaWiki 软件的目录的路径)。

接下来,尝试在 LocalSettings.php 中找到如下所示的行:

$wgLogo = "$wgStylePath/common/images/wiki.png";

如果没有这样的行,您可以将上面的行复制并粘贴到文件末尾。

然后,修改该行以指向您的标志。标志必须可通过网络访问。 该变量的值被传递到 Web 浏览器,浏览器使用它来获取标志。如果有疑问,选择要放在这里的内容的一个好方法是导航到 Web 浏览器中的标志(例如,此 wiki 标志的URL是 http://upload.wikimedia.org/wikipedia/mediawiki/b/bc/Wiki.png),并将完整的 url 作为该变量的值。

有些人只是用他们的标志替换 skins/common/images/wiki.png 文件。不推荐这样做,因为升级时会覆盖自定义标志。

MediaWiki 1.35 introduced $wgLogos , which allows multiple versions of the same logo. An example of using multiple logos is shown below.

$wgLogos = [
	'1x' => "$wgStylePath/common/images/1x_version.png",
	'1.5x' => "$wgStylePath/common/1.5x_version.png",
	...
	'tagline' => [
		'src' => "$wgStylePath/common/tagline_version.png",
		'width' => 135,
		'height' => 15,
	],
];

联络信息

$wgEmergencyContact is the email address of the user to contact if something goes wrong. This email address is used to send internal bug reports to. As an administrator, you want to include your email address here.

$wgPasswordSender is the email address where email gets sent from, when passwords are sent out to users who have forgotten their passwords. Choose an address people can reply to in case of trouble or confusion.

数据库设置

MediaWiki 需要访问数据库(使用 MySQLPostgreSQL)来存储页面、修改、用户信息和更多内容。

$wgDBserver 包含托管数据库的主机名。 在大多数情况下,这只是“localhost”,因为数据库运行在同一系统上,但对于分布式安装,您需要填写运行数据库的计算机的完全限定域名。

$wgDBname 是 MediaWiki 使用的数据库的"数据库名称"。 单个 MySQL 或 PostgreSQL 安装可以存储多个数据库,您甚至可以在单个服务器上运行多个 MediaWiki 安装。 确保您在此处声明了正确的数据库名称,并为同一数据库服务器上的不同 wiki 安装使用不同的数据库名称。

$wgDBuser $wgDBpassword 包含 MediaWiki 用于访问数据库的登录名和密码。 确保指定的用户具有适当的访问权限,以便能够操作数据库服务器上的 wiki 表。

Also see 手册:保护数据库密码 for a method to move the sensitive parts of LocalSettings.php to a different file in a different directory.

用户权限

$wgGroupPermissions is an associative array, controlling permissions for creating and editing pages for your different user groups. In this array, custom permission levels can be created, and permission levels for the different user groups can be set. See Help:User rights for more information about the different permissions and user groups available.

Force capital links

By default, no page name can start with a lowercase Roman letter: in an attempt to do so the first letter is converted to uppercase; if a link target, included page, image or category is specified with a name starting with a lowercase letter, the actual target etc. is the page starting with the corresponding capital.

Alternatively page names can start with a lowercase letter, in addition to the possibilities of starting with a capital, a digit, etc. For that you must adjust $wgCapitalLinks . Setting it to false allows lowercase characters, true chooses the default behavior.

启用子页面

In LocalSettings.php, subpages are enabled on a per-namespace basis using $wgNamespacesWithSubpages . For example, to enable subpages in the main namespace:

$wgNamespacesWithSubpages[NS_MAIN] = 1;

文件上传

Before users are allowed to upload files to the MediaWiki system, you have to enable that feature. Make sure the Upload Directory is properly configured and writeable by the Apache web server process. Then set $wgEnableUploads to true to allow uploading in the web user interface.

i.e. Here's some example code from includes/DefaultSettings.php to put in LocalSettings.php:

$wgUploadPath       = "$wgScriptPath/uploads";      ## Wiki 1.5 defaults to /images, but allows more than just images
$wgUploadDirectory  = "$IP/uploads";                ## Wiki 1.5 defaults to /images, but allows more than just images

## To enable image uploads, make sure the above '$wgUploadPath' directory is writable by Apache User or group.
## ''(i.e.  chmod og+w uploads images)''  then the following should be true:
$wgEnableUploads       = true;

$wgUseImageMagick      = true;
$wgImageMagickConvertCommand = "/usr/bin/convert";

## If you want to use image uploads under safe mode, create the directories images/archive, images/thumb and
## images/temp, and make them all writable. Then uncomment this, if it's not already uncommented:
$wgHashedUploadDirectory = false;

If you want to be able to resize images on the fly to support thumbnails, MediaWiki needs a working ImageMagick installation. Set $wgUseImageMagick to true once you have installed and tested ImageMagick on your system. Make sure $wgImageMagickConvertCommand points to the proper location of the convert command of your installation, that the command is executable by the web server process, and $wgMaxShellMemory is large enough. See Manual:Image administration#Image thumbnailing for detailed information and troubleshooting.

Also, you may want to modify the list of accepted extensions, which is stored within $wgFileExtensions :

$wgFileExtensions = [ 'png', 'jpg', 'jpeg', 'ogg', 'doc', 'xls', 'ppt', 'mp3', 'sxc', 'pdf' ];

In case ImageMagick outputs an error message similar to the following:

Fatal error: mime_magic could not be initialized, magic file is not available in includes/MimeMagic.php on line 506

then try adding the following line to LocalSettings.php:

$wgMimeDetectorCommand = "file -bi";

跨wiki支持

InterWiki support is built into MediaWiki but you need to configure the prefix to be used for your internal links. This prefix is usually the same as $wgSitename , but in case you need to change that, you set the $wgLocalInterwikis variable to the preferred name.


用户界面语言

MediaWiki allows for a variety of localized user interfaces languages instead of the English default. If you want to run your wiki in a non-English language, set the $wgLanguageCode variable to the proper language code (e.g. "de" for German, "es" for Spanish, etc.)

You may use any of the languages with a file in the directory languages/i18n. E.g. if you find es.json, then you can use "es" for Spanish. Do not use "Es" with a capital letter. Although it seems to work, not all texts are translated.

After changing the language code, you will need to run a PHP script to make it work. In a terminal window, change to your wiki directory, change to the maintenance folder, and enter php rebuildMessages.php --rebuild. On Windows, the PHP folder might not be in your PATH environment variable and you'll have to prefix php with the path to the program.

Not all languages are supported. See the translation statistics on TranslateWiki for an up-to-date list of which languages are supported and to what extent. That list reflects the current state of MediaWiki core, so depending on what version you are using there may be a discrepancy.

设置网站的版权

  • $wgRightsText is the text in the footer that follows "Content is available under". It will be linked to the page specified in $wgRightsPage.
  • $wgRightsIcon is the URL of the image placed at the left of the footer.
If $wgRightsPage is non-empty, the link in the copyright/license notice will link to that page on your site. If $wgRightsPage is empty then the copyright/license notice will link to $wgRightsUrl instead.

To modify the copyright statements of the site, add something like this to LocalSettings.php:

$wgRightsPage = "YourWiki:Copyright";
$wgRightsText = "copyright YourWiki";

Afterwards, edit MediaWiki:Copyright to provide an appropriate message, using "$1" to indicate the position where the link to your copyright page will be.

Example: setting a Creative Commons license

To set a Creative Commons license do the following:

  • Examine the returned HTML code, e.g.:
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/">
<img alt="Creative Commons License" border="0" src="http://creativecommons.org/images/public/somerights30.png" />
</a>
This work is licensed under a
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/">
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License</a>.
  • For $wgRightsURL enter the href info from the first anchor:
$wgRightsUrl = "http://creativecommons.org/licenses/by-nc-sa/3.0/";
  • For $wgRightsText enter the text the second anchor links from (add the "a" to the Wiki variable for a more grammatically correct version)
$wgRightsText = "a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License";
  • For $wgRightsIcon enter the src info from the img link in the first anchor. You want to copy the badge to your own site instead of using the one from the Creative Commons site.
$wgRightsIcon = "http://creativecommons.org/images/public/somerights30.png";
  • For using RDF metadata about copyright add one or both of the following lines to your LocalSettings.php:
$wgEnableCreativeCommonsRdf = true;
$wgEnableDublinCoreRdf = true;


自定义名字空间

By declaring $wgExtraNamespaces , and modifying $wgNamespacesWithSubpages , and $wgNamespacesToBeSearchedDefault , extra namespaces can be added to a MediaWiki installation; and by declaring the $wgNamespaceAliases array namespace aliases can be added. Take heed not to have any pages already titled in that namespace, for instance if you had a page called "Technical:Support" and you created the Technical: namespace, then that page would not only be lost, but you cannot remove it from Special:Allpages. To fix this delete the namespace, move "Technical:Support" to "Support" in mainspace, delete the redirect, reinsert the namespace, and move it back to "Technical:Support". See Custom namespaces for more information as to how to do so.

皮肤

站点的默认皮肤可以通过$wgDefaultSkin 进行修改。

参见