API: Changing all modules' getParamDescription(), getAllowedParams() and getDescripti...
[lhc/web/wiklou.git] / includes / api / ApiProtect.php
index 91fa57d..bd7fa90 100644 (file)
@@ -43,27 +43,23 @@ class ApiProtect extends ApiBase {
                
                $titleObj = NULL;
                if(!isset($params['title']))
-                       $this->dieUsage('The title parameter must be set', 'notitle');
+                       $this->dieUsageMsg(array('missingparam', 'title'));
                if(!isset($params['token']))
-                       $this->dieUsage('The token parameter must be set', 'notoken');
+                       $this->dieUsageMsg(array('missingparam', 'token'));
                if(!isset($params['protections']) || empty($params['protections']))
-                       $this->dieUsage('The protections parameter must be set', 'noprotections');
+                       $this->dieUsageMsg(array('missingparam', 'protections'));
 
-               if($wgUser->isBlocked())
-                       $this->dieUsage('You have been blocked from editing', 'blocked');
-               if(wfReadOnly())
-                       $this->dieUsage('The wiki is in read-only mode', 'readonly');
                if(!$wgUser->matchEditToken($params['token']))
-                       $this->dieUsage('Invalid token', 'badtoken');
+                       $this->dieUsageMsg(array('sessionfailure'));
 
                $titleObj = Title::newFromText($params['title']);
                if(!$titleObj)
-                       $this->dieUsage("Bad title ``{$params['title']}''", 'invalidtitle');
-               if(!$titleObj->exists())
-                       $this->dieUsage("``{$params['title']}'' doesn't exist", 'missingtitle');
-               if(!$titleObj->userCan('protect'))
-                       $this->dieUsage('You don\'t have permission to change protection levels', 'permissiondenied');
-               $articleObj = new Article($titleObj);
+                       $this->dieUsageMsg(array('invalidtitle', $params['title']));
+                       
+               $errors = $titleObj->getUserPermissionsErrors('protect', $wgUser);
+               if(!empty($errors))
+                       // We don't care about multiple errors, just report one of them
+                       $this->dieUsageMsg(current($errors));
                
                if(in_array($params['expiry'], array('infinite', 'indefinite', 'never')))
                        $expiry = Block::infinity();
@@ -71,11 +67,11 @@ class ApiProtect extends ApiBase {
                {
                        $expiry = strtotime($params['expiry']);
                        if($expiry < 0 || $expiry == false)
-                               $this->dieUsage('Invalid expiry time', 'invalidexpiry');
+                               $this->dieUsageMsg(array('invalidexpiry'));
                        
                        $expiry = wfTimestamp(TS_MW, $expiry);
                        if($expiry < wfTimestampNow())
-                               $this->dieUsage('Expiry time is in the past', 'pastexpiry');
+                               $this->dieUsageMsg(array('pastexpiry'));
                }
 
                $protections = array();
@@ -83,23 +79,39 @@ class ApiProtect extends ApiBase {
                {
                        $p = explode('=', $prot);
                        $protections[$p[0]] = ($p[1] == 'all' ? '' : $p[1]);
+                       if($titleObj->exists() && $p[0] == 'create')
+                               $this->dieUsageMsg(array('create-titleexists'));
+                       if(!$titleObj->exists() && $p[0] != 'create')
+                               $this->dieUsageMsg(array('missingtitles-createonly'));
                }
 
                $dbw = wfGetDb(DB_MASTER);
                $dbw->begin();
-               $ok = $articleObj->updateRestrictions($protections, $params['reason'], $params['cascade'], $expiry);
+               if($titleObj->exists()) {
+                       $articleObj = new Article($titleObj);
+                       $ok = $articleObj->updateRestrictions($protections, $params['reason'], $params['cascade'], $expiry);
+               } else
+                       $ok = $titleObj->updateTitleProtection($protections['create'], $params['reason'], $expiry);
                if(!$ok)
                        // This is very weird. Maybe the article was deleted or the user was blocked/desysopped in the meantime?
-                       $this->dieUsage('Unknown error', 'unknownerror');
+                       // Just throw an unknown error in this case, as it's very likely to be a race condition
+                       $this->dieUsageMsg(array());
                $dbw->commit();
-               $res = array('title' => $titleObj->getPrefixedText(), 'reason' => $params['reason'], 'expiry' => $expiry);
+               $res = array('title' => $titleObj->getPrefixedText(), 'reason' => $params['reason']);
+               if($expiry == Block::infinity())
+                       $res['expiry'] = 'infinity';
+               else
+                       $res['expiry'] = wfTimestamp(TS_ISO_8601, $expiry);
+
                if($params['cascade'])
                        $res['cascade'] = '';
                $res['protections'] = $protections;
                $this->getResult()->addValue(null, $this->getModuleName(), $res);
        }
 
-       protected function getAllowedParams() {
+       public function mustBePosted() { return true; }
+
+       public function getAllowedParams() {
                return array (
                        'title' => null,
                        'token' => null,
@@ -112,7 +124,7 @@ class ApiProtect extends ApiBase {
                );
        }
 
-       protected function getParamDescription() {
+       public function getParamDescription() {
                return array (
                        'title' => 'Title of the page you want to restore.',
                        'token' => 'A protect token previously retrieved through prop=info',
@@ -123,7 +135,7 @@ class ApiProtect extends ApiBase {
                );
        }
 
-       protected function getDescription() {
+       public function getDescription() {
                return array(
                        'Change the protection level of a page.'
                );