Parsoid

From mediawiki.org
This page is a translated version of the page Parsoid and the translation is 79% complete.
艺术家对Parsoid HTML5 + RDFa wiki运行时的印象

Parsoid是一个允许在wikitext和HTML之间来回转换的库。 原始应用程序是用JavaScript编写的(借助Node.js),并自2012年12月起在维基媒体集群上运行。 在2019年,Parsoid移植到PHP,维基媒体集群于2019年12月以PHP版本取代了JS版本。 Parsoid已经集成到MediaWiki核心,其目标是最终取代MediaWiki当前的本地解析器。 In early 2024, Parsoid began to be used on some production wikis as the default parser for read views.

Parsoid (PHP版本) 已经原生捆绑到2020年9月发布的MediaWiki版本1.35或更高版本中。 对于非维基媒体安装,Parsoid/JS在2021年9月MediaWiki 1.31(LTS)生命周期结束之前一直受支持。 The legacy parser will still be supported in MediaWiki 1.43 (LTS).

技术细节

Parsoid是一个应用程序,可以在MediaWiki的wiki文本语法和等效的HTML/RDFa文档模型之间来回转换,并增强了对自动处理和丰富编辑的支持。

自2012年以来,它一直由维基媒体基金会的一个团队开发。 它目前被VisualEditor Structured discussions 内容翻译 其他应用广泛使用。

Parsoid旨在提供完美的来回转换,即避免信息丢失并防止“脏差异”。

在维基媒体wiki上,对于一些应用程序,Parsoid目前被代理在RESTBase 之后,它存储了由Parsoid翻译的HTML。 预计RESTBase最终将被替换,与MediaWiki的缓存更紧密地集成在一起。

有关整个项目的更多信息,请参阅2013年3月的这篇博文。 要了解正在使用的HTML模型,请参阅MediaWiki DOM 规范

Parsoid最初是作为Web服务构建的,用JavaScript编写,利用Node.js2019年2月的技术讲座幻灯片)和博客文章描述了将其移植到PHP的过程。 Parsoid扩展API目前正在积极开发中;2020年8月的技术讲座描述了这项工作。

Github仓库: https://github.com/wikimedia/parsoid

用法

安裝

MediaWiki 1.35中,Parsoid/PHP包含在捆绑包中,并由可视化编辑器自动加载。 MediaWiki 1.35及更高版本无需配置。

Parsoid exports an internal REST API which was historically used by RESTBase and not accessible outside the WMF internal cluster. This is no longer required for Visual Editor or core read views, and the internal API is being deprecated and is planned for removal in MW 1.43.

Parsoid is nominally a composer library used by mediawiki core. If you still require the internal API for some reason, you can explicitly load Parsoid "as an extension" by adding the following to LocalSettings.php:

wfLoadExtension( 'Parsoid', "$IP/vendor/wikimedia/parsoid/extension.json" );

Any remaining third-party users of the internal Parsoid API are strongly encouraged to migrate to the core REST HTML page endpoint which provides equivalent functionality.

开发

开发地址在Parsoid Git repository。 代码审查地址在Gerrit。 请参阅Gerrit/教程为自己创建一个帐户。

