This page is a translated version of the page Composer and the translation is 34% complete.
Outdated translations are marked like this.

Composer是一个PHP库的依赖管理器。 自MediaWiki 1.25 版本起,MediaWiki核心的外部库依赖由Composer管理。 除此之外,自MediaWiki 1.22 版本起,它还可以管理安装的MediaWiki扩展。 然而目前对此的支持并不完美。 这将在未来有所改观。

安装Composer

在Unix / Linux和macOS上

安装Composer有多種方法,最简单是执行sudo apt install composer

Only Composer 1.x is supported by MediaWiki 1.31, 1.35.0 and 1.35.1. To grab the latest Composer 1.x stable release:

wget -cO - https://getcomposer.org/composer-1.phar > composer.phar

Composer 2.x and Composer 1.x are supported by MediaWiki >= 1.35.2 (along with the REL1_36 branch and master). To grab the latest Composer 2.x stable release:

wget -cO - https://getcomposer.org/composer-2.phar > composer.phar

对于OS X用户,您可以使用 Homebrew安装Composer: Note this will install version Composer 2.x, so depending on the version of MediaWiki you are using, you may need to follow the wget instructions above instead to download Composer 1.x. To install using brew:

brew install composer

如果这不起作用,或者您没有安装Homebrew,您可以尝试:

php -r "readfile('https://getcomposer.org/installer');" | php

Toolforge and the extension distributor use the copy of Composer contained in the "integration/composer" git repo.

Once Composer is installed, you can run commands via the downloaded phar:

php composer.phar someCommand

You may want to move the phar into your path so it can be used as a normal executable:

mv composer.phar /usr/local/bin/composer
composer someCommand

If the mv command fails due to permissions, execute it again with sudo.

sudo mv composer.phar /usr/local/bin/composer
composer someCommand

在Windows上

Just download and run the installer from the download page. If you prefer manual installation, you can follow the above steps except that Windows doesn’t have wget (or APT or Homebrew) by default, and there is no /usr/local/bin directory. In these cases, you can just download composer-1.phar in your web browser and then rename it to composer.phar.

Local installation

You're not out of luck if you don't have permissions to run Composer on the server. It is also possible to download the MediaWiki folder to your local machine, install and run Composer, and transfer the files back to the server.

Upgrading Composer

Composer 1.x

If you want to upgrade Composer to the latest 1.x stable (or downgrade from 2.x stable), you can use:

composer self-update --1

If the update command fails due to permissions, execute it again with sudo.

sudo composer self-update --1

If you are on a really old version of composer, it may not have the --1 parameter. In this case, you might want to run self-update twice. The first may update it to 2.x, so the second will downgrade it to the latest version of 1.x.

composer self-update
composer self-update --1

Composer 2.x

If you are using Composer 1.x, and want to upgrade to Composer 2.x (and are using an appropriate version of MediaWiki, such as >= 1.35.2), you can use:

composer self-update --2

If the update command fails due to permissions, execute it again with sudo.

sudo composer self-update --2

MediaWiki核心代码中的使用

MediaWiki 1.25+ depends on some external libraries which are managed with Composer. Composer creates an autoloader at vendor/autoload.php, which is included by WebStart.php.

Composer managed dependencies are bundled into tarball distributions of MediaWiki and extensions so system administrators do not need to use Composer directly. When installing MediaWiki using Git, dependencies declared in $IP/composer.json can either be installed locally by running composer update --no-dev or the mediawiki/vendor.git repository can be cloned to provide the same libraries used on the Wikimedia production cluster.[1]

$IP 代表MediaWiki安装的安装路径(或“目录”),即LocalSettings.php index.php 所在的目录

Using composer-merge-plugin

$IP 代表MediaWiki安装的安装路径(或“目录”),即LocalSettings.php index.php 所在的目录

MediaWiki core "owns" $IP/composer.json and will change the contents of that file in both tarball and Git managed updates. In order to allow local installs to use Composer to load optional libraries and/or manage extensions with Composer, a special plugin for Composer named composer-merge-plugin was developed.[2] This plugin and the MediaWiki core composer.json configuration allow a local deployment to add required extensions and libraries to a file composer.local.json inside the root MediaWiki directory.

composer.local.json can also be used to install Composer managed libraries that are needed by extensions that are not installed using Composer themselves. This is only needed when the extensions are not installed from tarballs generated by Extension Distributor.

To enable Composer to discover and process the composer.json files that may be included in any and all of your locally installed extensions, add something like this to $IP/composer.local.json:

{
    "extra": {
        "merge-plugin": {
            "include": [
                "extensions/*/composer.json",
                "skins/*/composer.json"
            ]
        }
    }
}

After any changes to composer.local.json, you need to run composer update so that Composer will recalculate the dependencies and the changes will take effect.

資源

參考資料