wikimedia/mediawiki-extensions-Wikibase

View on GitHub
repo/includes/Api/RemoveClaims.php

Summary

Maintainability
C
1 day
Test Coverage
<?php

declare( strict_types = 1 );

namespace Wikibase\Repo\Api;

use MediaWiki\Api\ApiBase;
use MediaWiki\Api\ApiCreateTempUserTrait;
use MediaWiki\Api\ApiMain;
use Wikibase\DataModel\Entity\EntityId;
use Wikibase\DataModel\Entity\EntityIdParser;
use Wikibase\DataModel\Services\Statement\StatementGuidParser;
use Wikibase\DataModel\Services\Statement\StatementGuidValidator;
use Wikibase\DataModel\Statement\Statement;
use Wikibase\DataModel\Statement\StatementList;
use Wikibase\DataModel\Statement\StatementListProvider;
use Wikibase\Lib\SettingsArray;
use Wikibase\Repo\ChangeOp\ChangeOp;
use Wikibase\Repo\ChangeOp\ChangeOpException;
use Wikibase\Repo\ChangeOp\ChangeOpFactoryProvider;
use Wikibase\Repo\ChangeOp\ChangeOps;
use Wikibase\Repo\ChangeOp\StatementChangeOpFactory;
use Wikibase\Repo\SnakFactory;
use Wikimedia\ParamValidator\ParamValidator;

/**
 * API module for removing claims.
 *
 * @license GPL-2.0-or-later
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 * @author Tobias Gritschacher < tobias.gritschacher@wikimedia.de >
 */
class RemoveClaims extends ApiBase {

    use FederatedPropertyApiValidatorTrait;
    use ApiCreateTempUserTrait;

    /**
     * @var StatementChangeOpFactory
     */
    private $statementChangeOpFactory;

    /**
     * @var ApiErrorReporter
     */
    protected $errorReporter;

    /**
     * @var StatementModificationHelper
     */
    private $modificationHelper;

    /**
     * @var StatementGuidParser
     */
    private $guidParser;

    /**
     * @var ResultBuilder
     */
    private $resultBuilder;

    /**
     * @var EntitySavingHelper
     */
    private $entitySavingHelper;

    /**
     * @var string[]
     */
    private $sandboxEntityIds;

    public function __construct(
        ApiMain $mainModule,
        string $moduleName,
        ApiErrorReporter $errorReporter,
        StatementChangeOpFactory $statementChangeOpFactory,
        StatementModificationHelper $modificationHelper,
        StatementGuidParser $guidParser,
        callable $resultBuilderInstantiator,
        callable $entitySavingHelperInstantiator,
        bool $federatedPropertiesEnabled,
        array $sandboxEntityIds
    ) {
        parent::__construct( $mainModule, $moduleName );

        $this->errorReporter = $errorReporter;
        $this->statementChangeOpFactory = $statementChangeOpFactory;
        $this->modificationHelper = $modificationHelper;

        $this->guidParser = $guidParser;
        $this->resultBuilder = $resultBuilderInstantiator( $this );
        $this->entitySavingHelper = $entitySavingHelperInstantiator( $this );
        $this->federatedPropertiesEnabled = $federatedPropertiesEnabled;
        $this->sandboxEntityIds = $sandboxEntityIds;
    }

    public static function factory(
        ApiMain $mainModule,
        string $moduleName,
        ApiHelperFactory $apiHelperFactory,
        ChangeOpFactoryProvider $changeOpFactoryProvider,
        EntityIdParser $entityIdParser,
        SettingsArray $settings,
        SnakFactory $snakFactory,
        StatementGuidParser $statementGuidParser,
        StatementGuidValidator $statementGuidValidator
    ): self {
        $modificationHelper = new StatementModificationHelper(
            $snakFactory,
            $entityIdParser,
            $statementGuidValidator,
            $apiHelperFactory->getErrorReporter( $mainModule )
        );

        return new self(
            $mainModule,
            $moduleName,
            $apiHelperFactory->getErrorReporter( $mainModule ),
            $changeOpFactoryProvider->getStatementChangeOpFactory(),
            $modificationHelper,
            $statementGuidParser,
            function ( $module ) use ( $apiHelperFactory ) {
                return $apiHelperFactory->getResultBuilder( $module );
            },
            function ( $module ) use ( $apiHelperFactory ) {
                return $apiHelperFactory->getEntitySavingHelper( $module );
            },
            $settings->getSetting( 'federatedPropertiesEnabled' ),
            $settings->getSetting( 'sandboxEntityIds' )
        );
    }

