Parsoid
Parsoid
双向运行时维基文本解析器。 使用RDFa在wikitext和HTML/XML DOM之间相互转换。
|
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旨在提供完美的来回转换,即避免信息丢失并防止“脏差异”。
On Wikimedia wikis, for several applications, Parsoid is currently proxied behind RESTBase , which stores the HTML translated by Parsoid. It is expected that RESTBase will eventually be replaced with a cache more tightly integrated with MediaWiki.
For more on the overall project, see this blog post from March 2013. To read about the HTML model being used, see MediaWiki DOM spec.
Parsoid was originally structured as a web service and written in JavaScript, making use of Node.js
.
A tech talk from February 2019 (slides) and blog post describes the process of porting it to PHP.
The Parsoid extension API is currently under active development; a tech talk from August 2020 describes this work.
GitHub Repository: https://github.com/wikimedia/parsoid
用法
- Parsoid/Releases - Parsoid发布的版本列表
- Parsoid/API - 用于网页API
- MediaWiki DOM spec - to make sense of the HTML that you get from the API, designed to be useful as a future storage format
- Parsoid/LanguageConverter - notes on Parsoid's implementation of LanguageConverter
- Parsoid/Known differences with Core Parser output
安裝
In MediaWiki 1.35 Parsoid/PHP is included in the bundle and loaded automatically by Visual Editor. No configuration necessary for MediaWiki 1.35 and later.
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.
开发
Development happens in the Parsoid Git repository. Code review happens in Gerrit. See Gerrit/Getting started to set up an account for yourself.
If you use the MediaWiki-Vagrant development environment using a virtual machine, you can simply add the role visualeditor
to it and it will set up a working Parsoid along with Extension:VisualEditor .
(This may have been broken by the switch to Parsoid/PHP: T258940)
Note that the most-recently released version of Parsoid is written in PHP, and installation of Parsoid/PHP is what is described below. This is what you should use if you are running MediaWiki 1.35 or later. Check Parsoid/JS if you are running the old version of Parsoid written in JavaScript, and used for MW 1.34 and earlier.
链接Parsoid的开发人员签出
In a standard MediaWiki installation, Parsoid is included from MediaWiki as a composer library, wikimedia/parsoid
.
For development purposes you usually want to use a git checkout of Parsoid, and not the version bundled in MediaWiki core as a composer library. The following lines added to LocalSettings.php allow use of a git checkout of Parsoid (optionally), load the Parsoid REST API with wfLoadExtension (rather than using the version bundled in VisualEditor) and manually do the Parsoid configuration which is usually done by VisualEditor:
$parsoidInstallDir = 'vendor/wikimedia/parsoid'; # bundled copy
#$parsoidInstallDir = '/my/path/to/git/checkout/of/Parsoid';
// For developers: ensure Parsoid is executed from $parsoidInstallDir,
// (not the version included in mediawiki-core by default)
// Must occur *before* 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 );
// AutoLoader::registerNamespaces was added in MW 1.39
AutoLoader::registerNamespaces( [
// Keep this in sync with the "autoload" clause in
// $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'] = [
// URL to the Parsoid instance.
// If Parsoid is not running locally, you should change $wgServer to match the non-local host
// While using Docker in macOS, you may need to replace $wgServer with http://host.docker.internal:8080
// While using Docker in linux, you may need to replace $wgServer with http://172.17.0.1:8080
'url' => $wgServer . $wgScriptPath . '/rest.php',
// Parsoid "domain", see below (optional, rarely needed)
// 'domain' => 'localhost',
];
unset( $parsoidInstallDir );
These lines are not necessary for most users of VisualEditor, who can use auto-configuration and the bundled Parsoid code included in MediaWiki 1.35 and VisualEditor, but they will be required for most developers.
If you're serving MediaWiki with Nginx, you'll need to also add something like this in your server block (Assuming your MediaWiki setup has its files residing in /w/
):
location /w/rest.php/ {
try_files $uri $uri/ /w/rest.php?$query_string;
}
To test proper configuration, visit {$wgScriptPath}/rest.php/{$domain}/v3/page/html/Main%20Page
where $domain
is the hostname in your $wgCanonicalServer
.
(Note that production WMF servers do not expose the Parsoid REST API to the external network.)
运行测试
To run all parser tests and mocha tests:
$ composer test
The parser tests have quite a few options now which can be listed using php bin/parserTests.php --help
.
If you have the environment variable MW_INSTALL_DIR
pointing to a configured MediaWiki installation, you can run some additional tests with:
$ composer phan-integrated
转换简单的wikitext
You can convert simple wikitext snippets from the command line using the parse.php
script in the bin/
directory:
echo 'Foo' | php bin/parse.php
The parse script has a lot of options.
php bin/parse.php --help
gives you information about this.
调试Parsoid(对于开发人员)
See Parsoid/Debugging for debugging tips.
持续集成
As of October 2021
Parsoid is always available as a library since it is a composer dependency of MediaWiki core. But two pieces are not enabled:
- Parsoid ServiceWiring
- Parsoid's external REST api
The test runner Quibble would enable it if it detects mediawiki/services/parsoid.git
has been cloned as part of the build.
In which case it:
- points the autoloader for
Wikimedia\Parsoid
to the cloned code (effectively replacing the version installed by composer) - 载入扩展
wfLoadExtension( 'Parsoid', '/path/to/cloned/repo' );
The ServiceWiring should be enabled in MediaWiki starting with 1.38.
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.
Release build
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/Internals: documentation about Parsoid internals with links to other details.
- PHP Porting notes and help-wanted tasks
- Parsoid deployment agenda on Wikimedia cluster (code normally deployed every Monday and Wednesday between 1pm - 1:30pm PST)
- Parsoid/Round-trip testing: The round-trip testing setup we are using to test the wikitext -> HTML DOM -> wikitext round-trip on actual Wikipedia content.
- Parsoid/Visual Diffs Testing: Info about visual diff testing for comparing Parsoid's html rendering with php parser's html rendering + a testreduce setup for doing mass visual diff tests.
- Parsoid/limitations: Limitations in Parsoid, mainly contrived templating (ab)uses that don't matter in practice. Could be extended to be similar to the preprocessor upgrade notes (Might need updating)
- Parsoid/Bibliography: Bibliography of related literature
对于Parsoid开发者的链接
- See Parsoid/Debugging for debugging tips.
- Upgrading or adding packages to Parsoid
- See these instructions for syncing Parsoid's copy of parser tests to/from core
- Parsoid has a limited library interface for invoking it programatically.
- Tech Talk about Retargeting extensions to work with Parsoid
- So you want your extension to work with Parsoid
- Parsoid HTML Specification Versioning
- So you are going to change Parsoid output
对于Parsoid部署者的链接(到维基媒体集群)
- Parsoid/Deployments
- RT testing commits (useful to check regressions and fixes)
- Deployment instructions for Parsoid
- Kibana仪表盘
- Grafana dashboard for wt2html metrics
- Grafana dashboard for html2wt metrics
- Prometheus breakdown for the Parsoid cluster on eqiad
- Prometheus breakdown for the Parsoid cluster on codfw
- Jenkins Job Builder docs for updating jenkins jobs
参见
- API
- RESTBase: a caching / storing API proxy for page HTML translated by Parsoid
- Quarterly review meetings of the Parsoid team: April 2015, January 2015 (earlier)
- Future/Parser plan: Early (now relatively old) design ideas and issues
- Special:PrefixIndex/Parsoid/: Parsoid-related pages on this wiki
- Extension:ParsoidBatchAPI (archived)
- parsoid-jsapi: a high-level interface for extraction and transformation of wikitext, similar to the mwparserfromhell API.
- 替代解析器
- Parsoid/Parser Unification
外部链接
- 源代码(GitHub镜像源)
- JS文档(旧版Parsoid)
- PHP文档
- 维基媒体共享资源上的Parsoid
联络
If you need help or have questions/feedback, you can contact us in #mediawiki-parsoid connect or the wikitext-l mailing list.
If all that fails, you can also contact us by email at content-transform-team
at the wikimedia.org
domain.
Parsoid由the Content Transform Team维护。
获取帮助:
|