Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 70
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiSetPageLanguage
0.00% covered (danger)
0.00%
0 / 69
0.00% covered (danger)
0.00%
0 / 9
272
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
 getExtendedDescription
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 execute
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
56
 mustBePosted
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
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 21
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
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Copyright © 2017 Justin Du "<justin.d128@gmail.com>"
4 *
5 * @license GPL-2.0-or-later
6 * @file
7 */
8
9namespace MediaWiki\Api;
10
11use MediaWiki\ChangeTags\ChangeTags;
12use MediaWiki\Language\LanguageNameUtils;
13use MediaWiki\MainConfigNames;
14use MediaWiki\Specials\SpecialPageLanguage;
15use MediaWiki\Title\Title;
16use Wikimedia\ParamValidator\ParamValidator;
17use Wikimedia\Rdbms\IConnectionProvider;
18
19/**
20 * API module that facilitates changing the language of a page.
21 * The API equivalent of SpecialPageLanguage.
22 * Requires API write mode to be enabled.
23 *
24 * @ingroup API
25 */
26class ApiSetPageLanguage extends ApiBase {
27
28    public function __construct(
29        ApiMain $mainModule,
30        string $moduleName,
31        private readonly IConnectionProvider $dbProvider,
32        private readonly LanguageNameUtils $languageNameUtils,
33    ) {
34        parent::__construct( $mainModule, $moduleName );
35    }
36
37    /** @inheritDoc */
38    protected function getExtendedDescription() {
39        // Check if change language feature is enabled
40        if ( !$this->getConfig()->get( MainConfigNames::PageLanguageUseDB ) ) {
41            return 'apihelp-setpagelanguage-extended-description-disabled';
42        }
43        return parent::getExtendedDescription();
44    }
45
46    /**
47     * Extracts the title and language from the request parameters and invokes
48     * the static SpecialPageLanguage::changePageLanguage() function with these as arguments.
49     * If the language change succeeds, the title, old language, and new language
50     * of the article changed, as well as the performer of the language change
51     * are added to the result object.
52     */
53    public function execute() {
54        // Check if change language feature is enabled
55        if ( !$this->getConfig()->get( MainConfigNames::PageLanguageUseDB ) ) {
56            $this->dieWithError( 'apierror-pagelang-disabled' );
57        }
58
59        // Check if the user has permissions
60        $this->checkUserRightsAny( 'pagelang' );
61
62        $this->useTransactionalTimeLimit();
63
64        $params = $this->extractRequestParams();
65
66        $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
67        $titleObj = $pageObj->getTitle();
68        $this->getErrorFormatter()->setContextTitle( $titleObj );
69        if ( !$pageObj->exists() ) {
70            $this->dieWithError( 'apierror-missingtitle' );
71        }
72
73        // Check that the user is allowed to edit the page
74        $this->checkTitleUserPermissions( $titleObj, 'edit' );
75
76        // If change tagging was requested, check that the user is allowed to tag,
77        // and the tags are valid
78        if ( $params['tags'] ) {
79            $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() );
80            if ( !$tagStatus->isOK() ) {
81                $this->dieStatus( $tagStatus );
82            }
83        }
84
85        $status = SpecialPageLanguage::changePageLanguage(
86            $this,
87            $titleObj,
88            $params['lang'],
89            $params['reason'] ?? '',
90            $params['tags'] ?: [],
91            $this->dbProvider->getPrimaryDatabase()
92        );
93
94        if ( !$status->isOK() ) {
95            $this->dieStatus( $status );
96        }
97
98        $r = [
99            'title' => $titleObj->getPrefixedText(),
100            'oldlanguage' => $status->value->oldLanguage,
101            'newlanguage' => $status->value->newLanguage,
102            'logid' => $status->value->logId
103        ];
104        $this->getResult()->addValue( null, $this->getModuleName(), $r );
105    }
106
107    /** @inheritDoc */
108    public function mustBePosted() {
109        return true;
110    }
111
112    /** @inheritDoc */
113    public function isWriteMode() {
114        return true;
115    }
116
117    /** @inheritDoc */
118    public function getAllowedParams() {
119        return [
120            'title' => null,
121            'pageid' => [
122                ParamValidator::PARAM_TYPE => 'integer'
123            ],
124            'lang' => [
125                ParamValidator::PARAM_TYPE => [
126                    'default',
127                    ...array_keys( $this->languageNameUtils->getLanguageNames(
128                        LanguageNameUtils::AUTONYMS,
129                        LanguageNameUtils::SUPPORTED
130                    ) )
131                ],
132                ParamValidator::PARAM_REQUIRED => true,
133            ],
134            'reason' => null,
135            'tags' => [
136                ParamValidator::PARAM_TYPE => 'tags',
137                ParamValidator::PARAM_ISMULTI => true,
138            ],
139        ];
140    }
141
142    /** @inheritDoc */
143    public function needsToken() {
144        return 'csrf';
145    }
146
147    /** @inheritDoc */
148    protected function getExamplesMessages() {
149        $title = Title::newMainPage()->getPrefixedText();
150        $mp = rawurlencode( $title );
151
152        return [
153            "action=setpagelanguage&title={$mp}&lang=eu&token=123ABC"
154                => 'apihelp-setpagelanguage-example-language',
155            'action=setpagelanguage&pageid=123&lang=default&token=123ABC'
156                => 'apihelp-setpagelanguage-example-default',
157        ];
158    }
159
160    /** @inheritDoc */
161    public function getHelpUrls() {
162        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:SetPageLanguage';
163    }
164}
165
166/** @deprecated class alias since 1.43 */
167class_alias( ApiSetPageLanguage::class, 'ApiSetPageLanguage' );