Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
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 / 37
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 / 1
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    public function __construct(
19        ApiMain $mainModule,
20        string $action,
21        private readonly TranslationCorporaStore $corporaStore,
22        private readonly TranslationStore $translationStore
23    ) {
24        parent::__construct( $mainModule, $action );
25    }
26
27    public function execute() {
28        $params = $this->extractRequestParams();
29        $user = $this->getUser();
30
31        $block = $user->getBlock();
32        if ( $block && $block->isSitewide() ) {
33            $this->dieBlocked( $block );
34        }
35
36        [ 'sourcetitle' => $sourceTitle, 'from' => $sourceLang, 'to' => $targetLang ] = $params;
37        $translation = $this->translationStore->findTranslationByUser( $user, $sourceTitle, $sourceLang, $targetLang );
38
39        if ( $translation === null ) {
40            $this->dieWithError(
41                [ 'apierror-invalidtitle', wfEscapeWikiText( $sourceTitle ) ]
42            );
43        }
44
45        if ( $translation->translation['targetURL'] !== null ) {
46            // Translation was once published. Don't delete, move it to published status.
47            $translation->translation['status'] = TranslationStore::TRANSLATION_STATUS_PUBLISHED;
48            $this->translationStore->updateTranslation( $translation );
49        } else {
50            $translationId = $translation->getData()['id'];
51            $this->translationStore->unlinkTranslationFromTranslator( $translationId );
52            $this->translationStore->deleteTranslation( $translationId );
53            $this->corporaStore->deleteTranslationData( $translationId );
54        }
55
56        $result = [ 'result' => 'success' ];
57        $this->getResult()->addValue( null, $this->getModuleName(), $result );
58    }
59
60    /** @inheritDoc */
61    public function getAllowedParams() {
62        return [
63            'from' => [
64                ParamValidator::PARAM_REQUIRED => true,
65            ],
66            'to' => [
67                ParamValidator::PARAM_REQUIRED => true,
68            ],
69            'sourcetitle' => [
70                ParamValidator::PARAM_REQUIRED => true,
71            ],
72        ];
73    }
74
75    /** @inheritDoc */
76    public function needsToken() {
77        return 'csrf';
78    }
79
80    /** @inheritDoc */
81    public function isWriteMode() {
82        return true;
83    }
84
85    /**
86     * @see ApiBase::getExamplesMessages()
87     * @return array
88     */
89    protected function getExamplesMessages() {
90        return [
91            'action=cxdelete&from=en&to=es&sourcetitle=Food' => 'apihelp-cxdelete-example-1'
92        ];
93    }
94
95}