r108352 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r108351‎ | r108352 | r108353 >
Date:08:10, 8 January 2012
Author:jeroendedauw
Status:deferred
Tags:
Comment:
work on automatic updating of summary data
Modified paths:
  • /trunk/extensions/EducationProgram/includes/EPCourse.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPDBObject.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPOrg.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPTerm.php (modified) (history)

Diff [purge]

Index: trunk/extensions/EducationProgram/includes/EPOrg.php
@@ -105,13 +105,18 @@
106106 if ( in_array( 'students', $summaryFields ) ) {
107107 $termIds = EPTerm::selectFields( 'id', array( 'org_id' => $this->getId() ) );
108108
109 - $fields['students'] = $dbr->select(
110 - 'ep_students_per_term',
111 - 'COUNT(*) AS rowcount',
112 - array( 'spt_term_id' => $termIds )
113 - );
 109+ if ( count( $termIds ) > 0 ) {
 110+ $fields['students'] = $dbr->select(
 111+ 'ep_students_per_term',
 112+ 'COUNT(*) AS rowcount',
 113+ array( 'spt_term_id' => $termIds )
 114+ );
114115
115 - $fields['students'] = $fields['students']->rowcount;
 116+ $fields['students'] = $fields['students']->fetchObject()->rowcount;
 117+ }
 118+ else {
 119+ $fields['students'] = 0;
 120+ }
116121 }
117122
118123 $this->setFields( $fields );
Index: trunk/extensions/EducationProgram/includes/EPTerm.php
@@ -64,9 +64,30 @@
6565 'token' => '',
6666 );
6767 }
68 -
 68+
6969 /**
7070 * (non-PHPdoc)
 71+ * @see EPDBObject::loadSummaryFields()
 72+ */
 73+ public function loadSummaryFields( $summaryFields = null ) {
 74+ if ( is_null( $summaryFields ) ) {
 75+ $summaryFields = array( 'org_id' );
 76+ }
 77+ else {
 78+ $summaryFields = (array)$summaryFields;
 79+ }
 80+
 81+ $fields = array();
 82+
 83+ if ( in_array( 'org_id', $summaryFields ) ) {
 84+ $fields['org_id'] = $this->getCourse( 'org_id' )->getField( 'org_id' );
 85+ }
 86+
 87+ $this->setFields( $fields );
 88+ }
 89+
 90+ /**
 91+ * (non-PHPdoc)
7192 * @see EPDBObject::insertIntoDB()
7293 */
7394 protected function insertIntoDB() {
@@ -74,7 +95,9 @@
7596 $this->setField( 'org_id', $this->getCourse( 'org_id' )->getField( 'org_id' ) );
7697 }
7798
78 - EPOrg::updateSummaryFields( 'terms', array( 'id' => $this->getField( 'org_id' ) ) );
 99+ if ( $this->updateSummaries ) {
 100+ EPOrg::updateSummaryFields( 'terms', array( 'id' => $this->getField( 'org_id' ) ) );
 101+ }
