*/
class ApiParse extends ApiBase {
- private $section;
+ private $section, $text, $pstText = null;
public function __construct( $main, $action ) {
parent::__construct( $main, $action );
if ( $titleObj->getLatestRevID() === intval( $oldid ) ) {
$articleObj = new Article( $titleObj, 0 );
- $p_result = $this->getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageid ) ;
+ $p_result = $this->getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageid,
+ isset( $prop['wikitext'] ) ) ;
} else { // This is an old revision, so get the text differently
- $text = $rev->getText( Revision::FOR_THIS_USER );
+ $this->text = $rev->getText( Revision::FOR_THIS_USER );
$wgTitle = $titleObj;
if ( $this->section !== false ) {
- $text = $this->getSectionText( $text, 'r' . $rev->getId() );
+ $this->text = $this->getSectionText( $this->text, 'r' . $rev->getId() );
}
- $p_result = $wgParser->parse( $text, $titleObj, $popts );
+ $p_result = $wgParser->parse( $this->text, $titleObj, $popts );
}
} else { // Not $oldid
$oldid = $articleObj->getRevIdFetched();
}
- $p_result = $this->getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageid ) ;
+ $p_result = $this->getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageid,
+ isset( $prop['wikitext'] ) ) ;
}
} else { // Not $oldid, $pageid, $page. Hence based on $text
+ $this->text = $text;
$titleObj = Title::newFromText( $title );
if ( !$titleObj ) {
$titleObj = Title::newFromText( 'API' );
$wgTitle = $titleObj;
if ( $this->section !== false ) {
- $text = $this->getSectionText( $text, $titleObj->getText() );
+ $this->text = $this->getSectionText( $this->text, $titleObj->getText() );
}
if ( $params['pst'] || $params['onlypst'] ) {
- $text = $wgParser->preSaveTransform( $text, $titleObj, $wgUser, $popts );
+ $this->pstText = $wgParser->preSaveTransform( $this->text, $titleObj, $wgUser, $popts );
}
if ( $params['onlypst'] ) {
// Build a result and bail out
$result_array['text'] = array();
- $this->getResult()->setContent( $result_array['text'], $text );
+ $this->getResult()->setContent( $result_array['text'], $this->pstText );
+ if ( isset( $prop['wikitext'] ) ) {
+ $result_array['wikitext'] = array();
+ $this->getResult()->setContent( $result_array['wikitext'], $this->text );
+ }
$this->getResult()->addValue( null, $this->getModuleName(), $result_array );
return;
}
- $p_result = $wgParser->parse( $text, $titleObj, $popts );
+ $p_result = $wgParser->parse( $this->text, $titleObj, $popts );
}
// Return result
if ( isset( $prop['iwlinks'] ) ) {
$result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
}
+
+ if ( isset( $prop['wikitext'] ) ) {
+ $result_array['wikitext'] = array();
+ $result->setContent( $result_array['wikitext'], $this->text );
+ if ( !is_null( $this->pstText ) ) {
+ $result_array['psttext'] = array();
+ $result->setContent( $result_array['psttext'], $this->pstText );
+ }
+ }
$result_mapping = array(
'redirects' => 'r',
* @param $titleObj Title
* @param $popts ParserOptions
* @param $pageId Int
+ * @param $getWikitext Bool
* @return ParserOutput
*/
- private function getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageId = null ) {
+ private function getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageId = null, $getWikitext = false ) {
if ( $this->section !== false ) {
global $wgParser;
- $text = $this->getSectionText( $articleObj->getRawText(), !is_null ( $pageId )
+ $this->text = $this->getSectionText( $articleObj->getRawText(), !is_null ( $pageId )
? 'page id ' . $pageId : $titleObj->getText() );
- return $wgParser->parse( $text, $titleObj, $popts );
+ return $wgParser->parse( $this->text, $titleObj, $popts );
} else {
// Try the parser cache first
- return $articleObj->getParserOutput();
+ $pout = $articleObj->getParserOutput();
+ if ( $getWikitext ) {
+ $rev = Revision::newFromTitle( $titleObj );
+ if ( $rev ) {
+ $this->text = $rev->getText();
+ }
+ }
+ return $pout;
}
}
'headitems',
'headhtml',
'iwlinks',
+ 'wikitext',
)
),
'pst' => false,
' headitems - Gives items to put in the <head> of the page',
' headhtml - Gives parsed <head> of the page',
' iwlinks - Gives interwiki links in the parsed wikitext',
+ ' wikitext - Gives the original wikitext that was parsed',
'NOTE: Section tree is only generated if there are more than 4 sections, or if the __TOC__ keyword is present'
),
'pst' => array(