r111056 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r111055‎ | r111056 | r111057 >
Date:18:34, 9 February 2012
Author:jeroendedauw
Status:deferred
Tags:
Comment:
work on deletion, revisioning and logging
Modified paths:
  • /trunk/extensions/EducationProgram/EducationProgram.i18n.php (modified) (history)
  • /trunk/extensions/EducationProgram/actions/EditCourseAction.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPCourse.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPCoursePager.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPDBObject.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPOrg.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPPageObject.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPPager.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPRevisionedObject.php (modified) (history)

Diff [purge]

Index: trunk/extensions/EducationProgram/actions/EditCourseAction.php
@@ -48,15 +48,21 @@
4949 $c = $this->getItemClass(); // Yeah, this is needed in PHP 5.3 >_>
5050
5151 if ( !$this->isNewPost() && !$c::hasIdentifier( $this->getTitle()->getText() ) ) {
 52+ $c::displayDeletionLog(
 53+ $this->getContext(),
 54+ 'ep-' . strtolower( $this->getName() ) . '-deleted'
 55+ );
 56+
5257 $name = $this->getTitle()->getText();
53 - $bracketPos = strpos( $name, '(' );
 58+ $term = '';
5459
55 - if ( $bracketPos !== false ) {
56 - if ( $bracketPos > 0 && in_array( $name{$bracketPos - 1}, array( ' ', '_' ) ) ) {
57 - $bracketPos -= 1;
58 - }
59 -
60 - $name = substr( $name, 0, $bracketPos );
 60+ $matches = array();
 61+
 62+ preg_match( '/(.*)(\(.*\))/', $name, $matches );
 63+
 64+ if ( count( $matches ) == 3 && trim( $matches[1] ) !== '' && $matches[2] !== '' ) {
 65+ $name = trim( $matches[1] );
 66+ $term = trim( $matches[2] );
6167 }
6268
6369 EPCourse::displayAddNewRegion(
@@ -66,12 +72,16 @@
6773 'newname',
6874 $name
6975 ),
70 - 'term' => $this->getRequest()->getText( 'newterm', '' ),
 76+ 'term' => $this->getRequest()->getText(
 77+ 'newterm',
 78+ $term
 79+ ),
7180 )
7281 );
7382
7483 $this->isNew = true;
7584 $this->getOutput()->setSubtitle( $this->getDescription() );
 85+ $this->getOutput()->setPageTitle( $this->getPageTitle() );
