https://www.mediawiki.org/wiki/Special:Code/MediaWiki/110555

Revision: 110555
Author:   jeroendedauw
Date:     2012-02-02 10:03:53 +0000 (Thu, 02 Feb 2012)
Log Message:
-----------
split off revisioning code from db object wrapper

Modified Paths:
--------------
    trunk/extensions/EducationProgram/EducationProgram.php
    trunk/extensions/EducationProgram/includes/EPDBObject.php
    trunk/extensions/EducationProgram/includes/EPPageObject.php
    trunk/extensions/EducationProgram/includes/EPRevision.php

Added Paths:
-----------
    trunk/extensions/EducationProgram/includes/EPRevisionedObject.php

Modified: trunk/extensions/EducationProgram/EducationProgram.php
===================================================================
--- trunk/extensions/EducationProgram/EducationProgram.php      2012-02-02 
09:55:04 UTC (rev 110554)
+++ trunk/extensions/EducationProgram/EducationProgram.php      2012-02-02 
10:03:53 UTC (rev 110555)
@@ -95,6 +95,7 @@
 $wgAutoloadClasses['EPPageObject']                                     = 
dirname( __FILE__ ) . '/includes/EPPageObject.php';
 $wgAutoloadClasses['EPFailForm']                                       = 
dirname( __FILE__ ) . '/includes/EPFailForm.php';
 $wgAutoloadClasses['EPIRole']                                          = 
dirname( __FILE__ ) . '/includes/EPIRole.php';
+$wgAutoloadClasses['EPRevisionedObject']                       = dirname( 
__FILE__ ) . '/includes/EPRevisionedObject.php';
 
 $wgAutoloadClasses['CoursePage']                                       = 
dirname( __FILE__ ) . '/pages/CoursePage.php';
 $wgAutoloadClasses['EPPage']                                           = 
dirname( __FILE__ ) . '/pages/EPPage.php';

Modified: trunk/extensions/EducationProgram/includes/EPDBObject.php
===================================================================
--- trunk/extensions/EducationProgram/includes/EPDBObject.php   2012-02-02 
09:55:04 UTC (rev 110554)
+++ trunk/extensions/EducationProgram/includes/EPDBObject.php   2012-02-02 
10:03:53 UTC (rev 110555)
@@ -49,25 +49,19 @@
         * @var bool
         */
        protected $updateSummaries = true;
-
+       
        /**
-        * If the object should log changes.
-        * Can be changed via disableLogging and enableLogging.
+        * Indicates if the object is in summary mode.
+        * This mode indicates that only summary fields got updated,
+        * which allows for optimizations.
         *
         * @since 0.1
         * @var bool
         */
-       protected $log = true;
+       protected $inSummaryMode = false;
+       
 
        /**
-        * If the object should store old revisions.
-        *
-        * @since 0.1
-        * @var bool
-        */
-       protected $storeRevisions = true;
-
-       /**
         * The database connection to use for read operations.
         * Can be changed via @see setReadDb.
         *
@@ -146,6 +140,19 @@
        public static function getDefaults() {
                return array();
        }
+       
+       /**
+        * Returns a list of the summary fields.
+        * These are fields that cache computed values, such as the amount of 
linked objects of $type.
+        * This is relevant as one might not want to do actions such as log 
changes when these get updated.
+        *
+        * @since 0.1
+        *
+        * @return array
+        */
+       public static function getSummaryFields() {
+               return array();
+       }
 
        /**
         * Constructor.
@@ -418,40 +425,10 @@
                        __METHOD__
                );
 
-               if ( $success ) {
-                       $this->storeRevision();
-                       $this->log( 'update' );
-               }
-
                return $success;
        }
 
        /**
-        * Store the current version of the object in the revisions table.
-        * TODO: add handling for comment, minor edit, ect stuff
-        *
-        * @since 0.1
-        *
-        * @param bool $isDelete
-        *
-        * @return boolean Success indicator
-        */
-       protected function storeRevision( $isDelete = false ) {
-               if ( $this->storeRevisions ) {
-                       static::setReadDb( DB_MASTER );
-                       $revison = static::selectRow( null, array( 'id' => 
$this->getId() ) );
-                       static::setReadDb( DB_SLAVE );
-
-                       $revison = EPRevision::newFromObject( $revison, 
$isDelete );
-                       $revison->setStoreRevisions( false );
-
-                       return $revison->writeToDB();
-               }
-
-               return true;
-       }
-
-       /**
         * Inserts the object into the database.
         *
         * @since 0.1
@@ -470,8 +447,6 @@
 
                if ( $result ) {
                        $this->setField( 'id', $dbw->insertId() );
-                       $this->storeRevision();
-                       $this->log( 'add' );
                }
 
                return $result;
@@ -489,44 +464,12 @@
 
                if ( $success ) {
                        $this->setField( 'id', null );
-                       $this->log( 'remove' );
                }
 
                return $success;
        }
 
        /**
-        * Log an action.
-        *
-        * @since 0.1
-        *
-        * @param string $subType
-        */
-       protected function log( $subType ) {
-               if ( $this->log ) {
-                       $info = $this->getLogInfo( $subType );
-                       
-                       if ( $info !== false ) {
-                               $info['subtype'] = $subType;
-                               EPUtils::log( $info );
-                       }
-               }
-       }
-
-       /**
-        * Returns the info for the log entry or false if no entry should be 
created.
-        *
-        * @since 0.1
-        *
-        * @param string $subType
-        *
-        * @return array|false
-        */
-       protected function getLogInfo( $subType ) {
-               return false;
-       }
-
-       /**
         * Return the names and values of the fields.
         *
         * @since 0.1
@@ -1228,9 +1171,8 @@
 
                foreach ( self::select( 'id', $conditions ) as /* EPDBObject */ 
