* (bug 15845) API: Added pageid/fromid parameter to action=delete/move, making manipu...
[lhc/web/wiklou.git] / includes / api / ApiBase.php
1 <?php
2
3 /*
4 * Created on Sep 5, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 /**
27 * This abstract class implements many basic API functions, and is the base of all API classes.
28 * The class functions are divided into several areas of functionality:
29 *
30 * Module parameters: Derived classes can define getAllowedParams() to specify which parameters to expect,
31 * how to parse and validate them.
32 *
33 * Profiling: various methods to allow keeping tabs on various tasks and their time costs
34 *
35 * Self-documentation: code to allow api to document its own state.
36 *
37 * @ingroup API
38 */
39 abstract class ApiBase {
40
41 // These constants allow modules to specify exactly how to treat incomming parameters.
42
43 const PARAM_DFLT = 0;
44 const PARAM_ISMULTI = 1;
45 const PARAM_TYPE = 2;
46 const PARAM_MAX = 3;
47 const PARAM_MAX2 = 4;
48 const PARAM_MIN = 5;
49 const PARAM_ALLOW_DUPLICATES = 6;
50
51 const LIMIT_BIG1 = 500; // Fast query, std user limit
52 const LIMIT_BIG2 = 5000; // Fast query, bot/sysop limit
53 const LIMIT_SML1 = 50; // Slow query, std user limit
54 const LIMIT_SML2 = 500; // Slow query, bot/sysop limit
55
56 private $mMainModule, $mModuleName, $mModulePrefix;
57
58 /**
59 * Constructor
60 */
61 public function __construct($mainModule, $moduleName, $modulePrefix = '') {
62 $this->mMainModule = $mainModule;
63 $this->mModuleName = $moduleName;
64 $this->mModulePrefix = $modulePrefix;
65 }
66
67 /*****************************************************************************
68 * ABSTRACT METHODS *
69 *****************************************************************************/
70
71 /**
72 * Evaluates the parameters, performs the requested query, and sets up the
73 * result. Concrete implementations of ApiBase must override this method to
74 * provide whatever functionality their module offers. Implementations must
75 * not produce any output on their own and are not expected to handle any
76 * errors.
77 *
78 * The execute method will be invoked directly by ApiMain immediately before
79 * the result of the module is output. Aside from the constructor, implementations
80 * should assume that no other methods will be called externally on the module
81 * before the result is processed.
82 *
83 * The result data should be stored in the result object referred to by
84 * "getResult()". Refer to ApiResult.php for details on populating a result
85 * object.
86 */
87 public abstract function execute();
88
89 /**
90 * Returns a String that identifies the version of the extending class. Typically
91 * includes the class name, the svn revision, timestamp, and last author. May
92 * be severely incorrect in many implementations!
93 */
94 public abstract function getVersion();
95
96 /**
97 * Get the name of the module being executed by this instance
98 */
99 public function getModuleName() {
100 return $this->mModuleName;
101 }
102
103 /**
104 * Get parameter prefix (usually two letters or an empty string).
105 */
106 public function getModulePrefix() {
107 return $this->mModulePrefix;
108 }
109
110 /**
111 * Get the name of the module as shown in the profiler log
112 */
113 public function getModuleProfileName($db = false) {
114 if ($db)
115 return 'API:' . $this->mModuleName . '-DB';
116 else
117 return 'API:' . $this->mModuleName;
118 }
119
120 /**
121 * Get main module
122 */
123 public function getMain() {
124 return $this->mMainModule;
125 }
126
127 /**
128 * Returns true if this module is the main module ($this === $this->mMainModule),
129 * false otherwise.
130 */
131 public function isMain() {
132 return $this === $this->mMainModule;
133 }
134
135 /**
136 * Get the result object. Please refer to the documentation in ApiResult.php
137 * for details on populating and accessing data in a result object.
138 */
139 public function getResult() {
140 // Main module has getResult() method overriden
141 // Safety - avoid infinite loop:
142 if ($this->isMain())
143 ApiBase :: dieDebug(__METHOD__, 'base method was called on main module. ');
144 return $this->getMain()->getResult();
145 }
146
147 /**
148 * Get the result data array
149 */
150 public function & getResultData() {
151 return $this->getResult()->getData();
152 }
153
154 /**
155 * Set warning section for this module. Users should monitor this section to
156 * notice any changes in API.
157 */
158 public function setWarning($warning) {
159 # If there is a warning already, append it to the existing one
160 $data =& $this->getResult()->getData();
161 if(isset($data['warnings'][$this->getModuleName()]))
162 {
163 # Don't add duplicate warnings
164 $warn_regex = preg_quote($warning, '/');
165 if(preg_match("/{$warn_regex}(\\n|$)/", $data['warnings'][$this->getModuleName()]['*']))
166 return;
167 $warning = "{$data['warnings'][$this->getModuleName()]['*']}\n$warning";
168 unset($data['warnings'][$this->getModuleName()]);
169 }
170 $msg = array();
171 ApiResult :: setContent($msg, $warning);
172 $this->getResult()->addValue('warnings', $this->getModuleName(), $msg);
173 }
174
175 /**
176 * If the module may only be used with a certain format module,
177 * it should override this method to return an instance of that formatter.
178 * A value of null means the default format will be used.
179 */
180 public function getCustomPrinter() {
181 return null;
182 }
183
184 /**
185 * Generates help message for this module, or false if there is no description
186 */
187 public function makeHelpMsg() {
188
189 static $lnPrfx = "\n ";
190
191 $msg = $this->getDescription();
192
193 if ($msg !== false) {
194
195 if (!is_array($msg))
196 $msg = array (
197 $msg
198 );
199 $msg = $lnPrfx . implode($lnPrfx, $msg) . "\n";
200
201 if ($this->mustBePosted())
202 $msg .= "\nThis module only accepts POST requests.\n";
203
204 // Parameters
205 $paramsMsg = $this->makeHelpMsgParameters();
206 if ($paramsMsg !== false) {
207 $msg .= "Parameters:\n$paramsMsg";
208 }
209
210 // Examples
211 $examples = $this->getExamples();
212 if ($examples !== false) {
213 if (!is_array($examples))
214 $examples = array (
215 $examples
216 );
217 $msg .= 'Example' . (count($examples) > 1 ? 's' : '') . ":\n ";
218 $msg .= implode($lnPrfx, $examples) . "\n";
219 }
220
221 if ($this->getMain()->getShowVersions()) {
222 $versions = $this->getVersion();
223 $pattern = '(\$.*) ([0-9a-z_]+\.php) (.*\$)';
224 $replacement = '\\0' . "\n " . 'http://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/includes/api/\\2';
225
226 if (is_array($versions)) {
227 foreach ($versions as &$v)
228 $v = eregi_replace($pattern, $replacement, $v);
229 $versions = implode("\n ", $versions);
230 }
231 else
232 $versions = eregi_replace($pattern, $replacement, $versions);
233
234 $msg .= "Version:\n $versions\n";
235 }
236 }
237
238 return $msg;
239 }
240
241 /**
242 * Generates the parameter descriptions for this module, to be displayed in the
243 * module's help.
244 */
245 public function makeHelpMsgParameters() {
246 $params = $this->getFinalParams();
247 if ($params !== false) {
248
249 $paramsDescription = $this->getFinalParamDescription();
250 $msg = '';
251 $paramPrefix = "\n" . str_repeat(' ', 19);
252 foreach ($params as $paramName => $paramSettings) {
253 $desc = isset ($paramsDescription[$paramName]) ? $paramsDescription[$paramName] : '';
254 if (is_array($desc))
255 $desc = implode($paramPrefix, $desc);
256
257 $type = isset($paramSettings[self :: PARAM_TYPE])? $paramSettings[self :: PARAM_TYPE] : null;
258 if (isset ($type)) {
259 if (isset ($paramSettings[self :: PARAM_ISMULTI]))
260 $prompt = 'Values (separate with \'|\'): ';
261 else
262 $prompt = 'One value: ';
263
264 if (is_array($type)) {
265 $choices = array();
266 $nothingPrompt = false;
267 foreach ($type as $t)
268 if ($t=='')
269 $nothingPrompt = 'Can be empty, or ';
270 else
271 $choices[] = $t;
272 $desc .= $paramPrefix . $nothingPrompt . $prompt . implode(', ', $choices);
273 } else {
274 switch ($type) {
275 case 'namespace':
276 // Special handling because namespaces are type-limited, yet they are not given
277 $desc .= $paramPrefix . $prompt . implode(', ', ApiBase :: getValidNamespaces());
278 break;
279 case 'limit':
280 $desc .= $paramPrefix . "No more than {$paramSettings[self :: PARAM_MAX]} ({$paramSettings[self :: PARAM_MAX2]} for bots) allowed.";
281 break;
282 case 'integer':
283 $hasMin = isset($paramSettings[self :: PARAM_MIN]);
284 $hasMax = isset($paramSettings[self :: PARAM_MAX]);
285 if ($hasMin || $hasMax) {
286 if (!$hasMax)
287 $intRangeStr = "The value must be no less than {$paramSettings[self :: PARAM_MIN]}";
288 elseif (!$hasMin)
289 $intRangeStr = "The value must be no more than {$paramSettings[self :: PARAM_MAX]}";
290 else
291 $intRangeStr = "The value must be between {$paramSettings[self :: PARAM_MIN]} and {$paramSettings[self :: PARAM_MAX]}";
292
293 $desc .= $paramPrefix . $intRangeStr;
294 }
295 break;
296 }
297 }
298 }
299
300 $default = is_array($paramSettings) ? (isset ($paramSettings[self :: PARAM_DFLT]) ? $paramSettings[self :: PARAM_DFLT] : null) : $paramSettings;
301 if (!is_null($default) && $default !== false)
302 $desc .= $paramPrefix . "Default: $default";
303
304 $msg .= sprintf(" %-14s - %s\n", $this->encodeParamName($paramName), $desc);
305 }
306 return $msg;
307
308 } else
309 return false;
310 }
311
312 /**
313 * Returns the description string for this module
314 */
315 protected function getDescription() {
316 return false;
317 }
318
319 /**
320 * Returns usage examples for this module. Return null if no examples are available.
321 */
322 protected function getExamples() {
323 return false;
324 }
325
326 /**
327 * Returns an array of allowed parameters (keys) => default value for that parameter.
328 * Don't call this function directly: use getFinalParams() to allow hooks
329 * to modify parameters as needed.
330 */
331 protected function getAllowedParams() {
332 return false;
333 }
334
335 /**
336 * Returns an array of parameter descriptions.
337 * Don't call this functon directly: use getFinalParamDescription() to allow
338 * hooks to modify descriptions as needed.
339 */
340 protected function getParamDescription() {
341 return false;
342 }
343
344 /**
345 * Get final list of parameters, after hooks have had
346 * a chance to tweak it as needed.
347 */
348 public function getFinalParams() {
349 $params = $this->getAllowedParams();
350 wfRunHooks('APIGetAllowedParams', array(&$this, &$params));
351 return $params;
352 }
353
354
355 public function getFinalParamDescription() {
356 $desc = $this->getParamDescription();
357 wfRunHooks('APIGetParamDescription', array(&$this, &$desc));
358 return $desc;
359 }
360
361 /**
362 * This method mangles parameter name based on the prefix supplied to the constructor.
363 * Override this method to change parameter name during runtime
364 */
365 public function encodeParamName($paramName) {
366 return $this->mModulePrefix . $paramName;
367 }
368
369 /**
370 * Using getAllowedParams(), makes an array of the values provided by the user,
371 * with key being the name of the variable, and value - validated value from user or default.
372 * This method can be used to generate local variables using extract().
373 * limit=max will not be parsed if $parseMaxLimit is set to false; use this
374 * when the max limit is not definite, e.g. when getting revisions.
375 */
376 public function extractRequestParams($parseMaxLimit = true) {
377 $params = $this->getFinalParams();
378 $results = array ();
379
380 foreach ($params as $paramName => $paramSettings)
381 $results[$paramName] = $this->getParameterFromSettings($paramName, $paramSettings, $parseMaxLimit);
382
383 return $results;
384 }
385
386 /**
387 * Get a value for the given parameter
388 */
389 protected function getParameter($paramName, $parseMaxLimit = true) {
390 $params = $this->getFinalParams();
391 $paramSettings = $params[$paramName];
392 return $this->getParameterFromSettings($paramName, $paramSettings, $parseMaxLimit);
393 }
394
395 /**
396 * Die if none or more than one of a certain set of parameters is set
397 */
398 public function requireOnlyOneParameter($params) {
399 $required = func_get_args();
400 array_shift($required);
401
402 $intersection = array_intersect(array_keys(array_filter($params,
403 create_function('$x', 'return !is_null($x);')
404 )), $required);
405 if (count($intersection) > 1) {
406 $this->dieUsage('The parameters '.implode(', ', $intersection).' can not be used together', 'invalidparammix');
407 } elseif (count($intersection) == 0) {
408 $this->dieUsage('One of the parameters '.implode(', ', $required).' is required', 'missingparam');
409 }
410 }
411
412 /**
413 * Returns an array of the namespaces (by integer id) that exist on the
414 * wiki. Used primarily in help documentation.
415 */
416 public static function getValidNamespaces() {
417 static $mValidNamespaces = null;
418 if (is_null($mValidNamespaces)) {
419
420 global $wgContLang;
421 $mValidNamespaces = array ();
422 foreach (array_keys($wgContLang->getNamespaces()) as $ns) {
423 if ($ns >= 0)
424 $mValidNamespaces[] = $ns;
425 }
426 }
427 return $mValidNamespaces;
428 }
429
430 /**
431 * Using the settings determine the value for the given parameter
432 *
433 * @param $paramName String: parameter name
434 * @param $paramSettings Mixed: default value or an array of settings using PARAM_* constants.
435 * @param $parseMaxLimit Boolean: parse limit when max is given?
436 */
437 protected function getParameterFromSettings($paramName, $paramSettings, $parseMaxLimit) {
438
439 // Some classes may decide to change parameter names
440 $encParamName = $this->encodeParamName($paramName);
441
442 if (!is_array($paramSettings)) {
443 $default = $paramSettings;
444 $multi = false;
445 $type = gettype($paramSettings);
446 $dupes = false;
447 } else {
448 $default = isset ($paramSettings[self :: PARAM_DFLT]) ? $paramSettings[self :: PARAM_DFLT] : null;
449 $multi = isset ($paramSettings[self :: PARAM_ISMULTI]) ? $paramSettings[self :: PARAM_ISMULTI] : false;
450 $type = isset ($paramSettings[self :: PARAM_TYPE]) ? $paramSettings[self :: PARAM_TYPE] : null;
451 $dupes = isset ($paramSettings[self:: PARAM_ALLOW_DUPLICATES]) ? $paramSettings[self :: PARAM_ALLOW_DUPLICATES] : false;
452
453 // When type is not given, and no choices, the type is the same as $default
454 if (!isset ($type)) {
455 if (isset ($default))
456 $type = gettype($default);
457 else
458 $type = 'NULL'; // allow everything
459 }
460 }
461
462 if ($type == 'boolean') {
463 if (isset ($default) && $default !== false) {
464 // Having a default value of anything other than 'false' is pointless
465 ApiBase :: dieDebug(__METHOD__, "Boolean param $encParamName's default is set to '$default'");
466 }
467
468 $value = $this->getMain()->getRequest()->getCheck($encParamName);
469 } else {
470 $value = $this->getMain()->getRequest()->getVal($encParamName, $default);
471
472 if (isset ($value) && $type == 'namespace')
473 $type = ApiBase :: getValidNamespaces();
474 }
475
476 if (isset ($value) && ($multi || is_array($type)))
477 $value = $this->parseMultiValue($encParamName, $value, $multi, is_array($type) ? $type : null);
478
479 // More validation only when choices were not given
480 // choices were validated in parseMultiValue()
481 if (isset ($value)) {
482 if (!is_array($type)) {
483 switch ($type) {
484 case 'NULL' : // nothing to do
485 break;
486 case 'string' : // nothing to do
487 break;
488 case 'integer' : // Force everything using intval() and optionally validate limits
489
490 $value = is_array($value) ? array_map('intval', $value) : intval($value);
491 $min = isset ($paramSettings[self :: PARAM_MIN]) ? $paramSettings[self :: PARAM_MIN] : null;
492 $max = isset ($paramSettings[self :: PARAM_MAX]) ? $paramSettings[self :: PARAM_MAX] : null;
493
494 if (!is_null($min) || !is_null($max)) {
495 $values = is_array($value) ? $value : array($value);
496 foreach ($values as $v) {
497 $this->validateLimit($paramName, $v, $min, $max);
498 }
499 }
500 break;
501 case 'limit' :
502 if (!isset ($paramSettings[self :: PARAM_MAX]) || !isset ($paramSettings[self :: PARAM_MAX2]))
503 ApiBase :: dieDebug(__METHOD__, "MAX1 or MAX2 are not defined for the limit $encParamName");
504 if ($multi)
505 ApiBase :: dieDebug(__METHOD__, "Multi-values not supported for $encParamName");
506 $min = isset ($paramSettings[self :: PARAM_MIN]) ? $paramSettings[self :: PARAM_MIN] : 0;
507 if( $value == 'max' ) {
508 if( $parseMaxLimit ) {
509 $value = $this->getMain()->canApiHighLimits() ? $paramSettings[self :: PARAM_MAX2] : $paramSettings[self :: PARAM_MAX];
510 $this->getResult()->addValue( 'limits', $this->getModuleName(), $value );
511 $this->validateLimit($paramName, $value, $min, $paramSettings[self :: PARAM_MAX], $paramSettings[self :: PARAM_MAX2]);
512 }
513 }
514 else {
515 $value = intval($value);
516 $this->validateLimit($paramName, $value, $min, $paramSettings[self :: PARAM_MAX], $paramSettings[self :: PARAM_MAX2]);
517 }
518 break;
519 case 'boolean' :
520 if ($multi)
521 ApiBase :: dieDebug(__METHOD__, "Multi-values not supported for $encParamName");
522 break;
523 case 'timestamp' :
524 if ($multi)
525 ApiBase :: dieDebug(__METHOD__, "Multi-values not supported for $encParamName");
526 $value = wfTimestamp(TS_UNIX, $value);
527 if ($value === 0)
528 $this->dieUsage("Invalid value '$value' for timestamp parameter $encParamName", "badtimestamp_{$encParamName}");
529 $value = wfTimestamp(TS_MW, $value);
530 break;
531 case 'user' :
532 $title = Title::makeTitleSafe( NS_USER, $value );
533 if ( is_null( $title ) )
534 $this->dieUsage("Invalid value for user parameter $encParamName", "baduser_{$encParamName}");
535 $value = $title->getText();
536 break;
537 default :
538 ApiBase :: dieDebug(__METHOD__, "Param $encParamName's type is unknown - $type");
539 }
540 }
541
542 // Throw out duplicates if requested
543 if (is_array($value) && !$dupes)
544 $value = array_unique($value);
545 }
546
547 return $value;
548 }
549
550 /**
551 * Return an array of values that were given in a 'a|b|c' notation,
552 * after it optionally validates them against the list allowed values.
553 *
554 * @param valueName - The name of the parameter (for error reporting)
555 * @param value - The value being parsed
556 * @param allowMultiple - Can $value contain more than one value separated by '|'?
557 * @param allowedValues - An array of values to check against. If null, all values are accepted.
558 * @return (allowMultiple ? an_array_of_values : a_single_value)
559 */
560 protected function parseMultiValue($valueName, $value, $allowMultiple, $allowedValues) {
561 if( trim($value) === "" )
562 return array();
563 $sizeLimit = $this->mMainModule->canApiHighLimits() ? self::LIMIT_SML2 : self::LIMIT_SML1;
564 $valuesList = explode('|', $value, $sizeLimit + 1);
565 if( count($valuesList) == $sizeLimit + 1 ) {
566 $junk = array_pop($valuesList); // kill last jumbled param
567 // Set a warning too
568 $this->setWarning("Too many values supplied for parameter '$valueName': the limit is $sizeLimit");
569 }
570 if (!$allowMultiple && count($valuesList) != 1) {
571 $possibleValues = is_array($allowedValues) ? "of '" . implode("', '", $allowedValues) . "'" : '';
572 $this->dieUsage("Only one $possibleValues is allowed for parameter '$valueName'", "multival_$valueName");
573 }
574 if (is_array($allowedValues)) {
575 # Check for unknown values
576 $unknown = array_diff($valuesList, $allowedValues);
577 if(!empty($unknown))
578 {
579 if($allowMultiple)
580 {
581 $s = count($unknown) > 1 ? "s" : "";
582 $vals = implode(", ", $unknown);
583 $this->setWarning("Unrecognized value$s for parameter '$valueName': $vals");
584 }
585 else
586 $this->dieUsage("Unrecognized value for parameter '$valueName': {$valuesList[0]}", "unknown_$valueName");
587 }
588 # Now throw them out
589 $valuesList = array_intersect($valuesList, $allowedValues);
590 }
591
592 return $allowMultiple ? $valuesList : $valuesList[0];
593 }
594
595 /**
596 * Validate the value against the minimum and user/bot maximum limits. Prints usage info on failure.
597 */
598 function validateLimit($paramName, $value, $min, $max, $botMax = null) {
599 if (!is_null($min) && $value < $min) {
600 $this->dieUsage($this->encodeParamName($paramName) . " may not be less than $min (set to $value)", $paramName);
601 }
602
603 // Minimum is always validated, whereas maximum is checked only if not running in internal call mode
604 if ($this->getMain()->isInternalMode())
605 return;
606
607 // Optimization: do not check user's bot status unless really needed -- skips db query
608 // assumes $botMax >= $max
609 if (!is_null($max) && $value > $max) {
610 if (!is_null($botMax) && $this->getMain()->canApiHighLimits()) {
611 if ($value > $botMax) {
612 $this->dieUsage($this->encodeParamName($paramName) . " may not be over $botMax (set to $value) for bots or sysops", $paramName);
613 }
614 } else {
615 $this->dieUsage($this->encodeParamName($paramName) . " may not be over $max (set to $value) for users", $paramName);
616 }
617 }
618 }
619
620 /**
621 * Call main module's error handler
622 */
623 public function dieUsage($description, $errorCode, $httpRespCode = 0) {
624 throw new UsageException($description, $this->encodeParamName($errorCode), $httpRespCode);
625 }
626
627 /**
628 * Array that maps message keys to error messages. $1 and friends are replaced.
629 */
630 public static $messageMap = array(
631 // This one MUST be present, or dieUsageMsg() will recurse infinitely
632 'unknownerror' => array('code' => 'unknownerror', 'info' => "Unknown error: ``\$1''"),
633 'unknownerror-nocode' => array('code' => 'unknownerror', 'info' => 'Unknown error'),
634
635 // Messages from Title::getUserPermissionsErrors()
636 'ns-specialprotected' => array('code' => 'unsupportednamespace', 'info' => "Pages in the Special namespace can't be edited"),
637 'protectedinterface' => array('code' => 'protectednamespace-interface', 'info' => "You're not allowed to edit interface messages"),
638 'namespaceprotected' => array('code' => 'protectednamespace', 'info' => "You're not allowed to edit pages in the ``\$1'' namespace"),
639 'customcssjsprotected' => array('code' => 'customcssjsprotected', 'info' => "You're not allowed to edit custom CSS and JavaScript pages"),
640 'cascadeprotected' => array('code' => 'cascadeprotected', 'info' =>"The page you're trying to edit is protected because it's included in a cascade-protected page"),
641 'protectedpagetext' => array('code' => 'protectedpage', 'info' => "The ``\$1'' right is required to edit this page"),
642 'protect-cantedit' => array('code' => 'cantedit', 'info' => "You can't protect this page because you can't edit it"),
643 'badaccess-group0' => array('code' => 'permissiondenied', 'info' => "Permission denied"), // Generic permission denied message
644 'badaccess-groups' => array('code' => 'permissiondenied', 'info' => "Permission denied"),
645 'titleprotected' => array('code' => 'protectedtitle', 'info' => "This title has been protected from creation"),
646 'nocreate-loggedin' => array('code' => 'cantcreate', 'info' => "You don't have permission to create new pages"),
647 'nocreatetext' => array('code' => 'cantcreate-anon', 'info' => "Anonymous users can't create new pages"),
648 'movenologintext' => array('code' => 'cantmove-anon', 'info' => "Anonymous users can't move pages"),
649 'movenotallowed' => array('code' => 'cantmove', 'info' => "You don't have permission to move pages"),
650 'confirmedittext' => array('code' => 'confirmemail', 'info' => "You must confirm your e-mail address before you can edit"),
651 'blockedtext' => array('code' => 'blocked', 'info' => "You have been blocked from editing"),
652 'autoblockedtext' => array('code' => 'autoblocked', 'info' => "Your IP address has been blocked automatically, because it was used by a blocked user"),
653
654 // Miscellaneous interface messages
655 'actionthrottledtext' => array('code' => 'ratelimited', 'info' => "You've exceeded your rate limit. Please wait some time and try again"),
656 'alreadyrolled' => array('code' => 'alreadyrolled', 'info' => "The page you tried to rollback was already rolled back"),
657 'cantrollback' => array('code' => 'onlyauthor', 'info' => "The page you tried to rollback only has one author"),
658 'readonlytext' => array('code' => 'readonly', 'info' => "The wiki is currently in read-only mode"),
659 'sessionfailure' => array('code' => 'badtoken', 'info' => "Invalid token"),
660 'cannotdelete' => array('code' => 'cantdelete', 'info' => "Couldn't delete ``\$1''. Maybe it was deleted already by someone else"),
661 'notanarticle' => array('code' => 'missingtitle', 'info' => "The page you requested doesn't exist"),
662 'selfmove' => array('code' => 'selfmove', 'info' => "Can't move a page to itself"),
663 'immobile_namespace' => array('code' => 'immobilenamespace', 'info' => "You tried to move pages from or to a namespace that is protected from moving"),
664 'articleexists' => array('code' => 'articleexists', 'info' => "The destination article already exists and is not a redirect to the source article"),
665 'protectedpage' => array('code' => 'protectedpage', 'info' => "You don't have permission to perform this move"),
666 'hookaborted' => array('code' => 'hookaborted', 'info' => "The modification you tried to make was aborted by an extension hook"),
667 'cantmove-titleprotected' => array('code' => 'protectedtitle', 'info' => "The destination article has been protected from creation"),
668 'imagenocrossnamespace' => array('code' => 'nonfilenamespace', 'info' => "Can't move a file to a non-file namespace"),
669 'imagetypemismatch' => array('code' => 'filetypemismatch', 'info' => "The new file extension doesn't match its type"),
670 // 'badarticleerror' => shouldn't happen
671 // 'badtitletext' => shouldn't happen
672 'ip_range_invalid' => array('code' => 'invalidrange', 'info' => "Invalid IP range"),
673 'range_block_disabled' => array('code' => 'rangedisabled', 'info' => "Blocking IP ranges has been disabled"),
674 'nosuchusershort' => array('code' => 'nosuchuser', 'info' => "The user you specified doesn't exist"),
675 'badipaddress' => array('code' => 'invalidip', 'info' => "Invalid IP address specified"),
676 'ipb_expiry_invalid' => array('code' => 'invalidexpiry', 'info' => "Invalid expiry time"),
677 'ipb_already_blocked' => array('code' => 'alreadyblocked', 'info' => "The user you tried to block was already blocked"),
678 'ipb_blocked_as_range' => array('code' => 'blockedasrange', 'info' => "IP address ``\$1'' was blocked as part of range ``\$2''. You can't unblock the IP invidually, but you can unblock the range as a whole."),
679 'ipb_cant_unblock' => array('code' => 'cantunblock', 'info' => "The block you specified was not found. It may have been unblocked already"),
680 'mailnologin' => array('code' => 'cantsend', 'info' => "You're not logged in or you don't have a confirmed e-mail address, so you can't send e-mail"),
681 'usermaildisabled' => array('code' => 'usermaildisabled', 'info' => "User email has been disabled"),
682 'blockedemailuser' => array('code' => 'blockedfrommail', 'info' => "You have been blocked from sending e-mail"),
683 'notarget' => array('code' => 'notarget', 'info' => "You have not specified a valid target for this action"),
684 'noemail' => array('code' => 'noemail', 'info' => "The user has not specified a valid e-mail address, or has chosen not to receive e-mail from other users"),
685 'rcpatroldisabled' => array('code' => 'patroldisabled', 'info' => "Patrolling is disabled on this wiki"),
686 'markedaspatrollederror-noautopatrol' => array('code' => 'noautopatrol', 'info' => "You don't have permission to patrol your own changes"),
687
688 // API-specific messages
689 'missingparam' => array('code' => 'no$1', 'info' => "The \$1 parameter must be set"),
690 'invalidtitle' => array('code' => 'invalidtitle', 'info' => "Bad title ``\$1''"),
691 'nosuchpageid' => array('code' => 'nosuchpageid', 'info' => "There is no page with ID \$1"),
692 'invaliduser' => array('code' => 'invaliduser', 'info' => "Invalid username ``\$1''"),
693 'invalidexpiry' => array('code' => 'invalidexpiry', 'info' => "Invalid expiry time ``\$1''"),
694 'pastexpiry' => array('code' => 'pastexpiry', 'info' => "Expiry time ``\$1'' is in the past"),
695 'create-titleexists' => array('code' => 'create-titleexists', 'info' => "Existing titles can't be protected with 'create'"),
696 'missingtitle-createonly' => array('code' => 'missingtitle-createonly', 'info' => "Missing titles can only be protected with 'create'"),
697 'cantblock' => array('code' => 'cantblock', 'info' => "You don't have permission to block users"),
698 'canthide' => array('code' => 'canthide', 'info' => "You don't have permission to hide user names from the block log"),
699 'cantblock-email' => array('code' => 'cantblock-email', 'info' => "You don't have permission to block users from sending e-mail through the wiki"),
700 'unblock-notarget' => array('code' => 'notarget', 'info' => "Either the id or the user parameter must be set"),
701 'unblock-idanduser' => array('code' => 'idanduser', 'info' => "The id and user parameters can't be used together"),
702 'cantunblock' => array('code' => 'permissiondenied', 'info' => "You don't have permission to unblock users"),
703 'cannotundelete' => array('code' => 'cantundelete', 'info' => "Couldn't undelete: the requested revisions may not exist, or may have been undeleted already"),
704 'permdenied-undelete' => array('code' => 'permissiondenied', 'info' => "You don't have permission to restore deleted revisions"),
705 'createonly-exists' => array('code' => 'articleexists', 'info' => "The article you tried to create has been created already"),
706 'nocreate-missing' => array('code' => 'missingtitle', 'info' => "The article you tried to edit doesn't exist"),
707 'nosuchrcid' => array('code' => 'nosuchrcid', 'info' => "There is no change with rcid ``\$1''"),
708 'cantpurge' => array('code' => 'cantpurge', 'info' => "Only users with the 'purge' right can purge pages via the API"),
709 'protect-invalidaction' => array('code' => 'protect-invalidaction', 'info' => "Invalid protection type ``\$1''"),
710 'protect-invalidlevel' => array('code' => 'protect-invalidlevel', 'info' => "Invalid protection level ``\$1''"),
711 'toofewexpiries' => array('code' => 'toofewexpiries', 'info' => "\$1 expiry timestamps were provided where \$2 were needed"),
712
713
714 // ApiEditPage messages
715 'noimageredirect-anon' => array('code' => 'noimageredirect-anon', 'info' => "Anonymous users can't create image redirects"),
716 'noimageredirect-logged' => array('code' => 'noimageredirect', 'info' => "You don't have permission to create image redirects"),
717 'spamdetected' => array('code' => 'spamdetected', 'info' => "Your edit was refused because it contained a spam fragment: ``\$1''"),
718 'filtered' => array('code' => 'filtered', 'info' => "The filter callback function refused your edit"),
719 'contenttoobig' => array('code' => 'contenttoobig', 'info' => "The content you supplied exceeds the article size limit of \$1 bytes"),
720 'noedit-anon' => array('code' => 'noedit-anon', 'info' => "Anonymous users can't edit pages"),
721 'noedit' => array('code' => 'noedit', 'info' => "You don't have permission to edit pages"),
722 'wasdeleted' => array('code' => 'pagedeleted', 'info' => "The page has been deleted since you fetched its timestamp"),
723 'blankpage' => array('code' => 'emptypage', 'info' => "Creating new, empty pages is not allowed"),
724 'editconflict' => array('code' => 'editconflict', 'info' => "Edit conflict detected"),
725 'hashcheckfailed' => array('code' => 'badmd5', 'info' => "The supplied MD5 hash was incorrect"),
726 'missingtext' => array('code' => 'notext', 'info' => "One of the text, appendtext and prependtext parameters must be set"),
727 'emptynewsection' => array('code' => 'emptynewsection', 'info' => 'Creating empty new sections is not possible.'),
728 );
729
730 /**
731 * Output the error message related to a certain array
732 * @param array $error Element of a getUserPermissionsErrors()
733 */
734 public function dieUsageMsg($error) {
735 $key = array_shift($error);
736 if(isset(self::$messageMap[$key]))
737 $this->dieUsage(wfMsgReplaceArgs(self::$messageMap[$key]['info'], $error), wfMsgReplaceArgs(self::$messageMap[$key]['code'], $error));
738 // If the key isn't present, throw an "unknown error"
739 $this->dieUsageMsg(array('unknownerror', $key));
740 }
741
742 /**
743 * Internal code errors should be reported with this method
744 */
745 protected static function dieDebug($method, $message) {
746 wfDebugDieBacktrace("Internal error in $method: $message");
747 }
748
749 /**
750 * Indicates if API needs to check maxlag
751 */
752 public function shouldCheckMaxlag() {
753 return true;
754 }
755
756 /**
757 * Indicates if this module requires edit mode
758 */
759 public function isEditMode() {
760 return false;
761 }
762
763 /**
764 * Indicates whether this module must be called with a POST request
765 */
766 public function mustBePosted() {
767 return false;
768 }
769
770
771 /**
772 * Profiling: total module execution time
773 */
774 private $mTimeIn = 0, $mModuleTime = 0;
775
776 /**
777 * Start module profiling
778 */
779 public function profileIn() {
780 if ($this->mTimeIn !== 0)
781 ApiBase :: dieDebug(__METHOD__, 'called twice without calling profileOut()');
782 $this->mTimeIn = microtime(true);
783 wfProfileIn($this->getModuleProfileName());
784 }
785
786 /**
787 * End module profiling
788 */
789 public function profileOut() {
790 if ($this->mTimeIn === 0)
791 ApiBase :: dieDebug(__METHOD__, 'called without calling profileIn() first');
792 if ($this->mDBTimeIn !== 0)
793 ApiBase :: dieDebug(__METHOD__, 'must be called after database profiling is done with profileDBOut()');
794
795 $this->mModuleTime += microtime(true) - $this->mTimeIn;
796 $this->mTimeIn = 0;
797 wfProfileOut($this->getModuleProfileName());
798 }
799
800 /**
801 * When modules crash, sometimes it is needed to do a profileOut() regardless
802 * of the profiling state the module was in. This method does such cleanup.
803 */
804 public function safeProfileOut() {
805 if ($this->mTimeIn !== 0) {
806 if ($this->mDBTimeIn !== 0)
807 $this->profileDBOut();
808 $this->profileOut();
809 }
810 }
811
812 /**
813 * Total time the module was executed
814 */
815 public function getProfileTime() {
816 if ($this->mTimeIn !== 0)
817 ApiBase :: dieDebug(__METHOD__, 'called without calling profileOut() first');
818 return $this->mModuleTime;
819 }
820
821 /**
822 * Profiling: database execution time
823 */
824 private $mDBTimeIn = 0, $mDBTime = 0;
825
826 /**
827 * Start module profiling
828 */
829 public function profileDBIn() {
830 if ($this->mTimeIn === 0)
831 ApiBase :: dieDebug(__METHOD__, 'must be called while profiling the entire module with profileIn()');
832 if ($this->mDBTimeIn !== 0)
833 ApiBase :: dieDebug(__METHOD__, 'called twice without calling profileDBOut()');
834 $this->mDBTimeIn = microtime(true);
835 wfProfileIn($this->getModuleProfileName(true));
836 }
837
838 /**
839 * End database profiling
840 */
841 public function profileDBOut() {
842 if ($this->mTimeIn === 0)
843 ApiBase :: dieDebug(__METHOD__, 'must be called while profiling the entire module with profileIn()');
844 if ($this->mDBTimeIn === 0)
845 ApiBase :: dieDebug(__METHOD__, 'called without calling profileDBIn() first');
846
847 $time = microtime(true) - $this->mDBTimeIn;
848 $this->mDBTimeIn = 0;
849
850 $this->mDBTime += $time;
851 $this->getMain()->mDBTime += $time;
852 wfProfileOut($this->getModuleProfileName(true));
853 }
854
855 /**
856 * Total time the module used the database
857 */
858 public function getProfileDBTime() {
859 if ($this->mDBTimeIn !== 0)
860 ApiBase :: dieDebug(__METHOD__, 'called without calling profileDBOut() first');
861 return $this->mDBTime;
862 }
863
864 public static function debugPrint($value, $name = 'unknown', $backtrace = false) {
865 print "\n\n<pre><b>Debuging value '$name':</b>\n\n";
866 var_export($value);
867 if ($backtrace)
868 print "\n" . wfBacktrace();
869 print "\n</pre>\n";
870 }
871
872
873 /**
874 * Returns a String that identifies the version of this class.
875 */
876 public static function getBaseVersion() {
877 return __CLASS__ . ': $Id$';
878 }
879 }