| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Math\Rest; |
| 4 |
|
| 5 |
use MediaWiki\Extension\Math\MathWikibaseConnector; |
| 6 |
use MediaWiki\Extension\Math\MathWikibaseInfo; |
| 7 |
use MediaWiki\Html\Html; |
| 8 |
use MediaWiki\Languages\LanguageFactory; |
| 9 |
use MediaWiki\Languages\LanguageNameUtils; |
| 10 |
use MediaWiki\Rest\Response; |
| 11 |
use MediaWiki\Rest\SimpleHandler; |
| 12 |
use MediaWiki\Title\Title; |
| 13 |
use MediaWiki\Title\TitleFactory; |
| 14 |
use Wikimedia\ParamValidator\ParamValidator; |
| 15 |
|
| 16 |
class Popup extends SimpleHandler { |
| 17 |
|
| 18 |
|
| 19 |
private $wikibase; |
| 20 |
|
| 21 |
|
| 22 |
private $languageFactory; |
| 23 |
|
| 24 |
|
| 25 |
private $languageNameUtils; |
| 26 |
|
| 27 |
|
| 28 |
private $specialPageTitle; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public function __construct( |
| 37 |
MathWikibaseConnector $wikibase, |
| 38 |
LanguageFactory $languageFactory, |
| 39 |
LanguageNameUtils $languageNameUtils, |
| 40 |
TitleFactory $titleFactory |
| 41 |
) { |
| 42 |
$this->wikibase = $wikibase; |
| 43 |
$this->languageFactory = $languageFactory; |
| 44 |
$this->languageNameUtils = $languageNameUtils; |
| 45 |
$this->specialPageTitle = $titleFactory->newFromText( 'Special:MathWikibase' ); |
| 46 |
} |
| 47 |
|
| 48 |
public function run( int $qid ): Response { |
| 49 |
$uselang = $this->getRequest()->getHeaderLine( 'Accept-Language' ); |
| 50 |
if ( $uselang === '' ) { |
| 51 |
$uselang = 'en'; |
| 52 |
} |
| 53 |
$rf = $this->getResponseFactory(); |
| 54 |
if ( $this->languageNameUtils->isValidCode( $uselang ) ) { |
| 55 |
$langObj = $this->languageFactory->getLanguage( $uselang ); |
| 56 |
} else { |
| 57 |
return $rf->createHttpError( 400, [ 'message' => 'Invalid language code.' ] ); |
| 58 |
} |
| 59 |
|
| 60 |
try { |
| 61 |
$info = $this->wikibase->fetchWikibaseFromId( "Q{$qid}", $uselang ); |
| 62 |
} catch ( \InvalidArgumentException $exception ) { |
| 63 |
return $rf->createHttpError( 400, [ 'message' => $exception->getMessage() ] ); |
| 64 |
} |
| 65 |
|
| 66 |
$html = $this->buildHTMLRepresentation( $info ); |
| 67 |
|
| 68 |
$response = [ |
| 69 |
'title' => $info->getLabel(), |
| 70 |
'contentmodel' => 'html', |
| 71 |
'pagelanguagedir' => $langObj->getDir(), |
| 72 |
'pagelanguage' => $langObj->getCode(), |
| 73 |
'pagelanguagehtmlcode' => $langObj->getHtmlCode(), |
| 74 |
'extract' => $html |
| 75 |
]; |
| 76 |
|
| 77 |
if ( $this->specialPageTitle ) { |
| 78 |
$response = array_merge( $response, [ |
| 79 |
'canonicalurl' => $this->specialPageTitle->getLocalURL( [ 'qid' => "Q{$qid}" ] ), |
| 80 |
'fullurl' => $this->specialPageTitle->getFullURL( [ 'qid' => "Q{$qid}" ] ) |
| 81 |
] ); |
| 82 |
} |
| 83 |
|
| 84 |
return $rf->createJson( $response ); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
private function buildHTMLRepresentation( MathWikibaseInfo $info ): string { |
| 93 |
$output = |
| 94 |
Html::openElement( "div", |
| 95 |
[ "style" => |
| 96 |
"width: 100%; display: flex; flex-direction: column; |
| 97 |
align-items: flex-start; flex-wrap: wrap;" ] ); |
| 98 |
$output .= Html::element( "span", [ |
| 99 |
"style" => "font-weight: bold; text-transform: capitalize;" |
| 100 |
], $info->getLabel() ); |
| 101 |
$output .= Html::element( "span", [ "style" => "font-size: small" ], " (" . $info->getDescription() . ")" ); |
| 102 |
if ( $info->hasParts() ) { |
| 103 |
$output .= Html::rawElement( "div", |
| 104 |
[ "style" => "width: 100%; display: flex; justify-content: left; padding-top: 5px;" ], |
| 105 |
$info->generateSmallTableOfParts() ); |
| 106 |
} |
| 107 |
$output .= Html::closeElement( "div" ); |
| 108 |
|
| 109 |
return $output; |
| 110 |
} |
| 111 |
|
| 112 |
public function getParamSettings(): array { |
| 113 |
return [ |
| 114 |
'qid' => [ |
| 115 |
self::PARAM_SOURCE => 'path', |
| 116 |
ParamValidator::PARAM_TYPE => 'integer', |
| 117 |
ParamValidator::PARAM_REQUIRED => true |
| 118 |
] |
| 119 |
]; |
| 120 |
} |
| 121 |
} |