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

Revision: 108352
Author:   jeroendedauw
Date:     2012-01-08 08:10:53 +0000 (Sun, 08 Jan 2012)
Log Message:
-----------
work on automatic updating of summary data

Modified Paths:
--------------
    trunk/extensions/EducationProgram/includes/EPCourse.php
    trunk/extensions/EducationProgram/includes/EPDBObject.php
    trunk/extensions/EducationProgram/includes/EPOrg.php
    trunk/extensions/EducationProgram/includes/EPTerm.php

Modified: trunk/extensions/EducationProgram/includes/EPCourse.php
===================================================================
--- trunk/extensions/EducationProgram/includes/EPCourse.php     2012-01-08 
06:59:41 UTC (rev 108351)
+++ trunk/extensions/EducationProgram/includes/EPCourse.php     2012-01-08 
08:10:53 UTC (rev 108352)
@@ -51,6 +51,40 @@
                        'description' => '',
                );
        }
+
+       /**
+        * (non-PHPdoc)
+        * @see EPDBObject::loadSummaryFields()
+        */
+       public function loadSummaryFields( $summaryFields = null ) {
+               if ( is_null( $summaryFields ) ) {
+                       $summaryFields = array( 'students' );
+               }
+               else {
+                       $summaryFields = (array)$summaryFields;
+               }
+
+               $fields = array();
+
+               if ( in_array( 'students', $summaryFields ) ) {
+                       $termIds = EPTerm::selectFields( 'id', array( 
'course_id' => $this->getId() ) );
+
+                       if ( count( $termIds ) > 0 ) {
+                               $fields['students'] = wfGetDB( DB_SLAVE 
)->select(
+                                       'ep_students_per_term',
+                                       'COUNT(*) AS rowcount',
+                                       array( 'spt_term_id' => $termIds )
+                               );
+
+                               $fields['students'] = 
$fields['students']->fetchObject()->rowcount;
+                       }
+                       else {
+                               $fields['students'] = 0;
+                       }
+               }
+
+               $this->setFields( $fields );
+       }
        
        /**
         * (non-PHPdoc)
@@ -58,19 +92,61 @@
         */
        public function removeFromDB() {
                $id = $this->getId();
-               
+
+               if ( $this->updateSummaries ) {
+                       $this->loadFields( array( 'org_id' ) );
+                       $orgId = $this->getField( 'org_id', false );
+               }
+
                $success = parent::removeFromDB();
                
                if ( $success ) {
                        foreach ( EPTerm::select( 'id', array( 'course_id' => 
$id ) ) as /* EPTerm */ $term ) {
+                               $term->setUpdateSummaries( false );
                                $success = $term->removeFromDB() && $success;
                        }
                }
+
+               if ( $this->updateSummaries && $orgId !== false ) {
+                       EPOrg::updateSummaryFields( array( 'terms', 'students', 
'courses' ), array( 'id' => $orgId ) );
+               }
                
                return $success;
        }
