Index: trunk/phase3/includes/api/ApiParse.php |
— | — | @@ -34,7 +34,7 @@ |
35 | 35 | */ |
36 | 36 | class ApiParse extends ApiBase { |
37 | 37 | |
38 | | - private $section; |
| 38 | + private $section, $text, $pstText = null; |
39 | 39 | |
40 | 40 | public function __construct( $main, $action ) { |
41 | 41 | parent::__construct( $main, $action ); |
— | — | @@ -100,18 +100,19 @@ |
101 | 101 | if ( $titleObj->getLatestRevID() === intval( $oldid ) ) { |
102 | 102 | $articleObj = new Article( $titleObj, 0 ); |
103 | 103 | |
104 | | - $p_result = $this->getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageid ) ; |
| 104 | + $p_result = $this->getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageid, |
| 105 | + isset( $prop['wikitext'] ) ) ; |
105 | 106 | |
106 | 107 | } 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 ); |
108 | 109 | |
109 | 110 | $wgTitle = $titleObj; |
110 | 111 | |
111 | 112 | if ( $this->section !== false ) { |
112 | | - $text = $this->getSectionText( $text, 'r' . $rev->getId() ); |
| 113 | + $this->text = $this->getSectionText( $this->text, 'r' . $rev->getId() ); |
113 | 114 | } |
114 | 115 | |
115 | | - $p_result = $wgParser->parse( $text, $titleObj, $popts ); |
| 116 | + $p_result = $wgParser->parse( $this->text, $titleObj, $popts ); |
116 | 117 | } |
117 | 118 | |
118 | 119 | } else { // Not $oldid |
— | — | @@ -153,11 +154,13 @@ |
154 | 155 | $oldid = $articleObj->getRevIdFetched(); |
155 | 156 | } |
156 | 157 | |
157 | | - $p_result = $this->getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageid ) ; |
| 158 | + $p_result = $this->getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageid, |
| 159 | + isset( $prop['wikitext'] ) ) ; |
158 | 160 | } |
159 | 161 | |
160 | 162 | } else { // Not $oldid, $pageid, $page. Hence based on $text |
161 | 163 | |
| 164 | + $this->text = $text; |
162 | 165 | $titleObj = Title::newFromText( $title ); |
163 | 166 | if ( !$titleObj ) { |
164 | 167 | $titleObj = Title::newFromText( 'API' ); |
— | — | @@ -165,20 +168,24 @@ |
166 | 169 | $wgTitle = $titleObj; |
167 | 170 | |
168 | 171 | if ( $this->section !== false ) { |
169 | | - $text = $this->getSectionText( $text, $titleObj->getText() ); |
| 172 | + $this->text = $this->getSectionText( $this->text, $titleObj->getText() ); |
170 | 173 | } |
171 | 174 | |
172 | 175 | if ( $params['pst'] || $params['onlypst'] ) { |
173 | | - $text = $wgParser->preSaveTransform( $text, $titleObj, $wgUser, $popts ); |
| 176 | + $this->pstText = $wgParser->preSaveTransform( $this->text, $titleObj, $wgUser, $popts ); |
174 | 177 | } |
175 | 178 | if ( $params['onlypst'] ) { |
176 | 179 | // Build a result and bail out |
177 | 180 | $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 | + } |
179 | 186 | $this->getResult()->addValue( null, $this->getModuleName(), $result_array ); |
180 | 187 | return; |
181 | 188 | } |
182 | | - $p_result = $wgParser->parse( $text, $titleObj, $popts ); |
| 189 | + $p_result = $wgParser->parse( $this->text, $titleObj, $popts ); |
183 | 190 | } |
184 | 191 | |
185 | 192 | // Return result |
— | — | @@ -268,6 +275,15 @@ |
269 | 276 | if ( isset( $prop['iwlinks'] ) ) { |
270 | 277 | $result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() ); |
271 | 278 | } |
| 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 | + } |
272 | 288 | |
273 | 289 | $result_mapping = array( |
274 | 290 | 'redirects' => 'r', |
— | — | @@ -294,19 +310,27 @@ |
295 | 311 | * @param $titleObj Title |
296 | 312 | * @param $popts ParserOptions |
297 | 313 | * @param $pageId Int |
| 314 | + * @param $getWikitext Bool |
298 | 315 | * @return ParserOutput |
299 | 316 | */ |
300 | | - private function getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageId = null ) { |
| 317 | + private function getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageId = null, $getWikitext = false ) { |
301 | 318 | if ( $this->section !== false ) { |
302 | 319 | global $wgParser; |
303 | 320 | |
304 | | - $text = $this->getSectionText( $articleObj->getRawText(), !is_null ( $pageId ) |
| 321 | + $this->text = $this->getSectionText( $articleObj->getRawText(), !is_null ( $pageId ) |
305 | 322 | ? 'page id ' . $pageId : $titleObj->getText() ); |
306 | 323 | |
307 | | - return $wgParser->parse( $text, $titleObj, $popts ); |
| 324 | + return $wgParser->parse( $this->text, $titleObj, $popts ); |
308 | 325 | } else { |
309 | 326 | // 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; |
311 | 335 | } |
312 | 336 | } |
313 | 337 | |
— | — | @@ -456,6 +480,7 @@ |
457 | 481 | 'headitems', |
458 | 482 | 'headhtml', |
459 | 483 | 'iwlinks', |
| 484 | + 'wikitext', |
460 | 485 | ) |
461 | 486 | ), |
462 | 487 | 'pst' => false, |
— | — | @@ -493,6 +518,7 @@ |
494 | 519 | ' headitems - Gives items to put in the <head> of the page', |
495 | 520 | ' headhtml - Gives parsed <head> of the page', |
496 | 521 | ' iwlinks - Gives interwiki links in the parsed wikitext', |
| 522 | + ' wikitext - Gives the original wikitext that was parsed', |
497 | 523 | 'NOTE: Section tree is only generated if there are more than 4 sections, or if the __TOC__ keyword is present' |
498 | 524 | ), |
499 | 525 | 'pst' => array( |