MediaWiki
REL1_20
|
00001 <?php 00028 class ApiParse extends ApiBase { 00029 private $section, $text, $pstText = null; 00030 00031 public function __construct( $main, $action ) { 00032 parent::__construct( $main, $action ); 00033 } 00034 00035 public function execute() { 00036 // The data is hot but user-dependent, like page views, so we set vary cookies 00037 $this->getMain()->setCacheMode( 'anon-public-user-private' ); 00038 00039 // Get parameters 00040 $params = $this->extractRequestParams(); 00041 $text = $params['text']; 00042 $title = $params['title']; 00043 $page = $params['page']; 00044 $pageid = $params['pageid']; 00045 $oldid = $params['oldid']; 00046 00047 if ( !is_null( $page ) && ( !is_null( $text ) || $title != 'API' ) ) { 00048 $this->dieUsage( 'The page parameter cannot be used together with the text and title parameters', 'params' ); 00049 } 00050 00051 $prop = array_flip( $params['prop'] ); 00052 00053 if ( isset( $params['section'] ) ) { 00054 $this->section = $params['section']; 00055 } else { 00056 $this->section = false; 00057 } 00058 00059 // The parser needs $wgTitle to be set, apparently the 00060 // $title parameter in Parser::parse isn't enough *sigh* 00061 // TODO: Does this still need $wgTitle? 00062 global $wgParser, $wgTitle; 00063 00064 // Currently unnecessary, code to act as a safeguard against any change in current behaviour of uselang breaks 00065 $oldLang = null; 00066 if ( isset( $params['uselang'] ) && $params['uselang'] != $this->getContext()->getLanguage()->getCode() ) { 00067 $oldLang = $this->getContext()->getLanguage(); // Backup language 00068 $this->getContext()->setLanguage( Language::factory( $params['uselang'] ) ); 00069 } 00070 00071 $redirValues = null; 00072 00073 // Return result 00074 $result = $this->getResult(); 00075 00076 if ( !is_null( $oldid ) || !is_null( $pageid ) || !is_null( $page ) ) { 00077 if ( !is_null( $oldid ) ) { 00078 // Don't use the parser cache 00079 $rev = Revision::newFromID( $oldid ); 00080 if ( !$rev ) { 00081 $this->dieUsage( "There is no revision ID $oldid", 'missingrev' ); 00082 } 00083 if ( !$rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) { 00084 $this->dieUsage( "You don't have permission to view deleted revisions", 'permissiondenied' ); 00085 } 00086 00087 $titleObj = $rev->getTitle(); 00088 $wgTitle = $titleObj; 00089 $pageObj = WikiPage::factory( $titleObj ); 00090 $popts = $pageObj->makeParserOptions( $this->getContext() ); 00091 $popts->enableLimitReport( !$params['disablepp'] ); 00092 00093 // If for some reason the "oldid" is actually the current revision, it may be cached 00094 if ( $titleObj->getLatestRevID() === intval( $oldid ) ) { 00095 // May get from/save to parser cache 00096 $p_result = $this->getParsedSectionOrText( $pageObj, $popts, $pageid, 00097 isset( $prop['wikitext'] ) ) ; 00098 } else { // This is an old revision, so get the text differently 00099 $this->text = $rev->getText( Revision::FOR_THIS_USER, $this->getUser() ); 00100 00101 if ( $this->section !== false ) { 00102 $this->text = $this->getSectionText( $this->text, 'r' . $rev->getId() ); 00103 } 00104 00105 // Should we save old revision parses to the parser cache? 00106 $p_result = $wgParser->parse( $this->text, $titleObj, $popts ); 00107 } 00108 } else { // Not $oldid, but $pageid or $page 00109 if ( $params['redirects'] ) { 00110 $reqParams = array( 00111 'action' => 'query', 00112 'redirects' => '', 00113 ); 00114 if ( !is_null ( $pageid ) ) { 00115 $reqParams['pageids'] = $pageid; 00116 } else { // $page 00117 $reqParams['titles'] = $page; 00118 } 00119 $req = new FauxRequest( $reqParams ); 00120 $main = new ApiMain( $req ); 00121 $main->execute(); 00122 $data = $main->getResultData(); 00123 $redirValues = isset( $data['query']['redirects'] ) 00124 ? $data['query']['redirects'] 00125 : array(); 00126 $to = $page; 00127 foreach ( (array)$redirValues as $r ) { 00128 $to = $r['to']; 00129 } 00130 $pageParams = array( 'title' => $to ); 00131 } elseif ( !is_null( $pageid ) ) { 00132 $pageParams = array( 'pageid' => $pageid ); 00133 } else { // $page 00134 $pageParams = array( 'title' => $page ); 00135 } 00136 00137 $pageObj = $this->getTitleOrPageId( $pageParams, 'fromdb' ); 00138 $titleObj = $pageObj->getTitle(); 00139 $wgTitle = $titleObj; 00140 00141 if ( isset( $prop['revid'] ) ) { 00142 $oldid = $pageObj->getLatest(); 00143 } 00144 00145 $popts = $pageObj->makeParserOptions( $this->getContext() ); 00146 $popts->enableLimitReport( !$params['disablepp'] ); 00147 00148 // Potentially cached 00149 $p_result = $this->getParsedSectionOrText( $pageObj, $popts, $pageid, 00150 isset( $prop['wikitext'] ) ) ; 00151 } 00152 } else { // Not $oldid, $pageid, $page. Hence based on $text 00153 00154 if ( is_null( $text ) ) { 00155 $this->dieUsage( 'The text parameter should be passed with the title parameter. Should you be using the "page" parameter instead?', 'params' ); 00156 } 00157 $this->text = $text; 00158 $titleObj = Title::newFromText( $title ); 00159 if ( !$titleObj ) { 00160 $this->dieUsageMsg( array( 'invalidtitle', $title ) ); 00161 } 00162 $wgTitle = $titleObj; 00163 $pageObj = WikiPage::factory( $titleObj ); 00164 00165 $popts = $pageObj->makeParserOptions( $this->getContext() ); 00166 $popts->enableLimitReport( !$params['disablepp'] ); 00167 00168 if ( $this->section !== false ) { 00169 $this->text = $this->getSectionText( $this->text, $titleObj->getText() ); 00170 } 00171 00172 if ( $params['pst'] || $params['onlypst'] ) { 00173 $this->pstText = $wgParser->preSaveTransform( $this->text, $titleObj, $this->getUser(), $popts ); 00174 } 00175 if ( $params['onlypst'] ) { 00176 // Build a result and bail out 00177 $result_array = array(); 00178 $result_array['text'] = array(); 00179 $result->setContent( $result_array['text'], $this->pstText ); 00180 if ( isset( $prop['wikitext'] ) ) { 00181 $result_array['wikitext'] = array(); 00182 $result->setContent( $result_array['wikitext'], $this->text ); 00183 } 00184 $result->addValue( null, $this->getModuleName(), $result_array ); 00185 return; 00186 } 00187 // Not cached (save or load) 00188 $p_result = $wgParser->parse( $params['pst'] ? $this->pstText : $this->text, $titleObj, $popts ); 00189 } 00190 00191 $result_array = array(); 00192 00193 $result_array['title'] = $titleObj->getPrefixedText(); 00194 00195 if ( !is_null( $oldid ) ) { 00196 $result_array['revid'] = intval( $oldid ); 00197 } 00198 00199 if ( $params['redirects'] && !is_null( $redirValues ) ) { 00200 $result_array['redirects'] = $redirValues; 00201 } 00202 00203 if ( isset( $prop['text'] ) ) { 00204 $result_array['text'] = array(); 00205 $result->setContent( $result_array['text'], $p_result->getText() ); 00206 } 00207 00208 if ( !is_null( $params['summary'] ) ) { 00209 $result_array['parsedsummary'] = array(); 00210 $result->setContent( $result_array['parsedsummary'], Linker::formatComment( $params['summary'], $titleObj ) ); 00211 } 00212 00213 if ( isset( $prop['langlinks'] ) ) { 00214 $result_array['langlinks'] = $this->formatLangLinks( $p_result->getLanguageLinks() ); 00215 } 00216 if ( isset( $prop['languageshtml'] ) ) { 00217 $languagesHtml = $this->languagesHtml( $p_result->getLanguageLinks() ); 00218 $result_array['languageshtml'] = array(); 00219 $result->setContent( $result_array['languageshtml'], $languagesHtml ); 00220 } 00221 if ( isset( $prop['categories'] ) ) { 00222 $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() ); 00223 } 00224 if ( isset( $prop['categorieshtml'] ) ) { 00225 $categoriesHtml = $this->categoriesHtml( $p_result->getCategories() ); 00226 $result_array['categorieshtml'] = array(); 00227 $result->setContent( $result_array['categorieshtml'], $categoriesHtml ); 00228 } 00229 if ( isset( $prop['links'] ) ) { 00230 $result_array['links'] = $this->formatLinks( $p_result->getLinks() ); 00231 } 00232 if ( isset( $prop['templates'] ) ) { 00233 $result_array['templates'] = $this->formatLinks( $p_result->getTemplates() ); 00234 } 00235 if ( isset( $prop['images'] ) ) { 00236 $result_array['images'] = array_keys( $p_result->getImages() ); 00237 } 00238 if ( isset( $prop['externallinks'] ) ) { 00239 $result_array['externallinks'] = array_keys( $p_result->getExternalLinks() ); 00240 } 00241 if ( isset( $prop['sections'] ) ) { 00242 $result_array['sections'] = $p_result->getSections(); 00243 } 00244 00245 if ( isset( $prop['displaytitle'] ) ) { 00246 $result_array['displaytitle'] = $p_result->getDisplayTitle() ? 00247 $p_result->getDisplayTitle() : 00248 $titleObj->getPrefixedText(); 00249 } 00250 00251 if ( isset( $prop['headitems'] ) || isset( $prop['headhtml'] ) ) { 00252 $context = $this->getContext(); 00253 $context->setTitle( $titleObj ); 00254 $context->getOutput()->addParserOutputNoText( $p_result ); 00255 00256 if ( isset( $prop['headitems'] ) ) { 00257 $headItems = $this->formatHeadItems( $p_result->getHeadItems() ); 00258 00259 $css = $this->formatCss( $context->getOutput()->buildCssLinksArray() ); 00260 00261 $scripts = array( $context->getOutput()->getHeadScripts() ); 00262 00263 $result_array['headitems'] = array_merge( $headItems, $css, $scripts ); 00264 } 00265 00266 if ( isset( $prop['headhtml'] ) ) { 00267 $result_array['headhtml'] = array(); 00268 $result->setContent( $result_array['headhtml'], $context->getOutput()->headElement( $context->getSkin() ) ); 00269 } 00270 } 00271 00272 if ( isset( $prop['iwlinks'] ) ) { 00273 $result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() ); 00274 } 00275 00276 if ( isset( $prop['wikitext'] ) ) { 00277 $result_array['wikitext'] = array(); 00278 $result->setContent( $result_array['wikitext'], $this->text ); 00279 if ( !is_null( $this->pstText ) ) { 00280 $result_array['psttext'] = array(); 00281 $result->setContent( $result_array['psttext'], $this->pstText ); 00282 } 00283 } 00284 if ( isset( $prop['properties'] ) ) { 00285 $result_array['properties'] = $this->formatProperties( $p_result->getProperties() ); 00286 } 00287 00288 if ( $params['generatexml'] ) { 00289 $wgParser->startExternalParse( $titleObj, $popts, OT_PREPROCESS ); 00290 $dom = $wgParser->preprocessToDom( $this->text ); 00291 if ( is_callable( array( $dom, 'saveXML' ) ) ) { 00292 $xml = $dom->saveXML(); 00293 } else { 00294 $xml = $dom->__toString(); 00295 } 00296 $result_array['parsetree'] = array(); 00297 $result->setContent( $result_array['parsetree'], $xml ); 00298 } 00299 00300 $result_mapping = array( 00301 'redirects' => 'r', 00302 'langlinks' => 'll', 00303 'categories' => 'cl', 00304 'links' => 'pl', 00305 'templates' => 'tl', 00306 'images' => 'img', 00307 'externallinks' => 'el', 00308 'iwlinks' => 'iw', 00309 'sections' => 's', 00310 'headitems' => 'hi', 00311 'properties' => 'pp', 00312 ); 00313 $this->setIndexedTagNames( $result_array, $result_mapping ); 00314 $result->addValue( null, $this->getModuleName(), $result_array ); 00315 00316 if ( !is_null( $oldLang ) ) { 00317 $this->getContext()->setLanguage( $oldLang ); // Reset language to $oldLang 00318 } 00319 } 00320 00328 private function getParsedSectionOrText( $page, $popts, $pageId = null, $getWikitext = false ) { 00329 global $wgParser; 00330 00331 if ( $this->section !== false ) { 00332 $this->text = $this->getSectionText( $page->getRawText(), !is_null( $pageId ) 00333 ? 'page id ' . $pageId : $page->getTitle()->getPrefixedText() ); 00334 00335 // Not cached (save or load) 00336 return $wgParser->parse( $this->text, $page->getTitle(), $popts ); 00337 } else { 00338 // Try the parser cache first 00339 // getParserOutput will save to Parser cache if able 00340 $pout = $page->getParserOutput( $popts ); 00341 if ( !$pout ) { 00342 $this->dieUsage( "There is no revision ID {$page->getLatest()}", 'missingrev' ); 00343 } 00344 if ( $getWikitext ) { 00345 $this->text = $page->getRawText(); 00346 } 00347 return $pout; 00348 } 00349 } 00350 00351 private function getSectionText( $text, $what ) { 00352 global $wgParser; 00353 // Not cached (save or load) 00354 $text = $wgParser->getSection( $text, $this->section, false ); 00355 if ( $text === false ) { 00356 $this->dieUsage( "There is no section {$this->section} in " . $what, 'nosuchsection' ); 00357 } 00358 return $text; 00359 } 00360 00361 private function formatLangLinks( $links ) { 00362 $result = array(); 00363 foreach ( $links as $link ) { 00364 $entry = array(); 00365 $bits = explode( ':', $link, 2 ); 00366 $title = Title::newFromText( $link ); 00367 00368 $entry['lang'] = $bits[0]; 00369 if ( $title ) { 00370 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT ); 00371 } 00372 $this->getResult()->setContent( $entry, $bits[1] ); 00373 $result[] = $entry; 00374 } 00375 return $result; 00376 } 00377 00378 private function formatCategoryLinks( $links ) { 00379 $result = array(); 00380 foreach ( $links as $link => $sortkey ) { 00381 $entry = array(); 00382 $entry['sortkey'] = $sortkey; 00383 $this->getResult()->setContent( $entry, $link ); 00384 $result[] = $entry; 00385 } 00386 return $result; 00387 } 00388 00389 private function categoriesHtml( $categories ) { 00390 $context = $this->getContext(); 00391 $context->getOutput()->addCategoryLinks( $categories ); 00392 return $context->getSkin()->getCategories(); 00393 } 00394 00401 private function languagesHtml( $languages ) { 00402 wfDeprecated( __METHOD__, '1.18' ); 00403 00404 global $wgContLang, $wgHideInterlanguageLinks; 00405 00406 if ( $wgHideInterlanguageLinks || count( $languages ) == 0 ) { 00407 return ''; 00408 } 00409 00410 $s = htmlspecialchars( wfMessage( 'otherlanguages' )->text() . wfMessage( 'colon-separator' )->text() ); 00411 00412 $langs = array(); 00413 foreach ( $languages as $l ) { 00414 $nt = Title::newFromText( $l ); 00415 $text = Language::fetchLanguageName( $nt->getInterwiki() ); 00416 00417 $langs[] = Html::element( 'a', 00418 array( 'href' => $nt->getFullURL(), 'title' => $nt->getText(), 'class' => "external" ), 00419 $text == '' ? $l : $text ); 00420 } 00421 00422 $s .= implode( wfMessage( 'pipe-separator' )->escaped(), $langs ); 00423 00424 if ( $wgContLang->isRTL() ) { 00425 $s = Html::rawElement( 'span', array( 'dir' => "LTR" ), $s ); 00426 } 00427 00428 return $s; 00429 } 00430 00431 private function formatLinks( $links ) { 00432 $result = array(); 00433 foreach ( $links as $ns => $nslinks ) { 00434 foreach ( $nslinks as $title => $id ) { 00435 $entry = array(); 00436 $entry['ns'] = $ns; 00437 $this->getResult()->setContent( $entry, Title::makeTitle( $ns, $title )->getFullText() ); 00438 if ( $id != 0 ) { 00439 $entry['exists'] = ''; 00440 } 00441 $result[] = $entry; 00442 } 00443 } 00444 return $result; 00445 } 00446 00447 private function formatIWLinks( $iw ) { 00448 $result = array(); 00449 foreach ( $iw as $prefix => $titles ) { 00450 foreach ( array_keys( $titles ) as $title ) { 00451 $entry = array(); 00452 $entry['prefix'] = $prefix; 00453 00454 $title = Title::newFromText( "{$prefix}:{$title}" ); 00455 if ( $title ) { 00456 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT ); 00457 } 00458 00459 $this->getResult()->setContent( $entry, $title->getFullText() ); 00460 $result[] = $entry; 00461 } 00462 } 00463 return $result; 00464 } 00465 00466 private function formatHeadItems( $headItems ) { 00467 $result = array(); 00468 foreach ( $headItems as $tag => $content ) { 00469 $entry = array(); 00470 $entry['tag'] = $tag; 00471 $this->getResult()->setContent( $entry, $content ); 00472 $result[] = $entry; 00473 } 00474 return $result; 00475 } 00476 00477 private function formatProperties( $properties ) { 00478 $result = array(); 00479 foreach ( $properties as $name => $value ) { 00480 $entry = array(); 00481 $entry['name'] = $name; 00482 $this->getResult()->setContent( $entry, $value ); 00483 $result[] = $entry; 00484 } 00485 return $result; 00486 } 00487 00488 private function formatCss( $css ) { 00489 $result = array(); 00490 foreach ( $css as $file => $link ) { 00491 $entry = array(); 00492 $entry['file'] = $file; 00493 $this->getResult()->setContent( $entry, $link ); 00494 $result[] = $entry; 00495 } 00496 return $result; 00497 } 00498 00499 private function setIndexedTagNames( &$array, $mapping ) { 00500 foreach ( $mapping as $key => $name ) { 00501 if ( isset( $array[$key] ) ) { 00502 $this->getResult()->setIndexedTagName( $array[$key], $name ); 00503 } 00504 } 00505 } 00506 00507 public function getAllowedParams() { 00508 return array( 00509 'title' => array( 00510 ApiBase::PARAM_DFLT => 'API', 00511 ), 00512 'text' => null, 00513 'summary' => null, 00514 'page' => null, 00515 'pageid' => array( 00516 ApiBase::PARAM_TYPE => 'integer', 00517 ), 00518 'redirects' => false, 00519 'oldid' => array( 00520 ApiBase::PARAM_TYPE => 'integer', 00521 ), 00522 'prop' => array( 00523 ApiBase::PARAM_DFLT => 'text|langlinks|categories|links|templates|images|externallinks|sections|revid|displaytitle|iwlinks|properties', 00524 ApiBase::PARAM_ISMULTI => true, 00525 ApiBase::PARAM_TYPE => array( 00526 'text', 00527 'langlinks', 00528 'languageshtml', 00529 'categories', 00530 'categorieshtml', 00531 'links', 00532 'templates', 00533 'images', 00534 'externallinks', 00535 'sections', 00536 'revid', 00537 'displaytitle', 00538 'headitems', 00539 'headhtml', 00540 'iwlinks', 00541 'wikitext', 00542 'properties', 00543 ) 00544 ), 00545 'pst' => false, 00546 'onlypst' => false, 00547 'uselang' => null, 00548 'section' => null, 00549 'disablepp' => false, 00550 'generatexml' => false, 00551 ); 00552 } 00553 00554 public function getParamDescription() { 00555 $p = $this->getModulePrefix(); 00556 return array( 00557 'text' => 'Wikitext to parse', 00558 'summary' => 'Summary to parse', 00559 'redirects' => "If the {$p}page or the {$p}pageid parameter is set to a redirect, resolve it", 00560 'title' => 'Title of page the text belongs to', 00561 'page' => "Parse the content of this page. Cannot be used together with {$p}text and {$p}title", 00562 'pageid' => "Parse the content of this page. Overrides {$p}page", 00563 'oldid' => "Parse the content of this revision. Overrides {$p}page and {$p}pageid", 00564 'prop' => array( 00565 'Which pieces of information to get', 00566 ' text - Gives the parsed text of the wikitext', 00567 ' langlinks - Gives the language links in the parsed wikitext', 00568 ' categories - Gives the categories in the parsed wikitext', 00569 ' categorieshtml - Gives the HTML version of the categories', 00570 ' languageshtml - Gives the HTML version of the language links', 00571 ' links - Gives the internal links in the parsed wikitext', 00572 ' templates - Gives the templates in the parsed wikitext', 00573 ' images - Gives the images in the parsed wikitext', 00574 ' externallinks - Gives the external links in the parsed wikitext', 00575 ' sections - Gives the sections in the parsed wikitext', 00576 ' revid - Adds the revision ID of the parsed page', 00577 ' displaytitle - Adds the title of the parsed wikitext', 00578 ' headitems - Gives items to put in the <head> of the page', 00579 ' headhtml - Gives parsed <head> of the page', 00580 ' iwlinks - Gives interwiki links in the parsed wikitext', 00581 ' wikitext - Gives the original wikitext that was parsed', 00582 ' properties - Gives various properties defined in the parsed wikitext', 00583 ), 00584 'pst' => array( 00585 'Do a pre-save transform on the input before parsing it', 00586 'Ignored if page, pageid or oldid is used' 00587 ), 00588 'onlypst' => array( 00589 'Do a pre-save transform (PST) on the input, but don\'t parse it', 00590 'Returns the same wikitext, after a PST has been applied. Ignored if page, pageid or oldid is used' 00591 ), 00592 'uselang' => 'Which language to parse the request in', 00593 'section' => 'Only retrieve the content of this section number', 00594 'disablepp' => 'Disable the PP Report from the parser output', 00595 'generatexml' => 'Generate XML parse tree', 00596 ); 00597 } 00598 00599 public function getDescription() { 00600 return array( 00601 'Parses wikitext and returns parser output', 00602 'See the various prop-Modules of action=query to get information from the current version of a page', 00603 ); 00604 } 00605 00606 public function getPossibleErrors() { 00607 return array_merge( parent::getPossibleErrors(), array( 00608 array( 'code' => 'params', 'info' => 'The page parameter cannot be used together with the text and title parameters' ), 00609 array( 'code' => 'params', 'info' => 'The text parameter should be passed with the title parameter. Should you be using the "page" parameter instead?' ), 00610 array( 'code' => 'missingrev', 'info' => 'There is no revision ID oldid' ), 00611 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revisions' ), 00612 array( 'code' => 'missingtitle', 'info' => 'The page you specified doesn\'t exist' ), 00613 array( 'code' => 'nosuchsection', 'info' => 'There is no section sectionnumber in page' ), 00614 array( 'nosuchpageid' ), 00615 array( 'invalidtitle', 'title' ), 00616 ) ); 00617 } 00618 00619 public function getExamples() { 00620 return array( 00621 'api.php?action=parse&text={{Project:Sandbox}}' 00622 ); 00623 } 00624 00625 public function getHelpUrls() { 00626 return 'https://www.mediawiki.org/wiki/API:Parsing_wikitext#parse'; 00627 } 00628 00629 public function getVersion() { 00630 return __CLASS__ . ': $Id$'; 00631 } 00632 }