7686
7787 return '';
7888 }
Index: trunk/extensions/EducationProgram/includes/EPDBObject.php
@@ -201,7 +201,7 @@
202202 *
203203 * @return Success indicator
204204 */
205 - public function loadFields( $fields = null, $override = true ) {
 205+ public function loadFields( $fields = null, $override = true, $skipLoaded = false ) {
206206 if ( is_null( $this->getId() ) ) {
207207 return false;
208208 }
@@ -209,19 +209,30 @@
210210 if ( is_null( $fields ) ) {
211211 $fields = array_keys( $this->getFieldTypes() );
212212 }
213 -
214 - $results = $this->rawSelect(
215 - $this->getPrefixedFields( $fields ),
216 - array( $this->getPrefixedField( 'id' ) => $this->getId() ),
217 - array( 'LIMIT' => 1 )
218 - );
219 -
220 - foreach ( $results as $result ) {
221 - $this->setFields( $this->getFieldsFromDBResult( $result ), $override );
222 - return true;
 213+
 214+ if ( $skipLoaded ) {
 215+ $loadedFields = array_keys( $this->fields );
 216+ $fields = array_filter( $fields, function( $field ) use ( $loadedFields ) {
 217+ return !in_array( $loadedFields );
 218+ } );
223219 }
 220+
 221+ if ( count( $fields ) > 0 ) {
 222+ $results = $this->rawSelect(
 223+ $this->getPrefixedFields( $fields ),
 224+ array( $this->getPrefixedField( 'id' ) => $this->getId() ),
 225+ array( 'LIMIT' => 1 )
 226+ );
 227+
 228+ foreach ( $results as $result ) {
 229+ $this->setFields( $this->getFieldsFromDBResult( $result ), $override );
 230+ return true;
 231+ }
 232+
 233+ return false;
 234+ }
224235
225 - return false;
 236+ return true;
226237 }
227238
228239 /**
@@ -477,16 +488,50 @@
478489 * @return boolean Success indicator
479490 */
480491 public function remove() {
481 - $success = $this->delete( array( 'id' => $this->getId() ) );
 492+ $this->beforeRemove();
 493+
 494+ $success = static::delete( array( 'id' => $this->getId() ) );
482495
483496 if ( $success ) {
484 - $this->setField( 'id', null );
 497+ $this->onRemoved();
485498 }
486499
487500 return $success;
488501 }
 502+
 503+ /**
 504+ * Gets called before an object is removed from the database.
 505+ *
 506+ * @since 0.1
 507+ */
 508+ protected function beforeRemove() {
 509+ $this->loadFields( $this->getBeforeRemoveFields(), false, true );
 510+ }
489511
490512 /**
 513+ * Before removal of an object happens, @see beforeRemove gets called.
 514+ * This method loads the fields of which the names have been returned by this one (or all fields if null is returned).
 515+ * This allows for loading info needed after removal to get rid of linked data and the like.
 516+ *
 517+ * @since 0.1
 518+ *
 519+ * @return array|null
 520+ */
 521+ protected function getBeforeRemoveFields() {
 522+ return array();
 523+ }
 524+
 525+ /**
 526+ * Gets called after successfull removal.
 527+ * Can be overriden to get rid of linked data.
 528+ *
 529+ * @since 0.1
 530+ */
 531+ protected function onRemoved() {
 532+ $this->setField( 'id', null );
 533+ }
 534+
 535+ /**
491536 * Return the names and values of the fields.
492537 *
493538 * @since 0.1
@@ -1043,7 +1088,7 @@
10441089 *
10451090 * @since 0.1
10461091 *
1047 - * @param array|null $fields
 1092+ * @param array $fields
10481093 * @param array $conditions
10491094 * @param array $options
10501095 * @param array $joinConds
@@ -1051,7 +1096,7 @@
10521097 *
10531098 * @return ResultWrapper
10541099 */
1055 - public static function rawSelect( $fields = null, array $conditions = array(), array $options = array(), array $joinConds = array(), array $tables = null ) {
 1100+ public static function rawSelect( array $fields, array $conditions = array(), array $options = array(), array $joinConds = array(), array $tables = null ) {
10561101 if ( is_null( $tables ) ) {
10571102 $tables = static::getDBTable();
10581103 }
Index: trunk/extensions/EducationProgram/includes/EPCoursePager.php
@@ -75,7 +75,13 @@
7676 $value = EPCourse::getLinkFor( $value );
7777 break;
7878 case 'org_id':
79 - $value = EPOrg::selectRow( 'name', array( 'id' => $value ) )->getLink();
 79+ $org = EPOrg::selectRow( 'name', array( 'id' => $value ) );
 80+
 81+ // This should not happen. A course should always have an org.
 82+ // But if something gets messed up somehow, just display the ID rather then throwing a fatal.
 83+ if ( $org !== false ) {
 84+ $value = $org->getLink();
 85+ }
8086 break;
8187 case 'term':
8288 $value = htmlspecialchars( $value );
Index: trunk/extensions/EducationProgram/includes/EPOrg.php
@@ -154,23 +154,32 @@
155155
156156 /**
157157 * (non-PHPdoc)
158 - * @see EPDBObject::remove()
 158+ * @see EPRevisionedObject::onRemoved()
159159 */
160 - public function remove() {
161 - $id = $this->getId();
162 - $this->loadFields( array( 'name' ) );
163 -
164 - $success = parent::remove();
165 -
166 - if ( $success ) {
167 - $success = wfGetDB( DB_MASTER )->delete( 'ep_cas_per_org', array( 'cpo_org_id' => $id ) ) && $success;
168 -
169 - foreach ( EPCourse::select( 'id', array( 'org_id' => $id ) ) as /* EPCourse */ $course ) {
170 - $success = $course->remove() && $success;
 160+ protected function onRemoved() {
 161+ foreach ( EPCourse::select( null, array( 'org_id' => $this->getId() ) ) as /* EPCourse */ $course ) {
 162+ $revAction = clone $this->revAction;
 163+
 164+ if ( trim( $revAction->getComment() ) === '' ) {
 165+ $revAction->setComment( wfMsgExt(
 166+ 'ep-org-course-delete',
 167+ 'parsemag',
 168+ $this->getField( 'name' )
 169+ ) );
171170 }
 171+ else {
 172+ $revAction->setComment( wfMsgExt(
 173+ 'ep-org-course-delete-comment',
 174+ 'parsemag',
 175+ $this->getField( 'name' ),
 176+ $revAction->getComment()
 177+ ) );
 178+ }
 179+
 180+ $course->revisionedRemove( $revAction );
172181 }
173 -
174 - return $success;
 182+
 183+ parent::onRemoved();
175184 }
176185
177186 /**
Index: trunk/extensions/EducationProgram/includes/EPPager.php
@@ -68,7 +68,7 @@
6969 function formatRow( $row ) {
7070 $c = $this->className; // Yeah, this is needed in PHP 5.3 >_>
7171 $this->currentObject = $c::newFromDBResult( $row );
72 -
 72+
7373 $cells = array();
7474
7575 foreach ( $this->getFieldNames() as $field => $name ) {
Index: trunk/extensions/EducationProgram/includes/EPRevisionedObject.php
@@ -191,41 +191,15 @@
192192 }
193193
194194 /**
195 - * (non-PHPdoc)
196 - * @see EPDBObject::remove()
197 - */
198 - public function remove() {
199 - $object = clone $this;
200 - $object->loadFields();
201 -
202 - $success = parent::remove();
203 -
204 - if ( $success ) {
205 - $object->onRemoved();
206 - }
207 -
208 - return $success;
209 - }
210 -
211 - /**
212 - * @since 0.1
213 - *
214 - * @param EPRevisionAction $revAction
215 - */
216 - public function handleRemoved( EPRevisionAction $revAction ) {
217 - $this->setRevisionAction( $revAction );
218 - $this->onRemoved();
219 - }
220 -
221 - /**
222195 * Do logging and revision storage after a removal.
223 - * The object needs to have all it's fields loaded.
 196+ * @see EPDBObject::onRemoved()
224197 *
225198 * @since 0.1
226199 */
227200 protected function onRemoved() {
228201 $this->storeRevision( $this );
229202 $this->log( 'remove' );
 203+ parent::onRemoved( $object );
230204 }
231205
232206 public function getIdentifier() {
@@ -261,9 +235,17 @@
262236 */
263237 public function revisionedRemove( EPRevisionAction $revAction ) {
264238 $this->setRevisionAction( $revAction );
265 - $success = $this->remove();
 239+ $success = $this->remove();
266240 $this->setRevisionAction( false );
267241 return $success;
268242 }
269243
 244+ /**
 245+ * (non-PHPdoc)
 246+ * @see EPDBObject::getBeforeRemoveFields()
 247+ */
 248+ protected function getBeforeRemoveFields() {
 249+ return null;
 250+ }
 251+
270252 }
\ No newline at end of file
Index: trunk/extensions/EducationProgram/includes/EPCourse.php
@@ -250,32 +250,21 @@
251251
252252 return $success;
253253 }
254 -
 254+
255255 /**
256256 * (non-PHPdoc)
257 - * @see EPDBObject::remove()
 257+ * @see EPRevisionedObject::onRemoved()
258258 */
259 - public function remove() {
260 - $id = $this->getId();
261 -
 259+ protected function onRemoved() {
262260 if ( $this->updateSummaries ) {
263 - $this->loadFields( array( 'org_id' ) );
264 - $orgId = $this->getField( 'org_id' );
 261+ EPOrg::updateSummaryFields( array( 'courses', 'students', 'active', 'instructors', 'oas', 'cas' ), array( 'id' => $this->getField( 'org_id' ) ) );
265262 }
266263
267 - $success = parent::remove();
268 -
269 - if ( $success && $this->updateSummaries ) {
270 - EPOrg::updateSummaryFields( array( 'courses', 'students', 'active', 'instructors', 'oas', 'cas' ), array( 'id' => $orgId ) );
271 - }
272 -
273 - if ( $success ) {
274 - $success = wfGetDB( DB_MASTER )->delete( 'ep_students_per_course', array( 'spc_course_id' => $id ) ) && $success;
275 - $success = wfGetDB( DB_MASTER )->delete( 'ep_cas_per_course', array( 'cpc_course_id' => $id ) ) && $success;
276 - $success = wfGetDB( DB_MASTER )->delete( 'ep_oas_per_course', array( 'opc_course_id' => $id ) ) && $success;
277 - }
278 -
279 - return $success;
 264+ wfGetDB( DB_MASTER )->delete( 'ep_students_per_course', array( 'spc_course_id' => $this->getId() ) );
 265+ wfGetDB( DB_MASTER )->delete( 'ep_cas_per_course', array( 'cpc_course_id' => $this->getId() ) );
 266+ wfGetDB( DB_MASTER )->delete( 'ep_oas_per_course', array( 'opc_course_id' => $this->getId() ) );
 267+
 268+ parent::onRemoved();
280269 }
281270
282271 /**
Index: trunk/extensions/EducationProgram/includes/EPPageObject.php
@@ -103,8 +103,8 @@
104104 }
105105
106106 /**
 107+ * Delete all objects matching the provided condirions.
107108 *
108 - *
109109 * @since 0.1
110110 *
111111 * @param EPRevisionAction $revAction
@@ -121,14 +121,10 @@
122122 $success = true;
123123
124124 if ( count( $objects ) > 0 ) {
125 - $success = static::delete( $conditions );
 125+ $revAction->setDelete( true );
126126
127 - if ( $success ) {
128 - $revAction->setDelete( true );
129 -
130 - foreach ( $objects as /* EPPageObject */ $object ) {
131 - $object->handleRemoved( $revAction );
132 - }
 127+ foreach ( $objects as /* EPPageObject */ $object ) {
 128+ $success = $object->revisionedRemove( $revAction ) && $success;
133129 }
134130 }
135131
Index: trunk/extensions/EducationProgram/EducationProgram.i18n.php
@@ -23,6 +23,8 @@
2424 // Misc
2525 'ep-item-summary' => 'Summary',
2626 'ep-toplink' => 'My courses',
 27+ 'ep-org-course-delete-comment' => "Deleted institution $1 and all it's courses with comment $2",
 28+ 'ep-org-course-delete' => "Deleted institution $1 and all it's courses",
2729
2830 // Tabs
2931 'ep-tab-view' => 'Read',

Status & tagging log