ApiProtect.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@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. * @ingroup API
  24. */
  25. class ApiProtect extends ApiBase {
  26. public function execute() {
  27. $params = $this->extractRequestParams();
  28. $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
  29. $titleObj = $pageObj->getTitle();
  30. $this->checkTitleUserPermissions( $titleObj, 'protect' );
  31. $user = $this->getUser();
  32. $tags = $params['tags'];
  33. // Check if user can add tags
  34. if ( !is_null( $tags ) ) {
  35. $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $tags, $user );
  36. if ( !$ableToTag->isOK() ) {
  37. $this->dieStatus( $ableToTag );
  38. }
  39. }
  40. $expiry = (array)$params['expiry'];
  41. if ( count( $expiry ) != count( $params['protections'] ) ) {
  42. if ( count( $expiry ) == 1 ) {
  43. $expiry = array_fill( 0, count( $params['protections'] ), $expiry[0] );
  44. } else {
  45. $this->dieWithError( [
  46. 'apierror-toofewexpiries',
  47. count( $expiry ),
  48. count( $params['protections'] )
  49. ] );
  50. }
  51. }
  52. $restrictionTypes = $titleObj->getRestrictionTypes();
  53. $protections = [];
  54. $expiryarray = [];
  55. $resultProtections = [];
  56. foreach ( $params['protections'] as $i => $prot ) {
  57. $p = explode( '=', $prot );
  58. $protections[$p[0]] = ( $p[1] == 'all' ? '' : $p[1] );
  59. if ( $titleObj->exists() && $p[0] == 'create' ) {
  60. $this->dieWithError( 'apierror-create-titleexists' );
  61. }
  62. if ( !$titleObj->exists() && $p[0] != 'create' ) {
  63. $this->dieWithError( 'apierror-missingtitle-createonly' );
  64. }
  65. if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' ) {
  66. $this->dieWithError( [ 'apierror-protect-invalidaction', wfEscapeWikiText( $p[0] ) ] );
  67. }
  68. if ( !in_array( $p[1], $this->getConfig()->get( 'RestrictionLevels' ) ) && $p[1] != 'all' ) {
  69. $this->dieWithError( [ 'apierror-protect-invalidlevel', wfEscapeWikiText( $p[1] ) ] );
  70. }
  71. if ( wfIsInfinity( $expiry[$i] ) ) {
  72. $expiryarray[$p[0]] = 'infinity';
  73. } else {
  74. $exp = strtotime( $expiry[$i] );
  75. if ( $exp < 0 || !$exp ) {
  76. $this->dieWithError( [ 'apierror-invalidexpiry', wfEscapeWikiText( $expiry[$i] ) ] );
  77. }
  78. $exp = wfTimestamp( TS_MW, $exp );
  79. if ( $exp < wfTimestampNow() ) {
  80. $this->dieWithError( [ 'apierror-pastexpiry', wfEscapeWikiText( $expiry[$i] ) ] );
  81. }
  82. $expiryarray[$p[0]] = $exp;
  83. }
  84. $resultProtections[] = [
  85. $p[0] => $protections[$p[0]],
  86. 'expiry' => ApiResult::formatExpiry( $expiryarray[$p[0]], 'infinite' ),
  87. ];
  88. }
  89. $cascade = $params['cascade'];
  90. $watch = $params['watch'] ? 'watch' : $params['watchlist'];
  91. $this->setWatch( $watch, $titleObj, 'watchdefault' );
  92. $status = $pageObj->doUpdateRestrictions(
  93. $protections,
  94. $expiryarray,
  95. $cascade,
  96. $params['reason'],
  97. $user,
  98. $tags
  99. );
  100. if ( !$status->isOK() ) {
  101. $this->dieStatus( $status );
  102. }
  103. $res = [
  104. 'title' => $titleObj->getPrefixedText(),
  105. 'reason' => $params['reason']
  106. ];
  107. if ( $cascade ) {
  108. $res['cascade'] = true;
  109. }
  110. $res['protections'] = $resultProtections;
  111. $result = $this->getResult();
  112. ApiResult::setIndexedTagName( $res['protections'], 'protection' );
  113. $result->addValue( null, $this->getModuleName(), $res );
  114. }
  115. public function mustBePosted() {
  116. return true;
  117. }
  118. public function isWriteMode() {
  119. return true;
  120. }
  121. public function getAllowedParams() {
  122. return [
  123. 'title' => [
  124. ApiBase::PARAM_TYPE => 'string',
  125. ],
  126. 'pageid' => [
  127. ApiBase::PARAM_TYPE => 'integer',
  128. ],
  129. 'protections' => [
  130. ApiBase::PARAM_ISMULTI => true,
  131. ApiBase::PARAM_REQUIRED => true,
  132. ],
  133. 'expiry' => [
  134. ApiBase::PARAM_ISMULTI => true,
  135. ApiBase::PARAM_ALLOW_DUPLICATES => true,
  136. ApiBase::PARAM_DFLT => 'infinite',
  137. ],
  138. 'reason' => '',
  139. 'tags' => [
  140. ApiBase::PARAM_TYPE => 'tags',
  141. ApiBase::PARAM_ISMULTI => true,
  142. ],
  143. 'cascade' => false,
  144. 'watch' => [
  145. ApiBase::PARAM_DFLT => false,
  146. ApiBase::PARAM_DEPRECATED => true,
  147. ],
  148. 'watchlist' => [
  149. ApiBase::PARAM_DFLT => 'preferences',
  150. ApiBase::PARAM_TYPE => [
  151. 'watch',
  152. 'unwatch',
  153. 'preferences',
  154. 'nochange'
  155. ],
  156. ],
  157. ];
  158. }
  159. public function needsToken() {
  160. return 'csrf';
  161. }
  162. protected function getExamplesMessages() {
  163. return [
  164. 'action=protect&title=Main%20Page&token=123ABC&' .
  165. 'protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never'
  166. => 'apihelp-protect-example-protect',
  167. 'action=protect&title=Main%20Page&token=123ABC&' .
  168. 'protections=edit=all|move=all&reason=Lifting%20restrictions'
  169. => 'apihelp-protect-example-unprotect',
  170. 'action=protect&title=Main%20Page&token=123ABC&' .
  171. 'protections=&reason=Lifting%20restrictions'
  172. => 'apihelp-protect-example-unprotect2',
  173. ];
  174. }
  175. public function getHelpUrls() {
  176. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Protect';
  177. }
  178. }