如果您使用虚拟机的MediaWiki-Vagrant 开发环境,您只需将角色visualeditor添加到其中,它就会与Extension:VisualEditor 一起设置一个工作的Parsoid。 (这可能已被切换到Parsoid/PHP时被打破:T258940

请注意,最近发布的Parsoid版本是用PHP编写的,Parsoid/PHP的安装向导如下所述。 如果您运行的是MediaWiki 1.35或更高版本,则应使用此功能。 如果您运行的是用JavaScript编写的旧版本的Parsoid,并且用于MW 1.34及更早版本,请查阅Parsoid/JS

链接Parsoid的开发人员签出

在标准的MediaWiki安装中,Parsoid作为一个composer库wikimedia/parsoid在MediaWiki中包含。

出于开发目的,您通常希望使用Parsoid的git签出,而不是MediaWiki核心中捆绑的版本作为composer库。 以下添加到LocalSettings.php 的行允许使用Parsoid的git签出(可选),以wfLoadExtension 加载Parsoid REST API(而不是使用可视化编辑器中捆绑的版本),并手动执行通常由可视化编辑器完成的Parsoid配置:

$parsoidInstallDir = 'vendor/wikimedia/parsoid'; # 捆绑包副本
#$parsoidInstallDir = '/my/path/to/git/checkout/of/Parsoid';

// 写给开发人员:确保Parsoid从$parsoidInstallDir执行,
// (不是默认包含在mediawiki-core中的版本)
// 必须在wfLoadExtension()*之前*执行
if ( $parsoidInstallDir !== 'vendor/wikimedia/parsoid' ) {
    function wfInterceptParsoidLoading( $className ) {
        // Only intercept Parsoid namespace classes
        if ( preg_match( '/(MW|Wikimedia\\\\)Parsoid\\\\/', $className ) ) {
           $fileName = Autoloader::find( $className );
           if ( $fileName !== null ) {
               require $fileName;
           }
        }
    }
    spl_autoload_register( 'wfInterceptParsoidLoading', true, true );
    // 在MW 1.39中添加了AutoLoader::registerNamespaces
    AutoLoader::registerNamespaces( [
        // 与下文的“autoload”子句保持同步
        // $parsoidInstallDir/composer.json
        'Wikimedia\\Parsoid\\' => "$parsoidInstallDir/src",
    ] );
}

wfLoadExtension( 'Parsoid', "$parsoidInstallDir/extension.json" );

# 手动配置Parsoid
$wgVisualEditorParsoidAutoConfig = false;
$wgParsoidSettings = [
    'useSelser' => true,
    'rtTestMode' => false,
    'linting' => false,
];
$wgVirtualRestConfig['modules']['parsoid'] = [
    // Parsoid实例的URL。
    // 如果Parsoid未在本地运行,则应更改$wgServer以匹配非本地主机 
    // 在macOS中使用Docker时,可能需要将$wgServer替换为http://host.docker.internal:8080
    // 在linux中使用Docker时,可能需要将$wgServer替换为http://172.17.0.1:8080
    'url' => $wgServer . $wgScriptPath . '/rest.php',
    // Parsoid“域名”,见下文(可选,很少需要)
    // 'domain' => 'localhost',
];
unset( $parsoidInstallDir );

对于大多数可视化编辑器用户来说,这些行不是必需的,他们可以使用自动配置以及MediaWiki 1.35和可视化编辑器中包含的捆绑的Parsoid代码,但对于大多数开发人员来说,这些行是必需的。

如果您使用Nginx为MediaWiki提供服务,则还需要在服务器块中添加类似这样的东西(假设您的MediaWiki设置的文件位于/w/):

location /w/rest.php/ {
    try_files $uri $uri/ /w/rest.php?$query_string;
}

要测试正确的配置,请访问{$wgScriptPath}/rest.php/{$domain}/v3/page/html/Main%20Page,其中$domain$wgCanonicalServer中的主机名。 (请注意,生产WMF服务器不会向外部网络公开Parsoid REST API。

运行测试

要运行所有解析器测试和mocha测试,请执行以下操作:

$ composer test

解析器测试现在有很多选项,可以使用php bin/parserTests.php --help命令列出。

如果您的环境变量MW_INSTALL_DIR指向已配置的MediaWiki安装,则可以使用以下命令运行一些额外的测试:

$ composer phan-integrated

转换简单的wikitext

您可以使用bin/目录中的parse.php脚本从命令行转换简单的wikitext片段:

echo 'Foo' | php bin/parse.php

解析脚本有很多选项。 php bin/parse.php --help命令能为您提供相关信息。

调试Parsoid(对于开发人员)

请参阅Parsoid/Debugging 了解调试提示。

持续集成

截至2021年10月

Parsoid始终可以作为库使用,因为它是MediaWiki核心的composer依赖项。但有两个部分未启用:

  • Parsoid ServiceWiring
  • Parsoid的外部REST api

如果测试运行程序Quibble检测到mediawiki/services/parsoid.git已被克隆为构建的一部分,它将启用它。 在此情况下,它:

  • Wikimedia\Parsoid将自动加载器指向克隆的代码(有效地替换了composer安装的版本)
  • 载入扩展 wfLoadExtension( 'Parsoid', '/path/to/cloned/repo' );

从1.38开始,应在MediaWiki中启用ServiceWiring。

The REST API would theorically never get merged in MediaWiki: a) it has never been exposed to the public in production, it is an internal API used by RESTBase which is going away; b) it never has been security audited and c) it is redundant with the enterprise MediaWiki API. The solution will be for VisualEditor to invoke Parsoid directly via the VisualEditor Action API which would save a round trip through the REST API.

Loading the extension is thus a hack which enables using interfaces subject to change and which we don't really want people to use yet.

For most purposes, parsoid should thus not be added as a CI dependency, the only exception as of October 2021 is the Disambiguator MediaWiki extension.

Loading parsoid as an extension let us run MediaWiki integration test jobs against mediawiki/services/parsoid.git (such as Quibble, apitesting) and ensure Parsoid and MediaWiki work together.

An extension may be able to write tests with Parsoid even when the repository has not been cloned. Since it is a composer dependency of MediaWiki core the MediaWiki\Parsoid namespace is available, but the service wiring part is not (it is extension/src in the Parsoid repository and exposed as the \MWParsoid namespace). The ParsoidTestFileSuite.php code would only run the parser tests if Parsoid has been loaded (which should be the default with MediaWiki 1.38).

For CI, Parsoid is tested against the tip of mediawiki, whereas mediawiki is tested with the composer dependency. In case of a breaking change, the Parsoid change get merged first (which breaks its CI but not MediaWiki one) and MediaWiki get adjusted when Parsoid is updated. It is thus a one way change.

发布版本

For MediaWiki release builds, we have an integration of Parsoid ServiceWiring into VisualEditor in order to have VisualEditor work without further configuration (beside a wfLoadExtension( 'VisualEditor' )). The release build also enables the REST API and hook everything us so that parsoid works out of the box. This is done by copying a bit of parsoid code into VisualEditor which is not in the master branch of VisualEditor since that would be obsolete as soon as Parsoid is updated. Instead the code is maintained in two places.

技术文档

Parsoid开发者的相关链接

对于Parsoid部署者的链接(到维基媒体集群)

参见

外部链接

联络

如果您需要帮助或有问题/反馈,您可以在#mediawiki-parsoid connectwikitext-l邮箱列表与我们联系。 如果所有这些方式都失败了,您也可以通过电子邮件与我们联系,地址为wikimedia.org域名的content-transform-team