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