PortableInfobox/includes/controllers/ApiPortableInfobox.php

92 lines
2.4 KiB
PHP
Raw Normal View History

2015-05-29 13:04:04 +00:00
<?php
2021-09-10 02:52:19 +00:00
use MediaWiki\MediaWikiServices;
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 ) {
$this->addWarning( 'apiwarn-infobox-invalidargs' );
2015-05-29 13:04:04 +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
);
if ( is_array( $arguments ) ) {
foreach ( $arguments as $key => &$value ) {
2021-06-03 23:38:46 +00:00
$value = $parser->replaceVariables( $value );
}
}
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 ] ] );
} 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'
);
} catch ( \PortableInfobox\Parser\XmlMarkupParseErrorException $e ) {
2021-09-10 02:52:19 +00:00
$this->dieWithError( $this->msg( 'portable-infobox-xml-parse-error' )->text(), 'badxml' );
} 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' => [
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;
}
}