ApiSetPageLanguage.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Copyright © 2017 Justin Du "<justin.d128@gmail.com>"
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. /**
  23. * API module that facilitates changing the language of a page.
  24. * The API equivalent of SpecialPageLanguage.
  25. * Requires API write mode to be enabled.
  26. *
  27. * @ingroup API
  28. */
  29. class ApiSetPageLanguage extends ApiBase {
  30. // Check if change language feature is enabled
  31. protected function getExtendedDescription() {
  32. if ( !$this->getConfig()->get( 'PageLanguageUseDB' ) ) {
  33. return 'apihelp-setpagelanguage-extended-description-disabled';
  34. }
  35. return parent::getExtendedDescription();
  36. }
  37. /**
  38. * Extracts the title and language from the request parameters and invokes
  39. * the static SpecialPageLanguage::changePageLanguage() function with these as arguments.
  40. * If the language change succeeds, the title, old language, and new language
  41. * of the article changed, as well as the performer of the language change
  42. * are added to the result object.
  43. */
  44. public function execute() {
  45. // Check if change language feature is enabled
  46. if ( !$this->getConfig()->get( 'PageLanguageUseDB' ) ) {
  47. $this->dieWithError( 'apierror-pagelang-disabled' );
  48. }
  49. // Check if the user has permissions
  50. $this->checkUserRightsAny( 'pagelang' );
  51. $this->useTransactionalTimeLimit();
  52. $params = $this->extractRequestParams();
  53. $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
  54. if ( !$pageObj->exists() ) {
  55. $this->dieWithError( 'apierror-missingtitle' );
  56. }
  57. $titleObj = $pageObj->getTitle();
  58. $user = $this->getUser();
  59. // Check that the user is allowed to edit the page
  60. $this->checkTitleUserPermissions( $titleObj, 'edit' );
  61. // If change tagging was requested, check that the user is allowed to tag,
  62. // and the tags are valid
  63. if ( $params['tags'] ) {
  64. $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
  65. if ( !$tagStatus->isOK() ) {
  66. $this->dieStatus( $tagStatus );
  67. }
  68. }
  69. $status = SpecialPageLanguage::changePageLanguage(
  70. $this,
  71. $titleObj,
  72. $params['lang'],
  73. $params['reason'] ?? '',
  74. $params['tags'] ?: []
  75. );
  76. if ( !$status->isOK() ) {
  77. $this->dieStatus( $status );
  78. }
  79. $r = [
  80. 'title' => $titleObj->getPrefixedText(),
  81. 'oldlanguage' => $status->value->oldLanguage,
  82. 'newlanguage' => $status->value->newLanguage,
  83. 'logid' => $status->value->logId
  84. ];
  85. $this->getResult()->addValue( null, $this->getModuleName(), $r );
  86. }
  87. public function mustBePosted() {
  88. return true;
  89. }
  90. public function isWriteMode() {
  91. return true;
  92. }
  93. public function getAllowedParams() {
  94. return [
  95. 'title' => null,
  96. 'pageid' => [
  97. ApiBase::PARAM_TYPE => 'integer'
  98. ],
  99. 'lang' => [
  100. ApiBase::PARAM_TYPE => array_merge(
  101. [ 'default' ],
  102. array_keys( Language::fetchLanguageNames( null, 'mwfile' ) )
  103. ),
  104. ApiBase::PARAM_REQUIRED => true,
  105. ],
  106. 'reason' => null,
  107. 'tags' => [
  108. ApiBase::PARAM_TYPE => 'tags',
  109. ApiBase::PARAM_ISMULTI => true,
  110. ],
  111. ];
  112. }
  113. public function needsToken() {
  114. return 'csrf';
  115. }
  116. protected function getExamplesMessages() {
  117. return [
  118. 'action=setpagelanguage&title=Main%20Page&lang=eu&token=123ABC'
  119. => 'apihelp-setpagelanguage-example-language',
  120. 'action=setpagelanguage&pageid=123&lang=default&token=123ABC'
  121. => 'apihelp-setpagelanguage-example-default',
  122. ];
  123. }
  124. public function getHelpUrls() {
  125. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:SetPageLanguage';
  126. }
  127. }