-       
+
        /**
+        * (non-PHPdoc)
+        * @see EPDBObject::insertIntoDB()
+        */
+       protected function insertIntoDB() {
+               $success = parent::insertIntoDB();
+
+               if ( $this->updateSummaries ) {
+                       EPOrg::updateSummaryFields( 'courses', array( 'id' => 
$this->getField( 'org_id' ) ) );
+               }
+
+               return $success;
+       }
+
+       /**
+        * (non-PHPdoc)
+        * @see EPDBObject::updateInDB()
+        */
+       protected function updateInDB() {
+               $oldOrgId = $this->hasField( 'org_id' ) ? 
self::selectFieldsRow( 'org_id', array( 'id' => $this->getId() ) ) : false;
+
+               $success = parent::updateInDB();
+
+               if ( $this->updateSummaries && $success && $oldOrgId !== false 
&& $oldOrgId !== $this->getField( 'org_id' ) ) {
+                       $conds = array( 'id' => array( $oldOrgId, 
$this->getField( 'org_id' ) ) );
+                       EPOrg::updateSummaryFields( array( 'terms', 'students', 
'courses' ), $conds );
+                       EPTerm::updateSummaryFields( 'org_id', array( 
'course_id' => $this->getId() ) );
+               }
+
+               return $success;
+       }
+
+       /**
         * Returns the org associated with this course.
         * 
         * @since 0.1

Modified: trunk/extensions/EducationProgram/includes/EPDBObject.php
===================================================================
--- trunk/extensions/EducationProgram/includes/EPDBObject.php   2012-01-08 
06:59:41 UTC (rev 108351)
+++ trunk/extensions/EducationProgram/includes/EPDBObject.php   2012-01-08 
08:10:53 UTC (rev 108352)
@@ -31,6 +31,17 @@
        protected $fields = array( 'id' => null );
 
        /**
+        * 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 0.1
+        * @var bool
+        */
+       protected $updateSummaries = true;
+
+       /**
         * The database connection to use for read operations.
         *
         * @since 0.2
@@ -184,6 +195,23 @@
        }
 
        /**
+        * Gets the value of a field but first loads it if not done so already.
+        *
+        * @since 0.1
+        *
+        * @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 0.1
@@ -1102,4 +1130,15 @@
                self::setReadDb( DB_SLAVE );
        }
 
+       /**
+        * Sets the value for the @see $updateSummaries field.
+        *
+        * @since 0.1
+        *
+        * @param boolean $update
+        */
+       public function setUpdateSummaries( $update ) {
+               $this->updateSummaries = $update;
+       }
+
 }

Modified: trunk/extensions/EducationProgram/includes/EPOrg.php
===================================================================
--- trunk/extensions/EducationProgram/includes/EPOrg.php        2012-01-08 
06:59:41 UTC (rev 108351)
+++ trunk/extensions/EducationProgram/includes/EPOrg.php        2012-01-08 
08:10:53 UTC (rev 108352)
@@ -105,13 +105,18 @@
                if ( in_array( 'students', $summaryFields ) ) {
                        $termIds = EPTerm::selectFields( 'id', array( 'org_id' 
=> $this->getId() ) );
 
-                       $fields['students'] = $dbr->select(
-                               'ep_students_per_term',
-                               'COUNT(*) AS rowcount',
-                               array( 'spt_term_id' => $termIds )
-                       );
+                       if ( count( $termIds ) > 0 ) {
+                               $fields['students'] = $dbr->select(
+                                       'ep_students_per_term',
+                                       'COUNT(*) AS rowcount',
+                                       array( 'spt_term_id' => $termIds )
+                               );
 
-                       $fields['students'] = $fields['students']->rowcount;
+                               $fields['students'] = 
$fields['students']->fetchObject()->rowcount;
+                       }
+                       else {
+                               $fields['students'] = 0;
+                       }
                }
 
                $this->setFields( $fields );

Modified: trunk/extensions/EducationProgram/includes/EPTerm.php
===================================================================
--- trunk/extensions/EducationProgram/includes/EPTerm.php       2012-01-08 
06:59:41 UTC (rev 108351)
+++ trunk/extensions/EducationProgram/includes/EPTerm.php       2012-01-08 
08:10:53 UTC (rev 108352)
@@ -64,9 +64,30 @@
                        'token' => '',
                );
        }
-       
+
        /**
         * (non-PHPdoc)
+        * @see EPDBObject::loadSummaryFields()
+        */
+       public function loadSummaryFields( $summaryFields = null ) {
+               if ( is_null( $summaryFields ) ) {
+                       $summaryFields = array( 'org_id' );
+               }
+               else {
+                       $summaryFields = (array)$summaryFields;
+               }
+
+               $fields = array();
+
+               if ( in_array( 'org_id', $summaryFields ) ) {
+                       $fields['org_id'] = $this->getCourse( 'org_id' 
)->getField( 'org_id' );
+               }
+
+               $this->setFields( $fields );
+       }
+
+       /**
+        * (non-PHPdoc)
         * @see EPDBObject::insertIntoDB()
         */
        protected function insertIntoDB() {
@@ -74,7 +95,9 @@
                        $this->setField( 'org_id', $this->getCourse( 'org_id' 
)->getField( 'org_id' ) );
                }
 
