r67843 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r67842‎ | r67843 | r67844 >
Date:07:41, 11 June 2010
Author:aaron
Status:deferred
Tags:
Comment:
* refactored ApiStabilize to use subclassing
* removed bad items from getPossibleErrors() and added TODO
* removed unused globals
Modified paths:
  • /trunk/extensions/FlaggedRevs/FlaggedRevs.php (modified) (history)
  • /trunk/extensions/FlaggedRevs/api/ApiStabilize.php (modified) (history)

Diff [purge]

Index: trunk/extensions/FlaggedRevs/FlaggedRevs.php
@@ -403,7 +403,8 @@
404404 $wgAPIModules['review'] = 'ApiReview';
405405 # Stability config module for API
406406 $wgAutoloadClasses['ApiStabilize'] = $dir . 'api/ApiStabilize.php';
407 -$wgAPIModules['stabilize'] = 'ApiStabilize';
 407+$wgAutoloadClasses['ApiStabilizeGeneral'] = $dir . 'api/ApiStabilize.php';
 408+$wgAutoloadClasses['ApiStabilizeProtect'] = $dir . 'api/ApiStabilize.php';
408409
409410 # New user preferences
410411 $wgDefaultUserOptions['flaggedrevssimpleui'] = (int)$wgSimpleFlaggedRevsUI;
@@ -580,8 +581,11 @@
581582 }
582583
583584 function efSetFlaggedRevsConditionalAPIModules() {
584 - global $wgAPIListModules;
585 - if ( !FlaggedRevs::stableOnlyIfConfigured() ) {
 585+ global $wgAPIModules, $wgAPIListModules;
 586+ if ( FlaggedRevs::stableOnlyIfConfigured() ) {
 587+ $wgAPIModules['stabilize'] = 'ApiStabilizeProtect';
 588+ } else {
 589+ $wgAPIModules['stabilize'] = 'ApiStabilizeGeneral';
586590 $wgAPIListModules['reviewedpages'] = 'ApiQueryReviewedpages';
587591 $wgAPIListModules['unreviewedpages'] = 'ApiQueryUnreviewedpages';
588592 }
Index: trunk/extensions/FlaggedRevs/api/ApiStabilize.php
@@ -26,9 +26,9 @@
2727 *
2828 * @ingroup FlaggedRevs
2929 */
30 -class ApiStabilize extends ApiBase {
 30+abstract class ApiStabilize extends ApiBase {
3131 public function execute() {
32 - global $wgUser, $wgContLang;
 32+ global $wgUser;
3333 $params = $this->extractRequestParams();
3434
3535 if ( !isset( $params['title'] ) ) {
@@ -47,63 +47,78 @@
4848 $this->dieUsageMsg( reset( $errors ) );
4949 }
5050
51 - $form = FlaggedRevs::getPageStabilityForm();
 51+ $this->doExecute(); // child class
 52+ }
5253
 54+ public function mustBePosted() {
 55+ return true;
 56+ }
 57+
 58+ public function isWriteMode() {
 59+ return true;
 60+ }
 61+
 62+ public function getTokenSalt() {
 63+ return '';
 64+ }
 65+
 66+ public function getVersion() {
 67+ return __CLASS__ . ': $Id$';
 68+ }
 69+}
 70+
 71+// Assumes $wgFlaggedRevsProtection is off
 72+class ApiStabilizeGeneral extends ApiStabilize {
 73+ public function doExecute() {
 74+ $params = $this->extractRequestParams();
 75+
 76+ $form = new PageStabilityGeneralForm();
5377 $form->setPage( $title ); # Our target page
5478 $form->setWatchThis( $params['watch'] ); # Watch this page
5579 $form->setReason( $params['reason'] ); # Reason
5680 $form->setReasonSelection( 'other' ); # Reason dropdown
5781 $form->setExpiry( $params['expiry'] ); # Expiry
5882 $form->setExpirySelection( 'other' ); # Expiry dropdown
59 - if ( FlaggedRevs::useProtectionLevels() ) {
60 - $restriction = $params['protectlevel'];
 83+ $restriction = $params['autoreview'];
 84+ // Fill in config fields from URL params
 85+ $form->setPrecedence( $this->precendenceFromKey( $params['precedence'] ) );
 86+ if ( $params['default'] === null ) {
 87+ // Default version setting not optional
 88+ $this->dieUsageMsg( array( 'missingparam', 'default' ) );
6189 } else {
62 - $restriction = $params['autoreview'];
63 - // Fill in config fields from URL params
64 - $form->setPrecedence( $this->precendenceFromKey( $params['precedence'] ) );
65 - if ( $params['default'] === null ) {
66 - // Default version setting not optional
67 - $this->dieUsageMsg( array( 'missingparam', 'default' ) );
68 - } else {
69 - $form->setOverride( $this->defaultFromKey( $params['default'] ) );
70 - }
71 - $form->setReviewThis( $params['review'] ); # Auto-review option
 90+ $form->setOverride( $this->defaultFromKey( $params['default'] ) );
7291 }
 92+ $form->setReviewThis( $params['review'] ); # Auto-review option
7393 if ( $restriction == 'none' ) {
7494 $restriction = ''; // 'none' => ''
7595 }
7696 $form->setAutoreview( $restriction ); # Autoreview restriction
7797 $form->ready();
78 -
 98+
7999 $status = $form->submit(); // true/error message key
80100 if ( $status !== true ) {
81101 $this->dieUsage( wfMsg( $status ) );
82102 }
83 -
 103+
84104 # Output success line with the title and config parameters
85105 $res = array();
86106 $res['title'] = $title->getPrefixedText();
87 - if ( FlaggedRevs::useProtectionLevels() ) {
88 - $res['protectlevel'] = $params['protectlevel'];
89 - } else {
90 - $res['default'] = $params['default'];
91 - $res['precedence'] = $params['precedence'];
92 - $res['autoreview'] = $params['autoreview'];
93 - }
 107+ $res['default'] = $params['default'];
 108+ $res['precedence'] = $params['precedence'];
 109+ $res['autoreview'] = $params['autoreview'];
94110 $res['expiry'] = $form->getExpiry();
95111 $this->getResult()->addValue( null, $this->getModuleName(), $res );
96112 }
97 -
 113+
98114 protected function defaultFromKey( $key ) {
99115 if ( $key == 'stable' ) {
100116 return 1;
101117 } else if ( $key == 'latest' ) {
102118 return 0;
103119 }
104 - // bad key?
105 - return null;
 120+ return null; // bad key?
106121 }
107 -
 122+
108123 protected function precendenceFromKey( $key ) {
109124 if ( $key == 'pristine' ) {
110125 return FLAGGED_VIS_PRISTINE;
@@ -112,10 +127,9 @@
113128 } else if ( $key == 'latest' ) {
114129 return FLAGGED_VIS_LATEST;
115130 }
116 - // bad key?
117 - return null;
 131+ return null; // bad key?
118132 }
119 -
 133+
120134 protected function keyFromPrecendence( $precedence ) {
121135 if ( $precedence == FLAGGED_VIS_PRISTINE ) {
122136 return 'pristine';
@@ -124,83 +138,55 @@
125139 } else if ( $precedence == FLAGGED_VIS_LATEST ) {
126140 return 'lastest';
127141 }
128 - // bad key?
129 - return null;
 142+ return null; // bad key?
130143 }
131144
132 - public function mustBePosted() {
133 - return true;
134 - }
135 -
136 - public function isWriteMode() {
137 - return true;
138 - }
139 -
140145 public function getAllowedParams() {
141146 // Replace '' with more readable 'none' in autoreview restiction levels
142147 $autoreviewLevels = FlaggedRevs::getRestrictionLevels();
143148 $autoreviewLevels[] = 'none';
144 - if ( FlaggedRevs::useProtectionLevels() ) {
145 - $pars = array(
146 - 'protectlevel' => array(
147 - ApiBase :: PARAM_TYPE => $autoreviewLevels,
148 - ApiBase :: PARAM_DFLT => 'none',
149 - )
150 - );
151 - } else {
152 - $pars = array(
153 - 'default' => array(
154 - ApiBase :: PARAM_TYPE => array( 'latest', 'stable' ),
155 - ApiBase :: PARAM_DFLT => null,
156 - ),
157 - 'precedence' => array(
158 - ApiBase :: PARAM_TYPE => array( 'pristine', 'quality', 'latest' ),
159 - ApiBase :: PARAM_DFLT => $this->keyFromPrecendence( FlaggedRevs::getPrecedence() )
160 - ),
161 - 'autoreview' => array(
162 - ApiBase :: PARAM_TYPE => $autoreviewLevels,
163 - ApiBase :: PARAM_DFLT => 'none',
164 - ),
165 - );
166 - }
167 - $pars += array(
168 - 'expiry' => 'infinite',
169 - 'reason' => '',
170 - 'review' => false,
171 - 'watch' => null,
172 - 'token' => null,
173 - 'title' => null,
 149+ $pars = array(
 150+ 'default' => array(
 151+ ApiBase :: PARAM_TYPE => array( 'latest', 'stable' ),
 152+ ApiBase :: PARAM_DFLT => null,
 153+ ),
 154+ 'precedence' => array(
 155+ ApiBase :: PARAM_TYPE => array( 'pristine', 'quality', 'latest' ),
 156+ ApiBase :: PARAM_DFLT => $this->keyFromPrecendence( FlaggedRevs::getPrecedence() )
 157+ ),
 158+ 'autoreview' => array(
 159+ ApiBase :: PARAM_TYPE => $autoreviewLevels,
 160+ ApiBase :: PARAM_DFLT => 'none',
 161+ ),
 162+ 'expiry' => 'infinite',
 163+ 'reason' => '',
 164+ 'review' => false,
 165+ 'watch' => null,
 166+ 'token' => null,
 167+ 'title' => null,
174168 );
175169 return $pars;
176170 }
177171
178172 public function getParamDescription() {
179 - if ( FlaggedRevs::useProtectionLevels() ) {
180 - $desc = array(
181 - 'protectlevel' => 'The review protection level',
182 - );
183 - } else {
184 - $desc = array(
185 - 'default' => 'Default revision to show',
186 - 'precedence' => 'What stable revision should be shown',
187 - 'autoreview' => 'Auto-review restriction',
188 - );
189 - }
190 - $desc += array(
191 - 'expiry' => 'Stabilization expiry',
 173+ return array(
 174+ 'default' => 'Default revision to show',
 175+ 'precedence' => 'How the stable version is selected by precedence',
 176+ 'autoreview' => 'Auto-review restriction',
 177+ 'expiry' => 'Expiry for these settings',
192178 'title' => 'Title of page to be stabilized',
193179 'reason' => 'Reason',
194180 'review' => 'Review this page',
195181 'watch' => 'Watch this page',
196 - 'token' => 'An edit token retrieved through prop=info',
 182+ 'token' => 'An edit token retrieved through prop=info'
197183 );
198 - return $desc;
199184 }
200185
201186 public function getDescription() {
202 - return 'Change page stabilization settings.';
 187+ return 'Change page stability settings.';
203188 }
204 -
 189+
 190+ // @TODO: fill in stabilize_* values from PageStabilityGeneralForm
205191 public function getPossibleErrors() {
206192 return array_merge( parent::getPossibleErrors(), array(
207193 array( 'missingparam', 'title' ),
@@ -209,23 +195,93 @@
210196 array( 'code' => 'invalidtitle', 'info' => 'Invalid title given.' ),
211197 array( 'code' => 'invalidtitle', 'info' => 'Target page does not exist.' ),
212198 array( 'code' => 'invalidtitle', 'info' => 'Title given does not correspond to a reviewable page.' ),
213 - array( 'code' => 'badprotectlevel', 'info' => 'Invalid protection level given.' ),
214 - array( 'code' => 'invalidconfig', 'info' => 'Invalid config parameters given. The precendence level may beyond your rights.' ),
215199 ) );
216200 }
217 -
218 - public function getTokenSalt() {
219 - return '';
220 - }
221201
222202 protected function getExamples() {
223 - if ( FlaggedRevs::useProtectionLevels() )
224 - return 'api.php?action=stabilize&title=Test&protectlevel=none&reason=Test&token=123ABC';
225 - else
226 - return 'api.php?action=stabilize&title=Test&default=stable&reason=Test&token=123ABC';
 203+ return 'api.php?action=stabilize&title=Test&default=stable&reason=Test&token=123ABC';
227204 }
 205+}
228206
229 - public function getVersion() {
230 - return __CLASS__ . ': $Id$';
 207+// Assumes $wgFlaggedRevsProtection is on
 208+class ApiStabilizeProtect extends ApiStabilize {
 209+ public function doExecute() {
 210+ $params = $this->extractRequestParams();
 211+
 212+ $form = new PageStabilityProtectForm();
 213+ $form->setPage( $title ); # Our target page
 214+ $form->setWatchThis( $params['watch'] ); # Watch this page
 215+ $form->setReason( $params['reason'] ); # Reason
 216+ $form->setReasonSelection( 'other' ); # Reason dropdown
 217+ $form->setExpiry( $params['expiry'] ); # Expiry
 218+ $form->setExpirySelection( 'other' ); # Expiry dropdown
 219+ $restriction = $params['protectlevel'];
 220+ if ( $restriction == 'none' ) {
 221+ $restriction = ''; // 'none' => ''
 222+ }
 223+ $form->setAutoreview( $restriction ); # Autoreview restriction
 224+ $form->ready();
 225+
 226+ $status = $form->submit(); // true/error message key
 227+ if ( $status !== true ) {
 228+ $this->dieUsage( wfMsg( $status ) );
 229+ }
 230+
 231+ # Output success line with the title and config parameters
 232+ $res = array();
 233+ $res['title'] = $title->getPrefixedText();
 234+ $res['protectlevel'] = $params['protectlevel'];
 235+ $res['expiry'] = $form->getExpiry();
 236+ $this->getResult()->addValue( null, $this->getModuleName(), $res );
231237 }
232 -}
 238+
 239+ public function getAllowedParams() {
 240+ // Replace '' with more readable 'none' in autoreview restiction levels
 241+ $autoreviewLevels = FlaggedRevs::getRestrictionLevels();
 242+ $autoreviewLevels[] = 'none';
 243+ return array(
 244+ 'protectlevel' => array(
 245+ ApiBase :: PARAM_TYPE => $autoreviewLevels,
 246+ ApiBase :: PARAM_DFLT => 'none',
 247+ ),
 248+ 'expiry' => 'infinite',
 249+ 'reason' => '',
 250+ 'review' => false,
 251+ 'watch' => null,
 252+ 'token' => null,
 253+ 'title' => null,
 254+ );
 255+ }
 256+
 257+ public function getParamDescription() {
 258+ return array(
 259+ 'protectlevel' => 'The review-protection level',
 260+ 'expiry' => 'Review-protection expiry',
 261+ 'title' => 'Title of page to be review-protected',
 262+ 'reason' => 'Reason',
 263+ 'review' => 'Review this page',
 264+ 'watch' => 'Watch this page',
 265+ 'token' => 'An edit token retrieved through prop=info',
 266+ );
 267+ }
 268+
 269+ public function getDescription() {
 270+ return 'Configure review-protection settings.';
 271+ }
 272+
 273+ // @TODO: fill in stabilize_* values from PageStabilityProtectForm
 274+ public function getPossibleErrors() {
 275+ return array_merge( parent::getPossibleErrors(), array(
 276+ array( 'missingparam', 'title' ),
 277+ array( 'missingparam', 'token' ),
 278+ array( 'missingparam', 'default' ),
 279+ array( 'code' => 'invalidtitle', 'info' => 'Invalid title given.' ),
 280+ array( 'code' => 'invalidtitle', 'info' => 'Target page does not exist.' ),
 281+ array( 'code' => 'invalidtitle', 'info' => 'Title given does not correspond to a reviewable page.' ),
 282+ ) );
 283+ }
 284+
 285+ protected function getExamples() {
 286+ return 'api.php?action=stabilize&title=Test&protectlevel=none&reason=Test&token=123ABC';
 287+ }
 288+}
\ No newline at end of file

Status & tagging log