ApiProtect.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /*
  3. * Created on Sep 1, 2007
  4. * API for MediaWiki 1.8+
  5. *
  6. * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. * http://www.gnu.org/copyleft/gpl.html
  22. */
  23. if (!defined('MEDIAWIKI')) {
  24. // Eclipse helper - will be ignored in production
  25. require_once ("ApiBase.php");
  26. }
  27. /**
  28. * @ingroup API
  29. */
  30. class ApiProtect extends ApiBase {
  31. public function __construct($main, $action) {
  32. parent :: __construct($main, $action);
  33. }
  34. public function execute() {
  35. global $wgUser, $wgRestrictionTypes, $wgRestrictionLevels;
  36. $params = $this->extractRequestParams();
  37. $titleObj = NULL;
  38. if(!isset($params['title']))
  39. $this->dieUsageMsg(array('missingparam', 'title'));
  40. if(!isset($params['token']))
  41. $this->dieUsageMsg(array('missingparam', 'token'));
  42. if(empty($params['protections']))
  43. $this->dieUsageMsg(array('missingparam', 'protections'));
  44. if(!$wgUser->matchEditToken($params['token']))
  45. $this->dieUsageMsg(array('sessionfailure'));
  46. $titleObj = Title::newFromText($params['title']);
  47. if(!$titleObj)
  48. $this->dieUsageMsg(array('invalidtitle', $params['title']));
  49. $errors = $titleObj->getUserPermissionsErrors('protect', $wgUser);
  50. if($errors)
  51. // We don't care about multiple errors, just report one of them
  52. $this->dieUsageMsg(reset($errors));
  53. $expiry = (array)$params['expiry'];
  54. if(count($expiry) != count($params['protections']))
  55. {
  56. if(count($expiry) == 1)
  57. $expiry = array_fill(0, count($params['protections']), $expiry[0]);
  58. else
  59. $this->dieUsageMsg(array('toofewexpiries', count($expiry), count($params['protections'])));
  60. }
  61. $protections = array();
  62. $expiryarray = array();
  63. $resultProtections = array();
  64. foreach($params['protections'] as $i => $prot)
  65. {
  66. $p = explode('=', $prot);
  67. $protections[$p[0]] = ($p[1] == 'all' ? '' : $p[1]);
  68. if($titleObj->exists() && $p[0] == 'create')
  69. $this->dieUsageMsg(array('create-titleexists'));
  70. if(!$titleObj->exists() && $p[0] != 'create')
  71. $this->dieUsageMsg(array('missingtitles-createonly'));
  72. if(!in_array($p[0], $wgRestrictionTypes) && $p[0] != 'create')
  73. $this->dieUsageMsg(array('protect-invalidaction', $p[0]));
  74. if(!in_array($p[1], $wgRestrictionLevels) && $p[1] != 'all')
  75. $this->dieUsageMsg(array('protect-invalidlevel', $p[1]));
  76. if(in_array($expiry[$i], array('infinite', 'indefinite', 'never')))
  77. $expiryarray[$p[0]] = Block::infinity();
  78. else
  79. {
  80. $exp = strtotime($expiry[$i]);
  81. if($exp < 0 || $exp == false)
  82. $this->dieUsageMsg(array('invalidexpiry', $expiry[$i]));
  83. $exp = wfTimestamp(TS_MW, $exp);
  84. if($exp < wfTimestampNow())
  85. $this->dieUsageMsg(array('pastexpiry', $expiry[$i]));
  86. $expiryarray[$p[0]] = $exp;
  87. }
  88. $resultProtections[] = array($p[0] => $protections[$p[0]],
  89. 'expiry' => ($expiryarray[$p[0]] == Block::infinity() ?
  90. 'infinite' :
  91. wfTimestamp(TS_ISO_8601, $expiryarray[$p[0]])));
  92. }
  93. $cascade = $params['cascade'];
  94. $articleObj = new Article($titleObj);
  95. if($params['watch'])
  96. $articleObj->doWatch();
  97. if($titleObj->exists())
  98. $ok = $articleObj->updateRestrictions($protections, $params['reason'], $cascade, $expiryarray);
  99. else
  100. $ok = $titleObj->updateTitleProtection($protections['create'], $params['reason'], $expiryarray['create']);
  101. if(!$ok)
  102. // This is very weird. Maybe the article was deleted or the user was blocked/desysopped in the meantime?
  103. // Just throw an unknown error in this case, as it's very likely to be a race condition
  104. $this->dieUsageMsg(array());
  105. $res = array('title' => $titleObj->getPrefixedText(), 'reason' => $params['reason']);
  106. if($cascade)
  107. $res['cascade'] = '';
  108. $res['protections'] = $resultProtections;
  109. $this->getResult()->setIndexedTagName($res['protections'], 'protection');
  110. $this->getResult()->addValue(null, $this->getModuleName(), $res);
  111. }
  112. public function mustBePosted() { return true; }
  113. public function isWriteMode() {
  114. return true;
  115. }
  116. public function getAllowedParams() {
  117. return array (
  118. 'title' => null,
  119. 'token' => null,
  120. 'protections' => array(
  121. ApiBase :: PARAM_ISMULTI => true
  122. ),
  123. 'expiry' => array(
  124. ApiBase :: PARAM_ISMULTI => true,
  125. ApiBase :: PARAM_ALLOW_DUPLICATES => true,
  126. ApiBase :: PARAM_DFLT => 'infinite',
  127. ),
  128. 'reason' => '',
  129. 'cascade' => false,
  130. 'watch' => false,
  131. );
  132. }
  133. public function getParamDescription() {
  134. return array (
  135. 'title' => 'Title of the page you want to (un)protect.',
  136. 'token' => 'A protect token previously retrieved through prop=info',
  137. 'protections' => 'Pipe-separated list of protection levels, formatted action=group (e.g. edit=sysop)',
  138. 'expiry' => array('Expiry timestamps. If only one timestamp is set, it\'ll be used for all protections.',
  139. 'Use \'infinite\', \'indefinite\' or \'never\', for a neverexpiring protection.'),
  140. 'reason' => 'Reason for (un)protecting (optional)',
  141. 'cascade' => array('Enable cascading protection (i.e. protect pages included in this page)',
  142. 'Ignored if not all protection levels are \'sysop\' or \'protect\''),
  143. 'watch' => 'If set, add the page being (un)protected to your watchlist',
  144. );
  145. }
  146. public function getDescription() {
  147. return array(
  148. 'Change the protection level of a page.'
  149. );
  150. }
  151. protected function getExamples() {
  152. return array (
  153. 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=sysop|move=sysop&cascade&expiry=20070901163000|never',
  154. 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=all|move=all&reason=Lifting%20restrictions'
  155. );
  156. }
  157. public function getVersion() {
  158. return __CLASS__ . ': $Id: ApiProtect.php 48122 2009-03-07 12:58:41Z catrope $';
  159. }
  160. }