wikimedia/mediawiki-extensions-Wikibase

View on GitHub
view/src/PropertyView.php

Summary

Maintainability
A
1 hr
Test Coverage
<?php

namespace Wikibase\View;

use InvalidArgumentException;
use OutOfBoundsException;
use Wikibase\DataModel\Entity\EntityDocument;
use Wikibase\DataModel\Entity\Property;
use Wikibase\DataModel\Term\AliasesProvider;
use Wikibase\DataModel\Term\DescriptionsProvider;
use Wikibase\DataModel\Term\LabelsProvider;
use Wikibase\Lib\DataTypeFactory;
use Wikibase\View\Template\TemplateFactory;

/**
 * Class for creating views for Property instances.
 * For the Property this basically is what the Parser is for WikitextContent.
 *
 * @license GPL-2.0-or-later
 * @author Daniel Werner
 * @author H. Snater < mediawiki@snater.com >
 */
class PropertyView extends EntityView {

    /**
     * @var StatementSectionsView
     */
    private $statementSectionsView;

    /**
     * @var DataTypeFactory
     */
    private $dataTypeFactory;

    /**
     * @var LocalizedTextProvider
     */
    private $textProvider;

    /**
     * @var CacheableEntityTermsView
     */
    private $entityTermsView;

    /**
     * @see EntityView::__construct
     *
     * @param TemplateFactory $templateFactory
     * @param CacheableEntityTermsView $entityTermsView
     * @param LanguageDirectionalityLookup $languageDirectionalityLookup
     * @param StatementSectionsView $statementSectionsView
     * @param DataTypeFactory $dataTypeFactory
     * @param string $languageCode
     * @param LocalizedTextProvider $textProvider
     */
    public function __construct(
        TemplateFactory $templateFactory,
        CacheableEntityTermsView $entityTermsView,
        LanguageDirectionalityLookup $languageDirectionalityLookup,
        StatementSectionsView $statementSectionsView,
        DataTypeFactory $dataTypeFactory,
        $languageCode,
        LocalizedTextProvider $textProvider
    ) {
        parent::__construct( $templateFactory, $languageDirectionalityLookup, $languageCode );

        $this->statementSectionsView = $statementSectionsView;
        $this->dataTypeFactory = $dataTypeFactory;
        $this->textProvider = $textProvider;
        $this->entityTermsView = $entityTermsView;
    }

    /**
     * @inheritDoc
     */
    public function getTitleHtml( EntityDocument $entity ) {
        if ( $entity instanceof LabelsProvider ) {
            return $this->entityTermsView->getTitleHtml(
                $entity->getId()
            );
        }

        return '';
    }

    /**
     * Builds and returns the main content representing a whole WikibaseEntity
     *
     * @param EntityDocument $entity the entity to render
     * @param int $revision The revision of the entity to render
     *
     * @return ViewContent
     */
    public function getContent( EntityDocument $entity, $revision ): ViewContent {
        return new ViewContent(
            $this->renderEntityView( $entity ),
            $this->entityTermsView->getPlaceholders( $entity, $revision, $this->languageCode )
        );
    }

    /**
     * @see EntityView::getMainHtml
     *
     * @param EntityDocument $property
     *
     * @throws InvalidArgumentException
     * @return string HTML
     */
    protected function getMainHtml( EntityDocument $property ) {
        if ( !( $property instanceof Property ) ) {
            throw new InvalidArgumentException( '$property must contain a Property.' );
        }

        $html = $this->getHtmlForTerms( $property )
            . $this->templateFactory->render( 'wikibase-toc' )
            . $this->getHtmlForDataType( $property->getDataTypeId() )
            . $this->statementSectionsView->getHtml( $property->getStatements() );

        $footer = wfMessage( 'wikibase-property-footer' );
        $footer = $footer->exists() ? $footer->parse() : '';

        if ( $footer !== '' ) {
            $html .= "\n" . $footer;
        }

        return $html;
    }

    /**
     * Builds and returns the HTML representing a property entity's data type information.
     *
     * @param string $propertyType
     *
     * @return string HTML
     */
    private function getHtmlForDataType( $propertyType ) {
        $html = $this->templateFactory->render( 'wb-section-heading',
            $this->textProvider->getEscaped( 'wikibase-propertypage-datatype' ),
            'datatype',
            'wikibase-propertypage-datatype'
        );

        try {
            $dataType = $this->dataTypeFactory->getType( $propertyType );
            $dataTypeLabelHtml = $this->textProvider->getEscaped( $dataType->getMessageKey() );
        } catch ( OutOfBoundsException $ex ) {
            $dataTypeLabelHtml = '<span class="error">' .
                $this->textProvider->getEscaped( 'wikibase-propertypage-bad-datatype', [ $propertyType ] ) .
                '</span>';
        }
        $html .= $this->templateFactory->render( 'wikibase-propertyview-datatype', $dataTypeLabelHtml );

        return $html;
    }

    /**
     * @see EntityView::getSideHtml
     *
     * @param EntityDocument $entity
     *
     * @return string HTML
     */
    protected function getSideHtml( EntityDocument $entity ) {
        return '';
    }

    /**
     * Builds and returns the HTML for the entity's fingerprint.
     *
     * @param EntityDocument $entity
     *
     * @return string HTML
     */
    private function getHtmlForTerms( EntityDocument $entity ) {
        $id = $entity->getId();

        if ( $entity instanceof LabelsProvider && $entity instanceof DescriptionsProvider ) {
            return $this->entityTermsView->getHtml(
                $this->languageCode,
                $entity->getLabels(),
                $entity->getDescriptions(),
                $entity instanceof AliasesProvider ? $entity->getAliasGroups() : null,
                $id
            );
        }

        return '';
    }

}