r29925 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r29924‎ | r29925 | r29926 >
Date:16:34, 18 January 2008
Author:catrope
Status:old
Tags:
Comment:
API:
* Refactored ApiProtect. No need for core modifications this time :)
* Added permissions check to ApiMove to protect against messages with arguments getting none. moveTo()'s return value should really be modified, see also comment
Modified paths:
  • /trunk/phase3/includes/api/ApiBase.php (modified) (history)
  • /trunk/phase3/includes/api/ApiMove.php (modified) (history)
  • /trunk/phase3/includes/api/ApiProtect.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiMove.php
@@ -61,12 +61,24 @@
6262 $this->dieUsageMsg(array('notanarticle'));
6363 $fromTalk = $fromTitle->getTalkPage();
6464
65 -
6665 $toTitle = Title::newFromText($params['to']);
6766 if(!$toTitle)
6867 $this->dieUsageMsg(array('invalidtitle', $params['to']));
6968 $toTalk = $toTitle->getTalkPage();
7069
 70+ // Run getUserPermissionsErrors() here so we get message arguments too,
 71+ // rather than just a message key. The latter is troublesome for messages
 72+ // that use arguments.
 73+ // FIXME: moveTo() should really return an array, requires some
 74+ // refactoring of other code, though (mainly SpecialMovepage.php)
 75+ $errors = array_merge($fromTitle->getUserPermissionsErrors('move', $wgUser),
 76+ $fromTitle->getUserPermissionsErrors('edit', $wgUser),
 77+ $toTitle->getUserPermissionsErrors('move', $wgUser),
 78+ $toTitle->getUserPermissionsErrors('edit', $wgUser));
 79+ if(!empty($errors))
 80+ // We don't care about multiple errors, just report one of them
 81+ $this->dieUsageMsg(current($errors));
 82+
7183 $dbw = wfGetDB(DB_MASTER);
7284 $dbw->begin();
7385 $retval = $fromTitle->moveTo($toTitle, true, $params['reason'], !$params['noredirect']);
Index: trunk/phase3/includes/api/ApiProtect.php
@@ -43,24 +43,23 @@
4444
4545 $titleObj = NULL;
4646 if(!isset($params['title']))
47 - $this->dieUsage('The title parameter must be set', 'notitle');
 47+ $this->dieUsageMsg(array('missingparam', 'title'));
4848 if(!isset($params['token']))
49 - $this->dieUsage('The token parameter must be set', 'notoken');
 49+ $this->dieUsageMsg(array('missingparam', 'token'));
5050 if(!isset($params['protections']) || empty($params['protections']))
51 - $this->dieUsage('The protections parameter must be set', 'noprotections');
 51+ $this->dieUsageMsg(array('missingparam', 'protections'));
5252
53 - if($wgUser->isBlocked())
54 - $this->dieUsage('You have been blocked from editing', 'blocked');
55 - if(wfReadOnly())
56 - $this->dieUsage('The wiki is in read-only mode', 'readonly');
5753 if(!$wgUser->matchEditToken($params['token']))
58 - $this->dieUsage('Invalid token', 'badtoken');
 54+ $this->dieUsageMsg(array('sessionfailure'));
5955
6056 $titleObj = Title::newFromText($params['title']);
6157 if(!$titleObj)
62 - $this->dieUsage("Bad title ``{$params['title']}''", 'invalidtitle');
63 - if(!$titleObj->userCan('protect'))
64 - $this->dieUsage('You don\'t have permission to change protection levels', 'permissiondenied');
 58+ $this->dieUsageMsg(array('invalidtitle', $params['title']));
 59+
 60+ $errors = $titleObj->getUserPermissionsErrors('protect', $wgUser);
 61+ if(!empty($errors))
 62+ // We don't care about multiple errors, just report one of them
 63+ $this->dieUsageMsg(current($errors));
6564
6665 if(in_array($params['expiry'], array('infinite', 'indefinite', 'never')))
6766 $expiry = Block::infinity();
@@ -68,11 +67,11 @@
6968 {
7069 $expiry = strtotime($params['expiry']);
7170 if($expiry < 0 || $expiry == false)
72 - $this->dieUsage('Invalid expiry time', 'invalidexpiry');
 71+ $this->dieUsageMsg(array('invalidexpiry'));
7372
7473 $expiry = wfTimestamp(TS_MW, $expiry);
7574 if($expiry < wfTimestampNow())
76 - $this->dieUsage('Expiry time is in the past', 'pastexpiry');
 75+ $this->dieUsageMsg(array('pastexpiry'));
7776 }
7877
7978 $protections = array();
@@ -81,9 +80,9 @@
8281 $p = explode('=', $prot);
8382 $protections[$p[0]] = ($p[1] == 'all' ? '' : $p[1]);
8483 if($titleObj->exists() && $p[0] == 'create')
85 - $this->dieUsage("Existing titles can't be protected with 'create'", 'create-titleexists');
 84+ $this->dieUsageMsg(array('create-titleexists'));
8685 if(!$titleObj->exists() && $p[0] != 'create')
87 - $this->dieUsage("Missing titles can only be protected with 'create'", 'missingtitle');
 86+ $this->dieUsageMsg(array('missingtitles-createonly'));
8887 }
8988
9089 $dbw = wfGetDb(DB_MASTER);
@@ -95,9 +94,15 @@
9695 $ok = $titleObj->updateTitleProtection($protections['create'], $params['reason'], $expiry);
9796 if(!$ok)
9897 // This is very weird. Maybe the article was deleted or the user was blocked/desysopped in the meantime?
99 - $this->dieUsage('Unknown error', 'unknownerror');
 98+ // Just throw an unknown error in this case, as it's very likely to be a race condition
 99+ $this->dieUsageMsg(array());
100100 $dbw->commit();
101 - $res = array('title' => $titleObj->getPrefixedText(), 'reason' => $params['reason'], 'expiry' => wfTimestamp(TS_ISO_8601, $expiry));
 101+ $res = array('title' => $titleObj->getPrefixedText(), 'reason' => $params['reason']);
 102+ if($expiry == Block::infinity())
 103+ $res['expiry'] = 'infinity';
 104+ else
 105+ $res['expiry'] = wfTimestamp(TS_ISO_8601, $expiry);
 106+
102107 if($params['cascade'])
103108 $res['cascade'] = '';
104109 $res['protections'] = $protections;
Index: trunk/phase3/includes/api/ApiBase.php
@@ -597,7 +597,11 @@
598598 // API-specific messages
599599 'missingparam' => array('code' => 'no$1', 'info' => "The \$1 parameter must be set"),
600600 'invalidtitle' => array('code' => 'invalidtitle', 'info' => "Bad title ``\$1''"),
601 - 'invaliduser' => array('code' => 'invaliduser', 'info' => "Invalid username ``\$1''")
 601+ 'invaliduser' => array('code' => 'invaliduser', 'info' => "Invalid username ``\$1''"),
 602+ 'invalidexpiry' => array('code' => 'invalidexpiry', 'info' => "Invalid expiry time"),
 603+ 'pastexpiry' => array('code' => 'pastexpiry', 'info' => "Expiry time is in the past"),
 604+ 'create-titleexists' => array('code' => 'create-titleexists', 'info' => "Existing titles can't be protected with 'create'"),
 605+ 'missingtitle-createonly' => array('code' => 'missingtitle-createonly', 'info' => "Missing titles can only be protected with 'create'"),
602606 );
603607
604608 /**

Status & tagging log