Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiSectionTranslationDelete
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 6
90
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 / 15
0.00% covered (danger)
0.00%
0 / 1
20
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 14
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 section translation
4 *
5 * @copyright See AUTHORS.txt
6 * @license GPL-2.0-or-later
7 */
8
9namespace ContentTranslation\ActionApi;
10
11use ContentTranslation\Store\SectionTranslationStore;
12use ContentTranslation\Store\TranslationCorporaStore;
13use ContentTranslation\Store\TranslationStore;
14use MediaWiki\Api\ApiBase;
15use MediaWiki\Api\ApiMain;
16use Wikimedia\ParamValidator\ParamValidator;
17
18class ApiSectionTranslationDelete extends ApiBase {
19    public function __construct(
20        ApiMain $mainModule,
21        string $action,
22        private readonly TranslationCorporaStore $corporaStore,
23        private readonly SectionTranslationStore $sectionTranslationStore,
24        private readonly TranslationStore $translationStore
25    ) {
26        parent::__construct( $mainModule, $action );
27    }
28
29    public function execute() {
30        $params = $this->extractRequestParams();
31        $user = $this->getUser();
32
33        $block = $user->getBlock();
34        if ( $block && $block->isSitewide() ) {
35            $this->dieBlocked( $block );
36        }
37
38        $sectionTranslationId = $params['sectiontranslationid'];
39        $translationId = $params['translationid'];
40        $sectionId = $params['sectionid'];
41        // delete all corpora translation units associated with this draft section translation
42        $this->corporaStore->deleteTranslationDataBySectionId( $translationId, $sectionId );
43        // delete the section translation from the database
44        $this->sectionTranslationStore->deleteTranslationById( $sectionTranslationId );
45
46        // if no other corpora units are associated with the "parent" translation id, update the
47        // status of the "parent" translation to "deleted" and remove the association with the
48        // current translator from inside the "cx_translators" table
49        if ( !$this->corporaStore->countByTranslationId( $translationId ) ) {
50            $this->translationStore->unlinkTranslationFromTranslator( $translationId );
51            $this->translationStore->deleteTranslation( $translationId );
52        }
53
54        $result = [ 'result' => 'success' ];
55        $this->getResult()->addValue( null, $this->getModuleName(), $result );
56    }
57
58    /** @inheritDoc */
59    public function getAllowedParams() {
60        return [
61            'sectiontranslationid' => [
62                ParamValidator::PARAM_TYPE => 'integer',
63                ParamValidator::PARAM_REQUIRED => true,
64            ],
65            'translationid' => [
66                ParamValidator::PARAM_TYPE => 'integer',
67                ParamValidator::PARAM_REQUIRED => true,
68            ],
69            'sectionid' => [
70                ParamValidator::PARAM_TYPE => 'string',
71                ParamValidator::PARAM_REQUIRED => true,
72            ],
73        ];
74    }
75
76    /** @inheritDoc */
77    public function needsToken() {
78        return 'csrf';
79    }
80
81    /** @inheritDoc */
82    public function isWriteMode() {
83        return true;
84    }
85
86    /**
87     * @see ApiBase::getExamplesMessages()
88     * @return array
89     */
90    protected function getExamplesMessages() {
91        return [
92            'action=sxdelete&translationid=1&sectionid=100_20' => 'apihelp-sxdelete-example-1'
93        ];
94    }
95
96}