2015-05-29 13:04:04 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class ApiPortableInfobox extends ApiBase {
|
|
|
|
|
|
|
|
public function __construct( $main, $action ) {
|
2018-08-16 09:25:53 +00:00
|
|
|
parent::__construct( $main, $action );
|
2015-05-29 13:04:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
$text = $this->getParameter( "text" );
|
|
|
|
$title = $this->getParameter( "title" );
|
|
|
|
$arguments = $this->getFrameArguments();
|
|
|
|
if ( $arguments === null ) {
|
2016-02-04 15:52:51 +00:00
|
|
|
$this->getResult()->setWarning( "Args param format is incorrect" );
|
2015-05-29 13:04:04 +00:00
|
|
|
}
|
2015-11-12 02:16:01 +00:00
|
|
|
|
|
|
|
/*
|
2015-11-12 21:23:38 +00:00
|
|
|
This commented block is an another/alternative approach to the same problem. Potentially
|
|
|
|
it is better because it parses wikitext in a way identical to how it is parser for article
|
|
|
|
view - however it is not able to throw exception if the passed text is not a valid infobox.
|
|
|
|
I'm keeping it here because I think it is a great approach and in the future I'm planning
|
|
|
|
to create an separated API that uses this approach. - Inez Korczyński
|
|
|
|
|
2015-11-12 02:16:01 +00:00
|
|
|
$fakeTemplate = 'FakeTemplate' . wfRandomString( 4 );
|
|
|
|
$fakeTemplateTitle = Title::newFromText( $fakeTemplate, NS_TEMPLATE );
|
|
|
|
|
|
|
|
$parametersWT = '';
|
|
|
|
foreach( $arguments as $key => $value ) {
|
|
|
|
$parametersWT .= '|' . $key . '=' . $value;
|
|
|
|
}
|
|
|
|
$callWT = '{{' . $fakeTemplate . $parametersWT . '}}';
|
|
|
|
|
|
|
|
global $wgParser;
|
|
|
|
$popts = ParserOptions::newFromContext( $this->getContext() );
|
|
|
|
$popts->setTemplateCallback( function ( $title, $parser = false ) use ( $fakeTemplateTitle, $text ) {
|
|
|
|
if ( $title->equals( $fakeTemplateTitle ) ) {
|
|
|
|
return array( 'text' => $text );
|
|
|
|
} else {
|
|
|
|
return Parser::statelessFetchTemplate( $title, $parser );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
$output = $wgParser->parse( $callWT, Title::newFromText( $title ), $popts )->getText();
|
|
|
|
$this->getResult()->addValue( null, $this->getModuleName(), [ 'text' => [ '*' => $output ] ] );
|
|
|
|
*/
|
|
|
|
|
|
|
|
global $wgParser;
|
|
|
|
$wgParser->firstCallInit();
|
|
|
|
$wgParser->startExternalParse( Title::newFromText( $title ), ParserOptions::newFromContext( $this->getContext() ), Parser::OT_HTML, true );
|
|
|
|
|
2015-11-14 00:05:41 +00:00
|
|
|
if ( is_array( $arguments ) ) {
|
2016-02-04 15:52:51 +00:00
|
|
|
foreach ( $arguments as $key => &$value ) {
|
|
|
|
$value = $wgParser->replaceVariables( $value );
|
2015-11-14 00:05:41 +00:00
|
|
|
}
|
2015-11-12 02:16:01 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 09:25:53 +00:00
|
|
|
$frame = $wgParser->getPreprocessor()->newCustomFrame( is_array( $arguments ) ? $arguments : [] );
|
2015-05-29 13:04:04 +00:00
|
|
|
|
|
|
|
try {
|
2015-11-12 02:16:01 +00:00
|
|
|
$output = PortableInfoboxParserTagController::getInstance()->render( $text, $wgParser, $frame );
|
2015-05-29 13:04:04 +00:00
|
|
|
$this->getResult()->addValue( null, $this->getModuleName(), [ 'text' => [ '*' => $output ] ] );
|
|
|
|
} catch ( \Wikia\PortableInfobox\Parser\Nodes\UnimplementedNodeException $e ) {
|
|
|
|
$this->dieUsage( wfMessage( 'unimplemented-infobox-tag', [ $e->getMessage() ] )->escaped(), "notimplemented" );
|
|
|
|
} catch ( \Wikia\PortableInfobox\Parser\XmlMarkupParseErrorException $e ) {
|
|
|
|
$this->dieUsage( wfMessage( 'xml-parse-error' )->text(), "badxml" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAllowedParams() {
|
2018-08-16 09:25:53 +00:00
|
|
|
return [
|
|
|
|
'text' => [
|
|
|
|
ApiBase::PARAM_TYPE => 'string'
|
|
|
|
],
|
|
|
|
'title' => [
|
|
|
|
ApiBase::PARAM_TYPE => 'string'
|
|
|
|
],
|
|
|
|
'args' => [
|
|
|
|
ApiBase::PARAM_TYPE => 'string'
|
|
|
|
]
|
|
|
|
];
|
2015-05-29 13:04:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getParamDescription() {
|
2018-08-16 09:25:53 +00:00
|
|
|
return [
|
2015-05-29 13:04:04 +00:00
|
|
|
'text' => 'Infobox to parse (xml string)',
|
|
|
|
'title' => 'Title of page the text belongs to',
|
|
|
|
'args' => 'Variable list to use during parse (json format)',
|
2018-08-16 09:25:53 +00:00
|
|
|
];
|
2015-05-29 13:04:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getDescription() {
|
2018-08-16 09:25:53 +00:00
|
|
|
return [ 'This module provides infobox parser' ];
|
2015-05-29 13:04:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Examples
|
|
|
|
*/
|
|
|
|
public function getExamples() {
|
2018-08-16 09:25:53 +00:00
|
|
|
return [
|
2015-05-29 13:04:04 +00:00
|
|
|
'api.php?action=infobox',
|
|
|
|
'api.php?action=infobox&text=<infobox><data><default>{{PAGENAME}}</default></data></infobox>&title=Test',
|
|
|
|
'api.php?action=infobox&text=<infobox><data source="test" /></infobox>&args={"test": "test value"}',
|
2018-08-16 09:25:53 +00:00
|
|
|
];
|
2015-05-29 13:04:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function getFrameArguments() {
|
|
|
|
$arguments = $this->getParameter( "args" );
|
|
|
|
return isset( $arguments ) ? json_decode( $arguments, true ) : false;
|
|
|
|
}
|
|
|
|
|
2015-11-14 00:05:41 +00:00
|
|
|
}
|