Parsoid/it

From mediawiki.org
This page is a translated version of the page Parsoid and the translation is 25% complete.
Artist's impression of the Parsoid HTML5 + RDFa wiki runtime

Parsoid is a library which allows converting back and forth between wikitext and HTML. The original application was written in JavaScript (using Node.js) and started running on the Wikimedia cluster in December 2012. In 2019, Parsoid was ported to PHP, and the PHP version replaced the JS version on the Wikimedia cluster in December 2019. Parsoid is being integrated into core MediaWiki, with the goal of eventually replacing MediaWiki's current native parser. In early 2024, Parsoid began to be used on some production wikis as the default parser for read views.

Parsoid (the PHP version) has been natively bundled with MediaWiki since version 1.35, released in September 2020. For non-Wikimedia installations, Parsoid/JS was supported until the end-of-life of MediaWiki 1.31 (LTS) in September 2021. The legacy parser will still be supported in MediaWiki 1.43 (LTS).

Technical details

Parsoid is an application which can translate back and forth between MediaWiki's wikitext syntax and an equivalent HTML/RDFa document model with enhanced support for automated processing and rich editing.

It has been under development by a team at the Wikimedia Foundation since 2012. It is currently used extensively by VisualEditor , Structured discussions , Traduzione contenuti and other applications.

Parsoid is intended to provide flawless back-and-forth conversion, i.e. to avoid information loss and also prevent "dirty diffs".

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

Usage

Installation

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

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.

Linking a developer checkout of 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" );

# Manually configure 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;
}

Per verificare la corretta configurazione, visitare {$wgScriptPath}/rest.php/{$domain}/v3/page/html/Main%20Page dove $domain è il nome dell'host in $wgCanonicalServer. (Si noti che i server WMF di produzione non espongono l'API REST di Parsoid alla rete esterna).

Running the tests

Per eseguire tutti i test del parser e i test di Mocha:

$ composer test

I test del parser hanno ora un gran numero di opzioni, che possono essere elencate usando php bin/parserTests.php --help.

Se la variabile d'ambiente MW_INSTALL_DIR punta a un'installazione di MediaWiki configurata, è possibile eseguire alcuni test aggiuntivi con:

$ composer phan-integrated

Converting simple wikitext

È possibile convertire semplici frammenti di wikitext dalla riga di comando utilizzando lo script parse.php nella directory bin/:

echo 'Foo' | php bin/parse.php

Lo script parse ti dà un sacco di opzioni. php bin/parse.php --help ti offre iformazioni riguardo questo.

Debugging Parsoid (for developers)

Vedi Parsoid/Debugging per consigli sul debugging.

Integrazione Continua

A partire da ottobre 2021

Parsoid è sempre disponibile come libreria, poiché è una dipendenza del nucleo di MediaWiki. Ma due pezzi non sono abilitati:

  • Parsoid ServiceWiring
  • API REST esterna di Parsoid

Il test runner Quibble lo abilita se rileva che mediawiki/services/parsoid.git è stato clonato come parte della compilazione. In questo caso:

  • punta l'autoloader per Wikimedia\Parsoid al codice clonato (sostituendo di fatto la versione installata da composer)
  • Carica l'estensione wfLoadExtension( 'Parsoid', '/path/to/cloned/repo' );

Il ServiceWiring dovrebbe essere abilitato in MediaWiki a partire dalla versione 1.38.

Teoricamente l'API REST non verrebbe mai fusa in MediaWiki: a) non è mai stata esposta al pubblico in produzione, è un'API interna usata da RESTBase che sta per scomparire; b) non è mai stata sottoposta a controlli di sicurezza e c) è ridondante con l'API MediaWiki aziendale. La soluzione sarà che VisualEditor invochi Parsoid direttamente tramite l'API di azione di VisualEditor, risparmiando un viaggio di andata e ritorno attraverso l'API REST.

Il caricamento dell'estensione è quindi un hack che consente di utilizzare interfacce soggette a modifiche e che non vogliamo che vengano ancora utilizzate.

Per la maggior parte degli scopi, parsoid non dovrebbe quindi essere aggiunto come dipendenza di CI; l'unica eccezione, a partire da ottobre 2021, è l'estensione MediaWiki Disambiguator.

Caricare parsoid come estensione ci permette di eseguire test di integrazione di MediaWiki con mediawiki/services/parsoid.git (come Quibble, apitesting) e di assicurarci che Parsoid e MediaWiki funzionino insieme.

Un'estensione può essere in grado di scrivere test con Parsoid anche quando il repository non è stato clonato. Poiché è una dipendenza da MediaWiki core, lo spazio dei nomi MediaWiki\Parsoid è disponibile, ma la parte di cablaggio del servizio non lo è (è extension/src nel repository Parsoid ed è esposto come spazio dei nomi \MWParsoid). Il ParsoidTestFileSuite.php codice esegue i test del parser solo se è stato caricato Parsoid (che dovrebbe essere l'impostazione predefinita con MediaWiki 1.38).

Per l'IC, Parsoid è testato contro la punta di mediawiki, mentre mediawiki è testato con la dipendenza dal composer. In caso di rottura, la modifica di Parsoid viene fusa per prima (il che rompe il suo CI ma non quello di MediaWiki) e MediaWiki viene modificato quando Parsoid viene aggiornato. Si tratta quindi di un cambiamento a senso unico.

Politiche di rilascio

Per le release di MediaWiki, abbiamo un'integrazione di Parsoid ServiceWiring in VisualEditor, in modo che VisualEditor funzioni senza ulteriori configurazioni (a parte wfLoadExtension( 'VisualEditor' )). La build di rilascio abilita anche l'API REST e aggancia tutto in modo che parsoid funzioni subito. Questo viene fatto copiando un pezzo del codice parsoid dentro VisualEditor il quale non si trova nel master di VisualEditor, poiché sarebbe obsoleto non appena Parsoid verrà aggiornato. Il codice viene invece viene conservato in due posti.

Technical documents

Links for Parsoid developers

Links for Parsoid deployers (to the Wikimedia cluster)

See also

External links

Contact

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.