79102
80103 return parent::insertIntoDB();
81104 }
@@ -85,16 +108,19 @@
86109 */
87110 public function removeFromDB() {
88111 $id = $this->getId();
89 - $this->loadFields( array( 'org_id' ) );
90 - $orgId = $this->getField( 'org_id', false );
91 -
 112+
 113+ if ( $this->updateSummaries ) {
 114+ $this->loadFields( array( 'org_id' ) );
 115+ $orgId = $this->getField( 'org_id', false );
 116+ }
 117+
92118 $success = parent::removeFromDB();
93119
94120 if ( $success ) {
95121 $success = wfGetDB( DB_MASTER )->delete( 'ep_students_per_term', array( 'spt_term_id' => $id ) ) && $success;
96122 }
97123
98 - if ( $orgId !== false ) {
 124+ if ( $this->updateSummaries && $orgId !== false ) {
99125 EPOrg::updateSummaryFields( array( 'terms', 'students' ), array( 'id' => $orgId ) );
100126 }
101127
@@ -106,11 +132,21 @@
107133 * @see EPDBObject::updateInDB()
108134 */
109135 protected function updateInDB() {
110 - $oldOrgId = $this->hasField( 'org_id' ) ? self::selectFieldsRow( 'org_id', array( 'id' => $this->getId() ) ) : false;
 136+ if ( $this->updateSummaries ) {
 137+ $oldOrgId = $this->hasField( 'org_id' ) ? self::selectFieldsRow( 'org_id', array( 'id' => $this->getId() ) ) : false;
 138+ }
111139
 140+ if ( $this->hasField( 'course_id' ) ) {
 141+ $oldCourseId = self::selectFieldsRow( 'course_id', array( 'id' => $this->getId() ) );
 142+
 143+ if ( $this->getField( 'course_id' ) !== $oldCourseId ) {
 144+ $this->setField( 'org_id', EPCourse::selectFieldsRow( 'org_id', array( 'id' => $this->getField( 'course_id' ) ) ) );
 145+ }
 146+ }
 147+
112148 $success = parent::updateInDB();
113149
114 - if ( $success && $oldOrgId !== false && $oldOrgId !== $this->getField( 'org_id' ) ) {
 150+ if ( $this->updateSummaries && $success && $oldOrgId !== false && $oldOrgId !== $this->getField( 'org_id' ) ) {
115151 $conds = array( 'id' => array( $oldOrgId, $this->getField( 'org_id' ) ) );
116152 EPOrg::updateSummaryFields( array( 'terms', 'students' ), $conds );
117153 }
@@ -129,7 +165,7 @@
130166 */
131167 public function getCourse( $fields = null ) {
132168 if ( $this->course === false ) {
133 - $this->course = EPCourse::selectRow( $fields, array( 'id' => $this->getField( 'course_id' ) ) );
 169+ $this->course = EPCourse::selectRow( $fields, array( 'id' => $this->loadAndGetField( 'course_id' ) ) );
134170 }
135171
136172 return $this->course;
@@ -146,7 +182,7 @@
147183 */
148184 public function getOrg( $fields = null ) {
149185 if ( $this->org === false ) {
150 - $this->org = EPOrg::selectRow( $fields, array( 'id' => $this->getField( 'org_id' ) ) );
 186+ $this->org = EPOrg::selectRow( $fields, array( 'id' => $this->loadAndGetField( 'org_id' ) ) );
151187 }
152188
153189 return $this->org;
Index: trunk/extensions/EducationProgram/includes/EPDBObject.php
@@ -31,6 +31,17 @@
3232 protected $fields = array( 'id' => null );
3333
3434 /**
 35+ * If the object should update summaries of linked items when changed.
 36+ * For example, update the course_count field in universities when a course in courses is deleted.
 37+ * Settings this to false can prevent needless updating work in situations
 38+ * such as deleting a university, which will then delete all it's courses.
 39+ *
 40+ * @since 0.1
 41+ * @var bool
 42+ */
 43+ protected $updateSummaries = true;
 44+
 45+ /**
3546 * The database connection to use for read operations.
3647 *
3748 * @since 0.2
@@ -184,6 +195,23 @@
185196 }
186197
187198 /**
 199+ * Gets the value of a field but first loads it if not done so already.
 200+ *
 201+ * @since 0.1
 202+ *
 203+ * @param string$name
 204+ *
 205+ * @return mixed
 206+ */
 207+ public function loadAndGetField( $name ) {
 208+ if ( !$this->hasField( $name ) ) {
 209+ $this->loadFields( array( $name ) );
 210+ }
 211+
 212+ return $this->getField( $name );
 213+ }
 214+
 215+ /**
188216 * Remove a field.
189217 *
190218 * @since 0.1
@@ -1102,4 +1130,15 @@
11031131 self::setReadDb( DB_SLAVE );
11041132 }
11051133
 1134+ /**
 1135+ * Sets the value for the @see $updateSummaries field.
 1136+ *
 1137+ * @since 0.1
 1138+ *
 1139+ * @param boolean $update
 1140+ */
 1141+ public function setUpdateSummaries( $update ) {
 1142+ $this->updateSummaries = $update;
 1143+ }
 1144+
11061145 }
Index: trunk/extensions/EducationProgram/includes/EPCourse.php
@@ -51,6 +51,40 @@
5252 'description' => '',
5353 );
5454 }
 55+
 56+ /**
 57+ * (non-PHPdoc)
 58+ * @see EPDBObject::loadSummaryFields()
 59+ */
 60+ public function loadSummaryFields( $summaryFields = null ) {
 61+ if ( is_null( $summaryFields ) ) {
 62+ $summaryFields = array( 'students' );
 63+ }
 64+ else {
 65+ $summaryFields = (array)$summaryFields;
 66+ }
 67+
 68+ $fields = array();
 69+
 70+ if ( in_array( 'students', $summaryFields ) ) {
 71+ $termIds = EPTerm::selectFields( 'id', array( 'course_id' => $this->getId() ) );
 72+
 73+ if ( count( $termIds ) > 0 ) {
 74+ $fields['students'] = wfGetDB( DB_SLAVE )->select(
 75+ 'ep_students_per_term',
 76+ 'COUNT(*) AS rowcount',
 77+ array( 'spt_term_id' => $termIds )
 78+ );
 79+
 80+ $fields['students'] = $fields['students']->fetchObject()->rowcount;
 81+ }
 82+ else {
 83+ $fields['students'] = 0;
 84+ }
 85+ }
 86+
 87+ $this->setFields( $fields );
 88+ }
5589
5690 /**
5791 * (non-PHPdoc)
@@ -58,19 +92,61 @@
5993 */
6094 public function removeFromDB() {
6195 $id = $this->getId();
62 -
 96+
 97+ if ( $this->updateSummaries ) {
 98+ $this->loadFields( array( 'org_id' ) );
 99+ $orgId = $this->getField( 'org_id', false );
 100+ }
 101+
63102 $success = parent::removeFromDB();
64103
65104 if ( $success ) {
66105 foreach ( EPTerm::select( 'id', array( 'course_id' => $id ) ) as /* EPTerm */ $term ) {
 106+ $term->setUpdateSummaries( false );
67107 $success = $term->removeFromDB() && $success;
68108 }
69109 }
 110+
 111+ if ( $this->updateSummaries && $orgId !== false ) {
 112+ EPOrg::updateSummaryFields( array( 'terms', 'students', 'courses' ), array( 'id' => $orgId ) );
 113+ }
70114
71115 return $success;
72116 }
73 -
 117+
74118 /**
 119+ * (non-PHPdoc)
 120+ * @see EPDBObject::insertIntoDB()
 121+ */
 122+ protected function insertIntoDB() {
 123+ $success = parent::insertIntoDB();
 124+
 125+ if ( $this->updateSummaries ) {
 126+ EPOrg::updateSummaryFields( 'courses', array( 'id' => $this->getField( 'org_id' ) ) );
 127+ }
 128+
 129+ return $success;
 130+ }
 131+
 132+ /**
 133+ * (non-PHPdoc)
 134+ * @see EPDBObject::updateInDB()
 135+ */
 136+ protected function updateInDB() {
 137+ $oldOrgId = $this->hasField( 'org_id' ) ? self::selectFieldsRow( 'org_id', array( 'id' => $this->getId() ) ) : false;
 138+
 139+ $success = parent::updateInDB();
 140+
 141+ if ( $this->updateSummaries && $success && $oldOrgId !== false && $oldOrgId !== $this->getField( 'org_id' ) ) {
 142+ $conds = array( 'id' => array( $oldOrgId, $this->getField( 'org_id' ) ) );
 143+ EPOrg::updateSummaryFields( array( 'terms', 'students', 'courses' ), $conds );
 144+ EPTerm::updateSummaryFields( 'org_id', array( 'course_id' => $this->getId() ) );
 145+ }
 146+
 147+ return $success;
 148+ }
 149+
 150+ /**
75151 * Returns the org associated with this course.
76152 *
77153 * @since 0.1

Status & tagging log