mediawiki-extensions-Math/src/ApiMathWikibaseExtracts.php
Moritz Schubotz (physikerwelt) 4a7bc3ee31 Add special page and API endpoint that show information from math Wikibase items
Add a special page and an API endpoint to fetch data from Wikibase
items with a given qID. The special page summarizes information from
Wikibase. The API endpoint allows to request the information
directly. Both, the API endpoint and the special page, fetch the data
from a new helper class for consistency.

Bug: T208758
Bug: T229939
Change-Id: Idd22057a88312bf1a1cb5546d0a6edca5678d80d
2019-11-14 23:42:35 +09:00

155 行
3.8 KiB
PHP

<?php
/**
* API extension to fetch data from mathematical Wikibase items
*/
class ApiMathWikibaseExtracts extends ApiQueryBase {
/**
* @var string the prefix of the parameter names of this API
*/
const PREFIX = "math";
/**
* @var MathWikibaseConnector
*/
private $wikibase;
/**
* @param ApiQuery $query
* @param string $moduleName
*/
public function __construct( ApiQuery $query, $moduleName ) {
parent::__construct( $query, $moduleName, self::PREFIX );
$this->wikibase = new MathWikibaseConnector(
MathWikibaseConfig::getDefaultMathWikibaseConfig()
);
}
/**
* @throws ApiUsageException
*/
public function execute() {
$result = $this->getResult();
$params = $this->extractRequestParams();
$qid = $params['qid'];
$lang = $params['uselang'] ?: 'en';
try {
$info = $this->wikibase->fetchWikibaseFromId( $qid, $lang );
$result->addValue(
[ 'query', 'mathwbextracts' ],
$qid,
$this->buildResponse( $qid, $lang, $info )
);
} catch ( MWException $e ) {
// impossible to fetch the data. Keep the answer empty
$result->addValue(
[ 'query', 'mathwbextracts' ],
$qid,
[]
);
}
}
/**
* @param string $qid
* @param string $lang language code
* @param MathWikibaseInfo $info information from MathWikibaseConnector
* @return array response
*/
private function buildResponse( $qid, $lang, $info ) {
$specialPageTitle = Title::newFromText( "Special:MathWikibase" );
$url = $specialPageTitle->getLocalURL( [
'qid' => $qid
] );
try {
$langObj = Language::factory( $lang );
} catch ( MWException $e ) {
throw new InvalidArgumentException( "Invalid language code." );
}
return [
"title" => $info->getLabel(),
"extract" => self::buildHTMLRepresentation( $info ),
"canonicalurl" => $url,
"fullurl" => $url,
"contentmodel" => "html",
"pagelanguage" => $langObj->getCode(),
"pagelanguagedir" => $langObj->getDir(),
"pagelanguagehtmlcode" => $langObj->getHtmlCode()
];
}
/**
* Generates an HTML string from the given data.
* @param MathWikibaseInfo $info an info object generated by fetchWikibaseFromId
* @return string an HTML representation of the given info object
*/
private function buildHTMLRepresentation( $info ) {
$output = Html::openElement(
"div",
[ "style" => "display: flex; flex-direction: row; align-items: flex-start; flow-wrap: wrap;" ]
);
if ( $info->hasParts() ) {
$output .= Html::openElement( "div", [ "style" => "width: 70%;" ] );
} else {
$output .= Html::openElement( "div", [ "style" => "width: 100%;" ] );
}
$output .= Html::element( "span", [ "style" => "font-weight: bold;" ], $info->getLabel() );
$output .= Html::element( "span", [], " (" . $info->getDescription() . ")" );
$output .= Html::closeElement( "div" );
if ( $info->hasParts() ) {
$output .= Html::openElement(
"div",
[ "style" => "width: 30%; display: flex; justify-content: center;" ]
);
$output .= $info->generateSmallTableOfParts();
$output .= Html::closeElement( "div" );
}
$output .= Html::closeElement( "div" );
return $output;
}
/**
* Defines the parameters for this API endpoint
* @return array
*/
public function getAllowedParams() {
return [
'qid' => [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_REQUIRED => true
],
'uselang' => [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_REQUIRED => false
]
];
}
/**
* @see ApiBase::getExamplesMessages()
* @return array
*/
protected function getExamplesMessages() {
return [
'action=query&prop=mathwbextracts&mathqid=Q35875&mathuselang=en'
=> 'apihelp-query+mathwbextracts-example-1',
];
}
/**
* @see ApiBase::getHelpUrls()
* @return string
*/
public function getHelpUrls() {
return 'https://www.mediawiki.org/wiki/Extension:Math#API';
}
}