r17064 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r17063‎ | r17064 | r17065 >
Date:02:01, 17 October 2006
Author:yurik
Status:old
Tags:
Comment:
API * Better log events info * Added RAW debugging format
Modified paths:
  • /trunk/phase3/includes/api/ApiBase.php (modified) (history)
  • /trunk/phase3/includes/api/ApiFormatJson.php (modified) (history)
  • /trunk/phase3/includes/api/ApiFormatXml.php (modified) (history)
  • /trunk/phase3/includes/api/ApiMain.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryAllpages.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryLogEvents.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryRevisions.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryWatchlist.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiQueryRevisions.php
@@ -245,11 +245,10 @@
246246 )
247247 ),
248248 'limit' => array (
249 - ApiBase :: PARAM_DFLT => null,
250249 ApiBase :: PARAM_TYPE => 'limit',
251 - ApiBase :: PARAM_MIN => 0,
252 - ApiBase :: PARAM_MAX1 => 50,
253 - ApiBase :: PARAM_MAX2 => 500
 250+ ApiBase :: PARAM_MIN => 1,
 251+ ApiBase :: PARAM_MAX1 => ApiBase :: LIMIT_SML1,
 252+ ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_SML2
254253 ),
255254 'startid' => array (
256255 ApiBase :: PARAM_TYPE => 'integer'
Index: trunk/phase3/includes/api/ApiFormatXml.php
@@ -104,8 +104,6 @@
105105 $subElements = array ();
106106 foreach ($elemValue as $subElemId => & $subElemValue) {
107107 if (gettype($subElemId) === 'integer') {
108 - if (!is_array($subElemValue))
109 - ApiBase :: dieDebug(__METHOD__, "($elemName, ...) has a scalar indexed value.");
110108 $indElements[] = $subElemValue;
111109 unset ($elemValue[$subElemId]);
112110 } elseif (is_array($subElemValue)) {
@@ -115,7 +113,7 @@
116114 }
117115
118116 if (is_null($subElemIndName) && !empty ($indElements))
119 - ApiBase :: dieDebug(__METHOD__, "($elemName, ...) has integer keys without _element value");
 117+ ApiBase :: dieDebug(__METHOD__, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName().");
120118
121119 if (!empty ($subElements) && !empty ($indElements) && !is_null($subElemContent))
122120 ApiBase :: dieDebug(__METHOD__, "($elemName, ...) has content and subelements");
Index: trunk/phase3/includes/api/ApiBase.php
@@ -35,6 +35,11 @@
3636 const PARAM_MAX2 = 4;
3737 const PARAM_MIN = 5;
3838
 39+ const LIMIT_BIG1 = 500; // Fast query, user's limit
 40+ const LIMIT_BIG2 = 5000; // Fast query, bot's limit
 41+ const LIMIT_SML1 = 50; // Slow query, user's limit
 42+ const LIMIT_SML2 = 500; // Slow query, bot's limit
 43+
3944 private $mMainModule, $mModuleName, $mParamPrefix;
4045
4146 /**
@@ -42,7 +47,7 @@
4348 */
4449 public function __construct($mainModule, $moduleName, $paramPrefix = '') {
4550 $this->mMainModule = $mainModule;
46 - $this->mModuleName = $moduleName;
 51+ $this->mModuleName = $moduleName;
4752 $this->mParamPrefix = $paramPrefix;
4853 }
4954
@@ -51,22 +56,22 @@
5257 */
5358 public abstract function execute();
5459
55 - /**
56 - * Get the name of the module being executed by this instance
57 - */
58 - public function getModuleName() {
59 - return $this->mModuleName;
60 - }
 60+ /**
 61+ * Get the name of the module being executed by this instance
 62+ */
 63+ public function getModuleName() {
 64+ return $this->mModuleName;
 65+ }
6166
62 - /**
63 - * Get the name of the module as shown in the profiler log
64 - */
65 - public function getModuleProfileName($db = false) {
66 - if ($db)
67 - return 'API:' . $this->mModuleName . '-DB';
68 - else
69 - return 'API:' . $this->mModuleName;
70 - }
 67+ /**
 68+ * Get the name of the module as shown in the profiler log
 69+ */
 70+ public function getModuleProfileName($db = false) {
 71+ if ($db)
 72+ return 'API:' . $this->mModuleName . '-DB';
 73+ else
 74+ return 'API:' . $this->mModuleName;
 75+ }
7176
7277 /**
7378 * Get main module
@@ -161,23 +166,21 @@
162167 $paramsDescription = $this->getParamDescription();
163168 $msg = '';
164169 $paramPrefix = "\n" . str_repeat(' ', 19);
165 - foreach ($params as $paramName => &$paramSettings) {
 170+ foreach ($params as $paramName => & $paramSettings) {
166171 $desc = isset ($paramsDescription[$paramName]) ? $paramsDescription[$paramName] : '';
167172 if (is_array($desc))
168173 $desc = implode($paramPrefix, $desc);
169174 if (isset ($paramSettings[self :: PARAM_TYPE])) {
170175 $type = $paramSettings[self :: PARAM_TYPE];
171176 if (is_array($type)) {
172 - $desc .= $paramPrefix . 'Allowed values: '. implode(', ', $type);
 177+ $desc .= $paramPrefix . 'Allowed values: ' . implode(', ', $type);
173178 }
174179 }
175 -
176 - $default = is_array($paramSettings)
177 - ? (isset ($paramSettings[self :: PARAM_DFLT]) ? $paramSettings[self :: PARAM_DFLT] : null)
178 - : $paramSettings;
 180+
 181+ $default = is_array($paramSettings) ? (isset ($paramSettings[self :: PARAM_DFLT]) ? $paramSettings[self :: PARAM_DFLT] : null) : $paramSettings;
179182 if (!is_null($default) && $default !== false)
180183 $desc .= $paramPrefix . "Default: $default";
181 -
 184+
182185 $msg .= sprintf(" %-14s - %s\n", $this->encodeParamName($paramName), $desc);
183186 }
184187 return $msg;
@@ -213,7 +216,7 @@
214217 protected function getParamDescription() {
215218 return false;
216219 }
217 -
 220+
218221 /**
219222 * This method mangles parameter name based on the prefix supplied to the constructor.
220223 * Override this method to change parameter name during runtime
@@ -250,7 +253,7 @@
251254 * Using the settings determine the value for the given parameter
252255 * @param $paramName String: parameter name
253256 * @param $paramSettings Mixed: default value or an array of settings using PARAM_* constants.
254 - */
 257+ */
255258 protected function getParameterFromSettings($paramName, $paramSettings) {
256259
257260 // Some classes may decide to change parameter names
@@ -284,7 +287,7 @@
285288 } else {
286289 $value = $this->getMain()->getRequest()->getVal($paramName, $default);
287290 }
288 -
 291+
289292 if (isset ($value) && ($multi || is_array($type)))
290293 $value = $this->parseMultiValue($paramName, $value, $multi, is_array($type) ? $type : null);
291294
@@ -328,7 +331,7 @@
329332 // There should never be any duplicate values in a list
330333 if (is_array($value))
331334 $value = array_unique($value);
332 -
 335+
333336 return $value;
334337 }
335338
@@ -418,10 +421,22 @@
419422
420423 $this->mModuleTime += microtime(true) - $this->mTimeIn;
421424 $this->mTimeIn = 0;
422 - wfProfileOut($this->getModuleProfileName());
 425+ wfProfileOut($this->getModuleProfileName());
423426 }
424427
425428 /**
 429+ * When modules crash, sometimes it is needed to do a profileOut() regardless
 430+ * of the profiling state the module was in. This method does such cleanup.
 431+ */
 432+ public function safeProfileOut() {
 433+ if ($this->mTimeIn !== 0) {
 434+ if ($this->mDBTimeIn !== 0)
 435+ $this->profileDBOut();
 436+ $this->profileOut();
 437+ }
 438+ }
 439+
 440+ /**
426441 * Total time the module was executed
427442 */
428443 public function getProfileTime() {
@@ -474,7 +489,7 @@
475490 }
476491
477492 public abstract function getVersion();
478 -
 493+
479494 public static function getBaseVersion() {
480495 return __CLASS__ . ': $Id$';
481496 }
Index: trunk/phase3/includes/api/ApiQueryAllpages.php
@@ -142,8 +142,8 @@
143143 ApiBase :: PARAM_DFLT => 10,
144144 ApiBase :: PARAM_TYPE => 'limit',
145145 ApiBase :: PARAM_MIN => 1,
146 - ApiBase :: PARAM_MAX1 => 500,
147 - ApiBase :: PARAM_MAX2 => 5000
 146+ ApiBase :: PARAM_MAX1 => ApiBase :: LIMIT_BIG1,
 147+ ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
148148 ));
149149 }
150150
Index: trunk/phase3/includes/api/ApiFormatJson.php
@@ -31,14 +31,21 @@
3232
3333 class ApiFormatJson extends ApiFormatBase {
3434
 35+ private $mIsRaw;
 36+
3537 public function __construct($main, $format) {
3638 parent :: __construct($main, $format);
 39+ $this->mIsRaw = ($format === 'raw' || $format === 'rawfm');
3740 }
3841
3942 public function getMimeType() {
4043 return 'application/json';
4144 }
42 -
 45+
 46+ public function getNeedsRawData() {
 47+ return $this->mIsRaw;
 48+ }
 49+
4350 public function execute() {
4451 if (!function_exists('json_encode') || $this->getIsHtml()) {
4552 $json = new Services_JSON();
Index: trunk/phase3/includes/api/ApiMain.php
@@ -57,6 +57,8 @@
5858 private static $Formats = array (
5959 'json' => 'ApiFormatJson',
6060 'jsonfm' => 'ApiFormatJson',
 61+ 'raw' => 'ApiFormatJson',
 62+ 'rawfm' => 'ApiFormatJson',
6163 'xml' => 'ApiFormatXml',
6264 'xmlfm' => 'ApiFormatXml',
6365 'yaml' => 'ApiFormatYaml',
@@ -169,6 +171,9 @@
170172 ob_clean();
171173 $this->mResult->Reset();
172174 $this->mResult->addValue(null, 'error', $errMessage);
 175+
 176+ // If the error occured during printing, do a printer->profileOut()
 177+ $this->mPrinter->safeProfileOut();
173178 $this->printResult(true);
174179 }
175180
Index: trunk/phase3/includes/api/ApiQueryWatchlist.php
@@ -232,8 +232,8 @@
233233 ApiBase :: PARAM_DFLT => 10,
234234 ApiBase :: PARAM_TYPE => 'limit',
235235 ApiBase :: PARAM_MIN => 1,
236 - ApiBase :: PARAM_MAX1 => 500,
237 - ApiBase :: PARAM_MAX2 => 5000
 236+ ApiBase :: PARAM_MAX1 => ApiBase :: LIMIT_BIG1,
 237+ ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
238238 ),
239239 'prop' => array (
240240 APIBase :: PARAM_ISMULTI => true,
Index: trunk/phase3/includes/api/ApiQueryLogEvents.php
@@ -36,14 +36,14 @@
3737 }
3838
3939 public function execute() {
40 - $limit = $type = $from = $to = $dir = $user = $title = $namespace = null;
 40+ $limit = $type = $start = $end = $dir = $user = $title = null;
4141 extract($this->extractRequestParams());
4242
4343 $db = $this->getDB();
4444
4545 extract($db->tableNames('logging', 'page', 'user'), EXTR_PREFIX_ALL, 'tbl');
46 -
47 - $tables = "$tbl_logging LEFT OUTER JOIN $tbl_page ON log_namespace=page_namespace AND log_title=page_title " .
 46+ $tables = "$tbl_logging LEFT OUTER JOIN $tbl_page ON " .
 47+ "log_namespace=page_namespace AND log_title=page_title " .
4848 "INNER JOIN $tbl_user ON user_id=log_user";
4949
5050 $fields = array (
@@ -80,11 +80,19 @@
8181 $where['log_title'] = $titleObj->getDBkey();
8282 }
8383
84 - // $where[] = "log_timestamp $direction '$safetime'";
 84+ $dirNewer = ($dir === 'newer');
 85+ $before = ($dirNewer ? '<=' : '>=');
 86+ $after = ($dirNewer ? '>=' : '<=');
8587
 88+ if (!is_null($start))
 89+ $where[] = 'log_timestamp' . $after . $db->addQuotes($start);
 90+ if (!is_null($end))
 91+ $where[] = 'log_timestamp' . $before . $db->addQuotes($end);
 92+
8693 $options = array (
87 - 'LIMIT' => $limit +1
88 - );
 94+ 'LIMIT' => $limit +1,
 95+ 'ORDER BY' => 'log_timestamp' . ($dirNewer ? '' : ' DESC'
 96+ ));
8997
9098 $this->profileDBIn();
9199 $res = $db->select($tables, $fields, $where, __METHOD__, $options);
@@ -95,27 +103,42 @@
96104 while ($row = $db->fetchObject($res)) {
97105 if (++ $count > $limit) {
98106 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
99 - $this->setContinueEnumParameter('from', ApiQueryBase :: keyToTitle($row->page_title));
 107+ $this->setContinueEnumParameter('start', ApiQueryBase :: keyToTitle($row->log_timestamp));
100108 break;
101109 }
102110
103111 $vals = array (
104 - 'type' => $row->log_type,
105 - 'action' => $row->log_action,
 112+ 'action' => "$row->log_type/$row->log_action",
106113 'timestamp' => $row->log_timestamp,
107114 'comment' => $row->log_comment,
108 - 'params' => $row->log_params,
109 - 'pageid' => intval($row->page_id)
110 - );
111 -
 115+ 'pageid' => intval($row->page_id
 116+ ));
 117+
112118 $title = Title :: makeTitle($row->log_namespace, $row->log_title);
113119 $vals['ns'] = $title->getNamespace();
114120 $vals['title'] = $title->getPrefixedText();
115121
 122+ if ($row->log_params !== '') {
 123+ $params = explode("\n", $row->log_params);
 124+ if ($row->log_type == 'move' && isset ($params[0])) {
 125+ $destTitle = Title :: newFromText($params[0]);
 126+ if ($destTitle) {
 127+ $vals['tons'] = $destTitle->getNamespace();
 128+ $vals['totitle'] = $destTitle->getPrefixedText();
 129+ $params = null;
 130+ }
 131+ }
 132+
 133+ if(!empty($params)) {
 134+ ApiResult :: setIndexedTagName($params, 'param');
 135+ $vals = array_merge($vals, $params);
 136+ }
 137+ }
 138+
116139 if (!$row->log_user)
117140 $vals['anon'] = '';
118141 $vals['user'] = $row->user_name;
119 -
 142+
120143 $data[] = $vals;
121144 }
122145 $db->freeResult($res);
@@ -125,21 +148,56 @@
126149 }
127150
128151 protected function getAllowedParams() {
129 -
130152 return array (
131153 'limit' => array (
132154 ApiBase :: PARAM_DFLT => 10,
133155 ApiBase :: PARAM_TYPE => 'limit',
134156 ApiBase :: PARAM_MIN => 1,
135 - ApiBase :: PARAM_MAX1 => 500,
136 - ApiBase :: PARAM_MAX2 => 5000
137 - )
 157+ ApiBase :: PARAM_MAX1 => ApiBase :: LIMIT_BIG1,
 158+ ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
 159+ ),
 160+ 'type' => array (
 161+ ApiBase :: PARAM_ISMULTI => true,
 162+ ApiBase :: PARAM_TYPE => array (
 163+ 'block',
 164+ 'protect',
 165+ 'rights',
 166+ 'delete',
 167+ 'upload',
 168+ 'move',
 169+ 'import',
 170+ 'renameuser',
 171+ 'newusers',
 172+ 'makebot'
 173+ )
 174+ ),
 175+ 'start' => array (
 176+ ApiBase :: PARAM_TYPE => 'timestamp'
 177+ ),
 178+ 'end' => array (
 179+ ApiBase :: PARAM_TYPE => 'timestamp'
 180+ ),
 181+ 'dir' => array (
 182+ ApiBase :: PARAM_DFLT => 'older',
 183+ ApiBase :: PARAM_TYPE => array (
 184+ 'newer',
 185+ 'older'
 186+ )
 187+ ),
 188+ 'user' => null,
 189+ 'title' => null
138190 );
139191 }
140192
141193 protected function getParamDescription() {
142194 return array (
143 - 'limit' => 'How many total items to return.'
 195+ 'limit' => '',
 196+ 'type' => '',
 197+ 'start' => '',
 198+ 'end' => '',
 199+ 'dir' => '',
 200+ 'user' => '',
 201+ 'title' => ''
144202 );
145203 }
146204
@@ -154,7 +212,7 @@
155213 }
156214
157215 public function getVersion() {
158 - return __CLASS__ . ': $Id:$';
 216+ return __CLASS__ . ': $Id$';
159217 }
160218 }
161219 ?>
\ No newline at end of file