Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiContentTranslationDelete
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 6
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
30
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 needsToken
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isWriteMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Delete a translation
4 *
5 * @copyright See AUTHORS.txt
6 * @license GPL-2.0-or-later
7 */
8
9namespace ContentTranslation\ActionApi;
10
11use ContentTranslation\Store\TranslationCorporaStore;
12use ContentTranslation\Store\TranslationStore;
13use MediaWiki\Api\ApiBase;
14use MediaWiki\Api\ApiMain;
15use Wikimedia\ParamValidator\ParamValidator;
16
17class ApiContentTranslationDelete extends ApiBase {
18    private TranslationCorporaStore $corporaStore;
19    private TranslationStore $translationStore;
20
21    public function __construct(
22        ApiMain $mainModule,
23        string $action,
24        TranslationCorporaStore $corporaStore,
25        TranslationStore $translationStore
26    ) {
27        parent::__construct( $mainModule, $action );
28        $this->corporaStore = $corporaStore;
29        $this->translationStore = $translationStore;
30    }
31
32    public function execute() {
33        $params = $this->extractRequestParams();
34        $user = $this->getUser();
35
36        $block = $user->getBlock();
37        if ( $block && $block->isSitewide() ) {
38            $this->dieBlocked( $block );
39        }
40
41        [ 'sourcetitle' => $sourceTitle, 'from' => $sourceLang, 'to' => $targetLang ] = $params;
42        $translation = $this->translationStore->findTranslationByUser( $user, $sourceTitle, $sourceLang, $targetLang );
43
44        if ( $translation === null ) {
45            $this->dieWithError(
46                [ 'apierror-invalidtitle', wfEscapeWikiText( $sourceTitle ) ]
47            );
48        }
49
50        if ( $translation->translation['targetURL'] !== null ) {
51            // Translation was once published. Don't delete, move it to published status.
52            $translation->translation['status'] = TranslationStore::TRANSLATION_STATUS_PUBLISHED;
53            $this->translationStore->updateTranslation( $translation );
54        } else {
55            $translationId = $translation->getData()['id'];
56            $this->translationStore->unlinkTranslationFromTranslator( $translationId );
57            $this->translationStore->deleteTranslation( $translationId );
58            $this->corporaStore->deleteTranslationData( $translationId );
59        }
60
61        $result = [ 'result' => 'success' ];
62        $this->getResult()->addValue( null, $this->getModuleName(), $result );
63    }
64
65    /** @inheritDoc */
66    public function getAllowedParams() {
67        return [
68            'from' => [
69                ParamValidator::PARAM_REQUIRED => true,
70            ],
71            'to' => [
72                ParamValidator::PARAM_REQUIRED => true,
73            ],
74            'sourcetitle' => [
75                ParamValidator::PARAM_REQUIRED => true,
76            ],
77        ];
78    }
79
80    /** @inheritDoc */
81    public function needsToken() {
82        return 'csrf';
83    }
84
85    /** @inheritDoc */
86    public function isWriteMode() {
87        return true;
88    }
89
90    /**
91     * @see ApiBase::getExamplesMessages()
92     * @return array
93     */
94    protected function getExamplesMessages() {
95        return [
96            'action=cxdelete&from=en&to=es&sourcetitle=Food' => 'apihelp-cxdelete-example-1'
97        ];
98    }
99
100}