r34440 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r34439‎ | r34440 | r34441 >
Date:15:46, 8 May 2008
Author:catrope
Status:old
Tags:
Comment:
Documenting some API stuff
Modified paths:
  • /trunk/phase3/includes/api/ApiMain.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryBase.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiMain.php
@@ -546,6 +546,10 @@
547547 return $this->mIsSysop;
548548 }
549549
 550+ /**
 551+ * Check whether the current user is allowed to use high limits
 552+ * @return bool
 553+ */
550554 public function canApiHighLimits() {
551555 if (!isset($this->mCanApiHighLimits)) {
552556 global $wgUser;
@@ -555,6 +559,10 @@
556560 return $this->mCanApiHighLimits;
557561 }
558562
 563+ /**
 564+ * Check whether the user wants us to show version information in the API help
 565+ * @return bool
 566+ */
559567 public function getShowVersions() {
560568 return $this->mShowVersions;
561569 }
Index: trunk/phase3/includes/api/ApiQueryBase.php
@@ -48,6 +48,9 @@
4949 $this->resetQueryParams();
5050 }
5151
 52+ /**
 53+ * Blank the internal arrays with query parameters
 54+ */
5255 protected function resetQueryParams() {
5356 $this->tables = array ();
5457 $this->where = array ();
@@ -55,6 +58,11 @@
5659 $this->options = array ();
5760 }
5861
 62+ /**
 63+ * Add a set of tables to the internal array
 64+ * @param mixed $tables Table name or array of table names
 65+ * @param mixed $alias Table alias, or null for no alias. Cannot be used with multiple tables
 66+ */
5967 protected function addTables($tables, $alias = null) {
6068 if (is_array($tables)) {
6169 if (!is_null($alias))
@@ -67,6 +75,18 @@
6876 }
6977 }
7078
 79+ /**
 80+ * Add a JOIN to the internal array.
 81+ *
 82+ * Example for: a LEFT JOIN page AS b ON foo=bar RIGHT JOIN c ON foo=baz AND bar=3
 83+ *
 84+ * addJoin(array('a', 'page', 'c'), array(ApiQueryBase::LEFT_JOIN, ApiQueryBase::RIGHT_JOIN),
 85+ * array('foo=bar', array('foo=baz', 'bar' => 3), array(null, 'b', null))
 86+ * @param array $tables Array of table names
 87+ * @param array $types Array of join types (either LEFT_JOIN or RIGHT_JOIN)
 88+ * @param array $onClauses Array of ON clauses. Each element is formed like addWhere()'s parameter
 89+ * @param array $aliases Array of table aliases, or null for no alias
 90+ */
7191 protected function addJoin($tables, $types, $onClauses, $aliases = null) {
7292 if(is_null($aliases))
7393 foreach($tables as $unused)
@@ -93,9 +113,11 @@
94114 }
95115 $this->addTables($sql);
96116 }
97 -
98 -
99117
 118+ /**
 119+ * Add a set of fields to select to the internal array
 120+ * @param mixed $value Field name or array of field names
 121+ */
100122 protected function addFields($value) {
101123 if (is_array($value))
102124 $this->fields = array_merge($this->fields, $value);
@@ -103,6 +125,12 @@
104126 $this->fields[] = $value;
105127 }
106128
 129+ /**
 130+ * Same as addFields(), but add the fields only if a condition is met
 131+ * @param mixed $value See addFields()
 132+ * @param bool $condition If false, do nothing
 133+ * @return bool $condition
 134+ */
