00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 if (!defined('MEDIAWIKI')) {
00026
00027 require_once ("ApiBase.php");
00028 }
00029
00033 class ApiProtect extends ApiBase {
00034
00035 public function __construct($main, $action) {
00036 parent :: __construct($main, $action);
00037 }
00038
00039 public function execute() {
00040 global $wgUser, $wgRestrictionTypes, $wgRestrictionLevels;
00041 $params = $this->extractRequestParams();
00042
00043 $titleObj = NULL;
00044 if(!isset($params['title']))
00045 $this->dieUsageMsg(array('missingparam', 'title'));
00046 if(!isset($params['token']))
00047 $this->dieUsageMsg(array('missingparam', 'token'));
00048 if(empty($params['protections']))
00049 $this->dieUsageMsg(array('missingparam', 'protections'));
00050
00051 if(!$wgUser->matchEditToken($params['token']))
00052 $this->dieUsageMsg(array('sessionfailure'));
00053
00054 $titleObj = Title::newFromText($params['title']);
00055 if(!$titleObj)
00056 $this->dieUsageMsg(array('invalidtitle', $params['title']));
00057
00058 $errors = $titleObj->getUserPermissionsErrors('protect', $wgUser);
00059 if($errors)
00060
00061 $this->dieUsageMsg(reset($errors));
00062
00063 $expiry = (array)$params['expiry'];
00064 if(count($expiry) != count($params['protections']))
00065 {
00066 if(count($expiry) == 1)
00067 $expiry = array_fill(0, count($params['protections']), $expiry[0]);
00068 else
00069 $this->dieUsageMsg(array('toofewexpiries', count($expiry), count($params['protections'])));
00070 }
00071
00072 $protections = array();
00073 $expiryarray = array();
00074 $resultProtections = array();
00075 foreach($params['protections'] as $i => $prot)
00076 {
00077 $p = explode('=', $prot);
00078 $protections[$p[0]] = ($p[1] == 'all' ? '' : $p[1]);
00079 if($titleObj->exists() && $p[0] == 'create')
00080 $this->dieUsageMsg(array('create-titleexists'));
00081 if(!$titleObj->exists() && $p[0] != 'create')
00082 $this->dieUsageMsg(array('missingtitles-createonly'));
00083 if(!in_array($p[0], $wgRestrictionTypes) && $p[0] != 'create')
00084 $this->dieUsageMsg(array('protect-invalidaction', $p[0]));
00085 if(!in_array($p[1], $wgRestrictionLevels) && $p[1] != 'all')
00086 $this->dieUsageMsg(array('protect-invalidlevel', $p[1]));
00087
00088 if(in_array($expiry[$i], array('infinite', 'indefinite', 'never')))
00089 $expiryarray[$p[0]] = Block::infinity();
00090 else
00091 {
00092 $exp = strtotime($expiry[$i]);
00093 if($exp < 0 || $exp == false)
00094 $this->dieUsageMsg(array('invalidexpiry', $expiry[$i]));
00095
00096 $exp = wfTimestamp(TS_MW, $exp);
00097 if($exp < wfTimestampNow())
00098 $this->dieUsageMsg(array('pastexpiry', $expiry[$i]));
00099 $expiryarray[$p[0]] = $exp;
00100 }
00101 $resultProtections[] = array($p[0] => $protections[$p[0]],
00102 'expiry' => ($expiryarray[$p[0]] == Block::infinity() ?
00103 'infinite' :
00104 wfTimestamp(TS_ISO_8601, $expiryarray[$p[0]])));
00105 }
00106
00107 $cascade = $params['cascade'];
00108 $articleObj = new Article($titleObj);
00109 if($params['watch'])
00110 $articleObj->doWatch();
00111 if($titleObj->exists())
00112 $ok = $articleObj->updateRestrictions($protections, $params['reason'], $cascade, $expiryarray);
00113 else
00114 $ok = $titleObj->updateTitleProtection($protections['create'], $params['reason'], $expiryarray['create']);
00115 if(!$ok)
00116
00117
00118 $this->dieUsageMsg(array());
00119 $res = array('title' => $titleObj->getPrefixedText(), 'reason' => $params['reason']);
00120 if($cascade)
00121 $res['cascade'] = '';
00122 $res['protections'] = $resultProtections;
00123 $this->getResult()->setIndexedTagName($res['protections'], 'protection');
00124 $this->getResult()->addValue(null, $this->getModuleName(), $res);
00125 }
00126
00127 public function mustBePosted() { return true; }
00128
00129 public function isWriteMode() {
00130 return true;
00131 }
00132
00133 public function getAllowedParams() {
00134 return array (
00135 'title' => null,
00136 'token' => null,
00137 'protections' => array(
00138 ApiBase :: PARAM_ISMULTI => true
00139 ),
00140 'expiry' => array(
00141 ApiBase :: PARAM_ISMULTI => true,
00142 ApiBase :: PARAM_ALLOW_DUPLICATES => true,
00143 ApiBase :: PARAM_DFLT => 'infinite',
00144 ),
00145 'reason' => '',
00146 'cascade' => false,
00147 'watch' => false,
00148 );
00149 }
00150
00151 public function getParamDescription() {
00152 return array (
00153 'title' => 'Title of the page you want to (un)protect.',
00154 'token' => 'A protect token previously retrieved through prop=info',
00155 'protections' => 'Pipe-separated list of protection levels, formatted action=group (e.g. edit=sysop)',
00156 'expiry' => array('Expiry timestamps. If only one timestamp is set, it\'ll be used for all protections.',
00157 'Use \'infinite\', \'indefinite\' or \'never\', for a neverexpiring protection.'),
00158 'reason' => 'Reason for (un)protecting (optional)',
00159 'cascade' => array('Enable cascading protection (i.e. protect pages included in this page)',
00160 'Ignored if not all protection levels are \'sysop\' or \'protect\''),
00161 'watch' => 'If set, add the page being (un)protected to your watchlist',
00162 );
00163 }
00164
00165 public function getDescription() {
00166 return array(
00167 'Change the protection level of a page.'
00168 );
00169 }
00170
00171 protected function getExamples() {
00172 return array (
00173 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=sysop|move=sysop&cascade&expiry=20070901163000|never',
00174 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=all|move=all&reason=Lifting%20restrictions'
00175 );
00176 }
00177
00178 public function getVersion() {
00179 return __CLASS__ . ': $Id: ApiProtect.php 48122 2009-03-07 12:58:41Z catrope $';
00180 }
00181 }