    /**
     * @inheritDoc
     */
    public function execute(): void {
        $params = $this->extractRequestParams();
        $entityId = $this->getEntityId( $params );

        $this->validateAlteringEntityById( $entityId );

        $entity = $this->entitySavingHelper->loadEntity( $params, $entityId );

        if ( $entity instanceof StatementListProvider ) {
            $this->assertStatementListContainsGuids( $entity->getStatements(), $params['claim'] );
        }

        $summary = $this->modificationHelper->createSummary( $params, $this );

        $changeOps = new ChangeOps();
        $changeOps->add( $this->getChangeOps( $params ) );

        try {
            $changeOps->apply( $entity, $summary );
        } catch ( ChangeOpException $e ) {
            $this->errorReporter->dieException( $e, 'failed-save' );
        }

        $status = $this->entitySavingHelper->attemptSaveEntity( $entity, $summary, $params, $this->getContext() );
        $this->resultBuilder->addRevisionIdFromStatusToResult( $status, 'pageinfo' );
        $this->resultBuilder->markSuccess();
        $this->resultBuilder->setList( null, 'claims', $params['claim'], 'claim' );
        $this->resultBuilder->addTempUser( $status, fn( $user ) => $this->getTempUserRedirectUrl( $params, $user ) );
    }

    /**
     * Validates the parameters and returns the EntityId to act upon on success
     *
     * @param array $params
     *
     * @return EntityId
     */
    private function getEntityId( array $params ): EntityId {
        $entityId = null;

        foreach ( $params['claim'] as $guid ) {
            if ( !$this->modificationHelper->validateStatementGuid( $guid ) ) {
                $this->errorReporter->dieError( "Invalid claim guid $guid", 'invalid-guid' );
            }

            if ( $entityId === null ) {
                $entityId = $this->guidParser->parse( $guid )->getEntityId();
            } else {
                if ( !$this->guidParser->parse( $guid )->getEntityId()->equals( $entityId ) ) {
                    $this->errorReporter->dieError( 'All claims must belong to the same entity', 'invalid-guid' );
                }
            }
        }

        if ( $entityId === null ) {
            $this->errorReporter->dieError( 'Could not find an entity for the claims', 'invalid-guid' );
        }

        return $entityId;
    }

    /**
     * @param StatementList $statements
     * @param string[] $requiredGuids
     */
    private function assertStatementListContainsGuids( StatementList $statements, array $requiredGuids ): void {
        $existingGuids = [];

        /** @var Statement $statement */
        foreach ( $statements as $statement ) {
            $guid = $statement->getGuid();
            // This array is used as a HashSet where only the keys are used.
            $existingGuids[$guid] = null;
        }

        // Not using array_diff but array_diff_key does have a huge performance impact.
        $missingGuids = array_diff_key( array_flip( $requiredGuids ), $existingGuids );

        if ( $missingGuids ) {
            $this->errorReporter->dieError(
                'Statement(s) with GUID(s) ' . implode( ', ', array_keys( $missingGuids ) ) . ' not found',
                'invalid-guid'
            );
        }
    }

    /**
     * @param array $params
     *
     * @return ChangeOp[]
     */
    private function getChangeOps( array $params ): array {
        $changeOps = [];

        foreach ( $params['claim'] as $guid ) {
            $changeOps[] = $this->statementChangeOpFactory->newRemoveStatementOp( $guid );
        }

        return $changeOps;
    }

    /**
     * @inheritDoc
     */
    public function isWriteMode(): bool {
        return true;
    }

    /**
     * @see ApiBase::needsToken
     *
     * @return string
     */
    public function needsToken(): string {
        return 'csrf';
    }

    /**
     * @inheritDoc
     */
    protected function getAllowedParams(): array {
        return array_merge(
            [
                'claim' => [
                    ParamValidator::PARAM_TYPE => 'string',
                    ParamValidator::PARAM_ISMULTI => true,
                    ParamValidator::PARAM_REQUIRED => true,
                ],
                'summary' => [
                    ParamValidator::PARAM_TYPE => 'string',
                ],
                'tags' => [
                    ParamValidator::PARAM_TYPE => 'tags',
                    ParamValidator::PARAM_ISMULTI => true,
                ],
                'token' => null,
                'baserevid' => [
                    ParamValidator::PARAM_TYPE => 'integer',
                ],
                'bot' => false,
            ],
            $this->getCreateTempUserParams(),
            parent::getAllowedParams()
        );
    }

    /**
     * @inheritDoc
     */
    protected function getExamplesMessages(): array {
        $guid = $this->sandboxEntityIds[ 'mainItem' ] . '$D8404CDA-25E4-4334-AF13-A3290BCD9C0N';

        return [
            'action=wbremoveclaims&claim=' . $guid . '&token=foobar'
                . '&baserevid=7201010'
                => [ 'apihelp-wbremoveclaims-example-1', $guid ],
        ];
    }

}