-               EPOrg::updateSummaryFields( 'terms', array( 'id' => 
$this->getField( 'org_id' ) ) );
+               if ( $this->updateSummaries ) {
+                       EPOrg::updateSummaryFields( 'terms', array( 'id' => 
$this->getField( 'org_id' ) ) );
+               }
 
                return parent::insertIntoDB();
        }
@@ -85,16 +108,19 @@
         */
        public function removeFromDB() {
                $id = $this->getId();
-               $this->loadFields( array( 'org_id' ) );
-               $orgId = $this->getField( 'org_id', false );
-               
+
+               if ( $this->updateSummaries ) {
+                       $this->loadFields( array( 'org_id' ) );
+                       $orgId = $this->getField( 'org_id', false );
+               }
+
                $success = parent::removeFromDB();
                
                if ( $success ) {
                        $success = wfGetDB( DB_MASTER )->delete( 
'ep_students_per_term', array( 'spt_term_id' => $id ) ) && $success;
                }
 
-               if ( $orgId !== false ) {
+               if ( $this->updateSummaries && $orgId !== false ) {
                        EPOrg::updateSummaryFields( array( 'terms', 'students' 
), array( 'id' => $orgId ) );
                }
 
@@ -106,11 +132,21 @@
         * @see EPDBObject::updateInDB()
         */
        protected function updateInDB() {
-               $oldOrgId = $this->hasField( 'org_id' ) ? 
self::selectFieldsRow( 'org_id', array( 'id' => $this->getId() ) ) : false;
+               if ( $this->updateSummaries ) {
+                       $oldOrgId = $this->hasField( 'org_id' ) ? 
self::selectFieldsRow( 'org_id', array( 'id' => $this->getId() ) ) : false;
+               }
 
+               if ( $this->hasField( 'course_id' ) ) {
+                       $oldCourseId = self::selectFieldsRow( 'course_id', 
array( 'id' => $this->getId() ) );
+
+                       if ( $this->getField( 'course_id' ) !== $oldCourseId ) {
+                               $this->setField( 'org_id', 
EPCourse::selectFieldsRow( 'org_id', array( 'id' => $this->getField( 
'course_id' ) ) ) );
+                       }
+               }
+
                $success = parent::updateInDB();
 
-               if ( $success && $oldOrgId !== false && $oldOrgId !== 
$this->getField( 'org_id' ) ) {
+               if ( $this->updateSummaries && $success && $oldOrgId !== false 
&& $oldOrgId !== $this->getField( 'org_id' ) ) {
                        $conds = array( 'id' => array( $oldOrgId, 
$this->getField( 'org_id' ) ) );
                        EPOrg::updateSummaryFields( array( 'terms', 'students' 
), $conds );
                }
@@ -129,7 +165,7 @@
         */
        public function getCourse( $fields = null ) {
                if ( $this->course === false ) {
-                       $this->course = EPCourse::selectRow( $fields, array( 
'id' => $this->getField( 'course_id' ) ) );
+                       $this->course = EPCourse::selectRow( $fields, array( 
'id' => $this->loadAndGetField( 'course_id' ) ) );
                }
                
                return $this->course;
@@ -146,7 +182,7 @@
         */
        public function getOrg( $fields = null ) {
                if ( $this->org === false ) {
-                       $this->org = EPOrg::selectRow( $fields, array( 'id' => 
$this->getField( 'org_id' ) ) );
+                       $this->org = EPOrg::selectRow( $fields, array( 'id' => 
$this->loadAndGetField( 'org_id' ) ) );
                }
                
                return $this->org;


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

Reply via email to