(bug 37926) Split 'deleterevision' right for log entries
[mediawiki.git] / includes / db / IORMTable.php
blob684f4b4638a010749179f08f06f078bba84bd652
1 <?php
2 /**
3 * Interface for objects representing a single database table.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @since 1.20
22 * @file
23 * @ingroup ORM
25 * @licence GNU GPL v2 or later
26 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
29 interface IORMTable {
31 /**
32 * Returns the name of the database table objects of this type are stored in.
34 * @since 1.20
36 * @return string
38 public function getName();
40 /**
41 * Returns the name of a IORMRow implementing class that
42 * represents single rows in this table.
44 * @since 1.20
46 * @return string
48 public function getRowClass();
50 /**
51 * Returns an array with the fields and their types this object contains.
52 * This corresponds directly to the fields in the database, without prefix.
54 * field name => type
56 * Allowed types:
57 * * id
58 * * str
59 * * int
60 * * float
61 * * bool
62 * * array
63 * * blob
65 * TODO: get rid of the id field. Every row instance needs to have
66 * one so this is just causing hassle at various locations by requiring an extra check for field name.
68 * @since 1.20
70 * @return array
72 public function getFields();
74 /**
75 * Returns a list of default field values.
76 * field name => field value
78 * @since 1.20
80 * @return array
82 public function getDefaults();
84 /**
85 * Returns a list of the summary fields.
86 * These are fields that cache computed values, such as the amount of linked objects of $type.
87 * This is relevant as one might not want to do actions such as log changes when these get updated.
89 * @since 1.20
91 * @return array
93 public function getSummaryFields();
95 /**
96 * Selects the the specified fields of the records matching the provided
97 * conditions and returns them as DBDataObject. Field names get prefixed.
99 * @since 1.20
101 * @param array|string|null $fields
102 * @param array $conditions
103 * @param array $options
104 * @param string|null $functionName
106 * @return ORMResult
108 public function select( $fields = null, array $conditions = array(),
109 array $options = array(), $functionName = null );
112 * Selects the the specified fields of the records matching the provided
113 * conditions and returns them as DBDataObject. Field names get prefixed.
115 * @since 1.20
117 * @param array|string|null $fields
118 * @param array $conditions
119 * @param array $options
120 * @param string|null $functionName
122 * @return array of self
124 public function selectObjects( $fields = null, array $conditions = array(),
125 array $options = array(), $functionName = null );
128 * Do the actual select.
130 * @since 1.20
132 * @param null|string|array $fields
133 * @param array $conditions
134 * @param array $options
135 * @param null|string $functionName
137 * @return ResultWrapper
139 public function rawSelect( $fields = null, array $conditions = array(),
140 array $options = array(), $functionName = null );
143 * Selects the the specified fields of the records matching the provided
144 * conditions and returns them as associative arrays.
145 * Provided field names get prefixed.
146 * Returned field names will not have a prefix.
148 * When $collapse is true:
149 * If one field is selected, each item in the result array will be this field.
150 * If two fields are selected, each item in the result array will have as key
151 * the first field and as value the second field.
152 * If more then two fields are selected, each item will be an associative array.
154 * @since 1.20
156 * @param array|string|null $fields
157 * @param array $conditions
158 * @param array $options
159 * @param boolean $collapse Set to false to always return each result row as associative array.
160 * @param string|null $functionName
162 * @return array of array
164 public function selectFields( $fields = null, array $conditions = array(),
165 array $options = array(), $collapse = true, $functionName = null );
168 * Selects the the specified fields of the first matching record.
169 * Field names get prefixed.
171 * @since 1.20
173 * @param array|string|null $fields
174 * @param array $conditions
175 * @param array $options
176 * @param string|null $functionName
178 * @return IORMRow|bool False on failure
180 public function selectRow( $fields = null, array $conditions = array(),
181 array $options = array(), $functionName = null );
184 * Selects the the specified fields of the records matching the provided
185 * conditions. Field names do NOT get prefixed.
187 * @since 1.20
189 * @param array $fields
190 * @param array $conditions
191 * @param array $options
192 * @param string|null $functionName
194 * @return ResultWrapper
196 public function rawSelectRow( array $fields, array $conditions = array(),
197 array $options = array(), $functionName = null );
200 * Selects the the specified fields of the first record matching the provided
201 * conditions and returns it as an associative array, or false when nothing matches.
202 * This method makes use of selectFields and expects the same parameters and
203 * returns the same results (if there are any, if there are none, this method returns false).
204 * @see IORMTable::selectFields
206 * @since 1.20
208 * @param array|string|null $fields
209 * @param array $conditions
210 * @param array $options
211 * @param boolean $collapse Set to false to always return each result row as associative array.
212 * @param string|null $functionName
214 * @return mixed|array|bool False on failure
216 public function selectFieldsRow( $fields = null, array $conditions = array(),
217 array $options = array(), $collapse = true, $functionName = null );
220 * Returns if there is at least one record matching the provided conditions.
221 * Condition field names get prefixed.
223 * @since 1.20
225 * @param array $conditions
227 * @return boolean
229 public function has( array $conditions = array() );
232 * Returns the amount of matching records.
233 * Condition field names get prefixed.
235 * Note that this can be expensive on large tables.
236 * In such cases you might want to use DatabaseBase::estimateRowCount instead.
238 * @since 1.20
240 * @param array $conditions
241 * @param array $options
243 * @return integer
245 public function count( array $conditions = array(), array $options = array() );
248 * Removes the object from the database.
250 * @since 1.20
252 * @param array $conditions
253 * @param string|null $functionName
255 * @return boolean Success indicator
257 public function delete( array $conditions, $functionName = null );
260 * Get API parameters for the fields supported by this object.
262 * @since 1.20
264 * @param boolean $requireParams
265 * @param boolean $setDefaults
267 * @return array
269 public function getAPIParams( $requireParams = false, $setDefaults = false );
272 * Returns an array with the fields and their descriptions.
274 * field name => field description
276 * @since 1.20
278 * @return array
280 public function getFieldDescriptions();
283 * Get the database type used for read operations.
285 * @since 1.20
287 * @return integer DB_ enum
289 public function getReadDb();
292 * Set the database type to use for read operations.
294 * @param integer $db
296 * @since 1.20
298 public function setReadDb( $db );
301 * Update the records matching the provided conditions by
302 * setting the fields that are keys in the $values param to
303 * their corresponding values.
305 * @since 1.20
307 * @param array $values
308 * @param array $conditions
310 * @return boolean Success indicator
312 public function update( array $values, array $conditions = array() );
315 * Computes the values of the summary fields of the objects matching the provided conditions.
317 * @since 1.20
319 * @param array|string|null $summaryFields
320 * @param array $conditions
322 public function updateSummaryFields( $summaryFields = null, array $conditions = array() );
325 * Takes in an associative array with field names as keys and
326 * their values as value. The field names are prefixed with the
327 * db field prefix.
329 * @since 1.20
331 * @param array $values
333 * @return array
335 public function getPrefixedValues( array $values );
338 * Takes in a field or array of fields and returns an
339 * array with their prefixed versions, ready for db usage.
341 * @since 1.20
343 * @param array|string $fields
345 * @return array
347 public function getPrefixedFields( array $fields );
350 * Takes in a field and returns an it's prefixed version, ready for db usage.
352 * @since 1.20
354 * @param string|array $field
356 * @return string
358 public function getPrefixedField( $field );
361 * Takes an array of field names with prefix and returns the unprefixed equivalent.
363 * @since 1.20
365 * @param array $fieldNames
367 * @return array
369 public function unprefixFieldNames( array $fieldNames );
372 * Takes a field name with prefix and returns the unprefixed equivalent.
374 * @since 1.20
376 * @param string $fieldName
378 * @return string
380 public function unprefixFieldName( $fieldName );
383 * Get an instance of this class.
385 * @since 1.20
387 * @return IORMTable
389 public static function singleton();
392 * Get an array with fields from a database result,
393 * that can be fed directly to the constructor or
394 * to setFields.
396 * @since 1.20
398 * @param stdClass $result
400 * @return array
402 public function getFieldsFromDBResult( stdClass $result );
405 * Get a new instance of the class from a database result.
407 * @since 1.20
409 * @param stdClass $result
411 * @return IORMRow
413 public function newRowFromDBResult( stdClass $result );
416 * Get a new instance of the class from an array.
418 * @since 1.20
420 * @param array $data
421 * @param boolean $loadDefaults
423 * @return IORMRow
425 public function newRow( array $data, $loadDefaults = false );
428 * Return the names of the fields.
430 * @since 1.20
432 * @return array
434 public function getFieldNames();
437 * Gets if the object can take a certain field.
439 * @since 1.20
441 * @param string $name
443 * @return boolean
445 public function canHaveField( $name );