2019-07-19 20:46:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
|
|
|
|
|
|
|
class SpecialMathWikibase extends SpecialPage {
|
|
|
|
/**
|
|
|
|
* The parameter for this special page
|
|
|
|
*/
|
|
|
|
const PARAMETER = "qid";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var MathWikibaseConnector Wikibase connection
|
|
|
|
*/
|
|
|
|
private $wikibase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Psr\Log\LoggerInterface
|
|
|
|
*/
|
|
|
|
private $logger;
|
|
|
|
|
|
|
|
public function __construct() {
|
2020-03-17 07:14:13 +00:00
|
|
|
parent::__construct( 'MathWikibase' );
|
2019-07-19 20:46:20 +00:00
|
|
|
|
|
|
|
$this->logger = LoggerFactory::getInstance( 'Math' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function execute( $par ) {
|
2020-03-14 19:30:38 +00:00
|
|
|
global $wgLanguageCode;
|
2019-07-19 20:46:20 +00:00
|
|
|
|
|
|
|
if ( !$this->isWikibaseAvailable() ) {
|
|
|
|
$out = $this->getOutput();
|
|
|
|
|
|
|
|
$out->setPageTitle(
|
|
|
|
$this->getPlainText( 'math-wikibase-special-error-header' )
|
|
|
|
);
|
|
|
|
$out->addHTML(
|
2020-05-24 15:11:57 +00:00
|
|
|
wfMessage( 'math-wikibase-special-error-no-wikibase' )->inContentLanguage()->parse()
|
2019-07-19 20:46:20 +00:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !$this->wikibase ) {
|
|
|
|
$this->wikibase = new MathWikibaseConnector(
|
|
|
|
MathWikibaseConfig::getDefaultMathWikibaseConfig()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$request = $this->getRequest();
|
|
|
|
$output = $this->getOutput();
|
|
|
|
$output->enableOOUI();
|
|
|
|
|
|
|
|
$this->setHeaders();
|
|
|
|
$output->addModules( [ 'ext.math.wikibase.scripts' ] );
|
|
|
|
|
|
|
|
$output->setPageTitle(
|
|
|
|
$this->getPlainText( 'math-wikibase-header' )
|
|
|
|
);
|
|
|
|
|
|
|
|
// Get request
|
2020-03-17 07:14:13 +00:00
|
|
|
$requestId = $request->getText( self::PARAMETER, $par );
|
2019-07-19 20:46:20 +00:00
|
|
|
|
|
|
|
// if there is no id requested, show the request form
|
|
|
|
if ( !$requestId ) {
|
|
|
|
$this->showForm();
|
|
|
|
} else {
|
|
|
|
$this->logger->debug( "Request qID: " . $requestId );
|
|
|
|
try {
|
2020-03-14 19:30:38 +00:00
|
|
|
$info = $this->wikibase->fetchWikibaseFromId( $requestId, $wgLanguageCode );
|
2019-07-19 20:46:20 +00:00
|
|
|
$this->logger->debug( "Successfully fetched information for qID: " . $requestId );
|
|
|
|
self::buildPageRepresentation( $info, $requestId, $output );
|
|
|
|
} catch ( Exception $e ) {
|
|
|
|
$this->showError( $e );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows the form to request information for a specific Wikibase id
|
|
|
|
*/
|
|
|
|
private function showForm() {
|
|
|
|
$actionField = new \OOUI\ActionFieldLayout(
|
|
|
|
new \OOUI\TextInputWidget( [
|
|
|
|
'name' => self::PARAMETER,
|
|
|
|
'placeholder' => $this->getPlainText( 'math-wikibase-special-form-placeholder' ),
|
|
|
|
'required' => true,
|
|
|
|
'autocomplete' => false,
|
|
|
|
'classes' => [ 'mwe-math-wikibase-entityselector-input' ]
|
|
|
|
] ),
|
|
|
|
new OOUI\ButtonInputWidget( [
|
|
|
|
'name' => 'request-qid',
|
|
|
|
'label' => $this->getPlainText( 'math-wikibase-special-form-button' ),
|
|
|
|
'type' => 'submit',
|
|
|
|
'flags' => [ 'primary', 'progressive' ],
|
|
|
|
'icon' => 'check',
|
|
|
|
] ),
|
|
|
|
[
|
|
|
|
'label' => $this->getPlainText( 'math-wikibase-special-form-header' ),
|
|
|
|
'align' => 'top'
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$formLayout = new \OOUI\FormLayout( [
|
|
|
|
'method' => 'POST',
|
|
|
|
'items' => [ $actionField ]
|
|
|
|
] );
|
|
|
|
|
|
|
|
$this->getOutput()->addHTML( $formLayout );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows an error message for the user and writes information to $logger
|
|
|
|
* @param Exception $e can potentially be any exception.
|
|
|
|
*/
|
|
|
|
private function showError( Exception $e ) {
|
|
|
|
$this->getOutput()->setPageTitle(
|
|
|
|
$this->getPlainText( 'math-wikibase-special-error-header' )
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( $e instanceof InvalidArgumentException ) {
|
|
|
|
$this->logger->warning( "An invalid ID was specified. Reason: " . $e->getMessage() );
|
|
|
|
$this->getOutput()->addHTML(
|
2020-05-24 15:11:57 +00:00
|
|
|
wfMessage( 'math-wikibase-special-error-invalid-argument' )->inContentLanguage()->parse()
|
2019-07-19 20:46:20 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$this->logger->error( "An unknown error occured due fetching data from Wikibase.", [
|
|
|
|
'exception' => $e
|
|
|
|
] );
|
|
|
|
$this->getOutput()->addHTML(
|
2020-05-24 15:11:57 +00:00
|
|
|
wfMessage( 'math-wikibase-special-error-unknown' )->inContentLanguage()->parse()
|
2019-07-19 20:46:20 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to shorten i18n text processing
|
|
|
|
* @param string $key
|
|
|
|
* @return string the plain text in current content language
|
|
|
|
*/
|
|
|
|
private function getPlainText( $key ) {
|
|
|
|
return wfMessage( $key )->inContentLanguage()->plain();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param MathWikibaseInfo $info
|
|
|
|
* @param string $qid
|
|
|
|
* @param OutputPage $output
|
|
|
|
*/
|
|
|
|
public static function buildPageRepresentation(
|
|
|
|
MathWikibaseInfo $info,
|
|
|
|
$qid,
|
2020-03-17 07:14:13 +00:00
|
|
|
OutputPage $output
|
|
|
|
) {
|
2019-07-19 20:46:20 +00:00
|
|
|
$output->setPageTitle( $info->getLabel() );
|
|
|
|
|
|
|
|
// if 'instance of' is specified, it can be found in the description before a colon
|
2020-03-17 07:14:13 +00:00
|
|
|
// FIXME: There are other reasons to have a colon in an Item's description, e.g.
|
|
|
|
// https://www.wikidata.org/wiki/Special:MathWikibase?qid=Q6203
|
|
|
|
if ( preg_match( '/(.*):\s*(.*)/', $info->getDescription(), $matches ) ) {
|
2019-07-19 20:46:20 +00:00
|
|
|
$output->setSubtitle( $matches[1] );
|
|
|
|
}
|
|
|
|
|
|
|
|
// add formula information
|
2020-03-17 07:14:13 +00:00
|
|
|
$header = wfMessage( 'math-wikibase-formula-information' )->inContentLanguage()->plain();
|
2019-07-19 20:46:20 +00:00
|
|
|
$output->addHTML( self::createHTMLHeader( $header ) );
|
|
|
|
|
|
|
|
if ( $info->getSymbol() ) {
|
|
|
|
$math = $info->getFormattedSymbol();
|
|
|
|
$formulaInfo = new Message( 'math-wikibase-formula-header-format' );
|
|
|
|
$formulaInfo->rawParams(
|
|
|
|
wfMessage( 'math-wikibase-formula' )->inContentLanguage(),
|
|
|
|
$math
|
|
|
|
);
|
2020-05-24 15:11:57 +00:00
|
|
|
$output->addHTML( Html::rawElement( "p", [], $formulaInfo->inContentLanguage()->parse() ) );
|
2019-07-19 20:46:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$labelName = wfMessage(
|
|
|
|
'math-wikibase-formula-header-format',
|
|
|
|
wfMessage( 'math-wikibase-formula-name' )->inContentLanguage(),
|
|
|
|
$info->getLabel()
|
2020-05-24 15:11:57 +00:00
|
|
|
)->inContentLanguage()->parse();
|
2019-11-15 22:03:48 +00:00
|
|
|
$output->addHTML( Html::rawElement( "p", [], $labelName ) );
|
2019-07-19 20:46:20 +00:00
|
|
|
|
2020-03-17 07:14:13 +00:00
|
|
|
if ( isset( $matches[2] ) ) {
|
2019-07-19 20:46:20 +00:00
|
|
|
$labelType = wfMessage(
|
|
|
|
'math-wikibase-formula-header-format',
|
|
|
|
wfMessage( 'math-wikibase-formula-type' )->inContentLanguage(),
|
|
|
|
$matches[1]
|
2020-05-24 15:11:57 +00:00
|
|
|
)->inContentLanguage()->parse();
|
2019-11-15 22:03:48 +00:00
|
|
|
$output->addHTML( Html::rawElement( "p", [], $labelType ) );
|
2020-03-17 07:14:13 +00:00
|
|
|
|
|
|
|
$description = $matches[2];
|
2019-07-19 20:46:20 +00:00
|
|
|
} else {
|
2020-03-17 07:14:13 +00:00
|
|
|
$description = $info->getDescription();
|
2019-07-19 20:46:20 +00:00
|
|
|
}
|
2020-03-17 07:14:13 +00:00
|
|
|
$labelDesc = wfMessage(
|
|
|
|
'math-wikibase-formula-header-format',
|
|
|
|
wfMessage( 'math-wikibase-formula-description' )->inContentLanguage(),
|
|
|
|
$description
|
2020-05-24 15:11:57 +00:00
|
|
|
)->inContentLanguage()->parse();
|
2020-03-17 07:14:13 +00:00
|
|
|
$output->addHTML( Html::rawElement( "p", [], $labelDesc ) );
|
2019-07-19 20:46:20 +00:00
|
|
|
|
|
|
|
// add parts of formula
|
|
|
|
if ( $info->hasParts() ) {
|
|
|
|
$elementsHeader = wfMessage( 'math-wikibase-formula-elements-header' )
|
2020-03-17 07:14:13 +00:00
|
|
|
->inContentLanguage()->plain();
|
2019-07-19 20:46:20 +00:00
|
|
|
$output->addHTML( self::createHTMLHeader( $elementsHeader ) );
|
|
|
|
$output->addHTML( $info->generateTableOfParts() );
|
|
|
|
}
|
|
|
|
|
|
|
|
// add link information
|
|
|
|
$wikibaseHeader = wfMessage(
|
|
|
|
'math-wikibase-formula-link-header',
|
|
|
|
$info->getDescription()
|
2020-03-17 07:14:13 +00:00
|
|
|
)->inContentLanguage()->plain();
|
2019-07-19 20:46:20 +00:00
|
|
|
|
|
|
|
$output->addHTML( self::createHTMLHeader( $wikibaseHeader ) );
|
|
|
|
|
|
|
|
$url = MathWikibaseConnector::buildURL( $qid );
|
2019-11-15 22:03:48 +00:00
|
|
|
$link = Html::linkButton( $url, [ "href" => $url ] );
|
|
|
|
$output->addHTML( Html::rawElement( "p", [], $link ) );
|
2019-07-19 20:46:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-03-17 07:14:13 +00:00
|
|
|
* @param string $header Plain text
|
|
|
|
* @return string Raw HTML
|
2019-07-19 20:46:20 +00:00
|
|
|
*/
|
2020-03-17 07:14:13 +00:00
|
|
|
private static function createHTMLHeader( string $header ) : string {
|
|
|
|
return Html::rawElement(
|
|
|
|
'h2',
|
|
|
|
[],
|
|
|
|
Html::element( 'span', [ 'class' => 'mw-headline' ], $header )
|
|
|
|
);
|
2019-07-19 20:46:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether Wikibase is available or not
|
2019-12-28 10:29:06 +00:00
|
|
|
* @return bool
|
2019-07-19 20:46:20 +00:00
|
|
|
*/
|
|
|
|
public static function isWikibaseAvailable() {
|
2019-11-15 16:22:36 +00:00
|
|
|
return class_exists( '\Wikibase\Client\WikibaseClient' );
|
2019-07-19 20:46:20 +00:00
|
|
|
}
|
|
|
|
}
|