107135 protected function addFieldsIf($value, $condition) {
108136 if ($condition) {
109137 $this->addFields($value);
@@ -111,6 +139,15 @@
112140 return false;
113141 }
114142
 143+ /**
 144+ * Add a set of WHERE clauses to the internal array.
 145+ * Clauses can be formatted as 'foo=bar' or array('foo' => 'bar'),
 146+ * the latter only works if the value is a constant (i.e. not another field)
 147+ *
 148+ * For example, array('foo=bar', 'baz' => 3, 'bla' => 'foo') translates
 149+ * to "foo=bar AND baz='3' AND bla='foo'"
 150+ * @param mixed $value String or array
 151+ */
115152 protected function addWhere($value) {
116153 if (is_array($value))
117154 $this->where = array_merge($this->where, $value);
@@ -118,6 +155,12 @@
119156 $this->where[] = $value;
120157 }
121158
 159+ /**
 160+ * Same as addWhere(), but add the WHERE clauses only if a condition is met
 161+ * @param mixed $value See addWhere()
 162+ * @param bool $condition If false, do nothing
 163+ * @return bool $condition
 164+ */
122165 protected function addWhereIf($value, $condition) {
123166 if ($condition) {
124167 $this->addWhere($value);
@@ -126,11 +169,24 @@
127170 return false;
128171 }
129172
 173+ /**
 174+ * Equivalent to addWhere(array($field => $value))
 175+ * @param string $field Field name
 176+ * @param string $value Value; ignored if nul;
 177+ */
130178 protected function addWhereFld($field, $value) {
131179 if (!is_null($value))
132180 $this->where[$field] = $value;
133181 }
134182
 183+ /**
 184+ * Add a WHERE clause corresponding to a range, and an ORDER BY
 185+ * clause to sort in the right direction
 186+ * @param string $field Field name
 187+ * @param string $dir If 'newer', sort in ascending order, otherwise sort in descending order
 188+ * @param string $start Value to start the list at. If $dir == 'newer' this is the lower boundary, otherwise it's the upper boundary
 189+ * @param string $end Value to end the list at. If $dir == 'newer' this is the upper boundary, otherwise it's the lower boundary
 190+ */
135191 protected function addWhereRange($field, $dir, $start, $end) {
136192 $isDirNewer = ($dir === 'newer');
137193 $after = ($isDirNewer ? '>=' : '<=');
@@ -150,6 +206,11 @@
151207 $this->addOption('ORDER BY', $this->options['ORDER BY'] . ', ' . $order);
152208 }
153209
 210+ /**
 211+ * Add an option such as LIMIT or USE INDEX
 212+ * @param string $name Option name
 213+ * @param string $value Option value
 214+ */
154215 protected function addOption($name, $value = null) {
155216 if (is_null($value))
156217 $this->options[] = $name;
@@ -157,6 +218,11 @@
158219 $this->options[$name] = $value;
159220 }
160221
 222+ /**
 223+ * Execute a SELECT query based on the values in the internal arrays
 224+ * @param string $method Function the query should be attributed to. You should usually use __METHOD__ here
 225+ * @return ResultWrapper
 226+ */
161227 protected function select($method) {
162228
163229 // getDB has its own profileDBIn/Out calls
@@ -169,6 +235,11 @@
170236 return $res;
171237 }
172238
 239+ /**
 240+ * Estimate the row count for the SELECT query that would be run if we
 241+ * called select() right now, and check if it's acceptable.
 242+ * @return bool true if acceptable, false otherwise
 243+ */
173244 protected function checkRowCount() {
174245 $db = $this->getDB();
175246 $this->profileDBIn();
@@ -181,6 +252,12 @@
182253 return true;
183254 }
184255
 256+ /**
 257+ * Add information (title and namespace) about a Title object to a result array
 258+ * @param array $arr Result array � la ApiResult
 259+ * @param Title $title Title object
 260+ * @param string $prefix Module prefix
 261+ */
185262 public static function addTitleInfo(&$arr, $title, $prefix='') {
186263 $arr[$prefix . 'ns'] = intval($title->getNamespace());
187264 $arr[$prefix . 'title'] = $title->getPrefixedText();
@@ -189,19 +266,23 @@
190267 /**
191268 * Override this method to request extra fields from the pageSet
192269 * using $pageSet->requestField('fieldName')
 270+ * @param ApiPageSet $pageSet
193271 */
194272 public function requestExtraData($pageSet) {
195273 }
196274
197275 /**
198276 * Get the main Query module
 277+ * @return ApiQuery
199278 */
200279 public function getQuery() {
201280 return $this->mQueryModule;
202281 }
203282
204283 /**
205 - * Add sub-element under the page element with the given pageId.
 284+ * Add a sub-element under the page element with the given page ID
 285+ * @param int $pageId Page ID
 286+ * @param array $data Data array � la ApiResult
206287 */
207288 protected function addPageSubItems($pageId, $data) {
208289 $result = $this->getResult();
@@ -211,19 +292,21 @@
212293 $data);
213294 }
214295
 296+ /**
 297+ * Set a query-continue value
 298+ * @param $paramName Parameter name
 299+ * @param $paramValue Parameter value
 300+ */
215301 protected function setContinueEnumParameter($paramName, $paramValue) {
216302
217303 $paramName = $this->encodeParamName($paramName);
218304 $msg = array( $paramName => $paramValue );
219 -
220 -// This is an alternative continue format as a part of the URL string
221 -// ApiResult :: setContent($msg, $paramName . '=' . urlencode($paramValue));
222 -
223305 $this->getResult()->addValue('query-continue', $this->getModuleName(), $msg);
224306 }
225307
226308 /**
227309 * Get the Query database connection (readonly)
 310+ * @return Database
228311 */
229312 protected function getDB() {
230313 if (is_null($this->mDb))
@@ -236,6 +319,10 @@
237320 * If no such connection has been requested before, it will be created.
238321 * Subsequent calls with the same $name will return the same connection
239322 * as the first, regardless of $db or $groups new values.
 323+ * @param string $name Name to assign to the database connection
 324+ * @param int $db One of the DB_* constants
 325+ * @param array $groups Query groups
 326+ * @return Database
240327 */
241328 public function selectNamedDB($name, $db, $groups) {
242329 $this->mDb = $this->getQuery()->getNamedDB($name, $db, $groups);
@@ -243,7 +330,7 @@
244331
245332 /**
246333 * Get the PageSet object to work on
247 - * @return ApiPageSet data
 334+ * @return ApiPageSet
248335 */
249336 protected function getPageSet() {
250337 return $this->getQuery()->getPageSet();
@@ -253,15 +340,29 @@
254341 * This is a very simplistic utility function
255342 * to convert a non-namespaced title string to a db key.
256343 * It will replace all ' ' with '_'
 344+ * @param string $title Page title with spaces
 345+ * @return string Page title with underscores
257346 */
258347 public static function titleToKey($title) {
259348 return str_replace(' ', '_', $title);
260349 }
261350
 351+ /**
 352+ * The inverse of titleToKey()
 353+ * @param string $key Page title with underscores
 354+ * @return string Page title with spaces
 355+ */
262356 public static function keyToTitle($key) {
263357 return str_replace('_', ' ', $key);
264358 }
265359
 360+ /**
 361+ * Check whether the current user requested a certain token and
 362+ * is actually allowed to request it.
 363+ * @param array $tokenArr Array of tokens the user requested
 364+ * @param string $action Action to check for
 365+ * @return bool true if the user requested the token and is allowed to, false otherwise
 366+ */
266367 public function getTokenFlag($tokenArr, $action) {
267368 if ($this->getMain()->getRequest()->getVal('callback') !== null) {
268369 // Don't do any session-specific data.
@@ -277,6 +378,10 @@
278379 return false;
279380 }
280381
 382+ /**
 383+ * Get version string for use in the API help output
 384+ * @return string
 385+ */
281386 public static function getBaseVersion() {
282387 return __CLASS__ . ': $Id$';
283388 }
@@ -294,6 +399,10 @@
295400 $this->mIsGenerator = false;
296401 }
297402
 403+ /**
 404+ * Switch this module to generator mode. By default, generator mode is
 405+ * switched off and the module acts like a normal query module.
 406+ */
298407 public function setGeneratorMode() {
299408 $this->mIsGenerator = true;
300409 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r34556Re-applying r34440 (documenting ApiQueryBase)catrope09:29, 10 May 2008

Status & tagging log