r44719 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r44718‎ | r44719 | r44720 >
Date:16:34, 17 December 2008
Author:catrope
Status:ok
Tags:
Comment:
API: Crusade against extract(). Left one extract() call alone in ApiQueryBacklinks.php because I don't have a better alternative for it.
Modified paths:
  • /trunk/phase3/includes/api/ApiBase.php (modified) (history)
  • /trunk/phase3/includes/api/ApiExpandTemplates.php (modified) (history)
  • /trunk/phase3/includes/api/ApiLogin.php (modified) (history)
  • /trunk/phase3/includes/api/ApiPageSet.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryBacklinks.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryRecentChanges.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryRevisions.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryWatchlist.php (modified) (history)
  • /trunk/phase3/includes/api/ApiRollback.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiQueryRecentChanges.php
@@ -83,11 +83,8 @@
8484 * Generates and outputs the result of this query based upon the provided parameters.
8585 */
8686 public function execute() {
87 - /* Initialize vars */
88 - $limit = $prop = $namespace = $titles = $show = $type = $dir = $start = $end = $token = null;
89 -
9087 /* Get the parameters of the request. */
91 - extract($this->extractRequestParams());
 88+ $params = $this->extractRequestParams();
9289
9390 /* Build our basic query. Namely, something along the lines of:
9491 * SELECT * FROM recentchanges WHERE rc_timestamp > $start
@@ -97,13 +94,13 @@
9895 $db = $this->getDB();
9996 $this->addTables('recentchanges');
10097 $this->addOption('USE INDEX', array('recentchanges' => 'rc_timestamp'));
101 - $this->addWhereRange('rc_timestamp', $dir, $start, $end);
102 - $this->addWhereFld('rc_namespace', $namespace);
 98+ $this->addWhereRange('rc_timestamp', $params['dir'], $params['start'], $params['end']);
 99+ $this->addWhereFld('rc_namespace', $params['namespace']);
103100 $this->addWhereFld('rc_deleted', 0);
104 - if($titles)
 101+ if($params['titles'])
105102 {
106103 $lb = new LinkBatch;
107 - foreach($titles as $t)
 104+ foreach($params['titles'] as $t)
108105 {
109106 $obj = Title::newFromText($t);
110107 $lb->addObj($obj);
@@ -120,11 +117,11 @@
121118 $this->addWhere($where);
122119 }
123120
124 - if(!is_null($type))
125 - $this->addWhereFld('rc_type', $this->parseRCType($type));
 121+ if(!is_null($params['type']))
 122+ $this->addWhereFld('rc_type', $this->parseRCType($params['type']));
126123
127 - if (!is_null($show)) {
128 - $show = array_flip($show);
 124+ if (!is_null($params['show'])) {
 125+ $show = array_flip($params['show']);
129126
130127 /* Check for conflicting parameters. */
131128 if ((isset ($show['minor']) && isset ($show['!minor']))
@@ -167,8 +164,8 @@
168165 ));
169166
170167 /* Determine what properties we need to display. */
171 - if (!is_null($prop)) {
172 - $prop = array_flip($prop);
 168+ if (!is_null($params['prop'])) {
 169+ $prop = array_flip($params['prop']);
173170
174171 /* Set up internal members based upon params. */
175172 $this->fld_comment = isset ($prop['comment']);
@@ -210,10 +207,8 @@
211208 $this->addFields('page_is_redirect');
212209 }
213210 }
214 - $this->token = $token;
215 - /* Specify the limit for our query. It's $limit+1 because we (possibly) need to
216 - * generate a "continue" parameter, to allow paging. */
217 - $this->addOption('LIMIT', $limit +1);
 211+ $this->token = $params['token'];
 212+ $this->addOption('LIMIT', $params['limit'] +1);
