- if(!is_null($page) && (!is_null($text) || $title != "API"))
- $this->dieUsage("The page parameter cannot be used together with the text and title parameters", 'params');
- $prop = array_flip($params['prop']);
-
- global $wgParser, $wgUser;
- if(!is_null($page)) {
- $titleObj = Title::newFromText($page);
- if(!$titleObj)
- $this->dieUsage("The page you specified doesn't exist", 'missingtitle');
-
- // Try the parser cache first
- $articleObj = new Article($titleObj);
- $pcache =& ParserCache::singleton();
- $p_result = $pcache->get($articleObj, $wgUser);
- if(!$p_result) {
- $p_result = $wgParser->parse($articleObj->getContent(), $titleObj, new ParserOptions());
- global $wgUseParserCache;
- if($wgUseParserCache)
- $pcache->save($p_result, $articleObj, $wgUser);
+ $pageid = $params['pageid'];
+ $oldid = $params['oldid'];
+
+ if ( !is_null( $page ) && ( !is_null( $text ) || $title != 'API' ) ) {
+ $this->dieUsage( 'The page parameter cannot be used together with the text and title parameters', 'params' );
+ }
+ $prop = array_flip( $params['prop'] );
+
+ if ( isset( $params['section'] ) ) {
+ $this->section = $params['section'];
+ } else {
+ $this->section = false;
+ }
+
+ // The parser needs $wgTitle to be set, apparently the
+ // $title parameter in Parser::parse isn't enough *sigh*
+ global $wgParser, $wgUser, $wgTitle, $wgEnableParserCache, $wgLang;
+
+ // Currently unnecessary, code to act as a safeguard against any change in current behaviour of uselang breaks
+ $oldLang = null;
+ if ( isset( $params['uselang'] ) && $params['uselang'] != $wgLang->getCode() ) {
+ $oldLang = $wgLang; // Backup wgLang
+ $wgLang = Language::factory( $params['uselang'] );
+ }
+
+ $popts = new ParserOptions();
+ $popts->setTidy( true );
+ $popts->enableLimitReport();
+ $redirValues = null;
+ if ( !is_null( $oldid ) || !is_null( $pageid ) || !is_null( $page ) ) {
+ if ( !is_null( $oldid ) ) {
+ // Don't use the parser cache
+ $rev = Revision::newFromID( $oldid );
+ if ( !$rev ) {
+ $this->dieUsage( "There is no revision ID $oldid", 'missingrev' );
+ }
+ if ( !$rev->userCan( Revision::DELETED_TEXT ) ) {
+ $this->dieUsage( "You don't have permission to view deleted revisions", 'permissiondenied' );
+ }
+
+ $text = $rev->getText( Revision::FOR_THIS_USER );
+ $titleObj = $rev->getTitle();
+ $wgTitle = $titleObj;
+
+ if ( $this->section !== false ) {
+ $text = $this->getSectionText( $text, 'r' . $rev );
+ }
+
+ $p_result = $wgParser->parse( $text, $titleObj, $popts );
+ } else {
+ if ( !is_null ( $pageid ) ) {
+ $titleObj = Title::newFromID( $pageid );
+
+ if ( !$titleObj ) {
+ $this->dieUsageMsg( array( 'nosuchpageid', $pageid ) );
+ }
+ } else {
+ if ( $params['redirects'] ) {
+ $req = new FauxRequest( array(
+ 'action' => 'query',
+ 'redirects' => '',
+ 'titles' => $page
+ ) );
+ $main = new ApiMain( $req );
+ $main->execute();
+ $data = $main->getResultData();
+ $redirValues = @$data['query']['redirects'];
+ $to = $page;
+ foreach ( (array)$redirValues as $r ) {
+ $to = $r['to'];
+ }
+ } else {
+ $to = $page;
+ }
+ $titleObj = Title::newFromText( $to );
+ if ( !$titleObj ) {
+ $this->dieUsage( "The page you specified doesn't exist", 'missingtitle' );
+ }
+ }
+ $wgTitle = $titleObj;
+
+ $articleObj = new Article( $titleObj );
+ if ( isset( $prop['revid'] ) ) {
+ $oldid = $articleObj->getRevIdFetched();
+ }
+
+ if ( $this->section !== false ) {
+ $text = $this->getSectionText( $text, !is_null ( $pageid ) ? 'page id ' . $pageid : $titleObj->getText() );
+ $p_result = $wgParser->parse( $text, $titleObj, $popts );
+ } else {
+ // Try the parser cache first
+ $p_result = false;
+ $pcache = ParserCache::singleton();
+ if ( $wgEnableParserCache ) {
+ $p_result = $pcache->get( $articleObj, $popts );
+ }
+ if ( !$p_result ) {
+ $p_result = $wgParser->parse( $articleObj->getContent(), $titleObj, $popts );
+
+ if ( $wgEnableParserCache ) {
+ $pcache->save( $p_result, $articleObj, $popts );
+ }
+ }
+ }