Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
2 / 3
CRAP
94.44% covered (success)
94.44%
34 / 36
EntitySchema\DataAccess\MediaWikiRevisionSchemaInserter
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
2 / 3
4.00
94.44% covered (success)
94.44%
34 / 36
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 insertSchema
0.00% covered (danger)
0.00%
0 / 1
2.00
93.33% covered (success)
93.33%
28 / 30
 truncateSchemaTextForCommentData
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
<?php
namespace EntitySchema\DataAccess;
use CommentStoreComment;
use Language;
use MediaWiki\Revision\SlotRecord;
use RuntimeException;
use EntitySchema\Domain\Model\SchemaId;
use EntitySchema\Domain\Storage\IdGenerator;
use EntitySchema\MediaWiki\Content\EntitySchemaContent;
use EntitySchema\Services\SchemaConverter\SchemaConverter;
/**
 * @license GPL-2.0-or-later
 */
class MediaWikiRevisionSchemaInserter implements SchemaInserter {
    const AUTOCOMMENT_NEWSCHEMA = 'entityschema-summary-newschema-nolabel';
    private $pageUpdaterFactory;
    private $idGenerator;
    private $watchListUpdater;
    public function __construct(
        MediaWikiPageUpdaterFactory $pageUpdaterFactory,
        WatchlistUpdater $watchListUpdater,
        IdGenerator $idGenerator = null
    ) {
        $this->idGenerator = $idGenerator;
        $this->pageUpdaterFactory = $pageUpdaterFactory;
        $this->watchListUpdater = $watchListUpdater;
    }
    /**
     * @param string $language
     * @param string $label
     * @param string $description
     * @param string[] $aliases
     * @param string $schemaText
     *
     * @return SchemaId id of the inserted Schema
     */
    public function insertSchema(
        $language,
        $label = '',
        $description = '',
        array $aliases = [],
        $schemaText = ''
    ): SchemaId {
        $id = new SchemaId( 'E' . $this->idGenerator->getNewId() );
        $persistentRepresentation = SchemaEncoder::getPersistentRepresentation(
            $id,
            [ $language => $label ],
            [ $language => $description ],
            [ $language => $aliases ],
            $schemaText
        );
        $updater = $this->pageUpdaterFactory->getPageUpdater( $id->getId() );
        $updater->setContent(
            SlotRecord::MAIN,
            new EntitySchemaContent( $persistentRepresentation )
        );
        $schemaConverter = new SchemaConverter();
        $schemaData = $schemaConverter->getMonolingualNameBadgeData(
            $persistentRepresentation,
            $language
        );
        $updater->saveRevision(
            CommentStoreComment::newUnsavedComment(
                '/* ' . self::AUTOCOMMENT_NEWSCHEMA . ' */' . $schemaData->label,
                [
                    'key' => 'entityschema-summary-newschema-nolabel',
                    'language' => $language,
                    'label' => $schemaData->label,
                    'description' => $schemaData->description,
                    'aliases' => $schemaData->aliases,
                    'schemaText_truncated' => $this->truncateSchemaTextForCommentData(
                        $schemaConverter->getSchemaText( $persistentRepresentation )
                    ),
                ]
            ),
            EDIT_NEW | EDIT_INTERNAL
        );
        if ( !$updater->wasSuccessful() ) {
            throw new RuntimeException( 'The revision could not be saved' );
        }
        $this->watchListUpdater->optionallyWatchNewSchema( $id );
        return $id;
    }
    private function truncateSchemaTextForCommentData( $schemaText ) {
        $language = Language::factory( 'en' );
        return $language->truncateForVisual( $schemaText, 5000 );
    }
}