Index: branches/REL1_17/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 ); |
— | — | @@ -77,8 +77,11 @@ |
78 | 78 | $popts = new ParserOptions(); |
79 | 79 | $popts->setTidy( true ); |
80 | 80 | $popts->enableLimitReport( !$params['disablepp'] ); |
| 81 | + |
81 | 82 | $redirValues = null; |
| 83 | + |
82 | 84 | if ( !is_null( $oldid ) || !is_null( $pageid ) || !is_null( $page ) ) { |
| 85 | + |
83 | 86 | if ( !is_null( $oldid ) ) { |
84 | 87 | // Don't use the parser cache |
85 | 88 | $rev = Revision::newFromID( $oldid ); |
— | — | @@ -95,38 +98,33 @@ |
96 | 99 | |
97 | 100 | //If for some reason the "oldid" is actually the current revision, it may be cached |
98 | 101 | if ( $titleObj->getLatestRevID() === $oldid ) { |
99 | | - $p_result = false; |
100 | | - $pcache = ParserCache::singleton(); |
101 | | - if ( $wgEnableParserCache ) { |
102 | | - $p_result = $pcache->get( $titleObj, $popts ); |
103 | | - } |
104 | | - if ( !$p_result ) { |
105 | | - $text = $rev->getText( Revision::FOR_THIS_USER ); |
106 | | - $p_result = $wgParser->parse( $text, $titleObj, $popts ); |
| 102 | + $articleObj = new Article( $titleObj, 0 ); |
107 | 103 | |
108 | | - if ( $wgEnableParserCache ) { |
109 | | - $pcache->save( $p_result, $titleObj, $popts ); |
110 | | - } |
111 | | - } |
112 | | - } else { |
113 | | - $text = $rev->getText( Revision::FOR_THIS_USER ); |
| 104 | + $p_result = $this->getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageid, |
| 105 | + isset( $prop['wikitext'] ) ) ; |
114 | 106 | |
| 107 | + } else { // This is an old revision, so get the text differently |
| 108 | + $this->text = $rev->getText( Revision::FOR_THIS_USER ); |
| 109 | + |
115 | 110 | $wgTitle = $titleObj; |
116 | 111 | |
117 | 112 | if ( $this->section !== false ) { |
118 | | - $text = $this->getSectionText( $text, 'r' . $rev->getId() ); |
| 113 | + $this->text = $this->getSectionText( $this->text, 'r' . $rev->getId() ); |
119 | 114 | } |
120 | 115 | |
121 | | - $p_result = $wgParser->parse( $text, $titleObj, $popts ); |
| 116 | + $p_result = $wgParser->parse( $this->text, $titleObj, $popts ); |
122 | 117 | } |
123 | | - } else { |
| 118 | + |
| 119 | + } else { // Not $oldid |
| 120 | + |
124 | 121 | if ( !is_null ( $pageid ) ) { |
125 | 122 | $titleObj = Title::newFromID( $pageid ); |
126 | 123 | |
127 | 124 | if ( !$titleObj ) { |
128 | 125 | $this->dieUsageMsg( array( 'nosuchpageid', $pageid ) ); |
129 | 126 | } |
130 | | - } else { |
| 127 | + } else { // $page |
| 128 | + |
131 | 129 | if ( $params['redirects'] ) { |
132 | 130 | $req = new FauxRequest( array( |
133 | 131 | 'action' => 'query', |
— | — | @@ -145,37 +143,24 @@ |
146 | 144 | $to = $page; |
147 | 145 | } |
148 | 146 | $titleObj = Title::newFromText( $to ); |
149 | | - if ( !$titleObj ) { |
| 147 | + if ( !$titleObj || !$titleObj->exists() ) { |
150 | 148 | $this->dieUsage( "The page you specified doesn't exist", 'missingtitle' ); |
151 | 149 | } |
152 | 150 | } |
153 | 151 | $wgTitle = $titleObj; |
154 | 152 | |
155 | | - $articleObj = new Article( $titleObj ); |
| 153 | + $articleObj = new Article( $titleObj, 0 ); |
156 | 154 | if ( isset( $prop['revid'] ) ) { |
157 | 155 | $oldid = $articleObj->getRevIdFetched(); |
158 | 156 | } |
159 | 157 | |
160 | | - if ( $this->section !== false ) { |
161 | | - $text = $this->getSectionText( $text, !is_null ( $pageid ) ? 'page id ' . $pageid : $titleObj->getText() ); |
162 | | - $p_result = $wgParser->parse( $text, $titleObj, $popts ); |
163 | | - } else { |
164 | | - // Try the parser cache first |
165 | | - $p_result = false; |
166 | | - $pcache = ParserCache::singleton(); |
167 | | - if ( $wgEnableParserCache ) { |
168 | | - $p_result = $pcache->get( $articleObj, $popts ); |
169 | | - } |
170 | | - if ( !$p_result ) { |
171 | | - $p_result = $wgParser->parse( $articleObj->getContent(), $titleObj, $popts ); |
| 158 | + $p_result = $this->getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageid, |
| 159 | + isset( $prop['wikitext'] ) ) ; |
| 160 | + } |
172 | 161 | |
173 | | - if ( $wgEnableParserCache ) { |
174 | | - $pcache->save( $p_result, $articleObj, $popts ); |
175 | | - } |
176 | | - } |
177 | | - } |
178 | | - } |
179 | | - } else { |
| 162 | + } else { // Not $oldid, $pageid, $page. Hence based on $text |
| 163 | + |
| 164 | + $this->text = $text; |
180 | 165 | $titleObj = Title::newFromText( $title ); |
181 | 166 | if ( !$titleObj ) { |
182 | 167 | $titleObj = Title::newFromText( 'API' ); |
— | — | @@ -183,20 +168,24 @@ |
184 | 169 | $wgTitle = $titleObj; |
185 | 170 | |
186 | 171 | if ( $this->section !== false ) { |
187 | | - $text = $this->getSectionText( $text, $titleObj->getText() ); |
| 172 | + $this->text = $this->getSectionText( $this->text, $titleObj->getText() ); |
188 | 173 | } |
189 | 174 | |
190 | 175 | if ( $params['pst'] || $params['onlypst'] ) { |
191 | | - $text = $wgParser->preSaveTransform( $text, $titleObj, $wgUser, $popts ); |
| 176 | + $this->pstText = $wgParser->preSaveTransform( $this->text, $titleObj, $wgUser, $popts ); |
192 | 177 | } |
193 | 178 | if ( $params['onlypst'] ) { |
194 | 179 | // Build a result and bail out |
195 | 180 | $result_array['text'] = array(); |
196 | | - $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 | + } |
197 | 186 | $this->getResult()->addValue( null, $this->getModuleName(), $result_array ); |
198 | 187 | return; |
199 | 188 | } |
200 | | - $p_result = $wgParser->parse( $text, $titleObj, $popts ); |
| 189 | + $p_result = $wgParser->parse( $params['pst'] ? $this->pstText : $this->text, $titleObj, $popts ); |
201 | 190 | } |
202 | 191 | |
203 | 192 | // Return result |
— | — | @@ -279,6 +268,15 @@ |
280 | 269 | if ( isset( $prop['iwlinks'] ) ) { |
281 | 270 | $result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() ); |
282 | 271 | } |
| 272 | + |
| 273 | + if ( isset( $prop['wikitext'] ) ) { |
| 274 | + $result_array['wikitext'] = array(); |
| 275 | + $result->setContent( $result_array['wikitext'], $this->text ); |
| 276 | + if ( !is_null( $this->pstText ) ) { |
| 277 | + $result_array['psttext'] = array(); |
| 278 | + $result->setContent( $result_array['psttext'], $this->pstText ); |
| 279 | + } |
| 280 | + } |
283 | 281 | |
284 | 282 | if ( !is_null( $oldid ) ) { |
285 | 283 | $result_array['revid'] = intval( $oldid ); |
— | — | @@ -304,6 +302,35 @@ |
305 | 303 | } |
306 | 304 | } |
307 | 305 | |
| 306 | + /** |
| 307 | + * @param $articleObj Article |
| 308 | + * @param $titleObj Title |
| 309 | + * @param $popts ParserOptions |
| 310 | + * @param $pageId Int |
| 311 | + * @param $getWikitext Bool |
| 312 | + * @return ParserOutput |
| 313 | + */ |
| 314 | + private function getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageId = null, $getWikitext = false ) { |
| 315 | + if ( $this->section !== false ) { |
| 316 | + global $wgParser; |
| 317 | + |
| 318 | + $this->text = $this->getSectionText( $articleObj->getRawText(), !is_null ( $pageId ) |
| 319 | + ? 'page id ' . $pageId : $titleObj->getText() ); |
| 320 | + |
| 321 | + return $wgParser->parse( $this->text, $titleObj, $popts ); |
| 322 | + } else { |
| 323 | + // Try the parser cache first |
| 324 | + $pout = $articleObj->getParserOutput(); |
| 325 | + if ( $getWikitext ) { |
| 326 | + $rev = Revision::newFromTitle( $titleObj ); |
| 327 | + if ( $rev ) { |
| 328 | + $this->text = $rev->getText(); |
| 329 | + } |
| 330 | + } |
| 331 | + return $pout; |
| 332 | + } |
| 333 | + } |
| 334 | + |
308 | 335 | private function getSectionText( $text, $what ) { |
309 | 336 | global $wgParser; |
310 | 337 | $text = $wgParser->getSection( $text, $this->section, false ); |
— | — | @@ -318,7 +345,12 @@ |
319 | 346 | foreach ( $links as $link ) { |
320 | 347 | $entry = array(); |
321 | 348 | $bits = explode( ':', $link, 2 ); |
| 349 | + $title = Title::newFromText( $link ); |
| 350 | + |
322 | 351 | $entry['lang'] = $bits[0]; |
| 352 | + if ( $title ) { |
| 353 | + $entry['url'] = $title->getFullURL(); |
| 354 | + } |
323 | 355 | $this->getResult()->setContent( $entry, $bits[1] ); |
324 | 356 | $result[] = $entry; |
325 | 357 | } |
— | — | @@ -445,6 +477,7 @@ |
446 | 478 | 'headitems', |
447 | 479 | 'headhtml', |
448 | 480 | 'iwlinks', |
| 481 | + 'wikitext', |
449 | 482 | ) |
450 | 483 | ), |
451 | 484 | 'pst' => false, |
— | — | @@ -482,6 +515,7 @@ |
483 | 516 | ' headitems - Gives items to put in the <head> of the page', |
484 | 517 | ' headhtml - Gives parsed <head> of the page', |
485 | 518 | ' iwlinks - Gives interwiki links in the parsed wikitext', |
| 519 | + ' wikitext - Gives the original wikitext that was parsed', |
486 | 520 | 'NOTE: Section tree is only generated if there are more than 4 sections, or if the __TOC__ keyword is present' |
487 | 521 | ), |
488 | 522 | 'pst' => array( |
Property changes on: branches/REL1_17/phase3/includes/api/ApiParse.php |
___________________________________________________________________ |
Modified: svn:mergeinfo |
489 | 523 | Merged /trunk/phase3/includes/api/ApiParse.php:r79454-79456,79536,79727,79733,79773-79774,80728,81121,81124,81406 |
Index: branches/REL1_17/phase3/includes/api/ApiParamInfo.php |
— | — | @@ -88,6 +88,7 @@ |
89 | 89 | $result = $this->getResult(); |
90 | 90 | $retval['classname'] = get_class( $obj ); |
91 | 91 | $retval['description'] = implode( "\n", (array)$obj->getDescription() ); |
| 92 | + $retval['examples'] = implode( "\n", (array)$obj->getExamples() ); |
92 | 93 | $retval['version'] = implode( "\n", (array)$obj->getVersion() ); |
93 | 94 | $retval['prefix'] = $obj->getModulePrefix(); |
94 | 95 | |
Index: branches/REL1_17/phase3/RELEASE-NOTES |
— | — | @@ -495,6 +495,7 @@ |
496 | 496 | * BREAKING CHANGE: action=patrol now requires POST |
497 | 497 | * BREAKING CHANGE: patrol token is no longer the same as edit token |
498 | 498 | * (bug 24650) Fix API to work with categorylinks changes |
| 499 | +* action=parse now correctly returns an error for nonexistent pages |
499 | 500 | |
500 | 501 | * (bug 22738) Allow filtering by action type on query=logevent. |
501 | 502 | * (bug 22764) uselang parameter for action=parse. |
Property changes on: branches/REL1_17/phase3/RELEASE-NOTES |
___________________________________________________________________ |
Modified: svn:mergeinfo |
502 | 503 | Merged /trunk/phase3/RELEASE-NOTES:r79454-79456,79536,79727,79733,79773-79774,80728,81121,81124 |