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