Link Wikipedia Articles from Specialpage Math Formula Information

Linking math formula on the special page to their own page, if this
a page exists

Bug: T239099
Change-Id: If7da0990a4fc1f8d8a70237dc19c7b2e67ec38cd
This commit is contained in:
AndreG-P 2019-11-27 02:24:58 +09:00
parent a10719db44
commit 912866b976
3 changed files with 129 additions and 24 deletions

View file

@ -1,5 +1,6 @@
<?php
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MediaWikiServices;
use Wikibase\Client\WikibaseClient;
use Wikibase\DataModel\Entity\EntityIdParser;
@ -27,6 +28,11 @@ class MathWikibaseConfig {
*/
private $labelLookupFactory;
/**
* @var Site
*/
private $site;
/**
* @var PropertyId
*/
@ -52,15 +58,18 @@ class MathWikibaseConfig {
* @param EntityIdParser $entityIdParser
* @param EntityRevisionLookup $entityRevisionLookup
* @param LanguageFallbackLabelDescriptionLookupFactory $labelDescriptionLookupFactory
* @param Site $site
*/
public function __construct(
EntityIdParser $entityIdParser,
EntityRevisionLookup $entityRevisionLookup,
LanguageFallbackLabelDescriptionLookupFactory $labelDescriptionLookupFactory
LanguageFallbackLabelDescriptionLookupFactory $labelDescriptionLookupFactory,
Site $site
) {
$this->idParser = $entityIdParser;
$this->entityRevisionLookup = $entityRevisionLookup;
$this->labelLookupFactory = $labelDescriptionLookupFactory;
$this->site = $site;
$config = MediaWikiServices::getInstance()->getMainConfig();
$this->propertyIdHasPart = $this->idParser->parse(
@ -95,6 +104,20 @@ class MathWikibaseConfig {
return $this->labelLookupFactory;
}
/**
* @return Site
*/
public function getSite() : Site {
return $this->site;
}
/**
* @return bool
*/
public function hasSite() {
return is_null( $this->site );
}
/**
* @return PropertyId
*/
@ -122,10 +145,20 @@ class MathWikibaseConfig {
public static function getDefaultMathWikibaseConfig() : MathWikibaseConfig {
if ( !self::$defaultConfig ) {
$wikibaseClient = WikibaseClient::getDefaultInstance();
$site = null;
try {
$site = $wikibaseClient->getSite();
} catch ( MWException $e ) {
$logger = LoggerFactory::getInstance( 'Math' );
$logger->warning( "Cannot get Site handler: " . $e->getMessage() );
}
self::$defaultConfig = new MathWikibaseConfig(
$wikibaseClient->getEntityIdParser(),
$wikibaseClient->getStore()->getEntityRevisionLookup(),
$wikibaseClient->getLanguageFallbackLabelDescriptionLookupFactory()
$wikibaseClient->getLanguageFallbackLabelDescriptionLookupFactory(),
$site
);
}
return self::$defaultConfig;

View file

@ -1,7 +1,9 @@
<?php
use DataValues\StringValue;
use MediaWiki\Logger\LoggerFactory;
use Wikibase\Client\WikibaseClient;
use Wikibase\DataModel\Entity\EntityId;
use Wikibase\DataModel\Entity\EntityIdParsingException;
use Wikibase\DataModel\Entity\EntityIdValue;
use Wikibase\DataModel\Entity\Item;
@ -181,6 +183,10 @@ class MathWikibaseConnector {
$innerEntityId = $entityIdValue->getEntityId();
$innerInfo = new MathWikibaseInfo( $innerEntityId );
$this->fetchLabelDescription( $innerInfo, $langLookup );
$url = $this->fetchPageUrl( $innerEntityId );
if ( $url ) {
$innerInfo->setUrl( $url );
}
}
}
}
@ -195,6 +201,36 @@ class MathWikibaseConnector {
return $output;
}
/**
* Fetch the page url for a given entity id.
* @param EntityId $entityId
* @return string|bool
*/
private function fetchPageUrl( EntityId $entityId ) {
try {
$entityRevisionLookup = $this->config->getEntityRevisionLookup();
$entityRevision = $entityRevisionLookup->getEntityRevision( $entityId );
$innerEntity = $entityRevision->getEntity();
if ( $innerEntity instanceof Item ) {
if ( !$this->config->hasSite() ) {
$site = $this->config->getSite();
$globalID = $site->getGlobalId();
if ( $innerEntity->hasLinkToSite( $globalID ) ) {
$siteLink = $innerEntity->getSiteLink( $globalID );
return $site->getPageUrl( $siteLink->getPageName() );
}
}
}
return false;
} catch ( StorageException $e ) {
$logger = LoggerFactory::getInstance( 'Math' );
$logger->warning(
"Cannot fetch URL for EntityId " . $entityId . ". Reason: " . $e->getMessage()
);
return false;
}
}
/**
* @param Snak $snak
* @return bool true if the given snak is either a defining formula or a quantity symbol

View file

@ -39,6 +39,11 @@ class MathWikibaseInfo {
*/
private $mathFormatter;
/**
* @var string
*/
private $url;
public function __construct( EntityId $entityId ) {
$this->id = $entityId;
$this->mathFormatter = new MathFormatter( SnakFormatter::FORMAT_HTML );
@ -72,6 +77,13 @@ class MathWikibaseInfo {
array_push( $this->hasParts, $info );
}
/**
* @param string $link
*/
public function setUrl( $link ) {
$this->url = $link;
}
/**
* @param MathWikibaseInfo[] $infos
*/
@ -145,55 +157,79 @@ class MathWikibaseInfo {
$labelAlign = $lang->isRTL() ? 'left' : 'right';
$labelAlignOpposite = !$lang->isRTL() ? 'left' : 'right';
$output = HTML::openElement( "table", [ "style" => "padding: 5px" ] );
$output .= HTML::openElement( "tbody" );
$output = Html::openElement( "table", [ "style" => "padding: 5px" ] );
$output .= Html::openElement( "tbody" );
foreach ( $this->hasParts as $part ) {
$output .= HTML::openElement( "tr" );
$output .= HTML::element(
$output .= Html::openElement( "tr" );
$output .= Html::openElement(
"td",
[ "style" => "font-weight: bold; text-align:$labelAlign;" ],
$part->getLabel()
[ "style" => "font-weight: bold; text-align:$labelAlign;" ]
);
$output .= HTML::rawElement(
if ( $part->url ) {
$output .= Html::element(
"a",
[ "href" => $part->url ],
$part->getLabel()
);
} else {
$output .= $part->getLabel();
}
$output .= Html::closeElement( "td" );
$output .= Html::openElement(
"td",
[ "style" => "text-align:center; padding: 2px; padding-left: 10px; padding-right: 10px;" ],
$part->getFormattedSymbol()
[ "style" => "text-align:center; padding: 2px; padding-left: 10px; padding-right: 10px;" ]
);
$output .= HTML::element(
if ( $part->url ) {
$output .= Html::rawElement(
"a",
[ "href" => $part->url ],
$part->getFormattedSymbol()
);
} else {
$output .= $part->getFormattedSymbol();
}
$output .= Html::closeElement( "td" );
$output .= Html::element(
"td",
[ "style" => "font-style: italic; text-align:$labelAlignOpposite;" ],
$part->getDescription()
);
$output .= HTML::closeElement( "tr" );
$output .= Html::closeElement( "tr" );
}
$output .= HTML::closeElement( "tbody" );
$output .= HTML::closeElement( "table" );
$output .= Html::closeElement( "tbody" );
$output .= Html::closeElement( "table" );
return $output;
}
/**
* Generates a minimalized HTML representation of the has-parts elements.
* Generates a minimalized Html representation of the has-parts elements.
* @return string
*/
public function generateSmallTableOfParts() {
$output = HTML::openElement( "table" );
$output .= HTML::openElement( "tbody" );
$output = Html::openElement( "table" );
$output .= Html::openElement( "tbody" );
foreach ( $this->hasParts as $part ) {
$output .= HTML::openElement( "tr" );
$output .= HTML::rawElement(
$output .= Html::openElement( "tr" );
$output .= Html::rawElement(
"td",
[ "style" => "text-align:right;" ],
$part->getFormattedSymbol()
);
$output .= HTML::element( "td", [ "style" => "text-align:left;" ], $part->getLabel() );
$output .= HTML::closeElement( "tr" );
$output .= Html::element( "td", [ "style" => "text-align:left;" ], $part->getLabel() );
$output .= Html::closeElement( "tr" );
}
$output .= HTML::closeElement( "tbody" );
$output .= HTML::closeElement( "table" );
$output .= Html::closeElement( "tbody" );
$output .= Html::closeElement( "table" );
return $output;
}
}