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