Index: trunk/phase3/includes/api/ApiBase.php |
— | — | @@ -1,11 +1,11 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | | -/* |
| 4 | +/** |
5 | 5 | * Created on Sep 5, 2006 |
6 | 6 | * |
7 | 7 | * API for MediaWiki 1.8+ |
8 | 8 | * |
9 | | - * Copyright (C) 2006, 2010 Yuri Astrakhan <Firstname><Lastname>@gmail.com |
| 9 | + * Copyright © 2006, 2010 Yuri Astrakhan <Firstname><Lastname>@gmail.com |
10 | 10 | * |
11 | 11 | * This program is free software; you can redistribute it and/or modify |
12 | 12 | * it under the terms of the GNU General Public License as published by |
— | — | @@ -121,10 +121,11 @@ |
122 | 122 | * @return string |
123 | 123 | */ |
124 | 124 | public function getModuleProfileName( $db = false ) { |
125 | | - if ( $db ) |
| 125 | + if ( $db ) { |
126 | 126 | return 'API:' . $this->mModuleName . '-DB'; |
127 | | - else |
| 127 | + } else { |
128 | 128 | return 'API:' . $this->mModuleName; |
| 129 | + } |
129 | 130 | } |
130 | 131 | |
131 | 132 | /** |
— | — | @@ -151,8 +152,9 @@ |
152 | 153 | public function getResult() { |
153 | 154 | // Main module has getResult() method overriden |
154 | 155 | // Safety - avoid infinite loop: |
155 | | - if ( $this->isMain() ) |
156 | | - ApiBase :: dieDebug( __METHOD__, 'base method was called on main module. ' ); |
| 156 | + if ( $this->isMain() ) { |
| 157 | + ApiBase::dieDebug( __METHOD__, 'base method was called on main module. ' ); |
| 158 | + } |
157 | 159 | return $this->getMain()->getResult(); |
158 | 160 | } |
159 | 161 | |
— | — | @@ -173,19 +175,20 @@ |
174 | 176 | */ |
175 | 177 | public function setWarning( $warning ) { |
176 | 178 | $data = $this->getResult()->getData(); |
177 | | - if ( isset( $data['warnings'][$this->getModuleName()] ) ) |
178 | | - { |
| 179 | + if ( isset( $data['warnings'][$this->getModuleName()] ) ) { |
179 | 180 | // Don't add duplicate warnings |
180 | 181 | $warn_regex = preg_quote( $warning, '/' ); |
181 | 182 | if ( preg_match( "/{$warn_regex}(\\n|$)/", $data['warnings'][$this->getModuleName()]['*'] ) ) |
| 183 | + { |
182 | 184 | return; |
| 185 | + } |
183 | 186 | $oldwarning = $data['warnings'][$this->getModuleName()]['*']; |
184 | 187 | // If there is a warning already, append it to the existing one |
185 | 188 | $warning = "$oldwarning\n$warning"; |
186 | 189 | $this->getResult()->unsetValue( 'warnings', $this->getModuleName() ); |
187 | 190 | } |
188 | 191 | $msg = array(); |
189 | | - ApiResult :: setContent( $msg, $warning ); |
| 192 | + ApiResult::setContent( $msg, $warning ); |
190 | 193 | $this->getResult()->disableSizeCheck(); |
191 | 194 | $this->getResult()->addValue( 'warnings', $this->getModuleName(), $msg ); |
192 | 195 | $this->getResult()->enableSizeCheck(); |
— | — | @@ -206,28 +209,33 @@ |
207 | 210 | * @return mixed string or false |
208 | 211 | */ |
209 | 212 | public function makeHelpMsg() { |
210 | | - |
211 | 213 | static $lnPrfx = "\n "; |
212 | 214 | |
213 | 215 | $msg = $this->getDescription(); |
214 | 216 | |
215 | 217 | if ( $msg !== false ) { |
216 | 218 | |
217 | | - if ( !is_array( $msg ) ) |
218 | | - $msg = array ( |
| 219 | + if ( !is_array( $msg ) ) { |
| 220 | + $msg = array( |
219 | 221 | $msg |
220 | 222 | ); |
| 223 | + } |
221 | 224 | $msg = $lnPrfx . implode( $lnPrfx, $msg ) . "\n"; |
222 | 225 | |
223 | | - if ( $this->isReadMode() ) |
| 226 | + if ( $this->isReadMode() ) { |
224 | 227 | $msg .= "\nThis module requires read rights."; |
225 | | - if ( $this->isWriteMode() ) |
| 228 | + } |
| 229 | + if ( $this->isWriteMode() ) { |
226 | 230 | $msg .= "\nThis module requires write rights."; |
227 | | - if ( $this->mustBePosted() ) |
| 231 | + } |
| 232 | + if ( $this->mustBePosted() ) { |
228 | 233 | $msg .= "\nThis module only accepts POST requests."; |
| 234 | + } |
229 | 235 | if ( $this->isReadMode() || $this->isWriteMode() || |
230 | 236 | $this->mustBePosted() ) |
| 237 | + { |
231 | 238 | $msg .= "\n"; |
| 239 | + } |
232 | 240 | |
233 | 241 | // Parameters |
234 | 242 | $paramsMsg = $this->makeHelpMsgParameters(); |
— | — | @@ -238,10 +246,11 @@ |
239 | 247 | // Examples |
240 | 248 | $examples = $this->getExamples(); |
241 | 249 | if ( $examples !== false ) { |
242 | | - if ( !is_array( $examples ) ) |
243 | | - $examples = array ( |
| 250 | + if ( !is_array( $examples ) ) { |
| 251 | + $examples = array( |
244 | 252 | $examples |
245 | 253 | ); |
| 254 | + } |
246 | 255 | $msg .= 'Example' . ( count( $examples ) > 1 ? 's' : '' ) . ":\n "; |
247 | 256 | $msg .= implode( $lnPrfx, $examples ) . "\n"; |
248 | 257 | } |
— | — | @@ -252,12 +261,13 @@ |
253 | 262 | $callback = array( $this, 'makeHelpMsg_callback' ); |
254 | 263 | |
255 | 264 | if ( is_array( $versions ) ) { |
256 | | - foreach ( $versions as &$v ) |
| 265 | + foreach ( $versions as &$v ) { |
257 | 266 | $v = preg_replace_callback( $pattern, $callback, $v ); |
| 267 | + } |
258 | 268 | $versions = implode( "\n ", $versions ); |
| 269 | + } else { |
| 270 | + $versions = preg_replace_callback( $pattern, $callback, $versions ); |
259 | 271 | } |
260 | | - else |
261 | | - $versions = preg_replace_callback( $pattern, $callback, $versions ); |
262 | 272 | |
263 | 273 | $msg .= "Version:\n $versions\n"; |
264 | 274 | } |
— | — | @@ -279,50 +289,55 @@ |
280 | 290 | $msg = ''; |
281 | 291 | $paramPrefix = "\n" . str_repeat( ' ', 19 ); |
282 | 292 | foreach ( $params as $paramName => $paramSettings ) { |
283 | | - $desc = isset ( $paramsDescription[$paramName] ) ? $paramsDescription[$paramName] : ''; |
284 | | - if ( is_array( $desc ) ) |
| 293 | + $desc = isset( $paramsDescription[$paramName] ) ? $paramsDescription[$paramName] : ''; |
| 294 | + if ( is_array( $desc ) ) { |
285 | 295 | $desc = implode( $paramPrefix, $desc ); |
| 296 | + } |
286 | 297 | |
287 | | - $deprecated = isset( $paramSettings[self :: PARAM_DEPRECATED] ) ? |
288 | | - $paramSettings[self :: PARAM_DEPRECATED] : false; |
289 | | - if ( $deprecated ) |
| 298 | + $deprecated = isset( $paramSettings[self::PARAM_DEPRECATED] ) ? |
| 299 | + $paramSettings[self::PARAM_DEPRECATED] : false; |
| 300 | + if ( $deprecated ) { |
290 | 301 | $desc = "DEPRECATED! $desc"; |
| 302 | + } |
291 | 303 | |
292 | | - $type = isset( $paramSettings[self :: PARAM_TYPE] ) ? $paramSettings[self :: PARAM_TYPE] : null; |
293 | | - if ( isset ( $type ) ) { |
294 | | - if ( isset ( $paramSettings[self :: PARAM_ISMULTI] ) ) |
| 304 | + $type = isset( $paramSettings[self::PARAM_TYPE] ) ? $paramSettings[self::PARAM_TYPE] : null; |
| 305 | + if ( isset( $type ) ) { |
| 306 | + if ( isset( $paramSettings[self::PARAM_ISMULTI] ) ) { |
295 | 307 | $prompt = 'Values (separate with \'|\'): '; |
296 | | - else |
| 308 | + } else { |
297 | 309 | $prompt = 'One value: '; |
| 310 | + } |
298 | 311 | |
299 | 312 | if ( is_array( $type ) ) { |
300 | 313 | $choices = array(); |
301 | 314 | $nothingPrompt = false; |
302 | 315 | foreach ( $type as $t ) |
303 | | - if ( $t === '' ) |
| 316 | + if ( $t === '' ) { |
304 | 317 | $nothingPrompt = 'Can be empty, or '; |
305 | | - else |
| 318 | + } else { |
306 | 319 | $choices[] = $t; |
| 320 | + } |
307 | 321 | $desc .= $paramPrefix . $nothingPrompt . $prompt . implode( ', ', $choices ); |
308 | 322 | } else { |
309 | 323 | switch ( $type ) { |
310 | 324 | case 'namespace': |
311 | 325 | // Special handling because namespaces are type-limited, yet they are not given |
312 | | - $desc .= $paramPrefix . $prompt . implode( ', ', ApiBase :: getValidNamespaces() ); |
| 326 | + $desc .= $paramPrefix . $prompt . implode( ', ', ApiBase::getValidNamespaces() ); |
313 | 327 | break; |
314 | 328 | case 'limit': |
315 | | - $desc .= $paramPrefix . "No more than {$paramSettings[self :: PARAM_MAX]} ({$paramSettings[self :: PARAM_MAX2]} for bots) allowed."; |
| 329 | + $desc .= $paramPrefix . "No more than {$paramSettings[self :: PARAM_MAX]} ({$paramSettings[self::PARAM_MAX2]} for bots) allowed."; |
316 | 330 | break; |
317 | 331 | case 'integer': |
318 | | - $hasMin = isset( $paramSettings[self :: PARAM_MIN] ); |
319 | | - $hasMax = isset( $paramSettings[self :: PARAM_MAX] ); |
| 332 | + $hasMin = isset( $paramSettings[self::PARAM_MIN] ); |
| 333 | + $hasMax = isset( $paramSettings[self::PARAM_MAX] ); |
320 | 334 | if ( $hasMin || $hasMax ) { |
321 | | - if ( !$hasMax ) |
322 | | - $intRangeStr = "The value must be no less than {$paramSettings[self :: PARAM_MIN]}"; |
323 | | - elseif ( !$hasMin ) |
324 | | - $intRangeStr = "The value must be no more than {$paramSettings[self :: PARAM_MAX]}"; |
325 | | - else |
326 | | - $intRangeStr = "The value must be between {$paramSettings[self :: PARAM_MIN]} and {$paramSettings[self :: PARAM_MAX]}"; |
| 335 | + if ( !$hasMax ) { |
| 336 | + $intRangeStr = "The value must be no less than {$paramSettings[self::PARAM_MIN]}"; |
| 337 | + } elseif ( !$hasMin ) { |
| 338 | + $intRangeStr = "The value must be no more than {$paramSettings[self::PARAM_MAX]}"; |
| 339 | + } else { |
| 340 | + $intRangeStr = "The value must be between {$paramSettings[self::PARAM_MIN]} and {$paramSettings[self::PARAM_MAX]}"; |
| 341 | + } |
327 | 342 | |
328 | 343 | $desc .= $paramPrefix . $intRangeStr; |
329 | 344 | } |
— | — | @@ -331,16 +346,18 @@ |
332 | 347 | } |
333 | 348 | } |
334 | 349 | |
335 | | - $default = is_array( $paramSettings ) ? ( isset ( $paramSettings[self :: PARAM_DFLT] ) ? $paramSettings[self :: PARAM_DFLT] : null ) : $paramSettings; |
336 | | - if ( !is_null( $default ) && $default !== false ) |
| 350 | + $default = is_array( $paramSettings ) ? ( isset( $paramSettings[self::PARAM_DFLT] ) ? $paramSettings[self::PARAM_DFLT] : null ) : $paramSettings; |
| 351 | + if ( !is_null( $default ) && $default !== false ) { |
337 | 352 | $desc .= $paramPrefix . "Default: $default"; |
| 353 | + } |
338 | 354 | |
339 | 355 | $msg .= sprintf( " %-14s - %s\n", $this->encodeParamName( $paramName ), $desc ); |
340 | 356 | } |
341 | 357 | return $msg; |
342 | 358 | |
343 | | - } else |
| 359 | + } else { |
344 | 360 | return false; |
| 361 | + } |
345 | 362 | } |
346 | 363 | |
347 | 364 | /** |
— | — | @@ -349,27 +366,30 @@ |
350 | 367 | */ |
351 | 368 | public function makeHelpMsg_callback( $matches ) { |
352 | 369 | global $wgAutoloadClasses, $wgAutoloadLocalClasses; |
353 | | - if ( isset( $wgAutoloadLocalClasses[get_class( $this )] ) ) |
| 370 | + if ( isset( $wgAutoloadLocalClasses[get_class( $this )] ) ) { |
354 | 371 | $file = $wgAutoloadLocalClasses[get_class( $this )]; |
355 | | - else if ( isset( $wgAutoloadClasses[get_class( $this )] ) ) |
| 372 | + } elseif ( isset( $wgAutoloadClasses[get_class( $this )] ) ) { |
356 | 373 | $file = $wgAutoloadClasses[get_class( $this )]; |
| 374 | + } |
357 | 375 | |
358 | 376 | // Do some guesswork here |
359 | 377 | $path = strstr( $file, 'includes/api/' ); |
360 | | - if ( $path === false ) |
| 378 | + if ( $path === false ) { |
361 | 379 | $path = strstr( $file, 'extensions/' ); |
362 | | - else |
| 380 | + } else { |
363 | 381 | $path = 'phase3/' . $path; |
| 382 | + } |
364 | 383 | |
365 | 384 | // Get the filename from $matches[2] instead of $file |
366 | 385 | // If they're not the same file, they're assumed to be in the |
367 | 386 | // same directory |
368 | 387 | // This is necessary to make stuff like ApiMain::getVersion() |
369 | 388 | // returning the version string for ApiBase work |
370 | | - if ( $path ) |
| 389 | + if ( $path ) { |
371 | 390 | return "{$matches[0]}\n http://svn.wikimedia.org/" . |
372 | 391 | "viewvc/mediawiki/trunk/" . dirname( $path ) . |
373 | 392 | "/{$matches[2]}"; |
| 393 | + } |
374 | 394 | return $matches[0]; |
375 | 395 | } |
376 | 396 | |
— | — | @@ -443,20 +463,21 @@ |
444 | 464 | } |
445 | 465 | |
446 | 466 | /** |
447 | | - * Using getAllowedParams(), this function makes an array of the values |
448 | | - * provided by the user, with key being the name of the variable, and |
449 | | - * value - validated value from user or default. limits will not be |
450 | | - * parsed if $parseLimit is set to false; use this when the max |
451 | | - * limit is not definitive yet, e.g. when getting revisions. |
452 | | - * @param $parseLimit bool |
453 | | - * @return array |
454 | | - */ |
| 467 | + * Using getAllowedParams(), this function makes an array of the values |
| 468 | + * provided by the user, with key being the name of the variable, and |
| 469 | + * value - validated value from user or default. limits will not be |
| 470 | + * parsed if $parseLimit is set to false; use this when the max |
| 471 | + * limit is not definitive yet, e.g. when getting revisions. |
| 472 | + * @param $parseLimit Boolean: true by default |
| 473 | + * @return array |
| 474 | + */ |
455 | 475 | public function extractRequestParams( $parseLimit = true ) { |
456 | 476 | $params = $this->getFinalParams(); |
457 | | - $results = array (); |
| 477 | + $results = array(); |
458 | 478 | |
459 | | - foreach ( $params as $paramName => $paramSettings ) |
| 479 | + foreach ( $params as $paramName => $paramSettings ) { |
460 | 480 | $results[$paramName] = $this->getParameterFromSettings( $paramName, $paramSettings, $parseLimit ); |
| 481 | + } |
461 | 482 | |
462 | 483 | return $results; |
463 | 484 | } |
— | — | @@ -498,15 +519,17 @@ |
499 | 520 | */ |
500 | 521 | public static function getValidNamespaces() { |
501 | 522 | static $mValidNamespaces = null; |
| 523 | + |
502 | 524 | if ( is_null( $mValidNamespaces ) ) { |
503 | | - |
504 | 525 | global $wgContLang; |
505 | | - $mValidNamespaces = array (); |
| 526 | + $mValidNamespaces = array(); |
506 | 527 | foreach ( array_keys( $wgContLang->getNamespaces() ) as $ns ) { |
507 | | - if ( $ns >= 0 ) |
| 528 | + if ( $ns >= 0 ) { |
508 | 529 | $mValidNamespaces[] = $ns; |
| 530 | + } |
509 | 531 | } |
510 | 532 | } |
| 533 | + |
511 | 534 | return $mValidNamespaces; |
512 | 535 | } |
513 | 536 | |
— | — | @@ -520,7 +543,6 @@ |
521 | 544 | * @return mixed Parameter value |
522 | 545 | */ |
523 | 546 | protected function getParameterFromSettings( $paramName, $paramSettings, $parseLimit ) { |
524 | | - |
525 | 547 | // Some classes may decide to change parameter names |
526 | 548 | $encParamName = $this->encodeParamName( $paramName ); |
527 | 549 | |
— | — | @@ -531,52 +553,55 @@ |
532 | 554 | $dupes = false; |
533 | 555 | $deprecated = false; |
534 | 556 | } else { |
535 | | - $default = isset ( $paramSettings[self :: PARAM_DFLT] ) ? $paramSettings[self :: PARAM_DFLT] : null; |
536 | | - $multi = isset ( $paramSettings[self :: PARAM_ISMULTI] ) ? $paramSettings[self :: PARAM_ISMULTI] : false; |
537 | | - $type = isset ( $paramSettings[self :: PARAM_TYPE] ) ? $paramSettings[self :: PARAM_TYPE] : null; |
538 | | - $dupes = isset ( $paramSettings[self:: PARAM_ALLOW_DUPLICATES] ) ? $paramSettings[self :: PARAM_ALLOW_DUPLICATES] : false; |
539 | | - $deprecated = isset ( $paramSettings[self:: PARAM_DEPRECATED] ) ? $paramSettings[self :: PARAM_DEPRECATED] : false; |
| 557 | + $default = isset( $paramSettings[self::PARAM_DFLT] ) ? $paramSettings[self::PARAM_DFLT] : null; |
| 558 | + $multi = isset( $paramSettings[self::PARAM_ISMULTI] ) ? $paramSettings[self::PARAM_ISMULTI] : false; |
| 559 | + $type = isset( $paramSettings[self::PARAM_TYPE] ) ? $paramSettings[self::PARAM_TYPE] : null; |
| 560 | + $dupes = isset( $paramSettings[self::PARAM_ALLOW_DUPLICATES] ) ? $paramSettings[self::PARAM_ALLOW_DUPLICATES] : false; |
| 561 | + $deprecated = isset( $paramSettings[self::PARAM_DEPRECATED] ) ? $paramSettings[self::PARAM_DEPRECATED] : false; |
540 | 562 | |
541 | 563 | // When type is not given, and no choices, the type is the same as $default |
542 | | - if ( !isset ( $type ) ) { |
543 | | - if ( isset ( $default ) ) |
| 564 | + if ( !isset( $type ) ) { |
| 565 | + if ( isset( $default ) ) { |
544 | 566 | $type = gettype( $default ); |
545 | | - else |
| 567 | + } else { |
546 | 568 | $type = 'NULL'; // allow everything |
| 569 | + } |
547 | 570 | } |
548 | 571 | } |
549 | 572 | |
550 | 573 | if ( $type == 'boolean' ) { |
551 | | - if ( isset ( $default ) && $default !== false ) { |
| 574 | + if ( isset( $default ) && $default !== false ) { |
552 | 575 | // Having a default value of anything other than 'false' is pointless |
553 | | - ApiBase :: dieDebug( __METHOD__, "Boolean param $encParamName's default is set to '$default'" ); |
| 576 | + ApiBase::dieDebug( __METHOD__, "Boolean param $encParamName's default is set to '$default'" ); |
554 | 577 | } |
555 | 578 | |
556 | 579 | $value = $this->getMain()->getRequest()->getCheck( $encParamName ); |
557 | 580 | } else { |
558 | 581 | $value = $this->getMain()->getRequest()->getVal( $encParamName, $default ); |
559 | 582 | |
560 | | - if ( isset ( $value ) && $type == 'namespace' ) |
561 | | - $type = ApiBase :: getValidNamespaces(); |
| 583 | + if ( isset( $value ) && $type == 'namespace' ) { |
| 584 | + $type = ApiBase::getValidNamespaces(); |
| 585 | + } |
562 | 586 | } |
563 | 587 | |
564 | | - if ( isset ( $value ) && ( $multi || is_array( $type ) ) ) |
| 588 | + if ( isset( $value ) && ( $multi || is_array( $type ) ) ) { |
565 | 589 | $value = $this->parseMultiValue( $encParamName, $value, $multi, is_array( $type ) ? $type : null ); |
| 590 | + } |
566 | 591 | |
567 | 592 | // More validation only when choices were not given |
568 | 593 | // choices were validated in parseMultiValue() |
569 | | - if ( isset ( $value ) ) { |
| 594 | + if ( isset( $value ) ) { |
570 | 595 | if ( !is_array( $type ) ) { |
571 | 596 | switch ( $type ) { |
572 | | - case 'NULL' : // nothing to do |
| 597 | + case 'NULL': // nothing to do |
573 | 598 | break; |
574 | | - case 'string' : // nothing to do |
| 599 | + case 'string': // nothing to do |
575 | 600 | break; |
576 | | - case 'integer' : // Force everything using intval() and optionally validate limits |
| 601 | + case 'integer': // Force everything using intval() and optionally validate limits |
577 | 602 | |
578 | 603 | $value = is_array( $value ) ? array_map( 'intval', $value ) : intval( $value ); |
579 | | - $min = isset ( $paramSettings[self :: PARAM_MIN] ) ? $paramSettings[self :: PARAM_MIN] : null; |
580 | | - $max = isset ( $paramSettings[self :: PARAM_MAX] ) ? $paramSettings[self :: PARAM_MAX] : null; |
| 604 | + $min = isset ( $paramSettings[self::PARAM_MIN] ) ? $paramSettings[self::PARAM_MIN] : null; |
| 605 | + $max = isset ( $paramSettings[self::PARAM_MAX] ) ? $paramSettings[self::PARAM_MAX] : null; |
581 | 606 | |
582 | 607 | if ( !is_null( $min ) || !is_null( $max ) ) { |
583 | 608 | $values = is_array( $value ) ? $value : array( $value ); |
— | — | @@ -585,50 +610,56 @@ |
586 | 611 | } |
587 | 612 | } |
588 | 613 | break; |
589 | | - case 'limit' : |
590 | | - if ( !$parseLimit ) |
| 614 | + case 'limit': |
| 615 | + if ( !$parseLimit ) { |
591 | 616 | // Don't do any validation whatsoever |
592 | 617 | break; |
593 | | - if ( !isset ( $paramSettings[self :: PARAM_MAX] ) || !isset ( $paramSettings[self :: PARAM_MAX2] ) ) |
594 | | - ApiBase :: dieDebug( __METHOD__, "MAX1 or MAX2 are not defined for the limit $encParamName" ); |
595 | | - if ( $multi ) |
596 | | - ApiBase :: dieDebug( __METHOD__, "Multi-values not supported for $encParamName" ); |
597 | | - $min = isset ( $paramSettings[self :: PARAM_MIN] ) ? $paramSettings[self :: PARAM_MIN] : 0; |
| 618 | + } |
| 619 | + if ( !isset( $paramSettings[self::PARAM_MAX] ) || !isset( $paramSettings[self::PARAM_MAX2] ) ) { |
| 620 | + ApiBase::dieDebug( __METHOD__, "MAX1 or MAX2 are not defined for the limit $encParamName" ); |
| 621 | + } |
| 622 | + if ( $multi ) { |
| 623 | + ApiBase::dieDebug( __METHOD__, "Multi-values not supported for $encParamName" ); |
| 624 | + } |
| 625 | + $min = isset( $paramSettings[self::PARAM_MIN] ) ? $paramSettings[self::PARAM_MIN] : 0; |
598 | 626 | if ( $value == 'max' ) { |
599 | | - $value = $this->getMain()->canApiHighLimits() ? $paramSettings[self :: PARAM_MAX2] : $paramSettings[self :: PARAM_MAX]; |
600 | | - $this->getResult()->addValue( 'limits', $this->getModuleName(), $value ); |
601 | | - } |
602 | | - else { |
| 627 | + $value = $this->getMain()->canApiHighLimits() ? $paramSettings[self::PARAM_MAX2] : $paramSettings[self::PARAM_MAX]; |
| 628 | + $this->getResult()->addValue( 'limits', $this->getModuleName(), $value ); |
| 629 | + } else { |
603 | 630 | $value = intval( $value ); |
604 | | - $this->validateLimit( $paramName, $value, $min, $paramSettings[self :: PARAM_MAX], $paramSettings[self :: PARAM_MAX2] ); |
| 631 | + $this->validateLimit( $paramName, $value, $min, $paramSettings[self::PARAM_MAX], $paramSettings[self::PARAM_MAX2] ); |
605 | 632 | } |
606 | 633 | break; |
607 | | - case 'boolean' : |
| 634 | + case 'boolean': |
608 | 635 | if ( $multi ) |
609 | | - ApiBase :: dieDebug( __METHOD__, "Multi-values not supported for $encParamName" ); |
| 636 | + ApiBase::dieDebug( __METHOD__, "Multi-values not supported for $encParamName" ); |
610 | 637 | break; |
611 | | - case 'timestamp' : |
612 | | - if ( $multi ) |
613 | | - ApiBase :: dieDebug( __METHOD__, "Multi-values not supported for $encParamName" ); |
| 638 | + case 'timestamp': |
| 639 | + if ( $multi ) { |
| 640 | + ApiBase::dieDebug( __METHOD__, "Multi-values not supported for $encParamName" ); |
| 641 | + } |
614 | 642 | $value = wfTimestamp( TS_UNIX, $value ); |
615 | | - if ( $value === 0 ) |
| 643 | + if ( $value === 0 ) { |
616 | 644 | $this->dieUsage( "Invalid value '$value' for timestamp parameter $encParamName", "badtimestamp_{$encParamName}" ); |
| 645 | + } |
617 | 646 | $value = wfTimestamp( TS_MW, $value ); |
618 | 647 | break; |
619 | | - case 'user' : |
| 648 | + case 'user': |
620 | 649 | $title = Title::makeTitleSafe( NS_USER, $value ); |
621 | | - if ( is_null( $title ) ) |
| 650 | + if ( is_null( $title ) ) { |
622 | 651 | $this->dieUsage( "Invalid value for user parameter $encParamName", "baduser_{$encParamName}" ); |
| 652 | + } |
623 | 653 | $value = $title->getText(); |
624 | 654 | break; |
625 | | - default : |
626 | | - ApiBase :: dieDebug( __METHOD__, "Param $encParamName's type is unknown - $type" ); |
| 655 | + default: |
| 656 | + ApiBase::dieDebug( __METHOD__, "Param $encParamName's type is unknown - $type" ); |
627 | 657 | } |
628 | 658 | } |
629 | 659 | |
630 | 660 | // Throw out duplicates if requested |
631 | | - if ( is_array( $value ) && !$dupes ) |
| 661 | + if ( is_array( $value ) && !$dupes ) { |
632 | 662 | $value = array_unique( $value ); |
| 663 | + } |
633 | 664 | |
634 | 665 | // Set a warning if a deprecated parameter has been passed |
635 | 666 | if ( $deprecated && $value !== false ) { |
— | — | @@ -640,21 +671,22 @@ |
641 | 672 | } |
642 | 673 | |
643 | 674 | /** |
644 | | - * Return an array of values that were given in a 'a|b|c' notation, |
645 | | - * after it optionally validates them against the list allowed values. |
646 | | - * |
647 | | - * @param $valueName string The name of the parameter (for error |
648 | | - * reporting) |
649 | | - * @param $value mixed The value being parsed |
650 | | - * @param $allowMultiple bool Can $value contain more than one value |
651 | | - * separated by '|'? |
652 | | - * @param $allowedValues mixed An array of values to check against. If |
653 | | - * null, all values are accepted. |
654 | | - * @return mixed (allowMultiple ? an_array_of_values : a_single_value) |
655 | | - */ |
| 675 | + * Return an array of values that were given in a 'a|b|c' notation, |
| 676 | + * after it optionally validates them against the list allowed values. |
| 677 | + * |
| 678 | + * @param $valueName string The name of the parameter (for error |
| 679 | + * reporting) |
| 680 | + * @param $value mixed The value being parsed |
| 681 | + * @param $allowMultiple bool Can $value contain more than one value |
| 682 | + * separated by '|'? |
| 683 | + * @param $allowedValues mixed An array of values to check against. If |
| 684 | + * null, all values are accepted. |
| 685 | + * @return mixed (allowMultiple ? an_array_of_values : a_single_value) |
| 686 | + */ |
656 | 687 | protected function parseMultiValue( $valueName, $value, $allowMultiple, $allowedValues ) { |
657 | | - if ( trim( $value ) === "" && $allowMultiple ) |
| 688 | + if ( trim( $value ) === '' && $allowMultiple ) { |
658 | 689 | return array(); |
| 690 | + } |
659 | 691 | |
660 | 692 | // This is a bit awkward, but we want to avoid calling canApiHighLimits() because it unstubs $wgUser |
661 | 693 | $valuesList = explode( '|', $value, self::LIMIT_SML2 + 1 ); |
— | — | @@ -673,16 +705,14 @@ |
674 | 706 | if ( is_array( $allowedValues ) ) { |
675 | 707 | // Check for unknown values |
676 | 708 | $unknown = array_diff( $valuesList, $allowedValues ); |
677 | | - if ( count( $unknown ) ) |
678 | | - { |
679 | | - if ( $allowMultiple ) |
680 | | - { |
681 | | - $s = count( $unknown ) > 1 ? "s" : ""; |
| 709 | + if ( count( $unknown ) ) { |
| 710 | + if ( $allowMultiple ) { |
| 711 | + $s = count( $unknown ) > 1 ? 's' : ''; |
682 | 712 | $vals = implode( ", ", $unknown ); |
683 | 713 | $this->setWarning( "Unrecognized value$s for parameter '$valueName': $vals" ); |
| 714 | + } else { |
| 715 | + $this->dieUsage( "Unrecognized value for parameter '$valueName': {$valuesList[0]}", "unknown_$valueName" ); |
684 | 716 | } |
685 | | - else |
686 | | - $this->dieUsage( "Unrecognized value for parameter '$valueName': {$valuesList[0]}", "unknown_$valueName" ); |
687 | 717 | } |
688 | 718 | // Now throw them out |
689 | 719 | $valuesList = array_intersect( $valuesList, $allowedValues ); |
— | — | @@ -707,8 +737,9 @@ |
708 | 738 | } |
709 | 739 | |
710 | 740 | // Minimum is always validated, whereas maximum is checked only if not running in internal call mode |
711 | | - if ( $this->getMain()->isInternalMode() ) |
| 741 | + if ( $this->getMain()->isInternalMode() ) { |
712 | 742 | return; |
| 743 | + } |
713 | 744 | |
714 | 745 | // Optimization: do not check user's bot status unless really needed -- skips db query |
715 | 746 | // assumes $botMax >= $max |
— | — | @@ -731,11 +762,9 @@ |
732 | 763 | * @param $limit int Maximum length |
733 | 764 | * @return bool True if the array was truncated, false otherwise |
734 | 765 | */ |
735 | | - public static function truncateArray( &$arr, $limit ) |
736 | | - { |
| 766 | + public static function truncateArray( &$arr, $limit ) { |
737 | 767 | $modified = false; |
738 | | - while ( count( $arr ) > $limit ) |
739 | | - { |
| 768 | + while ( count( $arr ) > $limit ) { |
740 | 769 | $junk = array_pop( $arr ); |
741 | 770 | $modified = true; |
742 | 771 | } |
— | — | @@ -919,12 +948,13 @@ |
920 | 949 | */ |
921 | 950 | public function parseMsg( $error ) { |
922 | 951 | $key = array_shift( $error ); |
923 | | - if ( isset( self::$messageMap[$key] ) ) |
924 | | - return array( 'code' => |
| 952 | + if ( isset( self::$messageMap[$key] ) ) { |
| 953 | + return array( 'code' => |
925 | 954 | wfMsgReplaceArgs( self::$messageMap[$key]['code'], $error ), |
926 | 955 | 'info' => |
927 | 956 | wfMsgReplaceArgs( self::$messageMap[$key]['info'], $error ) |
928 | 957 | ); |
| 958 | + } |
929 | 959 | // If the key isn't present, throw an "unknown error" |
930 | 960 | return $this->parseMsg( array( 'unknownerror', $key ) ); |
931 | 961 | } |
— | — | @@ -968,35 +998,35 @@ |
969 | 999 | public function mustBePosted() { |
970 | 1000 | return false; |
971 | 1001 | } |
972 | | - |
| 1002 | + |
973 | 1003 | /** |
974 | | - * Returns the token salt if there is one, '' if the module doesn't require a salt, else false if the module doesn't need a token |
975 | | - * @returns bool |
976 | | - */ |
| 1004 | + * Returns the token salt if there is one, '' if the module doesn't require a salt, else false if the module doesn't need a token |
| 1005 | + * @returns bool |
| 1006 | + */ |
977 | 1007 | public function getTokenSalt() { |
978 | 1008 | return false; |
979 | 1009 | } |
980 | 1010 | |
981 | 1011 | /** |
982 | | - * Returns a list of all possible errors returned by the module |
983 | | - * @return array in the format of array( key, param1, param2, ... ) or array( 'code' => ..., 'info' => ... ) |
984 | | - */ |
| 1012 | + * Returns a list of all possible errors returned by the module |
| 1013 | + * @return array in the format of array( key, param1, param2, ... ) or array( 'code' => ..., 'info' => ... ) |
| 1014 | + */ |
985 | 1015 | public function getPossibleErrors() { |
986 | 1016 | $ret = array(); |
987 | 1017 | |
988 | 1018 | if ( $this->mustBePosted() ) { |
989 | | - $ret[] = array ( 'mustbeposted', $this->getModuleName() ); |
| 1019 | + $ret[] = array( 'mustbeposted', $this->getModuleName() ); |
990 | 1020 | } |
991 | 1021 | |
992 | 1022 | if ( $this->isReadMode() ) { |
993 | | - $ret[] = array ( 'readrequired' ); |
| 1023 | + $ret[] = array( 'readrequired' ); |
994 | 1024 | } |
995 | 1025 | |
996 | 1026 | if ( $this->isWriteMode() ) { |
997 | | - $ret[] = array ( 'writerequired' ); |
998 | | - $ret[] = array ( 'writedisabled' ); |
| 1027 | + $ret[] = array( 'writerequired' ); |
| 1028 | + $ret[] = array( 'writedisabled' ); |
999 | 1029 | } |
1000 | | - |
| 1030 | + |
1001 | 1031 | if ( $this->getTokenSalt() !== false ) { |
1002 | 1032 | $ret[] = array( 'missingparam', 'token' ); |
1003 | 1033 | $ret[] = array( 'sessionfailure' ); |
— | — | @@ -1006,19 +1036,17 @@ |
1007 | 1037 | } |
1008 | 1038 | |
1009 | 1039 | /** |
1010 | | - * Parses a list of errors into a standardised format |
1011 | | - * @param $errors array List of errors. Items can be in the for array( key, param1, param2, ... ) or array( 'code' => ..., 'info' => ... ) |
1012 | | - * @return array Parsed list of errors with items in the form array( 'code' => ..., 'info' => ... ) |
1013 | | - */ |
| 1040 | + * Parses a list of errors into a standardised format |
| 1041 | + * @param $errors array List of errors. Items can be in the for array( key, param1, param2, ... ) or array( 'code' => ..., 'info' => ... ) |
| 1042 | + * @return array Parsed list of errors with items in the form array( 'code' => ..., 'info' => ... ) |
| 1043 | + */ |
1014 | 1044 | public function parseErrors( $errors ) { |
1015 | 1045 | $ret = array(); |
1016 | 1046 | |
1017 | | - foreach ( $errors as $row ) |
1018 | | - { |
| 1047 | + foreach ( $errors as $row ) { |
1019 | 1048 | if ( isset( $row['code'] ) && isset( $row['info'] ) ) { |
1020 | 1049 | $ret[] = $row; |
1021 | | - } |
1022 | | - else { |
| 1050 | + } else { |
1023 | 1051 | $ret[] = $this->parseMsg( $row ); |
1024 | 1052 | } |
1025 | 1053 | } |
— | — | @@ -1034,8 +1062,9 @@ |
1035 | 1063 | * Start module profiling |
1036 | 1064 | */ |
1037 | 1065 | public function profileIn() { |
1038 | | - if ( $this->mTimeIn !== 0 ) |
1039 | | - ApiBase :: dieDebug( __METHOD__, 'called twice without calling profileOut()' ); |
| 1066 | + if ( $this->mTimeIn !== 0 ) { |
| 1067 | + ApiBase::dieDebug( __METHOD__, 'called twice without calling profileOut()' ); |
| 1068 | + } |
1040 | 1069 | $this->mTimeIn = microtime( true ); |
1041 | 1070 | wfProfileIn( $this->getModuleProfileName() ); |
1042 | 1071 | } |
— | — | @@ -1044,10 +1073,12 @@ |
1045 | 1074 | * End module profiling |
1046 | 1075 | */ |
1047 | 1076 | public function profileOut() { |
1048 | | - if ( $this->mTimeIn === 0 ) |
1049 | | - ApiBase :: dieDebug( __METHOD__, 'called without calling profileIn() first' ); |
1050 | | - if ( $this->mDBTimeIn !== 0 ) |
1051 | | - ApiBase :: dieDebug( __METHOD__, 'must be called after database profiling is done with profileDBOut()' ); |
| 1077 | + if ( $this->mTimeIn === 0 ) { |
| 1078 | + ApiBase::dieDebug( __METHOD__, 'called without calling profileIn() first' ); |
| 1079 | + } |
| 1080 | + if ( $this->mDBTimeIn !== 0 ) { |
| 1081 | + ApiBase::dieDebug( __METHOD__, 'must be called after database profiling is done with profileDBOut()' ); |
| 1082 | + } |
1052 | 1083 | |
1053 | 1084 | $this->mModuleTime += microtime( true ) - $this->mTimeIn; |
1054 | 1085 | $this->mTimeIn = 0; |
— | — | @@ -1060,8 +1091,9 @@ |
1061 | 1092 | */ |
1062 | 1093 | public function safeProfileOut() { |
1063 | 1094 | if ( $this->mTimeIn !== 0 ) { |
1064 | | - if ( $this->mDBTimeIn !== 0 ) |
| 1095 | + if ( $this->mDBTimeIn !== 0 ) { |
1065 | 1096 | $this->profileDBOut(); |
| 1097 | + } |
1066 | 1098 | $this->profileOut(); |
1067 | 1099 | } |
1068 | 1100 | } |
— | — | @@ -1071,8 +1103,9 @@ |
1072 | 1104 | * @return float |
1073 | 1105 | */ |
1074 | 1106 | public function getProfileTime() { |
1075 | | - if ( $this->mTimeIn !== 0 ) |
1076 | | - ApiBase :: dieDebug( __METHOD__, 'called without calling profileOut() first' ); |
| 1107 | + if ( $this->mTimeIn !== 0 ) { |
| 1108 | + ApiBase::dieDebug( __METHOD__, 'called without calling profileOut() first' ); |
| 1109 | + } |
1077 | 1110 | return $this->mModuleTime; |
1078 | 1111 | } |
1079 | 1112 | |
— | — | @@ -1085,10 +1118,12 @@ |
1086 | 1119 | * Start module profiling |
1087 | 1120 | */ |
1088 | 1121 | public function profileDBIn() { |
1089 | | - if ( $this->mTimeIn === 0 ) |
1090 | | - ApiBase :: dieDebug( __METHOD__, 'must be called while profiling the entire module with profileIn()' ); |
1091 | | - if ( $this->mDBTimeIn !== 0 ) |
1092 | | - ApiBase :: dieDebug( __METHOD__, 'called twice without calling profileDBOut()' ); |
| 1122 | + if ( $this->mTimeIn === 0 ) { |
| 1123 | + ApiBase::dieDebug( __METHOD__, 'must be called while profiling the entire module with profileIn()' ); |
| 1124 | + } |
| 1125 | + if ( $this->mDBTimeIn !== 0 ) { |
| 1126 | + ApiBase::dieDebug( __METHOD__, 'called twice without calling profileDBOut()' ); |
| 1127 | + } |
1093 | 1128 | $this->mDBTimeIn = microtime( true ); |
1094 | 1129 | wfProfileIn( $this->getModuleProfileName( true ) ); |
1095 | 1130 | } |
— | — | @@ -1097,10 +1132,12 @@ |
1098 | 1133 | * End database profiling |
1099 | 1134 | */ |
1100 | 1135 | public function profileDBOut() { |
1101 | | - if ( $this->mTimeIn === 0 ) |
1102 | | - ApiBase :: dieDebug( __METHOD__, 'must be called while profiling the entire module with profileIn()' ); |
1103 | | - if ( $this->mDBTimeIn === 0 ) |
1104 | | - ApiBase :: dieDebug( __METHOD__, 'called without calling profileDBIn() first' ); |
| 1136 | + if ( $this->mTimeIn === 0 ) { |
| 1137 | + ApiBase::dieDebug( __METHOD__, 'must be called while profiling the entire module with profileIn()' ); |
| 1138 | + } |
| 1139 | + if ( $this->mDBTimeIn === 0 ) { |
| 1140 | + ApiBase::dieDebug( __METHOD__, 'called without calling profileDBIn() first' ); |
| 1141 | + } |
1105 | 1142 | |
1106 | 1143 | $time = microtime( true ) - $this->mDBTimeIn; |
1107 | 1144 | $this->mDBTimeIn = 0; |
— | — | @@ -1115,8 +1152,9 @@ |
1116 | 1153 | * @return float |
1117 | 1154 | */ |
1118 | 1155 | public function getProfileDBTime() { |
1119 | | - if ( $this->mDBTimeIn !== 0 ) |
1120 | | - ApiBase :: dieDebug( __METHOD__, 'called without calling profileDBOut() first' ); |
| 1156 | + if ( $this->mDBTimeIn !== 0 ) { |
| 1157 | + ApiBase::dieDebug( __METHOD__, 'called without calling profileDBOut() first' ); |
| 1158 | + } |
1121 | 1159 | return $this->mDBTime; |
1122 | 1160 | } |
1123 | 1161 | |
— | — | @@ -1129,12 +1167,12 @@ |
1130 | 1168 | public static function debugPrint( $value, $name = 'unknown', $backtrace = false ) { |
1131 | 1169 | print "\n\n<pre><b>Debugging value '$name':</b>\n\n"; |
1132 | 1170 | var_export( $value ); |
1133 | | - if ( $backtrace ) |
| 1171 | + if ( $backtrace ) { |
1134 | 1172 | print "\n" . wfBacktrace(); |
| 1173 | + } |
1135 | 1174 | print "\n</pre>\n"; |
1136 | 1175 | } |
1137 | 1176 | |
1138 | | - |
1139 | 1177 | /** |
1140 | 1178 | * Returns a string that identifies the version of this class. |
1141 | 1179 | * @return string |