Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiContentTranslationSplit
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 7
156
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
 validateRequest
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
30
 execute
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 6
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
 isInternal
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2declare( strict_types = 1 );
3
4namespace ContentTranslation\ActionApi;
5
6use ContentTranslation\Service\TranslationSplitter;
7use ContentTranslation\Store\SectionTranslationStore;
8use ContentTranslation\Store\TranslationStore;
9use MediaWiki\Api\ApiBase;
10use MediaWiki\Api\ApiMain;
11use MediaWiki\Api\ApiUsageException;
12use Wikimedia\ParamValidator\ParamValidator;
13use Wikimedia\Rdbms\IConnectionProvider;
14
15/**
16 * Action API module that is used to split a content translation
17 * into section translations.
18 *
19 * @author Nik Gkountas
20 * @license GPL-2.0-or-later
21 * @since 2024.01
22 */
23class ApiContentTranslationSplit extends ApiBase {
24    public function __construct(
25        ApiMain $mainModule,
26        string $action,
27        private readonly IConnectionProvider $connectionProvider,
28        private readonly TranslationSplitter $translationSplitter,
29        private readonly TranslationStore $translationStore,
30        private readonly SectionTranslationStore $sectionTranslationStore
31    ) {
32        parent::__construct( $mainModule, $action );
33    }
34
35    /**
36     * @return void
37     * @throws ApiUsageException
38     */
39    private function validateRequest(): void {
40        if ( $this->connectionProvider->getPrimaryDatabase()->isReadOnly() ) {
41            $this->dieReadOnly();
42        }
43
44        $user = $this->getUser();
45
46        if ( !$user->isNamed() ) {
47            $this->dieWithError( 'apierror-cxsplit-anon-user' );
48        }
49
50        $block = $user->getBlock();
51        if ( $block && $block->isSitewide() ) {
52            $this->dieBlocked( $block );
53        }
54    }
55
56    /**
57     * @throws ApiUsageException
58     */
59    public function execute() {
60        $this->validateRequest();
61
62        $params = $this->extractRequestParams();
63        $translationId = $params['translationid'];
64        $user = $this->getUser();
65
66        $translation = $this->translationStore->findByUserAndId( $user, $translationId );
67
68        if ( !$translation ) {
69            $this->dieWithError( 'apierror-cxsplit-no-translation-found' );
70        }
71        $newSectionTranslations = $this->translationSplitter->splitIntoSectionTranslations(
72            $translation
73        );
74        $this->sectionTranslationStore->insertMultipleTranslations( $newSectionTranslations );
75
76        $result = [ 'result' => 'success' ];
77        $this->getResult()->addValue( null, $this->getModuleName(), $result );
78    }
79
80    /** @inheritDoc */
81    public function getAllowedParams() {
82        return [
83            'translationid' => [
84                ParamValidator::PARAM_TYPE => 'integer',
85                ParamValidator::PARAM_REQUIRED => true,
86            ]
87        ];
88    }
89
90    /** @inheritDoc */
91    public function needsToken() {
92        return 'csrf';
93    }
94
95    /** @inheritDoc */
96    public function isWriteMode() {
97        return true;
98    }
99
100    /** @inheritDoc */
101    public function isInternal() {
102        return true;
103    }
104}