2019-07-19 20:46:20 +00:00
|
|
|
<?php
|
|
|
|
|
2021-04-07 22:22:05 +00:00
|
|
|
namespace MediaWiki\Extension\Math;
|
|
|
|
|
|
|
|
use Exception;
|
2021-06-21 11:32:26 +00:00
|
|
|
use ExtensionRegistry;
|
2021-04-07 22:22:05 +00:00
|
|
|
use Html;
|
|
|
|
use InvalidArgumentException;
|
2020-11-02 12:38:51 +00:00
|
|
|
use MediaWiki\Extension\Math\Widget\WikibaseEntitySelector;
|
2019-07-19 20:46:20 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2021-04-07 22:22:05 +00:00
|
|
|
use Message;
|
|
|
|
use OOUI\ButtonInputWidget;
|
|
|
|
use OOUI\FormLayout;
|
|
|
|
use OutputPage;
|
|
|
|
use SpecialPage;
|
2019-07-19 20:46:20 +00:00
|
|
|
|
|
|
|
class SpecialMathWikibase extends SpecialPage {
|
|
|
|
/**
|
|
|
|
* The parameter for this special page
|
|
|
|
*/
|
2020-05-30 01:13:31 +00:00
|
|
|
private const PARAMETER = "qid";
|
2019-07-19 20:46:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
2021-04-07 22:22:05 +00:00
|
|
|
if ( !self::isWikibaseAvailable() ) {
|
2019-07-19 20:46:20 +00:00
|
|
|
$out = $this->getOutput();
|
|
|
|
|
|
|
|
$out->setPageTitle(
|
|
|
|
$this->getPlainText( 'math-wikibase-special-error-header' )
|
|
|
|
);
|
|
|
|
$out->addHTML(
|
2020-12-19 19:07:50 +00:00
|
|
|
$this->msg( '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();
|
2020-11-02 12:38:51 +00:00
|
|
|
$output->addModules( [ 'mw.widgets.MathWbEntitySelector' ] );
|
2019-07-19 20:46:20 +00:00
|
|
|
|
|
|
|
$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(
|
2020-11-02 12:38:51 +00:00
|
|
|
new WikibaseEntitySelector( [
|
2019-07-19 20:46:20 +00:00
|
|
|
'name' => self::PARAMETER,
|
|
|
|
'placeholder' => $this->getPlainText( 'math-wikibase-special-form-placeholder' ),
|
|
|
|
'required' => true,
|
2020-11-02 12:38:51 +00:00
|
|
|
'infusable' => true,
|
|
|
|
'id' => 'wbEntitySelector'
|
2019-07-19 20:46:20 +00:00
|
|
|
] ),
|
2021-04-07 22:22:05 +00:00
|
|
|
new ButtonInputWidget( [
|
2019-07-19 20:46:20 +00:00
|
|
|
'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'
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2021-04-07 22:22:05 +00:00
|
|
|
$formLayout = new FormLayout( [
|
2019-07-19 20:46:20 +00:00
|
|
|
'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-12-19 19:07:50 +00:00
|
|
|
$this->msg( 'math-wikibase-special-error-invalid-argument' )->inContentLanguage()->parse()
|
2019-07-19 20:46:20 +00:00
|
|
|
);
|
|
|
|
} else {
|
2020-09-10 16:46:41 +00:00
|
|
|
$this->logger->error( "An unknown error occurred while fetching data from Wikibase.", [
|
2019-07-19 20:46:20 +00:00
|
|
|
'exception' => $e
|
|
|
|
] );
|
|
|
|
$this->getOutput()->addHTML(
|
2020-12-19 19:07:50 +00:00
|
|
|
$this->msg( '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 ) {
|
2020-12-19 19:07:50 +00:00
|
|
|
return $this->msg( $key )->inContentLanguage()->plain();
|
2019-07-19 20:46:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
*/
|
2021-07-22 11:56:28 +00:00
|
|
|
private static function createHTMLHeader( string $header ): string {
|
2020-03-17 07:14:13 +00:00
|
|
|
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() {
|
2021-06-21 11:32:26 +00:00
|
|
|
return ExtensionRegistry::getInstance()->isLoaded( 'WikibaseClient' );
|
2019-07-19 20:46:20 +00:00
|
|
|
}
|
|
|
|
}
|