Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
73.33% |
11 / 15 |
CRAP | |
97.10% |
134 / 138 |
EntitySchema\MediaWiki\Content\EntitySchemaSlotViewRenderer | |
0.00% |
0 / 1 |
|
73.33% |
11 / 15 |
22 | |
97.10% |
134 / 138 |
__construct | |
100.00% |
1 / 1 |
3 | |
100.00% |
4 / 4 |
|||
msg | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
fillParserOutput | |
100.00% |
1 / 1 |
1 | |
100.00% |
8 / 8 |
|||
renderNameBadges | |
100.00% |
1 / 1 |
2 | |
100.00% |
9 / 9 |
|||
renderNameBadgeHeader | |
100.00% |
1 / 1 |
2 | |
100.00% |
11 / 11 |
|||
renderNameBadge | |
100.00% |
1 / 1 |
1 | |
100.00% |
28 / 28 |
|||
renderNameBadgeEditLink | |
0.00% |
0 / 1 |
1.00 | |
90.00% |
9 / 10 |
|||
renderSchemaSection | |
100.00% |
1 / 1 |
2 | |
100.00% |
6 / 6 |
|||
renderSchemaText | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
renderSchemaTextLinks | |
100.00% |
1 / 1 |
1 | |
100.00% |
5 / 5 |
|||
renderSchemaCheckLink | |
0.00% |
0 / 1 |
2.00 | |
91.67% |
11 / 12 |
|||
makeExternalLink | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
renderSchemaAddTextLink | |
0.00% |
0 / 1 |
1.00 | |
87.50% |
7 / 8 |
|||
renderSchemaEditLink | |
0.00% |
0 / 1 |
1.00 | |
87.50% |
7 / 8 |
|||
renderHeading | |
100.00% |
1 / 1 |
2 | |
100.00% |
20 / 20 |
<?php | |
namespace EntitySchema\MediaWiki\Content; | |
use Config; | |
use Html; | |
use LanguageCode; | |
use Linker; | |
use MediaWiki\Linker\LinkRenderer; | |
use MediaWiki\MediaWikiServices; | |
use MessageLocalizer; | |
use ParserOutput; | |
use SpecialPage; | |
use Title; | |
use EntitySchema\MediaWiki\SpecificLanguageMessageLocalizer; | |
use EntitySchema\Services\SchemaConverter\FullViewSchemaData; | |
use EntitySchema\Services\SchemaConverter\NameBadge; | |
/** | |
* @license GPL-2.0-or-later | |
*/ | |
class EntitySchemaSlotViewRenderer { | |
/** @var MessageLocalizer */ | |
private $messageLocalizer; | |
/** @var LinkRenderer */ | |
private $linkRenderer; | |
/** @var Config */ | |
private $config; | |
/** | |
* @param string $languageCode The language in which to render the view. | |
*/ | |
public function __construct( | |
$languageCode, | |
LinkRenderer $linkRenderer = null, | |
Config $config = null | |
) { | |
$this->messageLocalizer = new SpecificLanguageMessageLocalizer( $languageCode ); | |
$this->linkRenderer = $linkRenderer ?: MediaWikiServices::getInstance()->getLinkRenderer(); | |
$this->config = $config ?: MediaWikiServices::getInstance()->getMainConfig(); | |
} | |
private function msg( $key ) { | |
return $this->messageLocalizer->msg( $key ); | |
} | |
public function fillParserOutput( | |
FullViewSchemaData $schemaData, | |
Title $title, | |
ParserOutput $output | |
) { | |
$output->addModules( 'ext.EntitySchema.action.view.trackclicks' ); | |
$output->addModuleStyles( 'ext.EntitySchema.view' ); | |
$output->setText( | |
$this->renderNameBadges( $title, $schemaData->nameBadges ) . | |
$this->renderSchemaSection( $title, $schemaData->schemaText ) | |
); | |
$output->setDisplayTitle( | |
$this->renderHeading( reset( $schemaData->nameBadges ), $title ) | |
); | |
} | |
private function renderNameBadges( Title $title, array $nameBadges ) { | |
$html = Html::openElement( 'table', [ 'class' => 'wikitable' ] ); | |
$html .= $this->renderNameBadgeHeader(); | |
$html .= Html::openElement( 'tbody' ); | |
foreach ( $nameBadges as $langCode => $nameBadge ) { | |
$html .= "\n"; | |
$html .= $this->renderNameBadge( $nameBadge, $langCode, $title->getText() ); | |
} | |
$html .= Html::closeElement( 'tbody' ); | |
$html .= Html::closeElement( 'table' ); | |
return $html; | |
} | |
private function renderNameBadgeHeader() { | |
$tableHeaders = ''; | |
// message keys: | |
// entityschema-namebadge-header-language-code | |
// entityschema-namebadge-header-label | |
// entityschema-namebadge-header-description | |
// entityschema-namebadge-header-aliases | |
// entityschema-namebadge-header-edit | |
foreach ( [ 'language-code', 'label', 'description', 'aliases', 'edit' ] as $key ) { | |
$tableHeaders .= Html::element( | |
'th', | |
[], | |
$this->msg( 'entityschema-namebadge-header-' . $key ) | |
->parse() | |
); | |
} | |
return Html::rawElement( 'thead', [], Html::rawElement( | |
'tr', | |
[], | |
$tableHeaders | |
) ); | |
} | |
private function renderNameBadge( NameBadge $nameBadge, $languageCode, $schemaId ) { | |
$language = Html::element( | |
'td', | |
[], | |
$languageCode | |
); | |
$bcp47 = LanguageCode::bcp47( $languageCode ); // 'simple' => 'en-simple' etc. | |
$label = Html::element( | |
'td', | |
[ | |
'class' => 'wbschema-label', | |
'lang' => $bcp47, | |
'dir' => 'auto', | |
], | |
$nameBadge->label | |
); | |
$description = Html::element( | |
'td', | |
[ | |
'class' => 'wbschema-description', | |
'lang' => $bcp47, | |
'dir' => 'auto' | |
], | |
$nameBadge->description | |
); | |
$aliases = Html::element( | |
'td', | |
[ | |
'class' => 'wbschema-aliases', | |
'lang' => $bcp47, | |
'dir' => 'auto' | |
], | |
implode( ' | ', $nameBadge->aliases ) | |
); | |
$editLink = $this->renderNameBadgeEditLink( $schemaId, $languageCode ); | |
return Html::rawElement( | |
'tr', | |
[], | |
$language . $label . $description . $aliases . $editLink | |
); | |
} | |
private function renderNameBadgeEditLink( $schemaId, $langCode ) { | |
$specialPageTitleValue = SpecialPage::getTitleValueFor( | |
'SetEntitySchemaLabelDescriptionAliases', | |
$schemaId . '/' . $langCode | |
); | |
return Html::rawElement( | |
'td', | |
[ | |
'class' => 'wbschema-edit-button', | |
], | |
$this->linkRenderer->makeKnownLink( | |
$specialPageTitleValue, | |
$this->msg( 'entityschema-edit' ), | |
[ 'class' => 'edit-icon' ] | |
) | |
); | |
} | |
private function renderSchemaSection( Title $title, $schemaText ) { | |
$schemaSectionContent = $schemaText | |
? $this->renderSchemaTextLinks( $title ) . $this->renderSchemaText( $schemaText ) | |
: $this->renderSchemaAddTextLink( $title ); | |
return Html::rawElement( 'div', [ | |
'id' => 'wbschema-schema-view-section', | |
'class' => 'wbschema-section', | |
], | |
$schemaSectionContent | |
); | |
} | |
private function renderSchemaText( $schemaText ) { | |
return Html::element( | |
'pre', | |
[ | |
'id' => 'wbschema-schema-text', | |
'class' => 'wbschema-schema-text', | |
'dir' => 'ltr', | |
], | |
$schemaText | |
); | |
} | |
private function renderSchemaTextLinks( Title $title ) { | |
return Html::rawElement( | |
'div', | |
[ | |
'class' => 'wbschema-schema-text-links', | |
], | |
$this->renderSchemaCheckLink( $title ) . | |
$this->renderSchemaEditLink( $title ) | |
); | |
} | |
private function renderSchemaCheckLink( Title $title ) { | |
$url = $this->config->get( 'WBSchemaShExSimpleUrl' ); | |
if ( !$url ) { | |
return ''; | |
} | |
$schemaTextTitle = SpecialPage::getTitleFor( 'EntitySchemaText', $title->getText() ); | |
$url = wfAppendQuery( $url, [ | |
'schemaURL' => $schemaTextTitle->getFullURL() | |
] ); | |
return $this->makeExternalLink( | |
$url, | |
wfMessage( 'entityschema-check-entities' )->parse(), | |
false, // link text already escaped in ->parse() | |
'', | |
[ 'class' => 'wbschema-check-schema' ] | |
); | |
} | |
/** | |
* Wrapper around {@see Linker::makeExternalLink} ensuring that the external link style | |
* is applied even though our whole output does not have class="mw-parser-output" | |
*/ | |
private function makeExternalLink( | |
$url, | |
$text, | |
$escape = true, | |
$linktype = '', | |
$attribs = [], | |
$title = null | |
) { | |
return Html::rawElement( | |
'span', | |
[ 'class' => 'mw-parser-output' ], | |
Linker::makeExternalLink( $url, $text, $escape, $linktype, $attribs, $title ) | |
); | |
} | |
private function renderSchemaAddTextLink( Title $title ) { | |
return Html::rawElement( | |
'span', | |
[ | |
'id' => 'wbschema-edit-schema-text', | |
'class' => 'wbschema-edit-button', | |
], | |
$this->linkRenderer->makeKnownLink( | |
$title, | |
$this->msg( 'entityschema-add-schema-text' ), | |
[ 'class' => 'add-icon' ], | |
[ 'action' => 'edit' ] | |
) | |
); | |
} | |
private function renderSchemaEditLink( Title $title ) { | |
return Html::rawElement( | |
'span', | |
[ | |
'id' => 'wbschema-edit-schema-text', | |
'class' => 'wbschema-edit-button', | |
], | |
$this->linkRenderer->makeKnownLink( | |
$title, | |
$this->msg( 'entityschema-edit' ), | |
[ 'class' => 'edit-icon' ], | |
[ 'action' => 'edit' ] | |
) | |
); | |
} | |
private function renderHeading( NameBadge $nameBadge, Title $title ) { | |
if ( $nameBadge->label !== '' ) { | |
$label = Html::element( | |
'span', | |
[ 'class' => 'wbschema-title-label' ], | |
$nameBadge->label | |
); | |
} else { | |
$label = Html::element( | |
'span', | |
[ 'class' => 'wbschema-title-label-empty' ], | |
$this->msg( 'entityschema-label-empty' ) | |
->text() | |
); | |
} | |
$id = Html::element( | |
'span', | |
[ 'class' => 'wbschema-title-id' ], | |
$this->msg( 'parentheses' ) | |
->plaintextParams( $title->getText() ) | |
->text() | |
); | |
return Html::rawElement( | |
'span', | |
[ 'class' => 'wbschema-title' ], | |
$label . ' ' . $id | |
); | |
} | |
} |