PortableInfobox/includes/controllers/ApiPortableInfobox.php

91 lines
2.4 KiB
PHP
Raw Normal View History

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 ) {
$this->addWarning( 'apiwarn-infobox-invalidargs' );
2015-05-29 13:04:04 +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
);
if ( is_array( $arguments ) ) {
foreach ( $arguments as $key => &$value ) {
$value = $wgParser->replaceVariables( $value );
}
}
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 {
$output = PortableInfoboxParserTagController::getInstance()->render( $text, $wgParser, $frame );
2015-05-29 13:04:04 +00:00
$this->getResult()->addValue( null, $this->getModuleName(), [ 'text' => [ '*' => $output ] ] );
} 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'
);
} 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;
}
}