jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/362331 )
Change subject: Add API to ParserMigration ...................................................................... Add API to ParserMigration Bug: T162275 Change-Id: I1f2be6fcacd9ec8be8a3f2cca3c929e555aea852 --- M extension.json M i18n/en.json M i18n/qqq.json A includes/ApiParserMigration.php A includes/Mechanism.php M includes/MigrationEditPage.php 6 files changed, 126 insertions(+), 34 deletions(-) Approvals: Subramanya Sastry: Looks good to me, approved Tim Starling: Looks good to me, approved jenkins-bot: Verified Anomie: Looks good to me, but someone else must approve diff --git a/extension.json b/extension.json index 0f9c888..94884c3 100644 --- a/extension.json +++ b/extension.json @@ -7,13 +7,18 @@ "license-name": "CC0-1.0", "manifest_version": 1, "AutoloadClasses": { + "MediaWiki\\ParserMigration\\ApiParserMigration": "includes/ApiParserMigration.php", "MediaWiki\\ParserMigration\\EditAction": "includes/EditAction.php", "MediaWiki\\ParserMigration\\MigrationEditPage": "includes/MigrationEditPage.php", - "MediaWiki\\ParserMigration\\Hooks": "includes/Hooks.php" + "MediaWiki\\ParserMigration\\Hooks": "includes/Hooks.php", + "MediaWiki\\ParserMigration\\Mechanism": "includes/Mechanism.php" }, "Actions": { "parsermigration-edit": "MediaWiki\\ParserMigration\\EditAction" }, + "APIModules": { + "parser-migration": "MediaWiki\\ParserMigration\\ApiParserMigration" + }, "MessagesDirs": { "ParserMigration": "i18n" }, diff --git a/i18n/en.json b/i18n/en.json index 53e2cba..c846ff7 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -4,6 +4,9 @@ "Tim Starling" ] }, + "apihelp-parser-migration-summary": "Parse a page with two different tidy configurations.", + "apihelp-parser-migration-param-title": "The title of the page to load and parse.", + "apihelp-parser-migration-param-config": "The parser configuration to use. May be \"old\", \"new\" or \"old|new\".", "parsermigration-desc": "Parser migration tool", "parsermigration-edit-subtitle": "with parser migration tool", "parsermigration-current": "Current", @@ -11,5 +14,4 @@ "parsermigration-pref-label": "Enable parser migration tool", "parsermigration-pref-help": "This adds a sidebar link giving access to [https://www.mediawiki.org/wiki/Extension:ParserMigration a tool] for migrating page text to new versions of the MediaWiki parser.", "parsermigration-toolbox-label": "Edit with migration tool" - } diff --git a/i18n/qqq.json b/i18n/qqq.json index c0cc37b..1cb85ab 100644 --- a/i18n/qqq.json +++ b/i18n/qqq.json @@ -7,6 +7,9 @@ "Nemo bis" ] }, + "apihelp-parser-migration-summary": "{{doc-apihelp-summary|parser-migration}}", + "apihelp-parser-migration-param-title": "{{doc-apihelp-param|parser-migration|title}}", + "apihelp-parser-migration-param-config": "{{doc-apihelp-param|parser-migration|config}}", "parsermigration-desc": "{{desc|name=ParserMigration}}", "parsermigration-edit-subtitle": "A subtitle shown below the h1 when editing a page with the tool", "parsermigration-current": "Adjective. The column heading for the article HTML as shown by the current parser.\n{{Identical|Current}}", diff --git a/includes/ApiParserMigration.php b/includes/ApiParserMigration.php new file mode 100644 index 0000000..62aef11 --- /dev/null +++ b/includes/ApiParserMigration.php @@ -0,0 +1,61 @@ +<?php + +namespace MediaWiki\ParserMigration; + +class ApiParserMigration extends \ApiBase { + static private $configNames = [ + 0 => 'old', + 1 => 'new', + ]; + + public function execute() { + $params = $this->extractRequestParams(); + + $title = $this->getTitleOrPageId( $params )->getTitle(); + $revision = \Revision::newFromTitle( $title ); + if ( !$revision ) { + $this->dieWithError( 'apierror-missingtitle' ); + } + $content = $revision->getContent(); + if ( !$content ) { + $this->dieWithError( [ 'apierror-missingcontent-pageid', $revision->getPage() ] ); + } + + $configIndexesByName = array_flip( self::$configNames ); + $configIndexes = []; + foreach ( $params['config'] as $configName ) { + if ( !isset( $configIndexesByName[$configName] ) ) { + throw new \Exception( 'Invalid config name, should have already been validated' ); + } + $configIndexes[] = $configIndexesByName[$configName]; + } + + $mechanism = new Mechanism( $this->getConfig()->get( 'ParserMigrationTidiers' ) ); + $user = $this->getUser(); + $options = \ParserOptions::newCanonical(); + $outputs = $mechanism->parse( $content, $title, $options, $user, $configIndexes ); + + $result = $this->getResult(); + foreach ( $configIndexes as $index ) { + $result->addValue( null, self::$configNames[$index], $outputs[$index]->getText() ); + } + } + + public function isInternal() { + return true; + } + + public function getAllowedParams() { + return [ + 'title' => [ + \ApiBase::PARAM_TYPE => 'string', + \ApiBase::PARAM_REQUIRED => true, + ], + 'config' => [ + \ApiBase::PARAM_TYPE => self::$configNames, + \ApiBase::PARAM_DFLT => 'old|new', + \ApiBase::PARAM_ISMULTI => true, + ] + ]; + } +} diff --git a/includes/Mechanism.php b/includes/Mechanism.php new file mode 100644 index 0000000..c4c0810 --- /dev/null +++ b/includes/Mechanism.php @@ -0,0 +1,42 @@ +<?php + +namespace MediaWiki\ParserMigration; + +class Mechanism { + public $tidiers; + + public function __construct( $tidiers ) { + if ( !is_array( $tidiers ) || !isset( $tidiers[0] ) || !isset( $tidiers[1] ) ) { + throw new \Exception( '$wgParserMigrationTidiers must have at least two elements' ); + } + $this->tidiers = $tidiers; + } + + public function parse( \Content $content, \Title $title, + \ParserOptions $baseOptions, \User $user, array $configIndexes + ) { + $options = clone $baseOptions; + $options->setTidy( false ); + $scopedCallback = $options->setupFakeRevision( $title, $content, $user ); + $parserOutput = $content->getParserOutput( $title, null, $options ); + \ScopedCallback::consume( $scopedCallback ); + + $outputs = []; + foreach ( $configIndexes as $i ) { + $outputs[$i] = $this->tidyParserOutput( $parserOutput, $this->tidiers[$i] ); + } + return $outputs; + } + + /** + * @param \ParserOutput $parserOutput + * @param array $config + * @return \ParserOutput + */ + protected function tidyParserOutput( $parserOutput, $config ) { + $tidier = \MWTidy::factory( $config ); + $newOutput = clone $parserOutput; + $newOutput->setText( $tidier->tidy( $newOutput->getRawText() ) ); + return $newOutput; + } +} diff --git a/includes/MigrationEditPage.php b/includes/MigrationEditPage.php index a42ad14..d936caa 100644 --- a/includes/MigrationEditPage.php +++ b/includes/MigrationEditPage.php @@ -24,53 +24,32 @@ return true; } - protected function getPreviewParserOptions() { - $parserOptions = parent::getPreviewParserOptions(); - $parserOptions->setTidy( false ); - return $parserOptions; - } - protected function doPreviewParse( \Content $content ) { $user = $this->context->getUser(); $parserOptions = $this->getPreviewParserOptions(); + $parserOptions->setTidy( false ); $pstContent = $content->preSaveTransform( $this->mTitle, $user, $parserOptions ); - $scopedCallback = $parserOptions->setupFakeRevision( - $this->mTitle, $pstContent, $user ); - $parserOutput = $pstContent->getParserOutput( $this->mTitle, null, $parserOptions ); - \ScopedCallback::consume( $scopedCallback ); + $mechanism = new Mechanism( + \RequestContext::getMain()->getConfig()->get( 'ParserMigrationTidiers' ) ); + $outputs = $mechanism->parse( $pstContent, $this->mTitle, $parserOptions, + $user, [ 0, 1 ] ); - $parserOutput->setEditSectionTokens( false ); // no section edit links + // no section edit links + $outputs[0]->setEditSectionTokens( false ); + $outputs[1]->setEditSectionTokens( false ); - $tidiers = \RequestContext::getMain()->getConfig()->get( 'ParserMigrationTidiers' ); - if ( !is_array( $tidiers ) || !isset( $tidiers[0] ) || !isset( $tidiers[1] ) ) { - throw new \Exception( '$wgParserMigrationTidiers must have at least two elements' ); - } - $leftOutput = $this->tidyParserOutput( $parserOutput, $tidiers[0] ); - $rightOutput = $this->tidyParserOutput( $parserOutput, $tidiers[1] ); $previewHTML = "<table class=\"mw-parsermigration-sxs\"><tbody><tr>\n" . "<th>" . wfMessage( 'parsermigration-current' )->parse() . "</th>\n" . "<th>" . wfMessage( 'parsermigration-new' )->parse() . "</th>\n" . "</tr><tr>\n" . "<td class=\"mw-parsermigration-left\">\n\n" . - $leftOutput->getText() . + $outputs[0]->getText() . "\n\n</td><td class=\"mw-parsermigration-right\">\n\n" . - $rightOutput->getText() . + $outputs[1]->getText() . "\n\n</td></tr></tbody></table>\n"; return [ - 'parserOutput' => $rightOutput, + 'parserOutput' => $outputs[1], 'html' => $previewHTML ]; - } - - /** - * @param \ParserOutput $parserOutput - * @param array $config - * @return \ParserOutput - */ - protected function tidyParserOutput( $parserOutput, $config ) { - $tidier = \MWTidy::factory( $config ); - $newOutput = clone $parserOutput; - $newOutput->setText( $tidier->tidy( $newOutput->getRawText() ) ); - return $newOutput; } } -- To view, visit https://gerrit.wikimedia.org/r/362331 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I1f2be6fcacd9ec8be8a3f2cca3c929e555aea852 Gerrit-PatchSet: 6 Gerrit-Project: mediawiki/extensions/ParserMigration Gerrit-Branch: master Gerrit-Owner: Tim Starling <tstarl...@wikimedia.org> Gerrit-Reviewer: Anomie <bjor...@wikimedia.org> Gerrit-Reviewer: Siebrand <siebr...@kitano.nl> Gerrit-Reviewer: Subramanya Sastry <ssas...@wikimedia.org> Gerrit-Reviewer: Tim Starling <tstarl...@wikimedia.org> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits