MediaWiki  REL1_23
ApiParse.php
Go to the documentation of this file.
00001 <?php
00028 class ApiParse extends ApiBase {
00029 
00031     private $section = null;
00032 
00034     private $content = null;
00035 
00037     private $pstContent = null;
00038 
00039     public function execute() {
00040         // The data is hot but user-dependent, like page views, so we set vary cookies
00041         $this->getMain()->setCacheMode( 'anon-public-user-private' );
00042 
00043         // Get parameters
00044         $params = $this->extractRequestParams();
00045         $text = $params['text'];
00046         $title = $params['title'];
00047         if ( $title === null ) {
00048             $titleProvided = false;
00049             // A title is needed for parsing, so arbitrarily choose one
00050             $title = 'API';
00051         } else {
00052             $titleProvided = true;
00053         }
00054 
00055         $page = $params['page'];
00056         $pageid = $params['pageid'];
00057         $oldid = $params['oldid'];
00058 
00059         $model = $params['contentmodel'];
00060         $format = $params['contentformat'];
00061 
00062         if ( !is_null( $page ) && ( !is_null( $text ) || $titleProvided ) ) {
00063             $this->dieUsage(
00064                 'The page parameter cannot be used together with the text and title parameters',
00065                 'params'
00066             );
00067         }
00068 
00069         $prop = array_flip( $params['prop'] );
00070 
00071         if ( isset( $params['section'] ) ) {
00072             $this->section = $params['section'];
00073         } else {
00074             $this->section = false;
00075         }
00076 
00077         // The parser needs $wgTitle to be set, apparently the
00078         // $title parameter in Parser::parse isn't enough *sigh*
00079         // TODO: Does this still need $wgTitle?
00080         global $wgParser, $wgTitle;
00081 
00082         // Currently unnecessary, code to act as a safeguard against any change
00083         // in current behavior of uselang
00084         $oldLang = null;
00085         if ( isset( $params['uselang'] )
00086             && $params['uselang'] != $this->getContext()->getLanguage()->getCode()
00087         ) {
00088             $oldLang = $this->getContext()->getLanguage(); // Backup language
00089             $this->getContext()->setLanguage( Language::factory( $params['uselang'] ) );
00090         }
00091 
00092         $redirValues = null;
00093 
00094         // Return result
00095         $result = $this->getResult();
00096 
00097         if ( !is_null( $oldid ) || !is_null( $pageid ) || !is_null( $page ) ) {
00098             if ( !is_null( $oldid ) ) {
00099                 // Don't use the parser cache
00100                 $rev = Revision::newFromID( $oldid );
00101                 if ( !$rev ) {
00102                     $this->dieUsage( "There is no revision ID $oldid", 'missingrev' );
00103                 }
00104                 if ( !$rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
00105                     $this->dieUsage( "You don't have permission to view deleted revisions", 'permissiondenied' );
00106                 }
00107 
00108                 $titleObj = $rev->getTitle();
00109                 $wgTitle = $titleObj;
00110                 $pageObj = WikiPage::factory( $titleObj );
00111                 $popts = $this->makeParserOptions( $pageObj, $params );
00112 
00113                 // If for some reason the "oldid" is actually the current revision, it may be cached
00114                 if ( $rev->isCurrent() ) {
00115                     // May get from/save to parser cache
00116                     $p_result = $this->getParsedContent( $pageObj, $popts,
00117                         $pageid, isset( $prop['wikitext'] ) );
00118                 } else { // This is an old revision, so get the text differently
00119                     $this->content = $rev->getContent( Revision::FOR_THIS_USER, $this->getUser() );
00120 
00121                     if ( $this->section !== false ) {
00122                         $this->content = $this->getSectionContent( $this->content, 'r' . $rev->getId() );
00123                     }
00124 
00125                     // Should we save old revision parses to the parser cache?
00126                     $p_result = $this->content->getParserOutput( $titleObj, $rev->getId(), $popts );
00127                 }
00128             } else { // Not $oldid, but $pageid or $page
00129                 if ( $params['redirects'] ) {
00130                     $reqParams = array(
00131                         'action' => 'query',
00132                         'redirects' => '',
00133                     );
00134                     if ( !is_null( $pageid ) ) {
00135                         $reqParams['pageids'] = $pageid;
00136                     } else { // $page
00137                         $reqParams['titles'] = $page;
00138                     }
00139                     $req = new FauxRequest( $reqParams );
00140                     $main = new ApiMain( $req );
00141                     $main->execute();
00142                     $data = $main->getResultData();
00143                     $redirValues = isset( $data['query']['redirects'] )
00144                         ? $data['query']['redirects']
00145                         : array();
00146                     $to = $page;
00147                     foreach ( (array)$redirValues as $r ) {
00148                         $to = $r['to'];
00149                     }
00150                     $pageParams = array( 'title' => $to );
00151                 } elseif ( !is_null( $pageid ) ) {
00152                     $pageParams = array( 'pageid' => $pageid );
00153                 } else { // $page
00154                     $pageParams = array( 'title' => $page );
00155                 }
00156 
00157                 $pageObj = $this->getTitleOrPageId( $pageParams, 'fromdb' );
00158                 $titleObj = $pageObj->getTitle();
00159                 if ( !$titleObj || !$titleObj->exists() ) {
00160                     $this->dieUsage( "The page you specified doesn't exist", 'missingtitle' );
00161                 }
00162                 $wgTitle = $titleObj;
00163 
00164                 if ( isset( $prop['revid'] ) ) {
00165                     $oldid = $pageObj->getLatest();
00166                 }
00167 
00168                 $popts = $this->makeParserOptions( $pageObj, $params );
00169 
00170                 // Potentially cached
00171                 $p_result = $this->getParsedContent( $pageObj, $popts, $pageid,
00172                     isset( $prop['wikitext'] ) );
00173             }
00174         } else { // Not $oldid, $pageid, $page. Hence based on $text
00175             $titleObj = Title::newFromText( $title );
00176             if ( !$titleObj || $titleObj->isExternal() ) {
00177                 $this->dieUsageMsg( array( 'invalidtitle', $title ) );
00178             }
00179             $wgTitle = $titleObj;
00180             if ( $titleObj->canExist() ) {
00181                 $pageObj = WikiPage::factory( $titleObj );
00182             } else {
00183                 // Do like MediaWiki::initializeArticle()
00184                 $article = Article::newFromTitle( $titleObj, $this->getContext() );
00185                 $pageObj = $article->getPage();
00186             }
00187 
00188             $popts = $this->makeParserOptions( $pageObj, $params );
00189             $textProvided = !is_null( $text );
00190 
00191             if ( !$textProvided ) {
00192                 if ( $titleProvided && ( $prop || $params['generatexml'] ) ) {
00193                     $this->setWarning(
00194                         "'title' used without 'text', and parsed page properties were requested " .
00195                         "(did you mean to use 'page' instead of 'title'?)"
00196                     );
00197                 }
00198                 // Prevent warning from ContentHandler::makeContent()
00199                 $text = '';
00200             }
00201 
00202             // If we are parsing text, do not use the content model of the default
00203             // API title, but default to wikitext to keep BC.
00204             if ( $textProvided && !$titleProvided && is_null( $model ) ) {
00205                 $model = CONTENT_MODEL_WIKITEXT;
00206                 $this->setWarning( "No 'title' or 'contentmodel' was given, assuming $model." );
00207             }
00208 
00209             try {
00210                 $this->content = ContentHandler::makeContent( $text, $titleObj, $model, $format );
00211             } catch ( MWContentSerializationException $ex ) {
00212                 $this->dieUsage( $ex->getMessage(), 'parseerror' );
00213             }
00214 
00215             if ( $this->section !== false ) {
00216                 $this->content = $this->getSectionContent( $this->content, $titleObj->getText() );
00217             }
00218 
00219             if ( $params['pst'] || $params['onlypst'] ) {
00220                 $this->pstContent = $this->content->preSaveTransform( $titleObj, $this->getUser(), $popts );
00221             }
00222             if ( $params['onlypst'] ) {
00223                 // Build a result and bail out
00224                 $result_array = array();
00225                 $result_array['text'] = array();
00226                 ApiResult::setContent( $result_array['text'], $this->pstContent->serialize( $format ) );
00227                 if ( isset( $prop['wikitext'] ) ) {
00228                     $result_array['wikitext'] = array();
00229                     ApiResult::setContent( $result_array['wikitext'], $this->content->serialize( $format ) );
00230                 }
00231                 $result->addValue( null, $this->getModuleName(), $result_array );
00232 
00233                 return;
00234             }
00235 
00236             // Not cached (save or load)
00237             if ( $params['pst'] ) {
00238                 $p_result = $this->pstContent->getParserOutput( $titleObj, null, $popts );
00239             } else {
00240                 $p_result = $this->content->getParserOutput( $titleObj, null, $popts );
00241             }
00242         }
00243 
00244         $result_array = array();
00245 
00246         $result_array['title'] = $titleObj->getPrefixedText();
00247 
00248         if ( !is_null( $oldid ) ) {
00249             $result_array['revid'] = intval( $oldid );
00250         }
00251 
00252         if ( $params['redirects'] && !is_null( $redirValues ) ) {
00253             $result_array['redirects'] = $redirValues;
00254         }
00255 
00256         if ( $params['disabletoc'] ) {
00257             $p_result->setTOCEnabled( false );
00258         }
00259 
00260         if ( isset( $prop['text'] ) ) {
00261             $result_array['text'] = array();
00262             ApiResult::setContent( $result_array['text'], $p_result->getText() );
00263         }
00264 
00265         if ( !is_null( $params['summary'] ) ) {
00266             $result_array['parsedsummary'] = array();
00267             ApiResult::setContent(
00268                 $result_array['parsedsummary'],
00269                 Linker::formatComment( $params['summary'], $titleObj )
00270             );
00271         }
00272 
00273         if ( isset( $prop['langlinks'] ) || isset( $prop['languageshtml'] ) ) {
00274             $langlinks = $p_result->getLanguageLinks();
00275 
00276             if ( $params['effectivelanglinks'] ) {
00277                 // Link flags are ignored for now, but may in the future be
00278                 // included in the result.
00279                 $linkFlags = array();
00280                 wfRunHooks( 'LanguageLinks', array( $titleObj, &$langlinks, &$linkFlags ) );
00281             }
00282         } else {
00283             $langlinks = false;
00284         }
00285 
00286         if ( isset( $prop['langlinks'] ) ) {
00287             $result_array['langlinks'] = $this->formatLangLinks( $langlinks );
00288         }
00289         if ( isset( $prop['languageshtml'] ) ) {
00290             $languagesHtml = $this->languagesHtml( $langlinks );
00291 
00292             $result_array['languageshtml'] = array();
00293             ApiResult::setContent( $result_array['languageshtml'], $languagesHtml );
00294         }
00295         if ( isset( $prop['categories'] ) ) {
00296             $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
00297         }
00298         if ( isset( $prop['categorieshtml'] ) ) {
00299             $categoriesHtml = $this->categoriesHtml( $p_result->getCategories() );
00300             $result_array['categorieshtml'] = array();
00301             ApiResult::setContent( $result_array['categorieshtml'], $categoriesHtml );
00302         }
00303         if ( isset( $prop['links'] ) ) {
00304             $result_array['links'] = $this->formatLinks( $p_result->getLinks() );
00305         }
00306         if ( isset( $prop['templates'] ) ) {
00307             $result_array['templates'] = $this->formatLinks( $p_result->getTemplates() );
00308         }
00309         if ( isset( $prop['images'] ) ) {
00310             $result_array['images'] = array_keys( $p_result->getImages() );
00311         }
00312         if ( isset( $prop['externallinks'] ) ) {
00313             $result_array['externallinks'] = array_keys( $p_result->getExternalLinks() );
00314         }
00315         if ( isset( $prop['sections'] ) ) {
00316             $result_array['sections'] = $p_result->getSections();
00317         }
00318 
00319         if ( isset( $prop['displaytitle'] ) ) {
00320             $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
00321                 $p_result->getDisplayTitle() :
00322                 $titleObj->getPrefixedText();
00323         }
00324 
00325         if ( isset( $prop['headitems'] ) || isset( $prop['headhtml'] ) ) {
00326             $context = $this->getContext();
00327             $context->setTitle( $titleObj );
00328             $context->getOutput()->addParserOutputNoText( $p_result );
00329 
00330             if ( isset( $prop['headitems'] ) ) {
00331                 $headItems = $this->formatHeadItems( $p_result->getHeadItems() );
00332 
00333                 $css = $this->formatCss( $context->getOutput()->buildCssLinksArray() );
00334 
00335                 $scripts = array( $context->getOutput()->getHeadScripts() );
00336 
00337                 $result_array['headitems'] = array_merge( $headItems, $css, $scripts );
00338             }
00339 
00340             if ( isset( $prop['headhtml'] ) ) {
00341                 $result_array['headhtml'] = array();
00342                 ApiResult::setContent(
00343                     $result_array['headhtml'],
00344                     $context->getOutput()->headElement( $context->getSkin() )
00345                 );
00346             }
00347         }
00348 
00349         if ( isset( $prop['iwlinks'] ) ) {
00350             $result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
00351         }
00352 
00353         if ( isset( $prop['wikitext'] ) ) {
00354             $result_array['wikitext'] = array();
00355             ApiResult::setContent( $result_array['wikitext'], $this->content->serialize( $format ) );
00356             if ( !is_null( $this->pstContent ) ) {
00357                 $result_array['psttext'] = array();
00358                 ApiResult::setContent( $result_array['psttext'], $this->pstContent->serialize( $format ) );
00359             }
00360         }
00361         if ( isset( $prop['properties'] ) ) {
00362             $result_array['properties'] = $this->formatProperties( $p_result->getProperties() );
00363         }
00364 
00365         if ( isset( $prop['limitreportdata'] ) ) {
00366             $result_array['limitreportdata'] = $this->formatLimitReportData( $p_result->getLimitReportData() );
00367         }
00368         if ( isset( $prop['limitreporthtml'] ) ) {
00369             $limitreportHtml = EditPage::getPreviewLimitReport( $p_result );
00370             $result_array['limitreporthtml'] = array();
00371             ApiResult::setContent( $result_array['limitreporthtml'], $limitreportHtml );
00372         }
00373 
00374         if ( $params['generatexml'] ) {
00375             if ( $this->content->getModel() != CONTENT_MODEL_WIKITEXT ) {
00376                 $this->dieUsage( "generatexml is only supported for wikitext content", "notwikitext" );
00377             }
00378 
00379             $wgParser->startExternalParse( $titleObj, $popts, OT_PREPROCESS );
00380             $dom = $wgParser->preprocessToDom( $this->content->getNativeData() );
00381             if ( is_callable( array( $dom, 'saveXML' ) ) ) {
00382                 $xml = $dom->saveXML();
00383             } else {
00384                 $xml = $dom->__toString();
00385             }
00386             $result_array['parsetree'] = array();
00387             ApiResult::setContent( $result_array['parsetree'], $xml );
00388         }
00389 
00390         $result_mapping = array(
00391             'redirects' => 'r',
00392             'langlinks' => 'll',
00393             'categories' => 'cl',
00394             'links' => 'pl',
00395             'templates' => 'tl',
00396             'images' => 'img',
00397             'externallinks' => 'el',
00398             'iwlinks' => 'iw',
00399             'sections' => 's',
00400             'headitems' => 'hi',
00401             'properties' => 'pp',
00402             'limitreportdata' => 'lr',
00403         );
00404         $this->setIndexedTagNames( $result_array, $result_mapping );
00405         $result->addValue( null, $this->getModuleName(), $result_array );
00406 
00407         if ( !is_null( $oldLang ) ) {
00408             $this->getContext()->setLanguage( $oldLang ); // Reset language to $oldLang
00409         }
00410     }
00411 
00420     protected function makeParserOptions( WikiPage $pageObj, array $params ) {
00421         wfProfileIn( __METHOD__ );
00422 
00423         $popts = $pageObj->makeParserOptions( $this->getContext() );
00424         $popts->enableLimitReport( !$params['disablepp'] );
00425         $popts->setIsPreview( $params['preview'] || $params['sectionpreview'] );
00426         $popts->setIsSectionPreview( $params['sectionpreview'] );
00427 
00428         wfProfileOut( __METHOD__ );
00429 
00430         return $popts;
00431     }
00432 
00440     private function getParsedContent( WikiPage $page, $popts, $pageId = null, $getWikitext = false ) {
00441         $this->content = $page->getContent( Revision::RAW ); //XXX: really raw?
00442 
00443         if ( $this->section !== false && $this->content !== null ) {
00444             $this->content = $this->getSectionContent(
00445                 $this->content,
00446                 !is_null( $pageId ) ? 'page id ' . $pageId : $page->getTitle()->getText()
00447             );
00448 
00449             // Not cached (save or load)
00450             return $this->content->getParserOutput( $page->getTitle(), null, $popts );
00451         }
00452 
00453         // Try the parser cache first
00454         // getParserOutput will save to Parser cache if able
00455         $pout = $page->getParserOutput( $popts );
00456         if ( !$pout ) {
00457             $this->dieUsage( "There is no revision ID {$page->getLatest()}", 'missingrev' );
00458         }
00459         if ( $getWikitext ) {
00460             $this->content = $page->getContent( Revision::RAW );
00461         }
00462 
00463         return $pout;
00464     }
00465 
00466     private function getSectionContent( Content $content, $what ) {
00467         // Not cached (save or load)
00468         $section = $content->getSection( $this->section );
00469         if ( $section === false ) {
00470             $this->dieUsage( "There is no section {$this->section} in " . $what, 'nosuchsection' );
00471         }
00472         if ( $section === null ) {
00473             $this->dieUsage( "Sections are not supported by " . $what, 'nosuchsection' );
00474             $section = false;
00475         }
00476 
00477         return $section;
00478     }
00479 
00480     private function formatLangLinks( $links ) {
00481         $result = array();
00482         foreach ( $links as $link ) {
00483             $entry = array();
00484             $bits = explode( ':', $link, 2 );
00485             $title = Title::newFromText( $link );
00486 
00487             $entry['lang'] = $bits[0];
00488             if ( $title ) {
00489                 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
00490                 // localised language name in user language (maybe set by uselang=)
00491                 $entry['langname'] = Language::fetchLanguageName( $title->getInterwiki(), $this->getLanguage()->getCode() );
00492                 // native language name
00493                 $entry['autonym'] = Language::fetchLanguageName( $title->getInterwiki() );
00494             }
00495             ApiResult::setContent( $entry, $bits[1] );
00496             $result[] = $entry;
00497         }
00498 
00499         return $result;
00500     }
00501 
00502     private function formatCategoryLinks( $links ) {
00503         $result = array();
00504 
00505         if ( !$links ) {
00506             return $result;
00507         }
00508 
00509         // Fetch hiddencat property
00510         $lb = new LinkBatch;
00511         $lb->setArray( array( NS_CATEGORY => $links ) );
00512         $db = $this->getDB();
00513         $res = $db->select( array( 'page', 'page_props' ),
00514             array( 'page_title', 'pp_propname' ),
00515             $lb->constructSet( 'page', $db ),
00516             __METHOD__,
00517             array(),
00518             array( 'page_props' => array(
00519                 'LEFT JOIN', array( 'pp_propname' => 'hiddencat', 'pp_page = page_id' )
00520             ) )
00521         );
00522         $hiddencats = array();
00523         foreach ( $res as $row ) {
00524             $hiddencats[$row->page_title] = isset( $row->pp_propname );
00525         }
00526 
00527         foreach ( $links as $link => $sortkey ) {
00528             $entry = array();
00529             $entry['sortkey'] = $sortkey;
00530             ApiResult::setContent( $entry, $link );
00531             if ( !isset( $hiddencats[$link] ) ) {
00532                 $entry['missing'] = '';
00533             } elseif ( $hiddencats[$link] ) {
00534                 $entry['hidden'] = '';
00535             }
00536             $result[] = $entry;
00537         }
00538 
00539         return $result;
00540     }
00541 
00542     private function categoriesHtml( $categories ) {
00543         $context = $this->getContext();
00544         $context->getOutput()->addCategoryLinks( $categories );
00545 
00546         return $context->getSkin()->getCategories();
00547     }
00548 
00555     private function languagesHtml( $languages ) {
00556         wfDeprecated( __METHOD__, '1.18' );
00557         $this->setWarning( '"action=parse&prop=languageshtml" is deprecated ' .
00558             'and will be removed in MediaWiki 1.24. Use "prop=langlinks" ' .
00559             'to generate your own HTML.' );
00560 
00561         global $wgContLang, $wgHideInterlanguageLinks;
00562 
00563         if ( $wgHideInterlanguageLinks || count( $languages ) == 0 ) {
00564             return '';
00565         }
00566 
00567         $s = htmlspecialchars( wfMessage( 'otherlanguages' )->text() .
00568             wfMessage( 'colon-separator' )->text() );
00569 
00570         $langs = array();
00571         foreach ( $languages as $l ) {
00572             $nt = Title::newFromText( $l );
00573             $text = Language::fetchLanguageName( $nt->getInterwiki() );
00574 
00575             $langs[] = Html::element( 'a',
00576                 array( 'href' => $nt->getFullURL(), 'title' => $nt->getText(), 'class' => 'external' ),
00577                 $text == '' ? $l : $text );
00578         }
00579 
00580         $s .= implode( wfMessage( 'pipe-separator' )->escaped(), $langs );
00581 
00582         if ( $wgContLang->isRTL() ) {
00583             $s = Html::rawElement( 'span', array( 'dir' => 'LTR' ), $s );
00584         }
00585 
00586         return $s;
00587     }
00588 
00589     private function formatLinks( $links ) {
00590         $result = array();
00591         foreach ( $links as $ns => $nslinks ) {
00592             foreach ( $nslinks as $title => $id ) {
00593                 $entry = array();
00594                 $entry['ns'] = $ns;
00595                 ApiResult::setContent( $entry, Title::makeTitle( $ns, $title )->getFullText() );
00596                 if ( $id != 0 ) {
00597                     $entry['exists'] = '';
00598                 }
00599                 $result[] = $entry;
00600             }
00601         }
00602 
00603         return $result;
00604     }
00605 
00606     private function formatIWLinks( $iw ) {
00607         $result = array();
00608         foreach ( $iw as $prefix => $titles ) {
00609             foreach ( array_keys( $titles ) as $title ) {
00610                 $entry = array();
00611                 $entry['prefix'] = $prefix;
00612 
00613                 $title = Title::newFromText( "{$prefix}:{$title}" );
00614                 if ( $title ) {
00615                     $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
00616                 }
00617 
00618                 ApiResult::setContent( $entry, $title->getFullText() );
00619                 $result[] = $entry;
00620             }
00621         }
00622 
00623         return $result;
00624     }
00625 
00626     private function formatHeadItems( $headItems ) {
00627         $result = array();
00628         foreach ( $headItems as $tag => $content ) {
00629             $entry = array();
00630             $entry['tag'] = $tag;
00631             ApiResult::setContent( $entry, $content );
00632             $result[] = $entry;
00633         }
00634 
00635         return $result;
00636     }
00637 
00638     private function formatProperties( $properties ) {
00639         $result = array();
00640         foreach ( $properties as $name => $value ) {
00641             $entry = array();
00642             $entry['name'] = $name;
00643             ApiResult::setContent( $entry, $value );
00644             $result[] = $entry;
00645         }
00646 
00647         return $result;
00648     }
00649 
00650     private function formatCss( $css ) {
00651         $result = array();
00652         foreach ( $css as $file => $link ) {
00653             $entry = array();
00654             $entry['file'] = $file;
00655             ApiResult::setContent( $entry, $link );
00656             $result[] = $entry;
00657         }
00658 
00659         return $result;
00660     }
00661 
00662     private function formatLimitReportData( $limitReportData ) {
00663         $result = array();
00664         $apiResult = $this->getResult();
00665 
00666         foreach ( $limitReportData as $name => $value ) {
00667             $entry = array();
00668             $entry['name'] = $name;
00669             if ( !is_array( $value ) ) {
00670                 $value = array( $value );
00671             }
00672             $apiResult->setIndexedTagName( $value, 'param' );
00673             $apiResult->setIndexedTagName_recursive( $value, 'param' );
00674             $entry = array_merge( $entry, $value );
00675             $result[] = $entry;
00676         }
00677 
00678         return $result;
00679     }
00680 
00681     private function setIndexedTagNames( &$array, $mapping ) {
00682         foreach ( $mapping as $key => $name ) {
00683             if ( isset( $array[$key] ) ) {
00684                 $this->getResult()->setIndexedTagName( $array[$key], $name );
00685             }
00686         }
00687     }
00688 
00689     public function getAllowedParams() {
00690         return array(
00691             'title' => null,
00692             'text' => null,
00693             'summary' => null,
00694             'page' => null,
00695             'pageid' => array(
00696                 ApiBase::PARAM_TYPE => 'integer',
00697             ),
00698             'redirects' => false,
00699             'oldid' => array(
00700                 ApiBase::PARAM_TYPE => 'integer',
00701             ),
00702             'prop' => array(
00703                 ApiBase::PARAM_DFLT => 'text|langlinks|categories|links|templates|' .
00704                     'images|externallinks|sections|revid|displaytitle|iwlinks|properties',
00705                 ApiBase::PARAM_ISMULTI => true,
00706                 ApiBase::PARAM_TYPE => array(
00707                     'text',
00708                     'langlinks',
00709                     'languageshtml',
00710                     'categories',
00711                     'categorieshtml',
00712                     'links',
00713                     'templates',
00714                     'images',
00715                     'externallinks',
00716                     'sections',
00717                     'revid',
00718                     'displaytitle',
00719                     'headitems',
00720                     'headhtml',
00721                     'iwlinks',
00722                     'wikitext',
00723                     'properties',
00724                     'limitreportdata',
00725                     'limitreporthtml',
00726                 )
00727             ),
00728             'pst' => false,
00729             'onlypst' => false,
00730             'effectivelanglinks' => false,
00731             'uselang' => null,
00732             'section' => null,
00733             'disablepp' => false,
00734             'generatexml' => false,
00735             'preview' => false,
00736             'sectionpreview' => false,
00737             'disabletoc' => false,
00738             'contentformat' => array(
00739                 ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
00740             ),
00741             'contentmodel' => array(
00742                 ApiBase::PARAM_TYPE => ContentHandler::getContentModels(),
00743             )
00744         );
00745     }
00746 
00747     public function getParamDescription() {
00748         $p = $this->getModulePrefix();
00749         $wikitext = CONTENT_MODEL_WIKITEXT;
00750 
00751         return array(
00752             'text' => "Text to parse. Use {$p}title or {$p}contentmodel to control the content model",
00753             'summary' => 'Summary to parse',
00754             'redirects' => "If the {$p}page or the {$p}pageid parameter is set to a redirect, resolve it",
00755             'title' => "Title of page the text belongs to. " .
00756                 "If omitted, {$p}contentmodel must be specified, and \"API\" will be used as the title",
00757             'page' => "Parse the content of this page. Cannot be used together with {$p}text and {$p}title",
00758             'pageid' => "Parse the content of this page. Overrides {$p}page",
00759             'oldid' => "Parse the content of this revision. Overrides {$p}page and {$p}pageid",
00760             'prop' => array(
00761                 'Which pieces of information to get',
00762                 ' text           - Gives the parsed text of the wikitext',
00763                 ' langlinks      - Gives the language links in the parsed wikitext',
00764                 ' categories     - Gives the categories in the parsed wikitext',
00765                 ' categorieshtml - Gives the HTML version of the categories',
00766                 ' languageshtml  - DEPRECATED. Will be removed in MediaWiki 1.24.',
00767                 '                  Gives the HTML version of the language links',
00768                 ' links          - Gives the internal links in the parsed wikitext',
00769                 ' templates      - Gives the templates in the parsed wikitext',
00770                 ' images         - Gives the images in the parsed wikitext',
00771                 ' externallinks  - Gives the external links in the parsed wikitext',
00772                 ' sections       - Gives the sections in the parsed wikitext',
00773                 ' revid          - Adds the revision ID of the parsed page',
00774                 ' displaytitle   - Adds the title of the parsed wikitext',
00775                 ' headitems      - Gives items to put in the <head> of the page',
00776                 ' headhtml       - Gives parsed <head> of the page',
00777                 ' iwlinks        - Gives interwiki links in the parsed wikitext',
00778                 ' wikitext       - Gives the original wikitext that was parsed',
00779                 ' properties     - Gives various properties defined in the parsed wikitext',
00780                 ' limitreportdata - Gives the limit report in a structured way.',
00781                 "                   Gives no data, when {$p}disablepp is set.",
00782                 ' limitreporthtml - Gives the HTML version of the limit report.',
00783                 "                   Gives no data, when {$p}disablepp is set.",
00784             ),
00785             'effectivelanglinks' => array(
00786                 'Includes language links supplied by extensions',
00787                 '(for use with prop=langlinks|languageshtml)',
00788             ),
00789             'pst' => array(
00790                 'Do a pre-save transform on the input before parsing it',
00791                 "Only valid when used with {$p}text",
00792             ),
00793             'onlypst' => array(
00794                 'Do a pre-save transform (PST) on the input, but don\'t parse it',
00795                 'Returns the same wikitext, after a PST has been applied.',
00796                 "Only valid when used with {$p}text",
00797             ),
00798             'uselang' => 'Which language to parse the request in',
00799             'section' => 'Only retrieve the content of this section number',
00800             'disablepp' => 'Disable the PP Report from the parser output',
00801             'generatexml' => "Generate XML parse tree (requires contentmodel=$wikitext)",
00802             'preview' => 'Parse in preview mode',
00803             'sectionpreview' => 'Parse in section preview mode (enables preview mode too)',
00804             'disabletoc' => 'Disable table of contents in output',
00805             'contentformat' => array(
00806                 'Content serialization format used for the input text',
00807                 "Only valid when used with {$p}text",
00808             ),
00809             'contentmodel' => array(
00810                 "Content model of the input text. If omitted, ${p}title must be specified, " .
00811                     "and default will be the model of the specified ${p}title",
00812                 "Only valid when used with {$p}text",
00813             ),
00814         );
00815     }
00816 
00817     public function getDescription() {
00818         $p = $this->getModulePrefix();
00819 
00820         return array(
00821             'Parses content and returns parser output.',
00822             'See the various prop-Modules of action=query to get information from the current' .
00823                 'version of a page.',
00824             'There are several ways to specify the text to parse:',
00825             "1) Specify a page or revision, using {$p}page, {$p}pageid, or {$p}oldid.",
00826             "2) Specify content explicitly, using {$p}text, {$p}title, and {$p}contentmodel.",
00827             "3) Specify only a summary to parse. {$p}prop should be given an empty value.",
00828         );
00829     }
00830 
00831     public function getPossibleErrors() {
00832         return array_merge( parent::getPossibleErrors(), array(
00833             array(
00834                 'code' => 'params',
00835                 'info' => 'The page parameter cannot be used together with the text and title parameters'
00836             ),
00837             array( 'code' => 'missingrev', 'info' => 'There is no revision ID oldid' ),
00838             array(
00839                 'code' => 'permissiondenied',
00840                 'info' => 'You don\'t have permission to view deleted revisions'
00841             ),
00842             array( 'code' => 'missingtitle', 'info' => 'The page you specified doesn\'t exist' ),
00843             array( 'code' => 'nosuchsection', 'info' => 'There is no section sectionnumber in page' ),
00844             array( 'nosuchpageid' ),
00845             array( 'invalidtitle', 'title' ),
00846             array( 'code' => 'parseerror', 'info' => 'Failed to parse the given text.' ),
00847             array(
00848                 'code' => 'notwikitext',
00849                 'info' => 'The requested operation is only supported on wikitext content.'
00850             ),
00851         ) );
00852     }
00853 
00854     public function getExamples() {
00855         return array(
00856             'api.php?action=parse&page=Project:Sandbox' => 'Parse a page',
00857             'api.php?action=parse&text={{Project:Sandbox}}&contentmodel=wikitext' => 'Parse wikitext',
00858             'api.php?action=parse&text={{PAGENAME}}&title=Test'
00859                 => 'Parse wikitext, specifying the page title',
00860             'api.php?action=parse&summary=Some+[[link]]&prop=' => 'Parse a summary',
00861         );
00862     }
00863 
00864     public function getHelpUrls() {
00865         return 'https://www.mediawiki.org/wiki/API:Parsing_wikitext#parse';
00866     }
00867 }