PortableInfobox/includes/controllers/ApiPortableInfobox.php

96 lines
2.6 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;
2022-03-11 20:35:51 +00:00
use PortableInfobox\Helpers\InvalidInfoboxParamsException;
use PortableInfobox\Parser\Nodes\UnimplementedNodeException;
use PortableInfobox\Parser\XmlMarkupParseErrorException;
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 ) {
$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 ] ] );
2022-03-11 20:35:51 +00:00
} catch ( 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'
);
2022-03-11 20:35:51 +00:00
} catch ( XmlMarkupParseErrorException $e ) {
2021-09-10 02:52:19 +00:00
$this->dieWithError( $this->msg( 'portable-infobox-xml-parse-error' )->text(), 'badxml' );
2022-03-11 20:35:51 +00:00
} catch ( 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' => [
ParamValidator::PARAM_TYPE => 'string'
2018-08-16 09:25:53 +00:00
],
'title' => [
ParamValidator::PARAM_TYPE => 'string'
2018-08-16 09:25:53 +00:00
],
'args' => [
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;
}
}