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 ) {
|
2018-09-08 08:09:03 +00:00
|
|
|
$this->addWarning( 'apiwarn-infobox-invalidargs' );
|
2015-05-29 13:04:04 +00:00
|
|
|
}
|
2015-11-12 02:16:01 +00:00
|
|
|
|
|
|
|
global $wgParser;
|
|
|
|
$wgParser->firstCallInit();
|
2018-10-02 07:41:19 +00:00
|
|
|
$wgParser->startExternalParse(
|
|
|
|
Title::newFromText( $title ),
|
|
|
|
ParserOptions::newFromContext( $this->getContext() ),
|
|
|
|
Parser::OT_HTML,
|
|
|
|
true
|
|
|
|
);
|
2015-11-12 02:16:01 +00:00
|
|
|
|
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 ] ] );
|
2018-09-08 08:09:03 +00:00
|
|
|
} catch ( \PortableInfobox\Parser\Nodes\UnimplementedNodeException $e ) {
|
2018-10-02 07:41:19 +00:00
|
|
|
$this->dieUsage(
|
|
|
|
wfMessage( 'portable-infobox-unimplemented-infobox-tag', [ $e->getMessage() ] )->escaped(),
|
|
|
|
'notimplemented'
|
|
|
|
);
|
2018-09-08 08:09:03 +00:00
|
|
|
} catch ( \PortableInfobox\Parser\XmlMarkupParseErrorException $e ) {
|
|
|
|
$this->dieUsage( wfMessage( 'portable-infobox-xml-parse-error' )->text(), 'badxml' );
|
|
|
|
} catch ( \PortableInfobox\Helpers\InvalidInfoboxParamsException $e ) {
|
2018-10-02 07:41:19 +00:00
|
|
|
$this->dieUsage(
|
|
|
|
wfMessage(
|
|
|
|
'portable-infobox-xml-parse-error-infobox-tag-attribute-unsupported',
|
|
|
|
[ $e->getMessage() ]
|
|
|
|
)->escaped(),
|
|
|
|
'invalidparams'
|
|
|
|
);
|
2015-05-29 13:04:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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',
|
2018-10-02 07:41:19 +00:00
|
|
|
'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
|
|
|
}
|