2015-05-29 13:04:04 +00:00
|
|
|
<?php
|
|
|
|
|
2021-09-10 02:52:19 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2021-10-13 19:58:43 +00:00
|
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
2021-09-10 02:52:19 +00:00
|
|
|
|
2015-05-29 13:04:04 +00:00
|
|
|
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
|
|
|
|
2021-06-03 23:38:46 +00:00
|
|
|
$parser = MediaWikiServices::getInstance()->getParser();
|
|
|
|
$parser->startExternalParse(
|
2018-10-02 07:41:19 +00:00
|
|
|
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 ) {
|
2021-06-03 23:38:46 +00:00
|
|
|
$value = $parser->replaceVariables( $value );
|
2015-11-14 00:05:41 +00:00
|
|
|
}
|
2015-11-12 02:16:01 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 23:38:46 +00:00
|
|
|
$frame = $parser->getPreprocessor()->newCustomFrame( is_array( $arguments ) ? $arguments : [] );
|
2015-05-29 13:04:04 +00:00
|
|
|
|
|
|
|
try {
|
2021-06-03 23:38:46 +00:00
|
|
|
$output = PortableInfoboxParserTagController::getInstance()->render( $text, $parser, $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 ) {
|
2021-09-10 02:52:19 +00:00
|
|
|
$this->dieWithError(
|
|
|
|
$this->msg( 'portable-infobox-unimplemented-infobox-tag', [ $e->getMessage() ] )->escaped(),
|
2018-10-02 07:41:19 +00:00
|
|
|
'notimplemented'
|
|
|
|
);
|
2018-09-08 08:09:03 +00:00
|
|
|
} catch ( \PortableInfobox\Parser\XmlMarkupParseErrorException $e ) {
|
2021-09-10 02:52:19 +00:00
|
|
|
$this->dieWithError( $this->msg( 'portable-infobox-xml-parse-error' )->text(), 'badxml' );
|
2018-09-08 08:09:03 +00:00
|
|
|
} catch ( \PortableInfobox\Helpers\InvalidInfoboxParamsException $e ) {
|
2021-09-10 02:52:19 +00:00
|
|
|
$this->dieWithError(
|
|
|
|
$this->msg(
|
2018-10-02 07:41:19 +00:00
|
|
|
'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' => [
|
2021-10-13 19:58:43 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'string'
|
2018-08-16 09:25:53 +00:00
|
|
|
],
|
|
|
|
'title' => [
|
2021-10-13 19:58:43 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'string'
|
2018-08-16 09:25:53 +00:00
|
|
|
],
|
|
|
|
'args' => [
|
2021-10-13 19:58:43 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'string'
|
2018-08-16 09:25:53 +00:00
|
|
|
]
|
|
|
|
];
|
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
|
|
|
}
|