$item ) {
                        $item->loadSummaryFields( $summaryFields );
-                       $item->disableLogging();
-                       $item->writeToDB();
-                       $item->enableLogging();
+                       $item->setSummaryMode( true );
+                       $item->updateInDB();
                }
 
                self::setReadDb( DB_SLAVE );
@@ -1247,33 +1189,15 @@
                $this->updateSummaries = $update;
        }
 
-       /**
-        * Sets the value for the @see $storeRevisions field.
+               /**
+        * Sets the value for the @see $updateSummaries field.
         *
         * @since 0.1
         *
-        * @param boolean $store
+        * @param boolean $update
         */
-       public function setStoreRevisions( $store ) {
-               $this->storeRevisions = $store;
+       public function setSummaryMode( $summaryMode ) {
+               $this->inSummaryMode = $summaryMode;
        }
 
-       /**
-        * Sets the value for the @see $log field.
-        *
-        * @since 0.1
-        */
-       public function enableLogging() {
-               $this->log = true;
-       }
-
-       /**
-        * Sets the value for the @see $log field.
-        *
-        * @since 0.1
-        */
-       public function disableLogging() {
-               $this->log = false;
-       }
-
 }

Modified: trunk/extensions/EducationProgram/includes/EPPageObject.php
===================================================================
--- trunk/extensions/EducationProgram/includes/EPPageObject.php 2012-02-02 
09:55:04 UTC (rev 110554)
+++ trunk/extensions/EducationProgram/includes/EPPageObject.php 2012-02-02 
10:03:53 UTC (rev 110555)
@@ -1,7 +1,7 @@
 <?php
 
 /**
- * Abstract base class for EPDBObjects that have associated view, edit and 
history pages
+ * Abstract base class for EPRevisionedObject that have associated view, edit 
and history pages.
  *
  * @since 0.1
  *
@@ -11,7 +11,7 @@
  * @licence GNU GPL v3 or later
  * @author Jeroen De Dauw < [email protected] >
  */
