r81124 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r81123‎ | r81124 | r81125 >
Date:01:47, 28 January 2011
Author:catrope
Status:resolved (Comments)
Tags:
Comment:
Implement prop=wikitext in action=parse, (optionally) returning the original wikitext. Requested by some Google folks wanting to get data about a revision in one request.
Modified paths:
  • /trunk/phase3/includes/api/ApiParse.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiParse.php
@@ -34,7 +34,7 @@
3535 */
3636 class ApiParse extends ApiBase {
3737
38 - private $section;
 38+ private $section, $text, $pstText = null;
3939
4040 public function __construct( $main, $action ) {
4141 parent::__construct( $main, $action );
@@ -100,18 +100,19 @@
101101 if ( $titleObj->getLatestRevID() === intval( $oldid ) ) {
102102 $articleObj = new Article( $titleObj, 0 );
103103
104 - $p_result = $this->getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageid ) ;
 104+ $p_result = $this->getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageid,
 105+ isset( $prop['wikitext'] ) ) ;
105106
106107 } else { // This is an old revision, so get the text differently
107 - $text = $rev->getText( Revision::FOR_THIS_USER );
 108+ $this->text = $rev->getText( Revision::FOR_THIS_USER );
108109
109110 $wgTitle = $titleObj;
110111
111112 if ( $this->section !== false ) {
112 - $text = $this->getSectionText( $text, 'r' . $rev->getId() );
 113+ $this->text = $this->getSectionText( $this->text, 'r' . $rev->getId() );
113114 }
114115
115 - $p_result = $wgParser->parse( $text, $titleObj, $popts );
 116+ $p_result = $wgParser->parse( $this->text, $titleObj, $popts );
116117 }
117118
118119 } else { // Not $oldid
@@ -153,11 +154,13 @@
154155 $oldid = $articleObj->getRevIdFetched();
155156 }
156157
157 - $p_result = $this->getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageid ) ;
 158+ $p_result = $this->getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageid,
 159+ isset( $prop['wikitext'] ) ) ;
158160 }
159161
160162 } else { // Not $oldid, $pageid, $page. Hence based on $text
161163
 164+ $this->text = $text;
162165 $titleObj = Title::newFromText( $title );
163166 if ( !$titleObj ) {
164167 $titleObj = Title::newFromText( 'API' );
@@ -165,20 +168,24 @@
166169 $wgTitle = $titleObj;
167170
168171 if ( $this->section !== false ) {
169 - $text = $this->getSectionText( $text, $titleObj->getText() );
 172+ $this->text = $this->getSectionText( $this->text, $titleObj->getText() );
170173 }
171174
172175 if ( $params['pst'] || $params['onlypst'] ) {
173 - $text = $wgParser->preSaveTransform( $text, $titleObj, $wgUser, $popts );
 176+ $this->pstText = $wgParser->preSaveTransform( $this->text, $titleObj, $wgUser, $popts );
174177 }
175178 if ( $params['onlypst'] ) {
176179 // Build a result and bail out
177180 $result_array['text'] = array();
178 - $this->getResult()->setContent( $result_array['text'], $text );
 181+ $this->getResult()->setContent( $result_array['text'], $this->pstText );
 182+ if ( isset( $prop['wikitext'] ) ) {
 183+ $result_array['wikitext'] = array();
 184+ $this->getResult()->setContent( $result_array['wikitext'], $this->text );
 185+ }
179186 $this->getResult()->addValue( null, $this->getModuleName(), $result_array );
180187 return;
181188 }
182 - $p_result = $wgParser->parse( $text, $titleObj, $popts );
 189+ $p_result = $wgParser->parse( $this->text, $titleObj, $popts );
183190 }
184191
185192 // Return result
@@ -268,6 +275,15 @@
269276 if ( isset( $prop['iwlinks'] ) ) {
270277 $result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
271278 }
 279+
 280+ if ( isset( $prop['wikitext'] ) ) {
 281+ $result_array['wikitext'] = array();
 282+ $result->setContent( $result_array['wikitext'], $this->text );
 283+ if ( !is_null( $this->pstText ) ) {
 284+ $result_array['psttext'] = array();
 285+ $result->setContent( $result_array['psttext'], $this->pstText );
 286+ }
 287+ }
272288
273289 $result_mapping = array(
274290 'redirects' => 'r',
@@ -294,19 +310,27 @@
295311 * @param $titleObj Title
296312 * @param $popts ParserOptions
297313 * @param $pageId Int
 314+ * @param $getWikitext Bool
298315 * @return ParserOutput
299316 */
300 - private function getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageId = null ) {
 317+ private function getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageId = null, $getWikitext = false ) {
301318 if ( $this->section !== false ) {
302319 global $wgParser;
303320
304 - $text = $this->getSectionText( $articleObj->getRawText(), !is_null ( $pageId )
 321+ $this->text = $this->getSectionText( $articleObj->getRawText(), !is_null ( $pageId )
305322 ? 'page id ' . $pageId : $titleObj->getText() );
306323
307 - return $wgParser->parse( $text, $titleObj, $popts );
 324+ return $wgParser->parse( $this->text, $titleObj, $popts );
308325 } else {
309326 // Try the parser cache first
310 - return $articleObj->getParserOutput();
 327+ $pout = $articleObj->getParserOutput();
 328+ if ( $getWikitext ) {
 329+ $rev = Revision::newFromTitle( $titleObj );
 330+ if ( $rev ) {
 331+ $this->text = $rev->getText();
 332+ }
 333+ }
 334+ return $pout;
311335 }
312336 }
313337
@@ -456,6 +480,7 @@
457481 'headitems',
458482 'headhtml',
459483 'iwlinks',
 484+ 'wikitext',
460485 )
461486 ),
462487 'pst' => false,
@@ -493,6 +518,7 @@
494519 ' headitems - Gives items to put in the <head> of the page',
495520 ' headhtml - Gives parsed <head> of the page',
496521 ' iwlinks - Gives interwiki links in the parsed wikitext',
 522+ ' wikitext - Gives the original wikitext that was parsed',
497523 'NOTE: Section tree is only generated if there are more than 4 sections, or if the __TOC__ keyword is present'
498524 ),
499525 'pst' => array(

Follow-up revisions

RevisionCommit summaryAuthorDate
r81406Fix regression in r81124, causing &pst to be ignored for the purposees of act...catrope19:16, 2 February 2011
r814071.17: MFT r79454, r79455, r79456, r79536, r79727, r79733, r79773, r79774, r80...catrope19:18, 2 February 2011
r109693* (bug 33865) Exception thrown when using API sandbox action=parse...reedy21:36, 21 January 2012

Comments

#Comment by MaxSem (talk | contribs)   21:10, 21 January 2012

Causes bug 33865 - weird, I know, but biset point at this very revsion. o_0

Status & tagging log