| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Math; |
| 4 |
|
| 5 |
use DataValues\StringValue; |
| 6 |
use InvalidArgumentException; |
| 7 |
use MediaWiki\Config\ConfigException; |
| 8 |
use MediaWiki\Config\ServiceOptions; |
| 9 |
use MediaWiki\Languages\LanguageFactory; |
| 10 |
use MediaWiki\Languages\LanguageNameUtils; |
| 11 |
use MediaWiki\Site\Site; |
| 12 |
use Psr\Log\LoggerInterface; |
| 13 |
use Wikibase\Client\RepoLinker; |
| 14 |
use Wikibase\DataModel\Entity\EntityId; |
| 15 |
use Wikibase\DataModel\Entity\EntityIdParser; |
| 16 |
use Wikibase\DataModel\Entity\EntityIdParsingException; |
| 17 |
use Wikibase\DataModel\Entity\EntityIdValue; |
| 18 |
use Wikibase\DataModel\Entity\Item; |
| 19 |
use Wikibase\DataModel\Entity\ItemId; |
| 20 |
use Wikibase\DataModel\Entity\PropertyId; |
| 21 |
use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup; |
| 22 |
use Wikibase\DataModel\Snak\PropertyValueSnak; |
| 23 |
use Wikibase\DataModel\Snak\Snak; |
| 24 |
use Wikibase\DataModel\Statement\StatementList; |
| 25 |
use Wikibase\Lib\Store\EntityRevisionLookup; |
| 26 |
use Wikibase\Lib\Store\FallbackLabelDescriptionLookupFactory; |
| 27 |
use Wikibase\Lib\Store\RevisionedUnresolvedRedirectException; |
| 28 |
use Wikibase\Lib\Store\StorageException; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class MathWikibaseConnector { |
| 37 |
|
| 38 |
public const CONSTRUCTOR_OPTIONS = [ |
| 39 |
'MathWikibasePropertyIdHasPart', |
| 40 |
'MathWikibasePropertyIdDefiningFormula', |
| 41 |
'MathWikibasePropertyIdInDefiningFormula', |
| 42 |
'MathWikibasePropertyIdQuantitySymbol', |
| 43 |
'MathWikibasePropertyIdSymbolRepresents' |
| 44 |
]; |
| 45 |
|
| 46 |
|
| 47 |
private $logger; |
| 48 |
|
| 49 |
|
| 50 |
private $repoLinker; |
| 51 |
|
| 52 |
|
| 53 |
private $languageFactory; |
| 54 |
|
| 55 |
|
| 56 |
private $languageNameUtils; |
| 57 |
|
| 58 |
|
| 59 |
private $entityRevisionLookup; |
| 60 |
|
| 61 |
|
| 62 |
private $site; |
| 63 |
|
| 64 |
|
| 65 |
private $labelDescriptionLookupFactory; |
| 66 |
|
| 67 |
|
| 68 |
private $mathFormatter; |
| 69 |
|
| 70 |
|
| 71 |
private $idParser; |
| 72 |
|
| 73 |
|
| 74 |
private $propertyIdHasPart; |
| 75 |
|
| 76 |
|
| 77 |
private $propertyIdDefiningFormula; |
| 78 |
|
| 79 |
|
| 80 |
private $propertyIdInDefiningFormula; |
| 81 |
|
| 82 |
|
| 83 |
private $propertyIdQuantitySymbol; |
| 84 |
|
| 85 |
|
| 86 |
private $propertyIdSymbolRepresents; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public function __construct( |
| 101 |
ServiceOptions $options, |
| 102 |
RepoLinker $repoLinker, |
| 103 |
LanguageFactory $languageFactory, |
| 104 |
LanguageNameUtils $languageNameUtils, |
| 105 |
EntityRevisionLookup $entityRevisionLookup, |
| 106 |
FallbackLabelDescriptionLookupFactory $labelDescriptionLookupFactory, |
| 107 |
Site $site, |
| 108 |
EntityIdParser $entityIdParser, |
| 109 |
MathFormatter $mathFormatter, |
| 110 |
LoggerInterface $logger |
| 111 |
) { |
| 112 |
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 113 |
$this->repoLinker = $repoLinker; |
| 114 |
$this->languageFactory = $languageFactory; |
| 115 |
$this->languageNameUtils = $languageNameUtils; |
| 116 |
$this->entityRevisionLookup = $entityRevisionLookup; |
| 117 |
$this->labelDescriptionLookupFactory = $labelDescriptionLookupFactory; |
| 118 |
$this->site = $site; |
| 119 |
$this->idParser = $entityIdParser; |
| 120 |
$this->mathFormatter = $mathFormatter; |
| 121 |
$this->logger = $logger; |
| 122 |
|
| 123 |
$this->propertyIdHasPart = $this->loadPropertyId( |
| 124 |
$options->get( "MathWikibasePropertyIdHasPart" ) |
| 125 |
); |
| 126 |
$this->propertyIdDefiningFormula = $this->loadPropertyId( |
| 127 |
$options->get( "MathWikibasePropertyIdDefiningFormula" ) |
| 128 |
); |
| 129 |
$this->propertyIdInDefiningFormula = $this->loadPropertyId( |
| 130 |
$options->get( "MathWikibasePropertyIdInDefiningFormula" ) |
| 131 |
); |
| 132 |
$this->propertyIdQuantitySymbol = $this->loadPropertyId( |
| 133 |
$options->get( "MathWikibasePropertyIdQuantitySymbol" ) |
| 134 |
); |
| 135 |
$this->propertyIdSymbolRepresents = $this->loadPropertyId( |
| 136 |
$options->get( "MathWikibasePropertyIdSymbolRepresents" ) |
| 137 |
); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
private function loadPropertyId( string $propertyId ): ?EntityId { |
| 146 |
try { |
| 147 |
return $this->idParser->parse( $propertyId ); |
| 148 |
} catch ( ConfigException $e ) { |
| 149 |
return null; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
private function getStatements( StatementList $statements, ?PropertyId $id ): StatementList { |
| 161 |
if ( $id === null ) { |
| 162 |
return new StatementList(); |
| 163 |
} |
| 164 |
return $statements->getByPropertyId( $id ); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public function fetchWikibaseFromId( string $qid, string $langCode ): MathWikibaseInfo { |
| 177 |
if ( $this->languageNameUtils->isValidCode( $langCode ) ) { |
| 178 |
$lang = $this->languageFactory->getLanguage( $langCode ); |
| 179 |
} else { |
| 180 |
throw new InvalidArgumentException( "Invalid language code specified." ); |
| 181 |
} |
| 182 |
|
| 183 |
$langLookup = $this->labelDescriptionLookupFactory->newLabelDescriptionLookup( $lang ); |
| 184 |
try { |
| 185 |
$entityId = $this->idParser->parse( $qid ); |
| 186 |
$entityRevision = $this->entityRevisionLookup->getEntityRevision( $entityId ); |
| 187 |
} catch ( EntityIdParsingException $e ) { |
| 188 |
throw new InvalidArgumentException( "Invalid Wikibase ID." ); |
| 189 |
} catch ( RevisionedUnresolvedRedirectException | StorageException $e ) { |
| 190 |
throw new InvalidArgumentException( "Non-existing Wikibase ID." ); |
| 191 |
} |
| 192 |
|
| 193 |
if ( !$entityId || !$entityRevision ) { |
| 194 |
throw new InvalidArgumentException( "Non-existing Wikibase ID." ); |
| 195 |
} |
| 196 |
|
| 197 |
$entity = $entityRevision->getEntity(); |
| 198 |
$output = new MathWikibaseInfo( $entityId, $this->mathFormatter ); |
| 199 |
|
| 200 |
if ( $entity instanceof Item ) { |
| 201 |
$this->fetchLabelDescription( $output, $langLookup ); |
| 202 |
$this->fetchStatements( $output, $entity, $langLookup ); |
| 203 |
return $output; |
| 204 |
} else { |
| 205 |
throw new InvalidArgumentException( "The specified Wikibase ID does not represented an item." ); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
private function fetchLabelDescription( |
| 216 |
MathWikibaseInfo $output, |
| 217 |
LabelDescriptionLookup $langLookup ) { |
| 218 |
$label = $langLookup->getLabel( $output->getId() ); |
| 219 |
$desc = $langLookup->getDescription( $output->getId() ); |
| 220 |
|
| 221 |
if ( $label ) { |
| 222 |
$output->setLabel( $label->getText() ); |
| 223 |
} |
| 224 |
|
| 225 |
if ( $desc ) { |
| 226 |
$output->setDescription( $desc->getText() ); |
| 227 |
} |
| 228 |
|
| 229 |
return $output; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
private function fetchStatements( |
| 241 |
MathWikibaseInfo $output, |
| 242 |
Item $item, |
| 243 |
LabelDescriptionLookup $langLookup ) { |
| 244 |
$statements = $item->getStatements(); |
| 245 |
$formulaComponentStatements = $this->getStatements( $statements, $this->propertyIdHasPart ); |
| 246 |
if ( $formulaComponentStatements->isEmpty() ) { |
| 247 |
$formulaComponentStatements = $this->getStatements( $statements, $this->propertyIdInDefiningFormula ); |
| 248 |
} |
| 249 |
$this->fetchHasPartSnaks( $output, $formulaComponentStatements, $langLookup ); |
| 250 |
|
| 251 |
$symbolStatement = $this->getStatements( $statements, $this->propertyIdDefiningFormula ); |
| 252 |
if ( $symbolStatement->count() < 1 ) { |
| 253 |
$symbolStatement = $this->getStatements( $statements, $this->propertyIdQuantitySymbol ); |
| 254 |
} |
| 255 |
$this->fetchSymbol( $output, $symbolStatement ); |
| 256 |
return $output; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
private function fetchSymbol( MathWikibaseInfo $output, StatementList $statements ) { |
| 267 |
foreach ( $statements as $statement ) { |
| 268 |
$snak = $statement->getMainSnak(); |
| 269 |
if ( $snak instanceof PropertyValueSnak && $this->isSymbolSnak( $snak ) ) { |
| 270 |
$dataVal = $snak->getDataValue(); |
| 271 |
$symbol = new StringValue( $dataVal->getValue() ); |
| 272 |
$output->setSymbol( $symbol ); |
| 273 |
return $output; |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
return $output; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
private function fetchHasPartSnaks( |
| 290 |
MathWikibaseInfo $output, |
| 291 |
StatementList $statements, |
| 292 |
LabelDescriptionLookup $langLookup ) { |
| 293 |
foreach ( $statements as $statement ) { |
| 294 |
$snaks = $statement->getAllSnaks(); |
| 295 |
$innerInfo = null; |
| 296 |
$symbol = null; |
| 297 |
|
| 298 |
foreach ( $snaks as $snak ) { |
| 299 |
if ( $snak instanceof PropertyValueSnak ) { |
| 300 |
if ( $this->isSymbolSnak( $snak ) ) { |
| 301 |
$dataVal = $snak->getDataValue(); |
| 302 |
$symbol = new StringValue( $dataVal->getValue() ); |
| 303 |
} elseif ( $this->isFormulaItemSnak( $snak ) ) { |
| 304 |
$dataVal = $snak->getDataValue(); |
| 305 |
$entityIdValue = $dataVal->getValue(); |
| 306 |
if ( $entityIdValue instanceof EntityIdValue ) { |
| 307 |
$innerEntityId = $entityIdValue->getEntityId(); |
| 308 |
$innerInfo = new MathWikibaseInfo( $innerEntityId, $output->getFormatter() ); |
| 309 |
$this->fetchLabelDescription( $innerInfo, $langLookup ); |
| 310 |
$url = $this->fetchPageUrl( $innerEntityId ); |
| 311 |
if ( $url ) { |
| 312 |
$innerInfo->setUrl( $url ); |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
if ( $innerInfo && $symbol ) { |
| 320 |
$innerInfo->setSymbol( $symbol ); |
| 321 |
$output->addHasPartElement( $innerInfo ); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
return $output; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
private function fetchPageUrl( EntityId $entityId ) { |
| 334 |
try { |
| 335 |
$entityRevision = $this->entityRevisionLookup->getEntityRevision( $entityId ); |
| 336 |
$innerEntity = $entityRevision->getEntity(); |
| 337 |
if ( $innerEntity instanceof Item ) { |
| 338 |
$globalID = $this->site->getGlobalId(); |
| 339 |
if ( $innerEntity->hasLinkToSite( $globalID ) ) { |
| 340 |
$siteLink = $innerEntity->getSiteLink( $globalID ); |
| 341 |
return $this->site->getPageUrl( $siteLink->getPageName() ); |
| 342 |
} |
| 343 |
} |
| 344 |
} catch ( StorageException | RevisionedUnresolvedRedirectException $e ) { |
| 345 |
$this->logger->warning( |
| 346 |
"Cannot fetch URL for EntityId " . $entityId . ". Reason: " . $e->getMessage() |
| 347 |
); |
| 348 |
} |
| 349 |
return false; |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
private function isSymbolSnak( Snak $snak ) { |
| 357 |
return $snak->getPropertyId()->equals( $this->propertyIdQuantitySymbol ) || |
| 358 |
$snak->getPropertyId()->equals( $this->propertyIdDefiningFormula ) || |
| 359 |
$snak->getPropertyId()->equals( $this->propertyIdInDefiningFormula ); |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
private function isFormulaItemSnak( Snak $snak ) { |
| 367 |
return $snak->getPropertyId()->equals( $this->propertyIdHasPart ) || |
| 368 |
$snak->getPropertyId()->equals( $this->propertyIdSymbolRepresents ); |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
public function buildURL( string $qID ): string { |
| 376 |
return $this->repoLinker->getEntityUrl( new ItemId( $qID ) ); |
| 377 |
} |
| 378 |
} |