218213
219214 $data = array ();
220215 $count = 0;
@@ -224,7 +219,7 @@
225220
226221 /* Iterate through the rows, adding data extracted from them to our query result. */
227222 while ($row = $db->fetchObject($res)) {
228 - if (++ $count > $limit) {
 223+ if (++ $count > $params['limit']) {
229224 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
230225 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->rc_timestamp));
231226 break;
Index: trunk/phase3/includes/api/ApiQueryBacklinks.php
@@ -60,7 +60,6 @@
6161 );
6262
6363 public function __construct($query, $moduleName) {
64 - $code = $prefix = $linktbl = null;
6564 extract($this->backlinksSettings[$moduleName]);
6665
6766 parent :: __construct($query, $moduleName, $code);
Index: trunk/phase3/includes/api/ApiRollback.php
@@ -70,16 +70,13 @@
7171 // We don't care about multiple errors, just report one of them
7272 $this->dieUsageMsg(current($retval));
7373
74 - $current = $target = $summary = NULL;
75 - extract($details);
76 -
7774 $info = array(
7875 'title' => $titleObj->getPrefixedText(),
79 - 'pageid' => $current->getPage(),
80 - 'summary' => $summary,
 76+ 'pageid' => $details['current']->getPage(),
 77+ 'summary' => $details['summary'],
8178 'revid' => $titleObj->getLatestRevID(),
82 - 'old_revid' => $current->getID(),
83 - 'last_revid' => $target->getID()
 79+ 'old_revid' => $details['current']->getID(),
 80+ 'last_revid' => $details['target']->getID()
8481 );
8582
8683 $this->getResult()->addValue(null, $this->getModuleName(), $info);
Index: trunk/phase3/includes/api/ApiExpandTemplates.php
@@ -43,23 +43,22 @@
4444
4545 public function execute() {
4646 // Get parameters
47 - extract( $this->extractRequestParams() );
48 - $retval = '';
 47+ $params = $this->extractRequestParams();
4948
5049 //Create title for parser
51 - $title_obj = Title :: newFromText( $title );
 50+ $title_obj = Title :: newFromText( $params['title'] );
5251 if(!$title_obj)
53 - $title_obj = Title :: newFromText( "API" ); // Default title is "API". For example, ExpandTemplates uses "ExpendTemplates" for it
 52+ $title_obj = Title :: newFromText( "API" ); // default
5453
5554 $result = $this->getResult();
5655
5756 // Parse text
5857 global $wgParser;
5958 $options = new ParserOptions();
60 - if ( $generatexml )
 59+ if ( $params['generatexml'] )
6160 {
6261 $wgParser->startExternalParse( $title_obj, $options, OT_PREPROCESS );
63 - $dom = $wgParser->preprocessToDom( $text );
 62+ $dom = $wgParser->preprocessToDom( $params['text'] );
6463 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
6564 $xml = $dom->saveXML();
6665 } else {
@@ -67,9 +66,9 @@
6867 }
6968 $xml_result = array();
7069 $result->setContent( $xml_result, $xml );
71 - $result->addValue( null, 'parsetree', $xml_result);
 70+ $result->addValue( null, 'parsetree', $xml_result);
7271 }
73 - $retval = $wgParser->preprocess( $text, $title_obj, $options );
 72+ $retval = $wgParser->preprocess( $params['text'], $title_obj, $options );
7473
7574 // Return result
7675 $retval_array = array();
Index: trunk/phase3/includes/api/ApiLogin.php
@@ -52,15 +52,14 @@
5353 * @access public
5454 */
5555 public function execute() {
56 - $name = $password = $domain = null;
57 - extract($this->extractRequestParams());
 56+ $params = $this->extractRequestParams();
5857
5958 $result = array ();
6059
61 - $params = new FauxRequest(array (
62 - 'wpName' => $name,
63 - 'wpPassword' => $password,
64 - 'wpDomain' => $domain,
 60+ $req = new FauxRequest(array (
 61+ 'wpName' => $params['name'],
 62+ 'wpPassword' => $params['password'],
 63+ 'wpDomain' => $params['domain'],
6564 'wpRemember' => ''
6665 ));
6766
@@ -69,7 +68,7 @@
7069 wfSetupSession();
7170 }
7271
73 - $loginForm = new LoginForm($params);
 72+ $loginForm = new LoginForm($req);
7473 switch ($authRes = $loginForm->authenticateUserData()) {
7574 case LoginForm :: SUCCESS :
7675 global $wgUser, $wgCookiePrefix;
Index: trunk/phase3/includes/api/ApiPageSet.php
@@ -228,19 +228,18 @@
229229 */
230230 public function execute() {
231231 $this->profileIn();
232 - $titles = $pageids = $revids = null;
233 - extract($this->extractRequestParams());
 232+ $params = $this->extractRequestParams();
234233
235234 // Only one of the titles/pageids/revids is allowed at the same time
236235 $dataSource = null;
237 - if (isset ($titles))
 236+ if (isset ($params['titles']))
238237 $dataSource = 'titles';
239 - if (isset ($pageids)) {
 238+ if (isset ($params['pageids'])) {
240239 if (isset ($dataSource))
241240 $this->dieUsage("Cannot use 'pageids' at the same time as '$dataSource'", 'multisource');
242241 $dataSource = 'pageids';
243242 }
244 - if (isset ($revids)) {
 243+ if (isset ($params['revids'])) {
245244 if (isset ($dataSource))
246245 $this->dieUsage("Cannot use 'revids' at the same time as '$dataSource'", 'multisource');
247246 $dataSource = 'revids';
@@ -248,17 +247,17 @@
249248
250249 switch ($dataSource) {
251250 case 'titles' :
252 - $this->initFromTitles($titles);
 251+ $this->initFromTitles($params['titles']);
253252 break;
254253 case 'pageids' :
255 - $this->initFromPageIds($pageids);
 254+ $this->initFromPageIds($params['pageids']);
256255 break;
257256 case 'revids' :
258257 if($this->mResolveRedirects)
259258 $this->setWarning('Redirect resolution cannot be used together with the revids= parameter. '.
260259 'Any redirects the revids= point to have not been resolved.');
261260 $this->mResolveRedirects = false;
262 - $this->initFromRevIDs($revids);
 261+ $this->initFromRevIDs($params['revids']);
263262 break;
264263 default :
265264 // Do nothing - some queries do not need any of the data sources.
Index: trunk/phase3/includes/api/ApiQueryWatchlist.php
@@ -59,12 +59,11 @@
6060 if (!$wgUser->isLoggedIn())
6161 $this->dieUsage('You must be logged-in to have a watchlist', 'notloggedin');
6262
63 - $allrev = $start = $end = $namespace = $dir = $limit = $prop = $show = null;
64 - extract($this->extractRequestParams());
 63+ $params = $this->extractRequestParams();
6564
66 - if (!is_null($prop) && is_null($resultPageSet)) {
 65+ if (!is_null($params['prop']) && is_null($resultPageSet)) {
6766
68 - $prop = array_flip($prop);
 67+ $prop = array_flip($params['prop']);
6968
7069 $this->fld_ids = isset($prop['ids']);
7170 $this->fld_title = isset($prop['title']);
@@ -100,7 +99,7 @@
101100 $this->addFieldsIf('rc_old_len', $this->fld_sizes);
102101 $this->addFieldsIf('rc_new_len', $this->fld_sizes);
103102 }
104 - elseif ($allrev) {
 103+ elseif ($params['allrev']) {
105104 $this->addFields(array (
106105 'rc_this_oldid',
107106 'rc_namespace',
@@ -131,12 +130,12 @@
132131 'rc_deleted' => 0,
133132 ));
134133
135 - $this->addWhereRange('rc_timestamp', $dir, $start, $end);
136 - $this->addWhereFld('wl_namespace', $namespace);
137 - $this->addWhereIf('rc_this_oldid=page_latest', !$allrev);
 134+ $this->addWhereRange('rc_timestamp', $params['dir'], $params['start'], $params['end']);
 135+ $this->addWhereFld('wl_namespace', $params['namespace']);
 136+ $this->addWhereIf('rc_this_oldid=page_latest', !$params['allrev']);
138137
139 - if (!is_null($show)) {
140 - $show = array_flip($show);
 138+ if (!is_null($params['show'])) {
 139+ $show = array_flip($params['show']);
141140
142141 /* Check for conflicting parameters. */
143142 if ((isset ($show['minor']) && isset ($show['!minor']))
@@ -165,9 +164,9 @@
166165
167166
168167 # This is an index optimization for mysql, as done in the Special:Watchlist page
169 - $this->addWhereIf("rc_timestamp > ''", !isset ($start) && !isset ($end) && $wgDBtype == 'mysql');
 168+ $this->addWhereIf("rc_timestamp > ''", !isset ($params['start']) && !isset ($params['end']) && $wgDBtype == 'mysql');
170169
171 - $this->addOption('LIMIT', $limit +1);
 170+ $this->addOption('LIMIT', $params['limit'] +1);
172171
173172 $data = array ();
174173 $count = 0;
@@ -175,7 +174,7 @@
176175
177176 $db = $this->getDB();
178177 while ($row = $db->fetchObject($res)) {
179 - if (++ $count > $limit) {
 178+ if (++ $count > $params['limit']) {
180179 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
181180 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->rc_timestamp));
182181 break;
@@ -186,7 +185,7 @@
187186 if ($vals)
188187 $data[] = $vals;
189188 } else {
190 - if ($allrev) {
 189+ if ($params['allrev']) {
191190 $data[] = intval($row->rc_this_oldid);
192191 } else {
193192 $data[] = intval($row->rc_cur_id);
@@ -200,7 +199,7 @@
201200 $this->getResult()->setIndexedTagName($data, 'item');
202201 $this->getResult()->addValue('query', $this->getModuleName(), $data);
203202 }
204 - elseif ($allrev) {
 203+ elseif ($params['allrev']) {
205204 $resultPageSet->populateFromRevisionIDs($data);
206205 } else {
207206 $resultPageSet->populateFromPageIDs($data);
Index: trunk/phase3/includes/api/ApiQueryRevisions.php
@@ -74,14 +74,16 @@
7575 }
7676
7777 public function execute() {
78 - $limit = $startid = $endid = $start = $end = $dir = $prop = $user = $excludeuser = $expandtemplates = $generatexml = $section = $token = null;
79 - extract($this->extractRequestParams(false));
 78+ $params = $this->extractRequestParams(false);
8079
8180 // If any of those parameters are used, work in 'enumeration' mode.
8281 // Enum mode can only be used when exactly one page is provided.
8382 // Enumerating revisions on multiple pages make it extremely
8483 // difficult to manage continuations and require additional SQL indexes
85 - $enumRevMode = (!is_null($user) || !is_null($excludeuser) || !is_null($limit) || !is_null($startid) || !is_null($endid) || $dir === 'newer' || !is_null($start) || !is_null($end));
 84+ $enumRevMode = (!is_null($params['user']) || !is_null($params['excludeuser']) ||
 85+ !is_null($params['limit']) || !is_null($params['startid']) ||
 86+ !is_null($params['endid']) || $params['dir'] === 'newer' ||
 87+ !is_null($params['start']) || !is_null($params['end']));
8688
8789
8890 $pageSet = $this->getPageSet();
@@ -103,7 +105,7 @@
104106 $this->addTables( 'page' );
105107 $this->addWhere('page_id = rev_page');
106108
107 - $prop = array_flip($prop);
 109+ $prop = array_flip($params['prop']);
108110
109111 // Optional fields
110112 $this->fld_ids = isset ($prop['ids']);
@@ -113,7 +115,7 @@
114116 $this->fld_comment = isset ($prop['comment']);
115117 $this->fld_size = isset ($prop['size']);
116118 $this->fld_user = isset ($prop['user']);
117 - $this->token = $token;
 119+ $this->token = $params['token'];
118120
119121 if ( !is_null($this->token) || $pageCount > 0) {
120122 $this->addFields( Revision::selectPageFields() );
@@ -136,16 +138,17 @@
137139
138140 $this->fld_content = true;
139141
140 - $this->expandTemplates = $expandtemplates;
141 - $this->generateXML = $generatexml;
142 - if(isset($section))
143 - $this->section = $section;
 142+ $this->expandTemplates = $params['expandtemplates'];
 143+ $this->generateXML = $params['generatexml'];
 144+ if(isset($params['section']))
 145+ $this->section = $params['section'];
144146 else
145147 $this->section = false;
146148 }
147149
148150 $userMax = ( $this->fld_content ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1 );
149151 $botMax = ( $this->fld_content ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2 );
 152+ $limit = $params['limit'];
150153 if( $limit == 'max' ) {
151154 $limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
152155 $this->getResult()->addValue( 'limits', $this->getModuleName(), $limit );
@@ -154,13 +157,13 @@
155158 if ($enumRevMode) {
156159
157160 // This is mostly to prevent parameter errors (and optimize SQL?)
158 - if (!is_null($startid) && !is_null($start))
 161+ if (!is_null($params['startid']) && !is_null($params['start']))
159162 $this->dieUsage('start and startid cannot be used together', 'badparams');
160163
161 - if (!is_null($endid) && !is_null($end))
 164+ if (!is_null($params['endid']) && !is_null($params['end']))
162165 $this->dieUsage('end and endid cannot be used together', 'badparams');
163166
164 - if(!is_null($user) && !is_null( $excludeuser))
 167+ if(!is_null($params['user']) && !is_null($params['excludeuser']))
165168 $this->dieUsage('user and excludeuser cannot be used together', 'badparams');
166169
167170 // This code makes an assumption that sorting by rev_id and rev_timestamp produces
@@ -170,10 +173,12 @@
171174 // one row with the same timestamp for the same page.
172175 // The order needs to be the same as start parameter to avoid SQL filesort.
173176
174 - if (is_null($startid) && is_null($endid))
175 - $this->addWhereRange('rev_timestamp', $dir, $start, $end);
 177+ if (is_null($params['startid']) && is_null($params['endid']))
 178+ $this->addWhereRange('rev_timestamp', $params['dir'],
 179+ $params['start'], $params['end']);
176180 else
177 - $this->addWhereRange('rev_id', $dir, $startid, $endid);
 181+ $this->addWhereRange('rev_id', $params['dir'],
 182+ $params['startid'], $params['endid']);
178183
179184 // must manually initialize unset limit
180185 if (is_null($limit))
@@ -183,10 +188,11 @@
184189 // There is only one ID, use it
185190 $this->addWhereFld('rev_page', current(array_keys($pageSet->getGoodTitles())));
186191
187 - if(!is_null($user)) {
188 - $this->addWhereFld('rev_user_text', $user);
189 - } elseif (!is_null( $excludeuser)) {
190 - $this->addWhere('rev_user_text != ' . $this->getDB()->addQuotes($excludeuser));
 192+ if(!is_null($params['user'])) {
 193+ $this->addWhereFld('rev_user_text', $params['user']);
 194+ } elseif (!is_null( $params['excludeuser'])) {
 195+ $this->addWhere('rev_user_text != ' .
 196+ $this->getDB()->addQuotes($params['excludeuser']));
191197 }
192198 }
193199 elseif ($revCount > 0) {
Index: trunk/phase3/includes/api/ApiBase.php
@@ -369,7 +369,6 @@
370370 /**
371371 * Using getAllowedParams(), makes an array of the values provided by the user,
372372 * with key being the name of the variable, and value - validated value from user or default.
373 - * This method can be used to generate local variables using extract().
374373 * limit=max will not be parsed if $parseMaxLimit is set to false; use this
375374 * when the max limit is not definite, e.g. when getting revisions.
376375 */

Status & tagging log