From 33dc60838cbb8fc1c78678230c4516c1a36c23b3 Mon Sep 17 00:00:00 2001 From: David Lynch Date: Mon, 25 Sep 2017 10:27:33 -0500 Subject: [PATCH] Api: include preload content when section=new Consistency with former editor behavior. Needed for some cases like Template:Meetup/signup which use preload + section=new to make as close to a one-click experience as they can. Bug: T51622 Change-Id: I0940898b56dbaea536f57560135665df89569232 --- ApiVisualEditor.php | 67 +++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/ApiVisualEditor.php b/ApiVisualEditor.php index 2ebba0549f..d337cabe21 100644 --- a/ApiVisualEditor.php +++ b/ApiVisualEditor.php @@ -142,6 +142,37 @@ class ApiVisualEditor extends ApiBase { ); } + protected function getPreloadContent( $preload, $params, $contextTitle, $parse = false ) { + $content = ''; + $preloadTitle = Title::newFromText( $preload ); + // Check for existence to avoid getting MediaWiki:Noarticletext + if ( $preloadTitle instanceof Title && + $preloadTitle->exists() && + $preloadTitle->userCan( 'read' ) + ) { + $preloadPage = WikiPage::factory( $preloadTitle ); + if ( $preloadPage->isRedirect() ) { + $preloadTitle = $preloadPage->getRedirectTarget(); + $preloadPage = WikiPage::factory( $preloadTitle ); + } + + $content = $preloadPage->getContent( Revision::RAW ); + $parserOptions = ParserOptions::newFromUser( $this->getUser() ); + + $content = $content->preloadTransform( + $preloadTitle, + $parserOptions, + $params['preloadparams'] + )->serialize(); + + if ( $parse ) { + // We need to turn this transformed wikitext into parsoid html + $content = $this->parseWikitextFragment( $contextTitle, $content ); + } + } + return $content; + } + protected function getLangLinks( $title ) { $apiParams = [ 'action' => 'query', @@ -251,6 +282,12 @@ class ApiVisualEditor extends ApiBase { if ( $section === 'new' ) { $content = ''; + if ( $params['preload'] ) { + $content = $this->getPreloadContent( + $params['preload'], $params['preloadparams'], $title, + $params['paction'] !== 'wikitext' + ); + } } else { $apiParams['rvsection'] = $section; @@ -286,32 +323,10 @@ class ApiVisualEditor extends ApiBase { $content = $this->parseWikitextFragment( $title, $content ); } if ( $content === '' && $params['preload'] ) { - $preloadTitle = Title::newFromText( $params['preload'] ); - // Check for existence to avoid getting MediaWiki:Noarticletext - if ( $preloadTitle instanceof Title && - $preloadTitle->exists() && - $preloadTitle->userCan( 'read' ) - ) { - $preloadPage = WikiPage::factory( $preloadTitle ); - if ( $preloadPage->isRedirect() ) { - $preloadTitle = $preloadPage->getRedirectTarget(); - $preloadPage = WikiPage::factory( $preloadTitle ); - } - - $content = $preloadPage->getContent( Revision::RAW ); - $parserOptions = ParserOptions::newFromUser( $user ); - - $content = $content->preloadTransform( - $preloadTitle, - $parserOptions, - $params['preloadparams'] - )->serialize(); - - if ( $params['paction'] !== 'wikitext' ) { - // We need to turn this transformed wikitext into parsoid html - $content = $this->parseWikitextFragment( $title, $content ); - } - } + $content = $this->getPreloadContent( + $params['preload'], $params['preloadparams'], $title, + $params['paction'] !== 'wikitext' + ); } $baseTimestamp = wfTimestampNow(); $oldid = 0;