Break long lines and formatting updates for includes/db/
[lhc/web/wiklou.git] / includes / db / ORMTable.php
index bcbe94a..cdc905b 100644 (file)
@@ -29,7 +29,6 @@
  */
 
 class ORMTable extends DBAccessBase implements IORMTable {
-
        /**
         * Cache for instances, used by the singleton method.
         *
@@ -96,7 +95,9 @@ class ORMTable extends DBAccessBase implements IORMTable {
         * @param string|null $rowClass
         * @param string $fieldPrefix
         */
-       public function __construct( $tableName = '', array $fields = array(), array $defaults = array(), $rowClass = null, $fieldPrefix = '' ) {
+       public function __construct( $tableName = '', array $fields = array(),
+               array $defaults = array(), $rowClass = null, $fieldPrefix = ''
+       ) {
                $this->tableName = $tableName;
                $this->fields = $fields;
                $this->defaults = $defaults;
@@ -201,8 +202,10 @@ class ORMTable extends DBAccessBase implements IORMTable {
         * @return ORMResult
         */
        public function select( $fields = null, array $conditions = array(),
-                                                       array $options = array(), $functionName = null ) {
+               array $options = array(), $functionName = null
+       ) {
                $res = $this->rawSelect( $fields, $conditions, $options, $functionName );
+
                return new ORMResult( $this, $res );
        }
 
@@ -221,7 +224,8 @@ class ORMTable extends DBAccessBase implements IORMTable {
         * @throws DBQueryError if the query failed (even if the database was in ignoreErrors mode).
         */
        public function selectObjects( $fields = null, array $conditions = array(),
-                                                                  array $options = array(), $functionName = null ) {
+               array $options = array(), $functionName = null
+       ) {
                $result = $this->selectFields( $fields, $conditions, $options, false, $functionName );
 
                $objects = array();
@@ -239,19 +243,19 @@ class ORMTable extends DBAccessBase implements IORMTable {
         * @since 1.20
         *
         * @param null|string|array $fields
-        * @param array             $conditions
-        * @param array             $options
-        * @param null|string       $functionName
+        * @param array $conditions
+        * @param array $options
+        * @param null|string $functionName
         *
         * @return ResultWrapper
         * @throws DBQueryError if the quey failed (even if the database was in ignoreErrors mode).
         */
        public function rawSelect( $fields = null, array $conditions = array(),
-                                                          array $options = array(), $functionName = null ) {
+               array $options = array(), $functionName = null
+       ) {
                if ( is_null( $fields ) ) {
                        $fields = array_keys( $this->getFields() );
-               }
-               else {
+               } else {
                        $fields = (array)$fields;
                }
 
@@ -313,7 +317,8 @@ class ORMTable extends DBAccessBase implements IORMTable {
         * @return array of array
         */
        public function selectFields( $fields = null, array $conditions = array(),
-                                                                 array $options = array(), $collapse = true, $functionName = null ) {
+               array $options = array(), $collapse = true, $functionName = null
+       ) {
                $objects = array();
 
                $result = $this->rawSelect( $fields, $conditions, $options, $functionName );
@@ -325,8 +330,7 @@ class ORMTable extends DBAccessBase implements IORMTable {
                if ( $collapse ) {
                        if ( count( $fields ) === 1 ) {
                                $objects = array_map( 'array_shift', $objects );
-                       }
-                       elseif ( count( $fields ) === 2 ) {
+                       } elseif ( count( $fields ) === 2 ) {
                                $o = array();
 
                                foreach ( $objects as $object ) {
@@ -354,7 +358,8 @@ class ORMTable extends DBAccessBase implements IORMTable {
         * @return IORMRow|bool False on failure
         */
        public function selectRow( $fields = null, array $conditions = array(),
-                                                          array $options = array(), $functionName = null ) {
+               array $options = array(), $functionName = null
+       ) {
                $options['LIMIT'] = 1;
 
                $objects = $this->select( $fields, $conditions, $options, $functionName );
@@ -376,7 +381,8 @@ class ORMTable extends DBAccessBase implements IORMTable {
         * @return ResultWrapper
         */
        public function rawSelectRow( array $fields, array $conditions = array(),
-                                                                 array $options = array(), $functionName = null ) {
+               array $options = array(), $functionName = null
+       ) {
                $dbr = $this->getReadDbConnection();
 
                $result = $dbr->selectRow(
@@ -388,6 +394,7 @@ class ORMTable extends DBAccessBase implements IORMTable {
                );
 
                $this->releaseConnection( $dbr );
+
                return $result;
        }
 
@@ -409,7 +416,8 @@ class ORMTable extends DBAccessBase implements IORMTable {
         * @return mixed|array|bool False on failure
         */
        public function selectFieldsRow( $fields = null, array $conditions = array(),
-                                                                        array $options = array(), $collapse = true, $functionName = null ) {
+               array $options = array(), $collapse = true, $functionName = null
+       ) {
                $options['LIMIT'] = 1;
 
                $objects = $this->selectFields( $fields, $conditions, $options, $collapse, $functionName );
@@ -491,6 +499,7 @@ class ORMTable extends DBAccessBase implements IORMTable {
                ) !== false; // DatabaseBase::delete does not always return true for success as documented...
 
                $this->releaseConnection( $dbw );
+
                return $result;
        }
 
@@ -535,7 +544,9 @@ class ORMTable extends DBAccessBase implements IORMTable {
                        }
 
                        if ( $setDefaults && $hasDefault ) {
-                               $default = is_array( $defaults[$field] ) ? implode( '|', $defaults[$field] ) : $defaults[$field];
+                               $default = is_array( $defaults[$field] )
+                                       ? implode( '|', $defaults[$field] )
+                                       : $defaults[$field];
                                $params[$field][ApiBase::PARAM_DFLT] = $default;
                        }
                }
@@ -568,7 +579,8 @@ class ORMTable extends DBAccessBase implements IORMTable {
        }
 
        /**
-        * Set the database ID to use for read operations, use DB_XXX constants or an index to the load balancer setup.
+        * Set the database ID to use for read operations, use DB_XXX constants or
+        *   an index to the load balancer setup.
         *
         * @param integer $db
         *
@@ -583,7 +595,8 @@ class ORMTable extends DBAccessBase implements IORMTable {
         *
         * @since 1.20
         *
-        * @return String|bool The target wiki, in a form that  LBFactory understands (or false if the local wiki is used)
+        * @return String|bool The target wiki, in a form that LBFactory understands
+        *   (or false if the local wiki is used)
         */
        public function getTargetWiki() {
                return $this->wiki;
@@ -592,7 +605,8 @@ class ORMTable extends DBAccessBase implements IORMTable {
        /**
         * Set the ID of the any foreign wiki to use as a target for database operations
         *
-        * @param string|bool $wiki The target wiki, in a form that  LBFactory understands (or false if the local wiki shall be used)
+        * @param string|bool $wiki The target wiki, in a form that  LBFactory
+        *   understands (or false if the local wiki shall be used)
         *
         * @since 1.20
         */
@@ -665,6 +679,7 @@ class ORMTable extends DBAccessBase implements IORMTable {
                ) !== false; // DatabaseBase::update does not always return true for success as documented...
 
                $this->releaseConnection( $dbw );
+
                return $result;
        }
 
@@ -711,8 +726,7 @@ class ORMTable extends DBAccessBase implements IORMTable {
                                if ( is_array( $value ) ) {
                                        $field = $value[0];
                                        $value = $value[1];
-                               }
-                               else {
+                               } else {
                                        $value = explode( ' ', $value, 2 );
                                        $value[0] = $this->getPrefixedField( $value[0] );
                                        $prefixedValues[] = implode( ' ', $value );
@@ -814,10 +828,59 @@ class ORMTable extends DBAccessBase implements IORMTable {
         */
        public function getFieldsFromDBResult( stdClass $result ) {
                $result = (array)$result;
-               return array_combine(
+
+               $rawFields = array_combine(
                        $this->unprefixFieldNames( array_keys( $result ) ),
                        array_values( $result )
                );
+
+               $fieldDefinitions = $this->getFields();
+               $fields = array();
+
+               foreach ( $rawFields as $name => $value ) {
+                       if ( array_key_exists( $name, $fieldDefinitions ) ) {
+                               switch ( $fieldDefinitions[$name] ) {
+                                       case 'int':
+                                               $value = (int)$value;
+                                               break;
+                                       case 'float':
+                                               $value = (float)$value;
+                                               break;
+                                       case 'bool':
+                                               if ( is_string( $value ) ) {
+                                                       $value = $value !== '0';
+                                               } elseif ( is_int( $value ) ) {
+                                                       $value = $value !== 0;
+                                               }
+                                               break;
+                                       case 'array':
+                                               if ( is_string( $value ) ) {
+                                                       $value = unserialize( $value );
+                                               }
+
+                                               if ( !is_array( $value ) ) {
+                                                       $value = array();
+                                               }
+                                               break;
+                                       case 'blob':
+                                               if ( is_string( $value ) ) {
+                                                       $value = unserialize( $value );
+                                               }
+                                               break;
+                                       case 'id':
+                                               if ( is_string( $value ) ) {
+                                                       $value = (int)$value;
+                                               }
+                                               break;
+                               }
+
+                               $fields[$name] = $value;
+                       } else {
+                               throw new MWException( 'Attempted to set unknown field ' . $name );
+                       }
+               }
+
+               return $fields;
        }
 
        /**
@@ -867,14 +930,15 @@ class ORMTable extends DBAccessBase implements IORMTable {
         *
         * @since 1.20
         *
-        * @param array $data
+        * @param array $fields
         * @param boolean $loadDefaults
         *
         * @return IORMRow
         */
-       public function newRow( array $data, $loadDefaults = false ) {
+       public function newRow( array $fields, $loadDefaults = false ) {
                $class = $this->getRowClass();
-               return new $class( $this, $data, $loadDefaults );
+
+               return new $class( $this, $fields, $loadDefaults );
        }
 
        /**
@@ -901,4 +965,156 @@ class ORMTable extends DBAccessBase implements IORMTable {
                return array_key_exists( $name, $this->getFields() );
        }
 
+       /**
+        * Updates the provided row in the database.
+        *
+        * @since 1.22
+        *
+        * @param IORMRow $row The row to save
+        * @param string|null $functionName
+        *
+        * @return boolean Success indicator
+        */
+       public function updateRow( IORMRow $row, $functionName = null ) {
+               $dbw = $this->getWriteDbConnection();
+
+               $success = $dbw->update(
+                       $this->getName(),
+                       $this->getWriteValues( $row ),
+                       $this->getPrefixedValues( array( 'id' => $row->getId() ) ),
+                       is_null( $functionName ) ? __METHOD__ : $functionName
+               );
+
+               $this->releaseConnection( $dbw );
+
+               // DatabaseBase::update does not always return true for success as documented...
+               return $success !== false;
+       }
+
+       /**
+        * Inserts the provided row into the database.
+        *
+        * @since 1.22
+        *
+        * @param IORMRow $row
+        * @param string|null $functionName
+        * @param array|null $options
+        *
+        * @return boolean Success indicator
+        */
+       public function insertRow( IORMRow $row, $functionName = null, array $options = null ) {
+               $dbw = $this->getWriteDbConnection();
+
+               $success = $dbw->insert(
+                       $this->getName(),
+                       $this->getWriteValues( $row ),
+                       is_null( $functionName ) ? __METHOD__ : $functionName,
+                       $options
+               );
+
+               // DatabaseBase::insert does not always return true for success as documented...
+               $success = $success !== false;
+
+               if ( $success ) {
+                       $row->setField( 'id', $dbw->insertId() );
+               }
+
+               $this->releaseConnection( $dbw );
+
+               return $success;
+       }
+
+       /**
+        * Gets the fields => values to write to the table.
+        *
+        * @since 1.22
+        *
+        * @param IORMRow $row
+        *
+        * @return array
+        */
+       protected function getWriteValues( IORMRow $row ) {
+               $values = array();
+
+               $rowFields = $row->getFields();
+
+               foreach ( $this->getFields() as $name => $type ) {
+                       if ( array_key_exists( $name, $rowFields ) ) {
+                               $value = $rowFields[$name];
+
+                               switch ( $type ) {
+                                       case 'array':
+                                               $value = (array)$value;
+                                       // fall-through!
+                                       case 'blob':
+                                               $value = serialize( $value );
+                                       // fall-through!
+                               }
+
+                               $values[$this->getPrefixedField( $name )] = $value;
+                       }
+               }
+
+               return $values;
+       }
+
+       /**
+        * Removes the provided row from the database.
+        *
+        * @since 1.22
+        *
+        * @param IORMRow $row
+        * @param string|null $functionName
+        *
+        * @return boolean Success indicator
+        */
+       public function removeRow( IORMRow $row, $functionName = null ) {
+               $success = $this->delete(
+                       array( 'id' => $row->getId() ),
+                       is_null( $functionName ) ? __METHOD__ : $functionName
+               );
+
+               // DatabaseBase::delete does not always return true for success as documented...
+               return $success !== false;
+       }
+
+       /**
+        * Add an amount (can be negative) to the specified field (needs to be numeric).
+        *
+        * @since 1.22
+        *
+        * @param array $conditions
+        * @param string $field
+        * @param integer $amount
+        *
+        * @return boolean Success indicator
+        * @throws MWException
+        */
+       public function addToField( array $conditions, $field, $amount ) {
+               if ( !array_key_exists( $field, $this->fields ) ) {
+                       throw new MWException( 'Unknown field "' . $field . '" provided' );
+               }
+
+               if ( $amount == 0 ) {
+                       return true;
+               }
+
+               $absoluteAmount = abs( $amount );
+               $isNegative = $amount < 0;
+
+               $fullField = $this->getPrefixedField( $field );
+
+               $dbw = $this->getWriteDbConnection();
+
+               $success = $dbw->update(
+                       $this->getName(),
+                       array( "$fullField=$fullField" . ( $isNegative ? '-' : '+' ) . $absoluteAmount ),
+                       $this->getPrefixedValues( $conditions ),
+                       __METHOD__
+               ) !== false; // DatabaseBase::update does not always return true for success as documented...
+
+               $this->releaseConnection( $dbw );
+
+               return $success;
+       }
 }