Jump to content

Manual:$wgForeignFileRepos

From mediawiki.org
This page is a translated version of the page Manual:$wgForeignFileRepos and the translation is 65% complete.
共享上传: $wgForeignFileRepos
一个包含外部存储库结构信息的数组
引进版本:1.11.0​(r22580
移除版本:仍在使用
允许的值:(数组)
默认值:(见下文)

细节

警告 警告: 当使用维基共享资源时,需要使用HTTPS链接而不是HTTP。自2015年起,维基媒体基金会重定向HTTP流量到HTTPS。由于安全原因MediaWiki不会追踪这些重定向(参见T102566)。

所有库所需的属性

class
存储库的类名。可能来自核心或扩展。核心存储库类包括LocalRepo、ForeignDBRepo、FileRepo和ForeignAPIRepo。
name
A unique name for the repository.

For all core repos

url
Base public URL
thumbUrl
Base thumb url, if different from url/thumb
hashLevels
The number of directory levels for hash-based division of files
thumbScriptUrl
The URL for thumb.php (optional, not recommended)
transformVia404
Whether to skip media file transformation on parse and rely on a 404 handler instead.
initialCapital
等同于$wgCapitalLinks ,用于确定文件名是否默认以大写字母开头。 当本地的$wgCapitalLinks 和initialCapital不匹配时,当前的实现可能会产生错误的描述页链接。
pathDisclosureProtection
可以是“paranoid”(从错误消息中移除所有参数)、“none”(保持路径不变)或“simple”(用占位符替换路径)。LocalRepo的默认值是“simple”。 请注意,某些图像缩略图生成软件会将缩略图路径放入生成的缩略图的元数据中,因此此设置可能无法提供全面保护。
descBaseUrl
图片描述页面的网址,例如 https://en.wikipedia.org/wiki/File:
scriptDirUrl
MediaWiki安装的URL,等同于$wgScriptPath ,例如 https://en.wikipedia.org/w
articleUrl
等同于$wgArticlePath ,例如 https://en.wikipedia.org/wiki/$1
fetchDescription
获取远程文件描述页的文本。等同于 $wgFetchCommonsDescriptions
警告 警告: 由于MediaWiki中存在一个漏洞,外部维基的$wgUploadPath 中应包含完整的URL或与协议相关的URL(但不是一般的相对URL),否则带有图片的描述页面将无法正确获取。
descriptionCacheExpiry
如果设置为0,则不使用缓存。设置为1或更大的值(秒)可定义描述页面的本地缓存将持续多长时间。必须将fetchDescription设置为true才能使用。

ForeignAPIRepo类

apibase
远程仓库API的基础URL(例如 https://commons.wikimedia.org/w/api.php)。 仅用于ForeignAPIRepo。
apiThumbCacheExpiry
本地缓存缩略图的时长。将此设置为0会禁用本地缩略图缓存。 本地缩略图缓存将减轻外部服务器的负载,并提高用户隐私性。不过,这可能会使您的维基速度稍慢一些。

ForeignDBRepo类

directory
服务器本地的MediaWiki媒体目录路径,例如/var/www/wiki/images
dbType, dbServer, dbUser, dbPassword, dbName, dbFlags
等同于 $wgDBservers 的对应成员
tablePrefix
表前缀,外部维基的 $wgDBprefix
hasSharedCache
如果维基的共享缓存可通过本地 $wgMemc 访问,则为真
favicon
远程存储库的网站图标

默认值(位于includes/SetupDynamicConfig.php中的代码)

if ( $wgUseSharedUploads ) {
	if ( $wgSharedUploadDBname ) {
		$wgForeignFileRepos[] = [
			'class' => ForeignDBRepo::class,
			'name' => 'shared',
			'directory' => $wgSharedUploadDirectory,
			'url' => $wgSharedUploadPath,
			'hashLevels' => $wgHashedSharedUploadDirectory ? 2 : 0,
			'thumbScriptUrl' => $wgSharedThumbnailScriptPath,
			'transformVia404' => !$wgGenerateThumbnailOnParse,
			'dbType' => $wgDBtype,
			'dbServer' => $wgDBserver,
			'dbUser' => $wgDBuser,
			'dbPassword' => $wgDBpassword,
			'dbName' => $wgSharedUploadDBname,
			'dbFlags' => ( $wgDebugDumpSql ? DBO_DEBUG : 0 ) | DBO_DEFAULT,
			'tablePrefix' => $wgSharedUploadDBprefix,
			'hasSharedCache' => $wgCacheSharedUploads,
			'descBaseUrl' => $wgRepositoryBaseUrl,
			'fetchDescription' => $wgFetchCommonsDescriptions,
		];
	} else {
		$wgForeignFileRepos[] = [
			'class' => FileRepo::class,
			'name' => 'shared',
			'directory' => $wgSharedUploadDirectory,
			'url' => $wgSharedUploadPath,
			'hashLevels' => $wgHashedSharedUploadDirectory ? 2 : 0,
			'thumbScriptUrl' => $wgSharedThumbnailScriptPath,
			'transformVia404' => !$wgGenerateThumbnailOnParse,
			'descBaseUrl' => $wgRepositoryBaseUrl,
			'fetchDescription' => $wgFetchCommonsDescriptions,
		];
	}
}

ForeignDBViaLBRepo class

wiki
wiki-db-name used in $wgLBFactoryConf

Directory permissions

对于PHP运行时所使用的任何用户,你都需要对$IP/images$IP/images/thumbs拥有读写权限。

用法

MediaWiki版本:
1.31

使用来自维基共享资源的文件:ForeignAPIRepo

自1.16.0版本起,有一个更便捷的简写方式可将维基共享用作外部仓库,即$wgUseInstantCommons
If your wiki is throttled by Wikimedia Commons, please update to MediaWiki 1.45.0 (or MediaWiki 1.39.14+, 1.43.4+, or 1.44.1+) to comply with the User-Agent Policy.

您可以将您的维基设置为直接使用来自维基共享资源(或来自任何其他基于MediaWiki的网站,见下文)的媒体。但请注意任何法律影响。

下面的代码可在您的网站上启用来自维基共享资源的媒体文件。您应将其放置在您的 LocalSettings.php 文件中:

$wgForeignFileRepos[] = [
	'class' => ForeignAPIRepo::class,
	'name' => 'commonswiki', // Must be a distinct name
	'apibase' => 'https://commons.wikimedia.org/w/api.php',
	'hashLevels' => 2,
	'fetchDescription' => true, // Optional
	'descriptionCacheExpiry' => 43200, // 12 hours, optional (values are seconds)
    'apiMetadataExpiry' => 14400, // 4 hours, optional. Consider increasing for better performance.
	'apiThumbCacheExpiry' => 86400, // 24 hours, optional, but required for local thumb caching (Note local thumb caching is often slower than not doing it)
];
If you decide to add this setting to your LocalSettings.php file instead of using $wgUseInstantCommons you cannot use wikimediacommons as a setting for name since it is already present in core MediaWiki code (in Setup.php).

To pull images from another Wikimedia project, set apibase to this wiki's "api.php" file like e.g. https://en.wikipedia.org/w/api.php. 例如:

$wgForeignFileRepos[] = [
	'class' => ForeignAPIRepo::class,
	'name' => 'enwikipedia',
	'apibase' => 'https://en.wikipedia.org/w/api.php',
	'hashLevels' => 2,
	'fetchDescription' => true,
	'descriptionCacheExpiry' => 43200,
	'apiThumbCacheExpiry' => 86400,
];

To embed an image in your installation, simply use [[File:name_of_commons_picture.jpg]].

Performance

你可能还需要配置$wgMainCacheType 。 默认值通常为 CACHE_NONE,这意味着它会在每次页面加载时从远程主机加载图像,速度会非常慢。 同样地,如果你更愿意使用速度更快的外部缩略图,就需要将apiThumbCacheExpiry设为零。 apiMetadataExpiry 的默认值为 4 小时。 这是对性能影响最大的设置之一。 考虑将其设置为一个非常高的值,例如604,800(1周),以获得最佳性能。 然而,这可能意味着文件的更改可能需要长达这么久的时间才能同步到你的维基上。

设置 $wgResponsiveImages = false; 可能会提高即时共享资源的性能,但代价是在高分辨率显示器上图像质量会降低。

目前MediaWiki不支持对外部API请求进行流水线处理。 一个高性能的网站可能会考虑设置一个本地代理(如nginx),它可以将多个请求合并为一个流水线请求,以减少TCP和TLS握手带来的往返时间。

Extension:QuickInstantCommons 处还有一个替代实现,可能会快得多

从数据库中使用文件您可以使用:ForeignDBRepo、ForeignDBViaLBRepo

ForeignDBRepo类对于创建维基家族非常有用。在维基家族中,每个维基都有自己的数据库或表前缀。 使用这个类,你可以让一个家庭成员知晓另一个家庭成员的表格。 通过ForeignDBRepo访问比通过ForeignAPIRepo访问更快。 This code should be deployed to LocalSettings.php.

$wgForeignFileRepos[] = [
	'class' => ForeignDBRepo::class,
	'name' => 'otherwiki',
	'url' => "https://example.org/w/images",
	'directory' => '/path/to/uploads',
	'hashLevels' => 2, // This must be the same for the other family member
	'dbType' => $wgDBtype,
	'dbServer' => $wgDBserver,
	'dbUser' => $wgDBuser,
	'dbPassword' => $wgDBpassword,
	'dbFlags' => DBO_DEFAULT,
	'dbName' => 'mydb',
	'tablePrefix' => 'mw_',
	'hasSharedCache' => false,
	'descBaseUrl' => 'https://example.org/wiki/File:',
	'fetchDescription' => false
];

Alternatively, if you have $wgLBFactoryConf set up for multiple wikis, you can use 'class' => ForeignDBViaLBRepo::class:

$wgForeignFileRepos[] = [
	'class' => ForeignDBViaLBRepo::class,
	'wiki' => 'wiki-db-name-used-with-wgLBFactoryConf',
	'name' => 'otherwiki', # displayname (eg. the vector-skin-tab “View on OtherWiki“) may be set at wiki-specific MediaWiki:Shared-repo-name-<otherwiki>
	'url' => 'https://example.org/w/images',
	'directory' => '/path/to/uploads',
	'hashLevels' => 2, # This must be the same for the other family member
	'tablePrefix' => 'mw_',
	'hasSharedCache' => false,
	'fetchDescription' => true,
	'scriptDirUrl' => 'https://example.org/w', # has effect whether description can be fetched
];

This needs not all the db* parameters as in ForeignDBRepo.

从本地文件夹使用文件:FileRepo

您可以将您的维基设置为使用单个文件夹中的媒体文件。目前这只是一个演示功能,由于缺乏缓存,对于繁忙的维基或速度较慢的服务器来说,它可能会太慢。此代码应添加到LocalSettings.php中。

The below code enabled media files from it:

$wgForeignFileRepos[] = [
	'class' => FileRepo::class,
	'name' => 'sharedFsRepo',
	'directory' => 'media',
	'hashLevels' => 0,
	'url' => 'https://shared.example.org/path/to/media',
];

Caveats

为获得最佳性能,请使用一个主要用途是作为共享资源库的维基作为$wgForeignFileRepos的目标。 例如,避免将两个内容维基相互设为外部资源库以实现文件共享,因为这样一来,在页面浏览和编辑时会产生过多的文件请求——两个维基会相互请求文件。

同样,避免在共享资源维基和其他内容维基之间共享数据库,特别是如果由于服务提供商或管理员施加的限制,你必须使用ForeignAPIRepo而不是ForeignDBRepo的话。 这会生成大量可能长期存在的数据库连接,这可能导致性能下降,甚至在流量或活动极少的维基上也可能超过数据库连接限制。

参阅

  • filerepoinfo API for getting information about the configuration of a repo, e.g.

https://www.mediawiki.org/w/api.php?action=query&format=json&meta=filerepoinfo