4 * Abstract base class for representing a single database table.
10 * @licence GNU GPL v2 or later
11 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
13 abstract class DBTable
{
16 * Returns the name of the database table objects of this type are stored in.
22 public abstract function getDBTable();
25 * Returns the name of a DBDataObject deriving class that
26 * represents single rows in this table.
32 public abstract function getDataObjectClass();
35 * Gets the db field prefix.
41 protected abstract function getFieldPrefix();
44 * Returns an array with the fields and their types this object contains.
45 * This corresponds directly to the fields in the database, without prefix.
61 public abstract function getFieldTypes();
64 * The database connection to use for read operations.
65 * Can be changed via @see setReadDb.
68 * @var integer DB_ enum
70 protected $readDb = DB_SLAVE
;
73 * Returns a list of default field values.
74 * field name => field value
80 public function getDefaults() {
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.
93 public function getSummaryFields() {
98 * Selects the the specified fields of the records matching the provided
99 * conditions and returns them as DBDataObject. Field names get prefixed.
103 * @param array|string|null $fields
104 * @param array $conditions
105 * @param array $options
107 * @return array of self
109 public function select( $fields = null, array $conditions = array(), array $options = array() ) {
110 $result = $this->selectFields( $fields, $conditions, $options, false );
114 foreach ( $result as $record ) {
115 $objects[] = $this->newFromArray( $record );
122 * Selects the the specified fields of the records matching the provided
123 * conditions and returns them as associative arrays.
124 * Provided field names get prefixed.
125 * Returned field names will not have a prefix.
127 * When $collapse is true:
128 * If one field is selected, each item in the result array will be this field.
129 * If two fields are selected, each item in the result array will have as key
130 * the first field and as value the second field.
131 * If more then two fields are selected, each item will be an associative array.
135 * @param array|string|null $fields
136 * @param array $conditions
137 * @param array $options
138 * @param boolean $collapse Set to false to always return each result row as associative array.
140 * @return array of array
142 public function selectFields( $fields = null, array $conditions = array(), array $options = array(), $collapse = true ) {
143 if ( is_null( $fields ) ) {
144 $fields = array_keys( $this->getFieldTypes() );
147 $fields = (array)$fields;
150 $dbr = wfGetDB( $this->getReadDb() );
151 $result = $dbr->select(
153 $this->getPrefixedFields( $fields ),
154 $this->getPrefixedValues( $conditions ),
161 foreach ( $result as $record ) {
162 $objects[] = $this->getFieldsFromDBResult( $record );
166 if ( count( $fields ) === 1 ) {
167 $objects = array_map( 'array_shift', $objects );
169 elseif ( count( $fields ) === 2 ) {
172 foreach ( $objects as $object ) {
173 $o[array_shift( $object )] = array_shift( $object );
184 * Selects the the specified fields of the first matching record.
185 * Field names get prefixed.
189 * @param array|string|null $fields
190 * @param array $conditions
191 * @param array $options
193 * @return DBObject|bool False on failure
195 public function selectRow( $fields = null, array $conditions = array(), array $options = array() ) {
196 $options['LIMIT'] = 1;
198 $objects = $this->select( $fields, $conditions, $options );
200 return count( $objects ) > 0 ?
$objects[0] : false;
204 * Selects the the specified fields of the records matching the provided
205 * conditions. Field names do NOT get prefixed.
209 * @param array $fields
210 * @param array $conditions
211 * @param array $options
213 * @return ResultWrapper
215 public function rawSelectRow( array $fields, array $conditions = array(), array $options = array() ) {
216 $dbr = wfGetDB( $this->getReadDb() );
218 return $dbr->selectRow(
228 * Selects the the specified fields of the first record matching the provided
229 * conditions and returns it as an associative array, or false when nothing matches.
230 * This method makes use of selectFields and expects the same parameters and
231 * returns the same results (if there are any, if there are none, this method returns false).
232 * @see DBDataObject::selectFields
236 * @param array|string|null $fields
237 * @param array $conditions
238 * @param array $options
239 * @param boolean $collapse Set to false to always return each result row as associative array.
241 * @return mixed|array|bool False on failure
243 public function selectFieldsRow( $fields = null, array $conditions = array(), array $options = array(), $collapse = true ) {
244 $options['LIMIT'] = 1;
246 $objects = $this->selectFields( $fields, $conditions, $options, $collapse );
248 return count( $objects ) > 0 ?
$objects[0] : false;
252 * Returns if there is at least one record matching the provided conditions.
253 * Condition field names get prefixed.
257 * @param array $conditions
261 public function has( array $conditions = array() ) {
262 return $this->selectRow( array( 'id' ), $conditions ) !== false;
266 * Returns the amount of matching records.
267 * Condition field names get prefixed.
269 * Note that this can be expensive on large tables.
270 * In such cases you might want to use DatabaseBase::estimateRowCount instead.
274 * @param array $conditions
275 * @param array $options
279 public function count( array $conditions = array(), array $options = array() ) {
280 $res = $this->rawSelectRow(
281 array( 'COUNT(*) AS rowcount' ),
282 $this->getPrefixedValues( $conditions ),
286 return $res->rowcount
;
290 * Removes the object from the database.
294 * @param array $conditions
296 * @return boolean Success indicator
298 public function delete( array $conditions ) {
299 return wfGetDB( DB_MASTER
)->delete(
301 $this->getPrefixedValues( $conditions )
306 * Get API parameters for the fields supported by this object.
310 * @param boolean $requireParams
311 * @param boolean $setDefaults
315 public function getAPIParams( $requireParams = false, $setDefaults = false ) {
327 $defaults = $this->getDefaults();
329 foreach ( $this->getFieldTypes() as $field => $type ) {
330 if ( $field == 'id' ) {
334 $hasDefault = array_key_exists( $field, $defaults );
336 $params[$field] = array(
337 ApiBase
::PARAM_TYPE
=> $typeMap[$type],
338 ApiBase
::PARAM_REQUIRED
=> $requireParams && !$hasDefault
341 if ( $type == 'array' ) {
342 $params[$field][ApiBase
::PARAM_ISMULTI
] = true;
345 if ( $setDefaults && $hasDefault ) {
346 $default = is_array( $defaults[$field] ) ?
implode( '|', $defaults[$field] ) : $defaults[$field];
347 $params[$field][ApiBase
::PARAM_DFLT
] = $default;
355 * Returns an array with the fields and their descriptions.
357 * field name => field description
363 public function getFieldDescriptions() {
368 * Get the database type used for read operations.
372 * @return integer DB_ enum
374 public function getReadDb() {
375 return $this->readDb
;
379 * Set the database type to use for read operations.
385 public function setReadDb( $db ) {
390 * Update the records matching the provided conditions by
391 * setting the fields that are keys in the $values param to
392 * their corresponding values.
396 * @param array $values
397 * @param array $conditions
399 * @return boolean Success indicator
401 public function update( array $values, array $conditions = array() ) {
402 $dbw = wfGetDB( DB_MASTER
);
406 $this->getPrefixedValues( $values ),
407 $this->getPrefixedValues( $conditions ),
413 * Computes the values of the summary fields of the objects matching the provided conditions.
417 * @param array|string|null $summaryFields
418 * @param array $conditions
420 public function updateSummaryFields( $summaryFields = null, array $conditions = array() ) {
421 $this->setReadDb( DB_MASTER
);
423 foreach ( $this->select( null, $conditions ) as /* DBDataObject */ $item ) {
424 $item->loadSummaryFields( $summaryFields );
425 $item->setSummaryMode( true );
429 $this->setReadDb( DB_SLAVE
);
433 * Takes in an associative array with field names as keys and
434 * their values as value. The field names are prefixed with the
437 * Field names can also be provided as an array with as first element a table name, such as
438 * $conditions = array(
439 * array( array( 'tablename', 'fieldname' ), $value ),
444 * @param array $values
448 public function getPrefixedValues( array $values ) {
449 $prefixedValues = array();
451 foreach ( $values as $field => $value ) {
452 if ( is_integer( $field ) ) {
453 if ( is_array( $value ) ) {
458 $value = explode( ' ', $value, 2 );
459 $value[0] = $this->getPrefixedField( $value[0] );
460 $prefixedValues[] = implode( ' ', $value );
465 $prefixedValues[$this->getPrefixedField( $field )] = $value;
468 return $prefixedValues;
472 * Takes in a field or array of fields and returns an
473 * array with their prefixed versions, ready for db usage.
477 * @param array|string $fields
481 public function getPrefixedFields( array $fields ) {
482 foreach ( $fields as &$field ) {
483 $field = $this->getPrefixedField( $field );
490 * Takes in a field and returns an it's prefixed version, ready for db usage.
494 * @param string|array $field
498 public function getPrefixedField( $field ) {
499 return $this->getFieldPrefix() . $field;
503 * Takes an array of field names with prefix and returns the unprefixed equivalent.
507 * @param array $fieldNames
511 public function unprefixFieldNames( array $fieldNames ) {
512 return array_map( array( $this, 'unprefixFieldName' ), $fieldNames );
516 * Takes a field name with prefix and returns the unprefixed equivalent.
520 * @param string $fieldName
524 public function unprefixFieldName( $fieldName ) {
525 return substr( $fieldName, strlen( $this->getFieldPrefix() ) );
528 public function __construct() {
533 * Get an instance of this class.
539 public static function singleton() {
542 if ( !isset( $instance ) ) {
543 $instance = new static;
550 * Get an array with fields from a database result,
551 * that can be fed directly to the constructor or
556 * @param stdClass $result
560 public function getFieldsFromDBResult( stdClass
$result ) {
561 $result = (array)$result;
562 return array_combine(
563 $this->unprefixFieldNames( array_keys( $result ) ),
564 array_values( $result )
569 * Get a new instance of the class from a database result.
573 * @param stdClass $result
575 * @return DBDataObject
577 public function newFromDBResult( stdClass
$result ) {
578 return $this->newFromArray( $this->getFieldsFromDBResult( $result ) );
582 * Get a new instance of the class from an array.
587 * @param boolean $loadDefaults
589 * @return DBDataObject
591 public function newFromArray( array $data, $loadDefaults = false ) {
592 $class = $this->getDataObjectClass();
593 return new $class( $this, $data, $loadDefaults );
597 * Return the names of the fields.
603 public function getFieldNames() {
604 return array_keys( $this->getFieldTypes() );
608 * Gets if the object can take a certain field.
612 * @param string $name
616 public function canHaveField( $name ) {
617 return array_key_exists( $name, $this->getFieldTypes() );