r62822 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r62821‎ | r62822 | r62823 >
Date:12:18, 22 February 2010
Author:ashley
Status:ok
Tags:
Comment:
ApiEditPage.php: fix copyright symbol, spacing and coding style cleanup, more braces
Modified paths:
  • /trunk/phase3/includes/api/ApiEditPage.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiEditPage.php
@@ -1,11 +1,11 @@
22 <?php
33
4 -/*
 4+/**
55 * Created on August 16, 2007
66 *
77 * API for MediaWiki 1.8+
88 *
9 - * Copyright (C) 2007 Iker Labarga <Firstname><Lastname>@gmail.com
 9+ * Copyright © 2007 Iker Labarga <Firstname><Lastname>@gmail.com
1010 *
1111 * This program is free software; you can redistribute it and/or modify
1212 * it under the terms of the GNU General Public License as published by
@@ -24,8 +24,8 @@
2525 */
2626
2727 if ( !defined( 'MEDIAWIKI' ) ) {
28 - // Eclipse helper - will be ignored in production
29 - require_once ( "ApiBase.php" );
 28+ // Eclipse helper - will be ignored in production
 29+ require_once( "ApiBase.php" );
3030 }
3131
3232 /**
@@ -38,40 +38,48 @@
3939 class ApiEditPage extends ApiBase {
4040
4141 public function __construct( $query, $moduleName ) {
42 - parent :: __construct( $query, $moduleName );
 42+ parent::__construct( $query, $moduleName );
4343 }
4444
4545 public function execute() {
4646 global $wgUser;
4747 $params = $this->extractRequestParams();
48 -
49 - if ( is_null( $params['title'] ) )
 48+
 49+ if ( is_null( $params['title'] ) ) {
5050 $this->dieUsageMsg( array( 'missingparam', 'title' ) );
 51+ }
5152
5253 if ( is_null( $params['text'] ) && is_null( $params['appendtext'] ) &&
5354 is_null( $params['prependtext'] ) &&
5455 $params['undo'] == 0 )
 56+ {
5557 $this->dieUsageMsg( array( 'missingtext' ) );
 58+ }
5659
5760 $titleObj = Title::newFromText( $params['title'] );
58 - if ( !$titleObj || $titleObj->isExternal() )
 61+ if ( !$titleObj || $titleObj->isExternal() ) {
5962 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
60 -
 63+ }
 64+
6165 // Some functions depend on $wgTitle == $ep->mTitle
6266 global $wgTitle;
6367 $wgTitle = $titleObj;
6468
65 - if ( $params['createonly'] && $titleObj->exists() )
 69+ if ( $params['createonly'] && $titleObj->exists() ) {
6670 $this->dieUsageMsg( array( 'createonly-exists' ) );
67 - if ( $params['nocreate'] && !$titleObj->exists() )
 71+ }
 72+ if ( $params['nocreate'] && !$titleObj->exists() ) {
6873 $this->dieUsageMsg( array( 'nocreate-missing' ) );
 74+ }
6975
7076 // Now let's check whether we're even allowed to do this
7177 $errors = $titleObj->getUserPermissionsErrors( 'edit', $wgUser );
72 - if ( !$titleObj->exists() )
 78+ if ( !$titleObj->exists() ) {
7379 $errors = array_merge( $errors, $titleObj->getUserPermissionsErrors( 'create', $wgUser ) );
74 - if ( count( $errors ) )
 80+ }
 81+ if ( count( $errors ) ) {
7582 $this->dieUsageMsg( $errors[0] );
 83+ }
7684
7785 $articleObj = new Article( $titleObj );
7886 $toMD5 = $params['text'];
@@ -81,103 +89,123 @@
8290 // returns an interface message rather than ''
8391 // We do want getContent()'s behavior for non-existent
8492 // MediaWiki: pages, though
85 - if ( $articleObj->getID() == 0 && $titleObj->getNamespace() != NS_MEDIAWIKI )
 93+ if ( $articleObj->getID() == 0 && $titleObj->getNamespace() != NS_MEDIAWIKI ) {
8694 $content = '';
87 - else
 95+ } else {
8896 $content = $articleObj->getContent();
89 -
90 - if ( !is_null( $params['section'] ) )
91 - {
 97+ }
 98+
 99+ if ( !is_null( $params['section'] ) ) {
92100 // Process the content for section edits
93101 global $wgParser;
94102 $section = intval( $params['section'] );
95103 $content = $wgParser->getSection( $content, $section, false );
96 - if ( $content === false )
 104+ if ( $content === false ) {
97105 $this->dieUsage( "There is no section {$section}.", 'nosuchsection' );
 106+ }
98107 }
99108 $params['text'] = $params['prependtext'] . $content . $params['appendtext'];
100109 $toMD5 = $params['prependtext'] . $params['appendtext'];
101110 }
102 -
103 - if ( $params['undo'] > 0 )
104 - {
105 - if ( $params['undoafter'] > 0 )
106 - {
107 - if ( $params['undo'] < $params['undoafter'] )
 111+
 112+ if ( $params['undo'] > 0 ) {
 113+ if ( $params['undoafter'] > 0 ) {
 114+ if ( $params['undo'] < $params['undoafter'] ) {
108115 list( $params['undo'], $params['undoafter'] ) =
109116 array( $params['undoafter'], $params['undo'] );
 117+ }
110118 $undoafterRev = Revision::newFromID( $params['undoafter'] );
111119 }
112120 $undoRev = Revision::newFromID( $params['undo'] );
113121 if ( is_null( $undoRev ) || $undoRev->isDeleted( Revision::DELETED_TEXT ) )
 122+ {
114123 $this->dieUsageMsg( array( 'nosuchrevid', $params['undo'] ) );
 124+ }
115125
116 - if ( $params['undoafter'] == 0 )
 126+ if ( $params['undoafter'] == 0 ) {
117127 $undoafterRev = $undoRev->getPrevious();
 128+ }
118129 if ( is_null( $undoafterRev ) || $undoafterRev->isDeleted( Revision::DELETED_TEXT ) )
 130+ {
119131 $this->dieUsageMsg( array( 'nosuchrevid', $params['undoafter'] ) );
 132+ }
120133
121 - if ( $undoRev->getPage() != $articleObj->getID() )
 134+ if ( $undoRev->getPage() != $articleObj->getID() ) {
122135 $this->dieUsageMsg( array( 'revwrongpage', $undoRev->getID(), $titleObj->getPrefixedText() ) );
123 - if ( $undoafterRev->getPage() != $articleObj->getID() )
 136+ }
 137+ if ( $undoafterRev->getPage() != $articleObj->getID() ) {
124138 $this->dieUsageMsg( array( 'revwrongpage', $undoafterRev->getID(), $titleObj->getPrefixedText() ) );
125 -
 139+ }
 140+
126141 $newtext = $articleObj->getUndoText( $undoRev, $undoafterRev );
127 - if ( $newtext === false )
 142+ if ( $newtext === false ) {
128143 $this->dieUsageMsg( array( 'undo-failure' ) );
 144+ }
129145 $params['text'] = $newtext;
130146 // If no summary was given and we only undid one rev,
131147 // use an autosummary
132148 if ( is_null( $params['summary'] ) && $titleObj->getNextRevisionID( $undoafterRev->getID() ) == $params['undo'] )
 149+ {
133150 $params['summary'] = wfMsgForContent( 'undo-summary', $params['undo'], $undoRev->getUserText() );
 151+ }
134152 }
135153
136154 // See if the MD5 hash checks out
137 - if ( !is_null( $params['md5'] ) && md5( $toMD5 ) !== $params['md5'] )
 155+ if ( !is_null( $params['md5'] ) && md5( $toMD5 ) !== $params['md5'] ) {
138156 $this->dieUsageMsg( array( 'hashcheckfailed' ) );
139 -
 157+ }
 158+
140159 $ep = new EditPage( $articleObj );
141160 // EditPage wants to parse its stuff from a WebRequest
142161 // That interface kind of sucks, but it's workable
143 - $reqArr = array( 'wpTextbox1' => $params['text'],
144 - 'wpEditToken' => $params['token'],
145 - 'wpIgnoreBlankSummary' => ''
 162+ $reqArr = array(
 163+ 'wpTextbox1' => $params['text'],
 164+ 'wpEditToken' => $params['token'],
 165+ 'wpIgnoreBlankSummary' => ''
146166 );
147167
148 - if ( !is_null( $params['summary'] ) )
 168+ if ( !is_null( $params['summary'] ) ) {
149169 $reqArr['wpSummary'] = $params['summary'];
 170+ }
150171
151172 // Watch out for basetimestamp == ''
152173 // wfTimestamp() treats it as NOW, almost certainly causing an edit conflict
153174 if ( !is_null( $params['basetimestamp'] ) && $params['basetimestamp'] != '' )
 175+ {
154176 $reqArr['wpEdittime'] = wfTimestamp( TS_MW, $params['basetimestamp'] );
155 - else
 177+ } else {
156178 $reqArr['wpEdittime'] = $articleObj->getTimestamp();
 179+ }
157180
158181 if ( !is_null( $params['starttimestamp'] ) && $params['starttimestamp'] != '' )
 182+ {
159183 $reqArr['wpStarttime'] = wfTimestamp( TS_MW, $params['starttimestamp'] );
160 - else
 184+ } else {
161185 $reqArr['wpStarttime'] = $reqArr['wpEdittime']; // Fake wpStartime
 186+ }
162187
163188 if ( $params['minor'] || ( !$params['notminor'] && $wgUser->getOption( 'minordefault' ) ) )
 189+ {
164190 $reqArr['wpMinoredit'] = '';
 191+ }
165192
166 - if ( $params['recreate'] )
 193+ if ( $params['recreate'] ) {
167194 $reqArr['wpRecreate'] = '';
 195+ }
168196
169 - if ( !is_null( $params['section'] ) )
170 - {
 197+ if ( !is_null( $params['section'] ) ) {
171198 $section = intval( $params['section'] );
172199 if ( $section == 0 && $params['section'] != '0' && $params['section'] != 'new' )
 200+ {
173201 $this->dieUsage( "The section parameter must be set to an integer or 'new'", "invalidsection" );
 202+ }
174203 $reqArr['wpSection'] = $params['section'];
 204+ } else {
 205+ $reqArr['wpSection'] = '';
175206 }
176 - else
177 - $reqArr['wpSection'] = '';
178207
179208 // Handle watchlist settings
180 - switch ( $params['watchlist'] )
181 - {
 209+ switch ( $params['watchlist'] ) {
182210 case 'watch':
183211 $watch = true;
184212 break;
@@ -185,23 +213,26 @@
186214 $watch = false;
187215 break;
188216 case 'preferences':
189 - if ( $titleObj->exists() )
 217+ if ( $titleObj->exists() ) {
190218 $watch = $wgUser->getOption( 'watchdefault' ) || $titleObj->userIsWatching();
191 - else
 219+ } else {
192220 $watch = $wgUser->getOption( 'watchcreations' );
 221+ }
193222 break;
194223 case 'nochange':
195224 default:
196225 $watch = $titleObj->userIsWatching();
197226 }
198227 // Deprecated parameters
199 - if ( $params['watch'] )
 228+ if ( $params['watch'] ) {
200229 $watch = true;
201 - elseif ( $params['unwatch'] )
 230+ } elseif ( $params['unwatch'] ) {
202231 $watch = false;
203 -
204 - if ( $watch )
 232+ }
 233+
 234+ if ( $watch ) {
205235 $reqArr['wpWatchthis'] = '';
 236+ }
206237
207238 $req = new FauxRequest( $reqArr, true );
208239 $ep->importFormData( $req );
@@ -209,22 +240,23 @@
210241 // Run hooks
211242 // Handle CAPTCHA parameters
212243 global $wgRequest;
213 - if ( !is_null( $params['captchaid'] ) )
 244+ if ( !is_null( $params['captchaid'] ) ) {
214245 $wgRequest->setVal( 'wpCaptchaId', $params['captchaid'] );
215 - if ( !is_null( $params['captchaword'] ) )
 246+ }
 247+ if ( !is_null( $params['captchaword'] ) ) {
216248 $wgRequest->setVal( 'wpCaptchaWord', $params['captchaword'] );
 249+ }
217250
218251 $r = array();
219252 if ( !wfRunHooks( 'APIEditBeforeSave', array( $ep, $ep->textbox1, &$r ) ) )
220253 {
221 - if ( count( $r ) )
222 - {
223 - $r['result'] = "Failure";
 254+ if ( count( $r ) ) {
 255+ $r['result'] = 'Failure';
224256 $this->getResult()->addValue( null, $this->getModuleName(), $r );
225257 return;
 258+ } else {
 259+ $this->dieUsageMsg( array( 'hookaborted' ) );
226260 }
227 - else
228 - $this->dieUsageMsg( array( 'hookaborted' ) );
229261 }
230262
231263 // Do the actual save
@@ -237,8 +269,7 @@
238270
239271 $retval = $ep->internalAttemptSave( $result, $wgUser->isAllowed( 'bot' ) && $params['bot'] );
240272 $wgRequest = $oldRequest;
241 - switch( $retval )
242 - {
 273+ switch( $retval ) {
243274 case EditPage::AS_HOOK_ERROR:
244275 case EditPage::AS_HOOK_ERROR_EXPECTED:
245276 $this->dieUsageMsg( array( 'hookaborted' ) );
@@ -294,7 +325,7 @@
295326 case EditPage::AS_SUCCESS_NEW_ARTICLE:
296327 $r['new'] = '';
297328 case EditPage::AS_SUCCESS_UPDATE:
298 - $r['result'] = "Success";
 329+ $r['result'] = 'Success';
299330 $r['pageid'] = intval( $titleObj->getArticleID() );
300331 $r['title'] = $titleObj->getPrefixedText();
301332 // HACK: We create a new Article object here because getRevIdFetched()
@@ -303,10 +334,9 @@
304335 // don't want to do.
305336 $newArticle = new Article( $titleObj );
306337 $newRevId = $newArticle->getRevIdFetched();
307 - if ( $newRevId == $oldRevId )
 338+ if ( $newRevId == $oldRevId ) {
308339 $r['nochange'] = '';
309 - else
310 - {
 340+ } else {
311341 $r['oldrevid'] = intval( $oldRevId );
312342 $r['newrevid'] = intval( $newRevId );
313343 $r['newtimestamp'] = wfTimestamp( TS_ISO_8601,
@@ -316,10 +346,10 @@
317347
318348 case EditPage::AS_END:
319349 // This usually means some kind of race condition
320 - // or DB weirdness occurred. Fall through to throw an unknown
 350+ // or DB weirdness occurred. Fall through to throw an unknown
321351 // error.
322352
323 - // This needs fixing higher up, as Article::doEdit should be
 353+ // This needs fixing higher up, as Article::doEdit should be
324354 // used rather than Article::updateArticle, so that specific
325355 // error conditions can be returned
326356 default:
@@ -339,10 +369,10 @@
340370 protected function getDescription() {
341371 return 'Create and edit pages.';
342372 }
343 -
 373+
344374 public function getPossibleErrors() {
345375 global $wgMaxArticleSize;
346 -
 376+
347377 return array_merge( parent::getPossibleErrors(), array(
348378 array( 'missingparam', 'title' ),
349379 array( 'missingtext' ),
@@ -376,7 +406,7 @@
377407 }
378408
379409 protected function getAllowedParams() {
380 - return array (
 410+ return array(
381411 'title' => null,
382412 'section' => null,
383413 'text' => null,
@@ -393,16 +423,16 @@
394424 'captchaword' => null,
395425 'captchaid' => null,
396426 'watch' => array(
397 - ApiBase :: PARAM_DFLT => false,
398 - ApiBase :: PARAM_DEPRECATED => true,
 427+ ApiBase::PARAM_DFLT => false,
 428+ ApiBase::PARAM_DEPRECATED => true,
399429 ),
400430 'unwatch' => array(
401 - ApiBase :: PARAM_DFLT => false,
402 - ApiBase :: PARAM_DEPRECATED => true,
 431+ ApiBase::PARAM_DFLT => false,
 432+ ApiBase::PARAM_DEPRECATED => true,
403433 ),
404434 'watchlist' => array(
405 - ApiBase :: PARAM_DFLT => 'preferences',
406 - ApiBase :: PARAM_TYPE => array(
 435+ ApiBase::PARAM_DFLT => 'preferences',
 436+ ApiBase::PARAM_TYPE => array(
407437 'watch',
408438 'unwatch',
409439 'preferences',
@@ -413,16 +443,16 @@
414444 'prependtext' => null,
415445 'appendtext' => null,
416446 'undo' => array(
417 - ApiBase :: PARAM_TYPE => 'integer'
 447+ ApiBase::PARAM_TYPE => 'integer'
418448 ),
419449 'undoafter' => array(
420 - ApiBase :: PARAM_TYPE => 'integer'
 450+ ApiBase::PARAM_TYPE => 'integer'
421451 ),
422452 );
423453 }
424454
425455 protected function getParamDescription() {
426 - return array (
 456+ return array(
427457 'title' => 'Page title',
428458 'section' => 'Section number. 0 for the top section, \'new\' for a new section',
429459 'text' => 'Page content',
@@ -453,19 +483,19 @@
454484 'undoafter' => 'Undo all revisions from undo to this one. If not set, just undo one revision',
455485 );
456486 }
457 -
 487+
458488 public function getTokenSalt() {
459489 return '';
460490 }
461491
462492 protected function getExamples() {
463 - return array (
464 - "Edit a page (anonymous user):",
465 - " api.php?action=edit&title=Test&summary=test%20summary&text=article%20content&basetimestamp=20070824123454&token=%2B\\",
466 - "Prepend __NOTOC__ to a page (anonymous user):",
467 - " api.php?action=edit&title=Test&summary=NOTOC&minor&prependtext=__NOTOC__%0A&basetimestamp=20070824123454&token=%2B\\",
468 - "Undo r13579 through r13585 with autosummary(anonymous user):",
469 - " api.php?action=edit&title=Test&undo=13585&undoafter=13579&basetimestamp=20070824123454&token=%2B\\",
 493+ return array(
 494+ 'Edit a page (anonymous user):',
 495+ ' api.php?action=edit&title=Test&summary=test%20summary&text=article%20content&basetimestamp=20070824123454&token=%2B\\',
 496+ 'Prepend __NOTOC__ to a page (anonymous user):',
 497+ ' api.php?action=edit&title=Test&summary=NOTOC&minor&prependtext=__NOTOC__%0A&basetimestamp=20070824123454&token=%2B\\',
 498+ 'Undo r13579 through r13585 with autosummary (anonymous user):',
 499+ ' api.php?action=edit&title=Test&undo=13585&undoafter=13579&basetimestamp=20070824123454&token=%2B\\',
470500 );
471501 }
472502

Status & tagging log