Move to EP untill slush ends
authorJeroen De Dauw <jeroendedauw@users.mediawiki.org>
Wed, 15 Feb 2012 22:56:53 +0000 (22:56 +0000)
committerJeroen De Dauw <jeroendedauw@users.mediawiki.org>
Wed, 15 Feb 2012 22:56:53 +0000 (22:56 +0000)
includes/DBDataObject.php [deleted file]

diff --git a/includes/DBDataObject.php b/includes/DBDataObject.php
deleted file mode 100644 (file)
index 89765b1..0000000
+++ /dev/null
@@ -1,640 +0,0 @@
-<?php
-
-/**
- * Abstract base class for representing objects that are stored in some DB table.
- * This is basically an ORM-like wrapper around rows in database tables that
- * aims to be both simple and very flexible. It is centered around an associative
- * array of fields and various methods to do common interaction with the database.
- *
- * These methods must be implemented in deriving classes:
- * * getDBTable
- * * getFieldPrefix
- * * getFieldTypes
- *
- * These methods are likely candidates for overriding:
- * * getDefaults
- * * remove
- * * insert
- * * saveExisting
- * * loadSummaryFields
- * * getSummaryFields
- *
- * Main instance methods:
- * * getField(s)
- * * setField(s)
- * * save
- * * remove
- * 
- * Main static methods:
- * * select
- * * update
- * * delete
- * * count
- * * has
- * * selectRow
- * * selectFields
- * * selectFieldsRow
- *
- * @since 1.20
- *
- * @file DBDataObject.php
- *
- * @licence GNU GPL v2 or later
- * @author Jeroen De Dauw < jeroendedauw@gmail.com >
- */
-abstract class DBDataObject {
-
-       /**
-        * The fields of the object.
-        * field name (w/o prefix) => value
-        *
-        * @since 1.20
-        * @var array
-        */
-       protected $fields = array( 'id' => null );
-
-       /**
-        * @since 1.20
-        * @var DBTable
-        */
-       protected $table;
-       
-       /**
-        * If the object should update summaries of linked items when changed.
-        * For example, update the course_count field in universities when a course in courses is deleted.
-        * Settings this to false can prevent needless updating work in situations
-        * such as deleting a university, which will then delete all it's courses.
-        *
-        * @since 1.20
-        * @var bool
-        */
-       protected $updateSummaries = true;
-       
-       /**
-        * Indicates if the object is in summary mode.
-        * This mode indicates that only summary fields got updated,
-        * which allows for optimizations.
-        *
-        * @since 1.20
-        * @var bool
-        */
-       protected $inSummaryMode = false;
-
-       /**
-        * Constructor.
-        *
-        * @since 1.20
-        *
-        * @param DBTable $table
-        * @param array|null $fields
-        * @param boolean $loadDefaults
-        */
-       public function __construct( DBTable $table, $fields = null, $loadDefaults = false ) {
-               $this->table = $table;
-               
-               if ( !is_array( $fields ) ) {
-                       $fields = array();
-               }
-
-               if ( $loadDefaults ) {
-                       $fields = array_merge( $this->table->getDefaults(), $fields );
-               }
-
-               $this->setFields( $fields );
-       }
-
-       /**
-        * Load the specified fields from the database.
-        *
-        * @since 1.20
-        *
-        * @param array|null $fields
-        * @param boolean $override
-        * @param boolean $skipLoaded
-        *
-        * @return bool Success indicator
-        */
-       public function loadFields( $fields = null, $override = true, $skipLoaded = false ) {
-               if ( is_null( $this->getId() ) ) {
-                       return false;
-               }
-
-               if ( is_null( $fields ) ) {
-                       $fields = array_keys( $this->table->getFieldTypes() );
-               }
-
-               if ( $skipLoaded ) {
-                       $fields = array_diff( $fields, array_keys( $this->fields ) );
-               }
-
-               if ( count( $fields ) > 0 ) {
-                       $result = $this->table->rawSelectRow(
-                               $this->table->getPrefixedFields( $fields ),
-                               array( $this->table->getPrefixedField( 'id' ) => $this->getId() ),
-                               array( 'LIMIT' => 1 )
-                       );
-
-                       if ( $result !== false ) {
-                               $this->setFields( $this->table->getFieldsFromDBResult( $result ), $override );
-                               return true;
-                       }
-       
-                       return false;
-               }
-
-               return true;
-       }
-
-       /**
-        * Gets the value of a field.
-        *
-        * @since 1.20
-        *
-        * @param string $name
-        * @param mixed $default
-        *
-        * @throws MWException
-        * @return mixed
-        */
-       public function getField( $name, $default = null ) {
-               if ( $this->hasField( $name ) ) {
-                       return $this->fields[$name];
-               } elseif ( !is_null( $default ) ) {
-                       return $default;
-               } else {
-                       throw new MWException( 'Attempted to get not-set field ' . $name );
-               }
-       }
-
-       /**
-        * Gets the value of a field but first loads it if not done so already.
-        *
-        * @since 1.20
-        *
-        * @param string$name
-        *
-        * @return mixed
-        */
-       public function loadAndGetField( $name ) {
-               if ( !$this->hasField( $name ) ) {
-                       $this->loadFields( array( $name ) );
-               }
-
-               return $this->getField( $name );
-       }
-
-       /**
-        * Remove a field.
-        *
-        * @since 1.20
-        *
-        * @param string $name
-        */
-       public function removeField( $name ) {
-               unset( $this->fields[$name] );
-       }
-
-       /**
-        * Returns the objects database id.
-        *
-        * @since 1.20
-        *
-        * @return integer|null
-        */
-       public function getId() {
-               return $this->getField( 'id' );
-       }
-
-       /**
-        * Sets the objects database id.
-        *
-        * @since 1.20
-        *
-        * @param integer|null $id
-        */
-       public function setId( $id ) {
-               return $this->setField( 'id', $id );
-       }
-
-       /**
-        * Gets if a certain field is set.
-        *
-        * @since 1.20
-        *
-        * @param string $name
-        *
-        * @return boolean
-        */
-       public function hasField( $name ) {
-               return array_key_exists( $name, $this->fields );
-       }
-
-       /**
-        * Gets if the id field is set.
-        *
-        * @since 1.20
-        *
-        * @return boolean
-        */
-       public function hasIdField() {
-               return $this->hasField( 'id' )
-                       && !is_null( $this->getField( 'id' ) );
-       }
-
-       /**
-        * Sets multiple fields.
-        *
-        * @since 1.20
-        *
-        * @param array $fields The fields to set
-        * @param boolean $override Override already set fields with the provided values?
-        */
-       public function setFields( array $fields, $override = true ) {
-               foreach ( $fields as $name => $value ) {
-                       if ( $override || !$this->hasField( $name ) ) {
-                               $this->setField( $name, $value );
-                       }
-               }
-       }
-
-       /**
-        * Gets the fields => values to write to the table.
-        *
-        * @since 1.20
-        *
-        * @return array
-        */
-       protected function getWriteValues() {
-               $values = array();
-
-               foreach ( $this->table->getFieldTypes() as $name => $type ) {
-                       if ( array_key_exists( $name, $this->fields ) ) {
-                               $value = $this->fields[$name];
-
-                               switch ( $type ) {
-                                       case 'array':
-                                               $value = (array)$value;
-                                       case 'blob':
-                                               $value = serialize( $value );
-                               }
-
-                               $values[$this->table->getPrefixedField( $name )] = $value;
-                       }
-               }
-
-               return $values;
-       }
-
-       /**
-        * Serializes the object to an associative array which
-        * can then easily be converted into JSON or similar.
-        *
-        * @since 1.20
-        *
-        * @param null|array $fields
-        * @param boolean $incNullId
-        *
-        * @return array
-        */
-       public function toArray( $fields = null, $incNullId = false ) {
-               $data = array();
-               $setFields = array();
-
-               if ( !is_array( $fields ) ) {
-                       $setFields = $this->getSetFieldNames();
-               } else {
-                       foreach ( $fields as $field ) {
-                               if ( $this->hasField( $field ) ) {
-                                       $setFields[] = $field;
-                               }
-                       }
-               }
-
-               foreach ( $setFields as $field ) {
-                       if ( $incNullId || $field != 'id' || $this->hasIdField() ) {
-                               $data[$field] = $this->getField( $field );
-                       }
-               }
-
-               return $data;
-       }
-
-       /**
-        * Load the default values, via getDefaults.
-        *
-        *  @since 1.20
-        *
-        * @param boolean $override
-        */
-       public function loadDefaults( $override = true ) {
-               $this->setFields( $this->table->getDefaults(), $override );
-       }
-
-       /**
-        * Writes the answer to the database, either updating it
-        * when it already exists, or inserting it when it doesn't.
-        *
-        * @since 1.20
-        *
-        * @return boolean Success indicator
-        */
-       public function save() {
-               if ( $this->hasIdField() ) {
-                       return $this->saveExisting();
-               } else {
-                       return $this->insert();
-               }
-       }
-
-       /**
-        * Updates the object in the database.
-        *
-        * @since 1.20
-        *
-        * @return boolean Success indicator
-        */
-       protected function saveExisting() {
-               $dbw = wfGetDB( DB_MASTER );
-
-               $success = $dbw->update(
-                       $this->table->getDBTable(),
-                       $this->getWriteValues(),
-                       array( $this->table->getPrefixedField( 'id' ) => $this->getId() ),
-                       __METHOD__
-               );
-
-               return $success;
-       }
-
-       /**
-        * Inserts the object into the database.
-        *
-        * @since 1.20
-        *
-        * @return boolean Success indicator
-        */
-       protected function insert() {
-               $dbw = wfGetDB( DB_MASTER );
-
-               $result = $dbw->insert(
-                       $this->table->getDBTable(),
-                       $this->getWriteValues(),
-                       __METHOD__,
-                       array( 'IGNORE' )
-               );
-
-               if ( $result ) {
-                       $this->setField( 'id', $dbw->insertId() );
-               }
-
-               return $result;
-       }
-
-       /**
-        * Removes the object from the database.
-        *
-        * @since 1.20
-        *
-        * @return boolean Success indicator
-        */
-       public function remove() {
-               $this->beforeRemove();
-               
-               $success = $this->table->delete( array( 'id' => $this->getId() ) );
-
-               if ( $success ) {
-                       $this->onRemoved();
-               }
-
-               return $success;
-       }
-       
-       /**
-        * Gets called before an object is removed from the database.
-        * 
-        * @since 1.20
-        */
-       protected function beforeRemove() {
-               $this->loadFields( $this->getBeforeRemoveFields(), false, true );
-       }
-
-       /**
-        * Before removal of an object happens, @see beforeRemove gets called.
-        * This method loads the fields of which the names have been returned by this one (or all fields if null is returned).
-        * This allows for loading info needed after removal to get rid of linked data and the like.
-        * 
-        * @since 1.20
-        * 
-        * @return array|null
-        */
-       protected function getBeforeRemoveFields() {
-               return array();
-       }
-       
-       /**
-        * Gets called after successfull removal.
-        * Can be overriden to get rid of linked data.
-        * 
-        * @since 1.20
-        */
-       protected function onRemoved() {
-               $this->setField( 'id', null );
-       }
-
-       /**
-        * Return the names and values of the fields.
-        *
-        * @since 1.20
-        *
-        * @return array
-        */
-       public function getFields() {
-               return $this->fields;
-       }
-
-       /**
-        * Return the names of the fields.
-        *
-        * @since 1.20
-        *
-        * @return array
-        */
-       public function getSetFieldNames() {
-               return array_keys( $this->fields );
-       }
-
-       /**
-        * Sets the value of a field.
-        * Strings can be provided for other types,
-        * so this method can be called from unserialization handlers.
-        *
-        * @since 1.20
-        *
-        * @param string $name
-        * @param mixed $value
-        *
-        * @throws MWException
-        */
-       public function setField( $name, $value ) {
-               $fields = $this->table->getFieldTypes();
-
-               if ( array_key_exists( $name, $fields ) ) {
-                       switch ( $fields[$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;
-                       }
-
-                       $this->fields[$name] = $value;
-               } else {
-                       throw new MWException( 'Attempted to set unknown field ' . $name );
-               }
-       }
-
-       /**
-        * Add an amount (can be negative) to the specified field (needs to be numeric).
-        *
-        * @since 1.20
-        *
-        * @param string $field
-        * @param integer $amount
-        *
-        * @return boolean Success indicator
-        */
-       public function addToField( $field, $amount ) {
-               if ( $amount == 0 ) {
-                       return true;
-               }
-
-               if ( !$this->hasIdField() ) {
-                       return false;
-               }
-
-               $absoluteAmount = abs( $amount );
-               $isNegative = $amount < 0;
-
-               $dbw = wfGetDB( DB_MASTER );
-
-               $fullField = $this->table->getPrefixedField( $field );
-
-               $success = $dbw->update(
-                       $this->table->getDBTable(),
-                       array( "$fullField=$fullField" . ( $isNegative ? '-' : '+' ) . $absoluteAmount ),
-                       array( $this->table->getPrefixedField( 'id' ) => $this->getId() ),
-                       __METHOD__
-               );
-
-               if ( $success && $this->hasField( $field ) ) {
-                       $this->setField( $field, $this->getField( $field ) + $amount );
-               }
-
-               return $success;
-       }
-
-       /**
-        * Return the names of the fields.
-        *
-        * @since 1.20
-        *
-        * @return array
-        */
-       public function getFieldNames() {
-               return array_keys( $this->table->getFieldTypes() );
-       }
-
-       /**
-        * Computes and updates the values of the summary fields.
-        *
-        * @since 1.20
-        *
-        * @param array|string|null $summaryFields
-        */
-       public function loadSummaryFields( $summaryFields = null ) {
-
-       }
-
-       /**
-        * Sets the value for the @see $updateSummaries field.
-        *
-        * @since 1.20
-        *
-        * @param boolean $update
-        */
-       public function setUpdateSummaries( $update ) {
-               $this->updateSummaries = $update;
-       }
-
-       /**
-        * Sets the value for the @see $inSummaryMode field.
-        *
-        * @since 1.20
-        *
-        * @param boolean $summaryMode
-        */
-       public function setSummaryMode( $summaryMode ) {
-               $this->inSummaryMode = $summaryMode;
-       }
-
-       /**
-        * Return if any fields got changed.
-        *
-        * @since 1.20
-        *
-        * @param DBDataObject $object
-        * @param boolean $excludeSummaryFields When set to true, summary field changes are ignored.
-        *
-        * @return boolean
-        */
-       protected function fieldsChanged( DBDataObject $object, $excludeSummaryFields = false ) {
-               foreach ( $this->fields as $name => $value ) {
-                       $excluded = $excludeSummaryFields && in_array( $name, $this->table->getSummaryFields() );
-
-                       if ( !$excluded && $object->getField( $name ) !== $value ) {
-                               return true;
-                       }
-               }
-
-               return false;
-       }
-
-       /**
-        * Returns the table this DBDataObject is a row in.
-        * 
-        * @since 1.20
-        * 
-        * @return DBTable
-        */
-       public function getTable() {
-               return $this->table;
-       }
-
-}