Implement extension registration from an extension.json file
[mediawiki.git] / includes / db / ORMTable.php
blob562a8106ffef7bcf4a0a4ef6143094eb4f1ca25a
1 <?php
2 /**
3 * Abstract base class for representing a single database table.
4 * Documentation inline and at https://www.mediawiki.org/wiki/Manual:ORMTable
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
21 * @since 1.20
22 * Non-abstract since 1.21
24 * @file ORMTable.php
25 * @ingroup ORM
27 * @license GNU GPL v2 or later
28 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
31 class ORMTable extends DBAccessBase implements IORMTable {
32 /**
33 * Cache for instances, used by the singleton method.
35 * @since 1.20
36 * @deprecated since 1.21
38 * @var ORMTable[]
40 protected static $instanceCache = array();
42 /**
43 * @since 1.21
45 * @var string
47 protected $tableName;
49 /**
50 * @since 1.21
52 * @var string[]
54 protected $fields = array();
56 /**
57 * @since 1.21
59 * @var string
61 protected $fieldPrefix = '';
63 /**
64 * @since 1.21
66 * @var string
68 protected $rowClass = 'ORMRow';
70 /**
71 * @since 1.21
73 * @var array
75 protected $defaults = array();
77 /**
78 * ID of the database connection to use for read operations.
79 * Can be changed via @see setReadDb.
81 * @since 1.20
83 * @var int DB_ enum
85 protected $readDb = DB_SLAVE;
87 /**
88 * Constructor.
90 * @since 1.21
92 * @param string $tableName
93 * @param string[] $fields
94 * @param array $defaults
95 * @param string|null $rowClass
96 * @param string $fieldPrefix
98 public function __construct( $tableName = '', array $fields = array(),
99 array $defaults = array(), $rowClass = null, $fieldPrefix = ''
101 $this->tableName = $tableName;
102 $this->fields = $fields;
103 $this->defaults = $defaults;
105 if ( is_string( $rowClass ) ) {
106 $this->rowClass = $rowClass;
109 $this->fieldPrefix = $fieldPrefix;
113 * @see IORMTable::getName
115 * @since 1.21
117 * @return string
118 * @throws MWException
120 public function getName() {
121 if ( $this->tableName === '' ) {
122 throw new MWException( 'The table name needs to be set' );
125 return $this->tableName;
129 * Gets the db field prefix.
131 * @since 1.20
132 * @deprecated since 1.25, use the $this->fieldPrefix property instead
134 * @return string
136 protected function getFieldPrefix() {
137 return $this->fieldPrefix;
141 * @see IORMTable::getRowClass
143 * @since 1.21
145 * @return string
147 public function getRowClass() {
148 return $this->rowClass;
152 * @see ORMTable::getFields
154 * @since 1.21
156 * @return array
157 * @throws MWException
159 public function getFields() {
160 if ( $this->fields === array() ) {
161 throw new MWException( 'The table needs to have one or more fields' );
164 return $this->fields;
168 * Returns a list of default field values.
169 * field name => field value
171 * @since 1.20
173 * @return array
175 public function getDefaults() {
176 return $this->defaults;
180 * Returns a list of the summary fields.
181 * These are fields that cache computed values, such as the amount of linked objects of $type.
182 * This is relevant as one might not want to do actions such as log changes when these get updated.
184 * @since 1.20
186 * @return array
188 public function getSummaryFields() {
189 return array();
193 * Selects the specified fields of the records matching the provided
194 * conditions and returns them as DBDataObject. Field names get prefixed.
196 * @since 1.20
198 * @param array|string|null $fields
199 * @param array $conditions
200 * @param array $options
201 * @param string|null $functionName
203 * @return ORMResult
205 public function select( $fields = null, array $conditions = array(),
206 array $options = array(), $functionName = null
208 $res = $this->rawSelect( $fields, $conditions, $options, $functionName );
210 return new ORMResult( $this, $res );
214 * Selects the specified fields of the records matching the provided
215 * conditions and returns them as DBDataObject. Field names get prefixed.
217 * @since 1.20
219 * @param array|string|null $fields
220 * @param array $conditions
221 * @param array $options
222 * @param string|null $functionName
224 * @return array Array of row objects
225 * @throws DBQueryError If the query failed (even if the database was in ignoreErrors mode).
227 public function selectObjects( $fields = null, array $conditions = array(),
228 array $options = array(), $functionName = null
230 $result = $this->selectFields( $fields, $conditions, $options, false, $functionName );
232 $objects = array();
234 foreach ( $result as $record ) {
235 $objects[] = $this->newRow( $record );
238 return $objects;
242 * Do the actual select.
244 * @since 1.20
246 * @param null|string|array $fields
247 * @param array $conditions
248 * @param array $options
249 * @param null|string $functionName
250 * @return ResultWrapper
251 * @throws Exception
252 * @throws MWException
254 public function rawSelect( $fields = null, array $conditions = array(),
255 array $options = array(), $functionName = null
257 if ( is_null( $fields ) ) {
258 $fields = array_keys( $this->getFields() );
259 } else {
260 $fields = (array)$fields;
263 $dbr = $this->getReadDbConnection();
264 $result = $dbr->select(
265 $this->getName(),
266 $this->getPrefixedFields( $fields ),
267 $this->getPrefixedValues( $conditions ),
268 is_null( $functionName ) ? __METHOD__ : $functionName,
269 $options
272 /* @var Exception $error */
273 $error = null;
275 if ( $result === false ) {
276 // Database connection was in "ignoreErrors" mode. We don't like that.
277 // So, we emulate the DBQueryError that should have been thrown.
278 $error = new DBQueryError(
279 $dbr,
280 $dbr->lastError(),
281 $dbr->lastErrno(),
282 $dbr->lastQuery(),
283 is_null( $functionName ) ? __METHOD__ : $functionName
287 $this->releaseConnection( $dbr );
289 if ( $error ) {
290 // Note: construct the error before releasing the connection,
291 // but throw it after.
292 throw $error;
295 return $result;
299 * Selects the specified fields of the records matching the provided
300 * conditions and returns them as associative arrays.
301 * Provided field names get prefixed.
302 * Returned field names will not have a prefix.
304 * When $collapse is true:
305 * If one field is selected, each item in the result array will be this field.
306 * If two fields are selected, each item in the result array will have as key
307 * the first field and as value the second field.
308 * If more then two fields are selected, each item will be an associative array.
310 * @since 1.20
312 * @param array|string|null $fields
313 * @param array $conditions
314 * @param array $options
315 * @param bool $collapse Set to false to always return each result row as associative array.
316 * @param string|null $functionName
318 * @return array Array of array
320 public function selectFields( $fields = null, array $conditions = array(),
321 array $options = array(), $collapse = true, $functionName = null
323 $objects = array();
325 $result = $this->rawSelect( $fields, $conditions, $options, $functionName );
327 foreach ( $result as $record ) {
328 $objects[] = $this->getFieldsFromDBResult( $record );
331 if ( $collapse ) {
332 if ( count( $fields ) === 1 ) {
333 $objects = array_map( 'array_shift', $objects );
334 } elseif ( count( $fields ) === 2 ) {
335 $o = array();
337 foreach ( $objects as $object ) {
338 $o[array_shift( $object )] = array_shift( $object );
341 $objects = $o;
345 return $objects;
349 * Selects the specified fields of the first matching record.
350 * Field names get prefixed.
352 * @since 1.20
354 * @param array|string|null $fields
355 * @param array $conditions
356 * @param array $options
357 * @param string|null $functionName
359 * @return IORMRow|bool False on failure
361 public function selectRow( $fields = null, array $conditions = array(),
362 array $options = array(), $functionName = null
364 $options['LIMIT'] = 1;
366 $objects = $this->select( $fields, $conditions, $options, $functionName );
368 return ( !$objects || $objects->isEmpty() ) ? false : $objects->current();
372 * Selects the specified fields of the records matching the provided
373 * conditions. Field names do NOT get prefixed.
375 * @since 1.20
377 * @param array $fields
378 * @param array $conditions
379 * @param array $options
380 * @param string|null $functionName
382 * @return stdClass
384 public function rawSelectRow( array $fields, array $conditions = array(),
385 array $options = array(), $functionName = null
387 $dbr = $this->getReadDbConnection();
389 $result = $dbr->selectRow(
390 $this->getName(),
391 $fields,
392 $conditions,
393 is_null( $functionName ) ? __METHOD__ : $functionName,
394 $options
397 $this->releaseConnection( $dbr );
399 return $result;
403 * Selects the specified fields of the first record matching the provided
404 * conditions and returns it as an associative array, or false when nothing matches.
405 * This method makes use of selectFields and expects the same parameters and
406 * returns the same results (if there are any, if there are none, this method returns false).
407 * @see ORMTable::selectFields
409 * @since 1.20
411 * @param array|string|null $fields
412 * @param array $conditions
413 * @param array $options
414 * @param bool $collapse Set to false to always return each result row as associative array.
415 * @param string|null $functionName
417 * @return mixed|array|bool False on failure
419 public function selectFieldsRow( $fields = null, array $conditions = array(),
420 array $options = array(), $collapse = true, $functionName = null
422 $options['LIMIT'] = 1;
424 $objects = $this->selectFields( $fields, $conditions, $options, $collapse, $functionName );
426 return empty( $objects ) ? false : $objects[0];
430 * Returns if there is at least one record matching the provided conditions.
431 * Condition field names get prefixed.
433 * @since 1.20
435 * @param array $conditions
437 * @return bool
439 public function has( array $conditions = array() ) {
440 return $this->selectRow( array( 'id' ), $conditions ) !== false;
444 * Checks if the table exists
446 * @since 1.21
448 * @return bool
450 public function exists() {
451 $dbr = $this->getReadDbConnection();
452 $exists = $dbr->tableExists( $this->getName() );
453 $this->releaseConnection( $dbr );
455 return $exists;
459 * Returns the amount of matching records.
460 * Condition field names get prefixed.
462 * Note that this can be expensive on large tables.
463 * In such cases you might want to use DatabaseBase::estimateRowCount instead.
465 * @since 1.20
467 * @param array $conditions
468 * @param array $options
470 * @return int
472 public function count( array $conditions = array(), array $options = array() ) {
473 $res = $this->rawSelectRow(
474 array( 'rowcount' => 'COUNT(*)' ),
475 $this->getPrefixedValues( $conditions ),
476 $options,
477 __METHOD__
480 return $res->rowcount;
484 * Removes the object from the database.
486 * @since 1.20
488 * @param array $conditions
489 * @param string|null $functionName
491 * @return bool Success indicator
493 public function delete( array $conditions, $functionName = null ) {
494 $dbw = $this->getWriteDbConnection();
496 $result = $dbw->delete(
497 $this->getName(),
498 $conditions === array() ? '*' : $this->getPrefixedValues( $conditions ),
499 is_null( $functionName ) ? __METHOD__ : $functionName
500 ) !== false; // DatabaseBase::delete does not always return true for success as documented...
502 $this->releaseConnection( $dbw );
504 return $result;
508 * Get API parameters for the fields supported by this object.
510 * @since 1.20
512 * @param bool $requireParams
513 * @param bool $setDefaults
515 * @return array
517 public function getAPIParams( $requireParams = false, $setDefaults = false ) {
518 $typeMap = array(
519 'id' => 'integer',
520 'int' => 'integer',
521 'float' => 'NULL',
522 'str' => 'string',
523 'bool' => 'integer',
524 'array' => 'string',
525 'blob' => 'string',
528 $params = array();
529 $defaults = $this->getDefaults();
531 foreach ( $this->getFields() as $field => $type ) {
532 if ( $field == 'id' ) {
533 continue;
536 $hasDefault = array_key_exists( $field, $defaults );
538 $params[$field] = array(
539 ApiBase::PARAM_TYPE => $typeMap[$type],
540 ApiBase::PARAM_REQUIRED => $requireParams && !$hasDefault
543 if ( $type == 'array' ) {
544 $params[$field][ApiBase::PARAM_ISMULTI] = true;
547 if ( $setDefaults && $hasDefault ) {
548 $default = is_array( $defaults[$field] )
549 ? implode( '|', $defaults[$field] )
550 : $defaults[$field];
551 $params[$field][ApiBase::PARAM_DFLT] = $default;
555 return $params;
559 * Returns an array with the fields and their descriptions.
561 * field name => field description
563 * @since 1.20
565 * @return array
567 public function getFieldDescriptions() {
568 return array();
572 * Get the database ID used for read operations.
574 * @since 1.20
576 * @return int DB_ enum
578 public function getReadDb() {
579 return $this->readDb;
583 * Set the database ID to use for read operations, use DB_XXX constants or
584 * an index to the load balancer setup.
586 * @param int $db
588 * @since 1.20
590 public function setReadDb( $db ) {
591 $this->readDb = $db;
595 * Get the ID of the any foreign wiki to use as a target for database operations
597 * @since 1.20
599 * @return string|bool The target wiki, in a form that LBFactory understands
600 * (or false if the local wiki is used)
602 public function getTargetWiki() {
603 return $this->wiki;
607 * Set the ID of the any foreign wiki to use as a target for database operations
609 * @param string|bool $wiki The target wiki, in a form that LBFactory
610 * understands (or false if the local wiki shall be used)
612 * @since 1.20
614 public function setTargetWiki( $wiki ) {
615 $this->wiki = $wiki;
619 * Get the database type used for read operations.
620 * This is to be used instead of wfGetDB.
622 * @see LoadBalancer::getConnection
624 * @since 1.20
626 * @return DatabaseBase The database object
628 public function getReadDbConnection() {
629 return $this->getConnection( $this->getReadDb(), array() );
633 * Get the database type used for read operations.
634 * This is to be used instead of wfGetDB.
636 * @see LoadBalancer::getConnection
638 * @since 1.20
640 * @return DatabaseBase The database object
642 public function getWriteDbConnection() {
643 return $this->getConnection( DB_MASTER, array() );
647 * Releases the lease on the given database connection. This is useful mainly
648 * for connections to a foreign wiki. It does nothing for connections to the local wiki.
650 * @see LoadBalancer::reuseConnection
652 * @param DatabaseBase $db
654 * @since 1.20
656 // @codingStandardsIgnoreStart Suppress "useless method overriding" sniffer warning
657 public function releaseConnection( DatabaseBase $db ) {
658 parent::releaseConnection( $db ); // just make it public
660 // @codingStandardsIgnoreEnd
663 * Update the records matching the provided conditions by
664 * setting the fields that are keys in the $values param to
665 * their corresponding values.
667 * @since 1.20
669 * @param array $values
670 * @param array $conditions
672 * @return bool Success indicator
674 public function update( array $values, array $conditions = array() ) {
675 $dbw = $this->getWriteDbConnection();
677 $result = $dbw->update(
678 $this->getName(),
679 $this->getPrefixedValues( $values ),
680 $this->getPrefixedValues( $conditions ),
681 __METHOD__
682 ) !== false; // DatabaseBase::update does not always return true for success as documented...
684 $this->releaseConnection( $dbw );
686 return $result;
690 * Computes the values of the summary fields of the objects matching the provided conditions.
692 * @since 1.20
694 * @param array|string|null $summaryFields
695 * @param array $conditions
697 public function updateSummaryFields( $summaryFields = null, array $conditions = array() ) {
698 $slave = $this->getReadDb();
699 $this->setReadDb( DB_MASTER );
702 * @var IORMRow $item
704 foreach ( $this->select( null, $conditions ) as $item ) {
705 $item->loadSummaryFields( $summaryFields );
706 $item->setSummaryMode( true );
707 $item->save();
710 $this->setReadDb( $slave );
714 * Takes in an associative array with field names as keys and
715 * their values as value. The field names are prefixed with the
716 * db field prefix.
718 * @since 1.20
720 * @param array $values
722 * @return array
724 public function getPrefixedValues( array $values ) {
725 $prefixedValues = array();
727 foreach ( $values as $field => $value ) {
728 if ( is_integer( $field ) ) {
729 if ( is_array( $value ) ) {
730 $field = $value[0];
731 $value = $value[1];
732 } else {
733 $value = explode( ' ', $value, 2 );
734 $value[0] = $this->getPrefixedField( $value[0] );
735 $prefixedValues[] = implode( ' ', $value );
736 continue;
740 $prefixedValues[$this->getPrefixedField( $field )] = $value;
743 return $prefixedValues;
747 * Takes in a field or array of fields and returns an
748 * array with their prefixed versions, ready for db usage.
750 * @since 1.20
752 * @param array $fields
754 * @return array
756 public function getPrefixedFields( array $fields ) {
757 foreach ( $fields as &$field ) {
758 $field = $this->getPrefixedField( $field );
761 return $fields;
765 * Takes in a field and returns an it's prefixed version, ready for db usage.
767 * @since 1.20
769 * @param string|array $field
771 * @return string
773 public function getPrefixedField( $field ) {
774 return $this->fieldPrefix . $field;
778 * Takes an array of field names with prefix and returns the unprefixed equivalent.
780 * @since 1.20
781 * @deprecated since 1.25, will be removed
783 * @param string[] $fieldNames
785 * @return string[]
787 public function unprefixFieldNames( array $fieldNames ) {
788 wfDeprecated( __METHOD__, '1.25' );
790 return $this->stripFieldPrefix( $fieldNames );
794 * Takes an array of field names with prefix and returns the unprefixed equivalent.
796 * @param string[] $fieldNames
798 * @return string[]
800 private function stripFieldPrefix( array $fieldNames ) {
801 $start = strlen( $this->fieldPrefix );
803 return array_map( function ( $fieldName ) use ( $start ) {
804 return substr( $fieldName, $start );
805 }, $fieldNames );
809 * Takes a field name with prefix and returns the unprefixed equivalent.
811 * @since 1.20
812 * @deprecated since 1.25, will be removed
814 * @param string $fieldName
816 * @return string
818 public function unprefixFieldName( $fieldName ) {
819 wfDeprecated( __METHOD__, '1.25' );
821 return substr( $fieldName, strlen( $this->fieldPrefix ) );
825 * Get an instance of this class.
827 * @since 1.20
828 * @deprecated since 1.21
830 * @return IORMTable
832 public static function singleton() {
833 $class = get_called_class();
835 if ( !array_key_exists( $class, self::$instanceCache ) ) {
836 self::$instanceCache[$class] = new $class;
839 return self::$instanceCache[$class];
843 * Get an array with fields from a database result,
844 * that can be fed directly to the constructor or
845 * to setFields.
847 * @since 1.20
849 * @param stdClass $result
850 * @throws MWException
851 * @return array
853 public function getFieldsFromDBResult( stdClass $result ) {
854 $result = (array)$result;
856 $rawFields = array_combine(
857 $this->stripFieldPrefix( array_keys( $result ) ),
858 array_values( $result )
861 $fieldDefinitions = $this->getFields();
862 $fields = array();
864 foreach ( $rawFields as $name => $value ) {
865 if ( array_key_exists( $name, $fieldDefinitions ) ) {
866 switch ( $fieldDefinitions[$name] ) {
867 case 'int':
868 $value = (int)$value;
869 break;
870 case 'float':
871 $value = (float)$value;
872 break;
873 case 'bool':
874 if ( is_string( $value ) ) {
875 $value = $value !== '0';
876 } elseif ( is_int( $value ) ) {
877 $value = $value !== 0;
879 break;
880 case 'array':
881 if ( is_string( $value ) ) {
882 $value = unserialize( $value );
885 if ( !is_array( $value ) ) {
886 $value = array();
888 break;
889 case 'blob':
890 if ( is_string( $value ) ) {
891 $value = unserialize( $value );
893 break;
894 case 'id':
895 if ( is_string( $value ) ) {
896 $value = (int)$value;
898 break;
901 $fields[$name] = $value;
902 } else {
903 throw new MWException( 'Attempted to set unknown field ' . $name );
907 return $fields;
911 * @see ORMTable::newRowFromFromDBResult
913 * @deprecated since 1.20 use newRowFromDBResult instead
914 * @since 1.20
916 * @param stdClass $result
918 * @return IORMRow
920 public function newFromDBResult( stdClass $result ) {
921 return self::newRowFromDBResult( $result );
925 * Get a new instance of the class from a database result.
927 * @since 1.20
929 * @param stdClass $result
931 * @return IORMRow
933 public function newRowFromDBResult( stdClass $result ) {
934 return $this->newRow( $this->getFieldsFromDBResult( $result ) );
938 * @see ORMTable::newRow
940 * @deprecated since 1.20 use newRow instead
941 * @since 1.20
943 * @param array $data
944 * @param bool $loadDefaults
946 * @return IORMRow
948 public function newFromArray( array $data, $loadDefaults = false ) {
949 return static::newRow( $data, $loadDefaults );
953 * Get a new instance of the class from an array.
955 * @since 1.20
957 * @param array $fields
958 * @param bool $loadDefaults
960 * @return IORMRow
962 public function newRow( array $fields, $loadDefaults = false ) {
963 $class = $this->getRowClass();
965 return new $class( $this, $fields, $loadDefaults );
969 * Return the names of the fields.
971 * @since 1.20
973 * @return array
975 public function getFieldNames() {
976 return array_keys( $this->getFields() );
980 * Gets if the object can take a certain field.
982 * @since 1.20
984 * @param string $name
986 * @return bool
988 public function canHaveField( $name ) {
989 return array_key_exists( $name, $this->getFields() );
993 * Updates the provided row in the database.
995 * @since 1.22
997 * @param IORMRow $row The row to save
998 * @param string|null $functionName
1000 * @return bool Success indicator
1002 public function updateRow( IORMRow $row, $functionName = null ) {
1003 $dbw = $this->getWriteDbConnection();
1005 $success = $dbw->update(
1006 $this->getName(),
1007 $this->getWriteValues( $row ),
1008 $this->getPrefixedValues( array( 'id' => $row->getId() ) ),
1009 is_null( $functionName ) ? __METHOD__ : $functionName
1012 $this->releaseConnection( $dbw );
1014 // DatabaseBase::update does not always return true for success as documented...
1015 return $success !== false;
1019 * Inserts the provided row into the database.
1021 * @since 1.22
1023 * @param IORMRow $row
1024 * @param string|null $functionName
1025 * @param array|null $options
1027 * @return bool Success indicator
1029 public function insertRow( IORMRow $row, $functionName = null, array $options = null ) {
1030 $dbw = $this->getWriteDbConnection();
1032 $success = $dbw->insert(
1033 $this->getName(),
1034 $this->getWriteValues( $row ),
1035 is_null( $functionName ) ? __METHOD__ : $functionName,
1036 $options
1039 // DatabaseBase::insert does not always return true for success as documented...
1040 $success = $success !== false;
1042 if ( $success ) {
1043 $row->setField( 'id', $dbw->insertId() );
1046 $this->releaseConnection( $dbw );
1048 return $success;
1052 * Gets the fields => values to write to the table.
1054 * @since 1.22
1056 * @param IORMRow $row
1058 * @return array
1060 protected function getWriteValues( IORMRow $row ) {
1061 $values = array();
1063 $rowFields = $row->getFields();
1065 foreach ( $this->getFields() as $name => $type ) {
1066 if ( array_key_exists( $name, $rowFields ) ) {
1067 $value = $rowFields[$name];
1069 switch ( $type ) {
1070 case 'array':
1071 $value = (array)$value;
1072 // fall-through!
1073 case 'blob':
1074 $value = serialize( $value );
1075 // fall-through!
1078 $values[$this->getPrefixedField( $name )] = $value;
1082 return $values;
1086 * Removes the provided row from the database.
1088 * @since 1.22
1090 * @param IORMRow $row
1091 * @param string|null $functionName
1093 * @return bool Success indicator
1095 public function removeRow( IORMRow $row, $functionName = null ) {
1096 $success = $this->delete(
1097 array( 'id' => $row->getId() ),
1098 is_null( $functionName ) ? __METHOD__ : $functionName
1101 // DatabaseBase::delete does not always return true for success as documented...
1102 return $success !== false;
1106 * Add an amount (can be negative) to the specified field (needs to be numeric).
1108 * @since 1.22
1110 * @param array $conditions
1111 * @param string $field
1112 * @param int $amount
1114 * @return bool Success indicator
1115 * @throws MWException
1117 public function addToField( array $conditions, $field, $amount ) {
1118 if ( !array_key_exists( $field, $this->fields ) ) {
1119 throw new MWException( 'Unknown field "' . $field . '" provided' );
1122 if ( $amount == 0 ) {
1123 return true;
1126 $absoluteAmount = abs( $amount );
1127 $isNegative = $amount < 0;
1129 $fullField = $this->getPrefixedField( $field );
1131 $dbw = $this->getWriteDbConnection();
1133 $success = $dbw->update(
1134 $this->getName(),
1135 array( "$fullField=$fullField" . ( $isNegative ? '-' : '+' ) . $absoluteAmount ),
1136 $this->getPrefixedValues( $conditions ),
1137 __METHOD__
1138 ) !== false; // DatabaseBase::update does not always return true for success as documented...
1140 $this->releaseConnection( $dbw );
1142 return $success;