-abstract class EPPageObject extends EPDBObject {
+abstract class EPPageObject extends EPRevisionedObject {
 
        protected static $info = array(
                'EPCourse' => array(

Modified: trunk/extensions/EducationProgram/includes/EPRevision.php
===================================================================
--- trunk/extensions/EducationProgram/includes/EPRevision.php   2012-02-02 
09:55:04 UTC (rev 110554)
+++ trunk/extensions/EducationProgram/includes/EPRevision.php   2012-02-02 
10:03:53 UTC (rev 110555)
@@ -31,7 +31,6 @@
         * @param bool $loadDefaults
         */
        public function __construct( $fields = null, $loadDefaults = false ) {
-               $this->setStoreRevisions( false );
                parent::__construct( $fields, $loadDefaults );
        }
 

Added: trunk/extensions/EducationProgram/includes/EPRevisionedObject.php
===================================================================
--- trunk/extensions/EducationProgram/includes/EPRevisionedObject.php           
                (rev 0)
+++ trunk/extensions/EducationProgram/includes/EPRevisionedObject.php   
2012-02-02 10:03:53 UTC (rev 110555)
@@ -0,0 +1,197 @@
+<?php
+
+/**
+ * Abstract base class for EPDBObjects with revision history and logging 
support.
+ *
+ * @since 0.1
+ *
+ * @file EPRevisionedObject.php
+ * @ingroup EducationProgram
+ *
+ * @licence GNU GPL v3 or later
+ * @author Jeroen De Dauw < [email protected] >
+ */
+abstract class EPRevisionedObject extends EPDBObject {
+       
+       /**
+        * If the object should log changes.
+        * Can be changed via disableLogging and enableLogging.
+        *
+        * @since 0.1
+        * @var bool
+        */
+       protected $log = true;
+
+       /**
+        * If the object should store old revisions.
+        *
+        * @since 0.1
+        * @var bool
+        */
+       protected $storeRevisions = true;
+       
+       /**
+        * Sets the value for the @see $storeRevisions field.
+        *
+        * @since 0.1
+        *
+        * @param boolean $store
+        */
+       public function setStoreRevisions( $store ) {
+               $this->storeRevisions = $store;
+       }
+
+       /**
+        * Sets the value for the @see $log field.
+        *
+        * @since 0.1
+        */
+       public function enableLogging() {
+               $this->log = true;
+       }
+
+       /**
+        * Sets the value for the @see $log field.
+        *
+        * @since 0.1
+        */
+       public function disableLogging() {
+               $this->log = false;
+       }
+       
+       /**
+        * Returns the info for the log entry or false if no entry should be 
created.
+        *
+        * @since 0.1
+        *
+        * @param string $subType
+        *
+        * @return array|false
+        */
+       protected function getLogInfo( $subType ) {
+               return false;
+       }
+       
+       /**
+        * Store the current version of the object in the revisions table.
+        * TODO: add handling for comment, minor edit, ect stuff
+        *
+        * @since 0.1
+        *
+        * @param bool $isDelete
+        *
+        * @return boolean Success indicator
+        */
+       protected function storeRevision( EPRevisionedObject $revision, 
$isDelete = false ) {
+               if ( $this->storeRevisions ) {
+                       $revison->setStoreRevisions( false );
+                       return $revison->writeToDB();
+               }
+
+               return true;
+       }
+       
+       /**
+        * Log an action.
+        *
+        * @since 0.1
+        *
+        * @param string $subType
+        */
+       protected function log( $subType ) {
+               if ( $this->log ) {
+                       $info = $this->getLogInfo( $subType );
+                       
+                       if ( $info !== false ) {
+                               $info['subtype'] = $subType;
+                               EPUtils::log( $info );
+                       }
+               }
+       }
+       
+       /**
+        * Returns the current revision of the object, ie what is in the 
database.
+        * 
+        * @since 0.1
+        * 
+        * @return EPRevisionedObject
+        */
+       protected function getCurrentRevision() {
+               static::setReadDb( DB_MASTER );
+               $revison = static::selectRow( null, array( 'id' => 
$this->getId() ) );
+               static::setReadDb( DB_SLAVE );
+               
+               return EPRevision::newFromObject( $revison, $isDelete );;
+       }
+       
+       /**
+        * Return if any fields got changed.
+        * 
+        * @since 0.1
+        * 
+        * @param EPRevisionedObject $revision
+        * @param boolean $excludeSummaryFields When set to true, summaty field 
changes are ignored.
+        * 
+        * @return boolean
+        */
+       protected function fieldsChanged( EPRevisionedObject $revision, 
$excludeSummaryFields = false ) {
+               foreach ( $this->fields as $name => $value ) {
+                       $excluded = $excludeSummaryFields && in_array( $name, 
$this->getSummaryFields() );
+                       
+                       if ( !$excluded && $revision->getField( $name ) !== 
$value ) {
+                               return true;
+                       }
+               }
+               
+               return false;
+       }
+       
+       /**
+        * (non-PHPdoc)
+        * @see EPDBObject::updateInDB()
+        */
+       protected function updateInDB() {
+               $success = parent::updateInDB();
+
+               if ( $success && !$this->inSummaryMode ) {
+                       $revision = $this->getCurrentRevision();
+                       
+                       if ( $this->fieldsChanged( $revision, true ) ) {
+                               $this->storeRevision( $revision );
+                               $this->log( 'update' );
+                       }
+               }
+
+               return $success;
+       }
+       
+       /**
+        * (non-PHPdoc)
+        * @see EPDBObject::insertIntoDB()
+        */
+       protected function insertIntoDB() {
+               $result = parent::insertIntoDB();
+
+               if ( $result ) {
+                       $this->storeRevision( $this );
+                       $this->log( 'add' );
+               }
+
+               return $result;
+       }
+       
+       /**
+        * (non-PHPdoc)
+        * @see EPDBObject::removeFromDB()
+        */
+       public function removeFromDB() {
+               $success = parent::removeFromDB();
+
+               if ( $success ) {
+                       $this->log( 'remove' );
+               }
+
+               return $success;
+       }
+       
+}


Property changes on: 
trunk/extensions/EducationProgram/includes/EPRevisionedObject.php
___________________________________________________________________
Added: svn:eol-style
   + native


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to