r88832 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r88831‎ | r88832 | r88833 >
Date:20:39, 25 May 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
implemented set merging to not show duplicate changes on the watchlist
Modified paths:
  • /trunk/extensions/SemanticWatchlist/api/ApiQuerySemanticWatchlist.php (modified) (history)
  • /trunk/extensions/SemanticWatchlist/includes/SWL_ChangeSet.php (modified) (history)
  • /trunk/extensions/SemanticWatchlist/includes/SWL_PropertyChange.php (modified) (history)
  • /trunk/extensions/SemanticWatchlist/specials/SpecialSemanticWatchlist.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticWatchlist/specials/SpecialSemanticWatchlist.php
@@ -258,7 +258,8 @@
259259 'format' => 'json',
260260 'swuserid' => $GLOBALS['wgUser']->getId(),
261261 'swlimit' => $limit,
262 - 'swcontinue' => $continue
 262+ 'swcontinue' => $continue,
 263+ 'swmerge' => '1'
263264 );
264265
265266 $api = new ApiMain( new FauxRequest( $requestData, true ), true );
Index: trunk/extensions/SemanticWatchlist/includes/SWL_PropertyChange.php
@@ -96,5 +96,16 @@
9797 }
9898 }
9999
 100+ /**
 101+ * Returns a serialized version of the change, suitable to
 102+ * do equal comparisions but not to unserialize.
 103+ *
 104+ * @return string
 105+ */
 106+ public function getSerialization() {
 107+ return is_null( $this->oldValue ) ? '' : $this->oldValue->getSerialization() . '|' .
 108+ is_null( $this->newValue ) ? '' : $this->newValue->getSerialization();
 109+ }
 110+
100111 }
101112
\ No newline at end of file
Index: trunk/extensions/SemanticWatchlist/includes/SWL_ChangeSet.php
@@ -376,6 +376,8 @@
377377 /**
378378 * Adds a SWLPropertyChange to the set for the specified SMWDIProperty.
379379 *
 380+ * @since 0.1
 381+ *
380382 * @param SMWDIProperty $property
381383 * @param SWLPropertyChange $change
382384 */
@@ -385,15 +387,39 @@
386388 $this->changes->addPropertyObjectChange( $property, $change );
387389 break;
388390 case SWLPropertyChange::TYPE_INSERT:
389 - $this->insertions->addPropertyObjectValue( $property, $change->getNewValue() );
 391+ $this->addInsertion( $property, $change->getNewValue() );
390392 break;
391393 case SWLPropertyChange::TYPE_DELETE:
392 - $this->deletions->addPropertyObjectValue( $property, $change->getOldValue() );
 394+ $this->addDeletion( $property, $change->getOldValue() );
393395 break;
394396 }
395397 }
396398
397399 /**
 400+ * Adds a SMWDataItem representing an insertion to the set for the specified SMWDIProperty.
 401+ *
 402+ * @since 0.1
 403+ *
 404+ * @param SMWDIProperty $property
 405+ * @param SMWDataItem $dataItem
 406+ */
 407+ public function addInsertion( SMWDIProperty $property, SMWDataItem $dataItem ) {
 408+ $this->insertions->addPropertyObjectValue( $property, $dataItem );
 409+ }
 410+
 411+ /**
 412+ * Adds a SMWDataItem representing a deletion to the set for the specified SMWDIProperty.
 413+ *
 414+ * @since 0.1
 415+ *
 416+ * @param SMWDIProperty $property
 417+ * @param SMWDataItem $dataItem
 418+ */
 419+ public function addDeletion( SMWDIProperty $property, SMWDataItem $dataItem ) {
 420+ $this->deletions->addPropertyObjectValue( $property, $dataItem );
 421+ }
 422+
 423+ /**
398424 * Returns a list of all properties.
399425 *
400426 * @return array of SMWDIProperty
@@ -613,4 +639,104 @@
614640 return $this->edit;
615641 }
616642
 643+ /**
 644+ * Returns if a certain insertion is present in the set of changes.
 645+ *
 646+ * @since 0.1
 647+ *
 648+ * @param SMWDIProperty $property
 649+ * @param string $value
 650+ *
 651+ * @return boolean
 652+ */
 653+ public function hasInsertion( SMWDIProperty $property, $value ) {
 654+ $has = false;
 655+
 656+ foreach ( $this->insertions->getPropertyValues( $property ) as /* SMWDataItem */ $insertion ) {
 657+ if ( $insertion->getSerialization() == $value ) {
 658+ $has = true;
 659+ break;
 660+ }
 661+ }
 662+
 663+ return $has;
 664+ }
 665+
 666+ /**
 667+ * Returns if a certain insertion is present in the set of changes.
 668+ *
 669+ * @since 0.1
 670+ *
 671+ * @param SMWDIProperty $property
 672+ * @param string $value
 673+ *
 674+ * @return boolean
 675+ */
 676+ public function hasDeletion( SMWDIProperty $property, $value ) {
 677+ $has = false;
 678+
 679+ foreach ( $this->deletions->getPropertyValues( $property ) as /* SMWDataItem */ $deletion ) {
 680+ if ( $deletion->getSerialization() == $value ) {
 681+ $has = true;
 682+ break;
 683+ }
 684+ }
 685+
 686+ return $has;
 687+ }
 688+
 689+ /**
 690+ * Returns if a certain change is present in the set of changes.
 691+ *
 692+ * @since 0.1
 693+ *
 694+ * @param SMWDIProperty $property
 695+ * @param SWLPropertyChange $change
 696+ *
 697+ * @return boolean
 698+ */
 699+ public function hasChange( SMWDIProperty $property, SWLPropertyChange $change ) {
 700+ $has = false;
 701+
 702+ foreach ( $this->changes->getPropertyChanges( $property ) as /* SWLPropertyChange */ $propChange ) {
 703+ if ( $propChange->getSerialization() == $change->getSerialization() ) {
 704+ $has = true;
 705+ break;
 706+ }
 707+ }
 708+
 709+ return $has;
 710+ }
 711+
 712+ /**
 713+ * Merges in the changes of another change set.
 714+ * Duplicate changes are detected and only kept as a single change.
 715+ * This is usefull for merging sets with (possibly overlapping) changes belonging to a single edit.
 716+ *
 717+ * @since 0.1
 718+ *
 719+ * @param SWLChangeSet $set
 720+ */
 721+ public function mergeInChangeSet( SWLChangeSet $set ) {
 722+ foreach ( $set->getAllProperties() as $property ) {
 723+ foreach ( $set->getChanges()->getPropertyChanges( $property ) as /* SWLPropertyChange */ $change ) {
 724+ if ( !$this->hasChange( $property, $change ) ) {
 725+ $this->addChange( $property, $change );
 726+ }
 727+ }
 728+
 729+ foreach ( $set->getInsertions()->getPropertyValues( $property ) as /* SMWDataItem */ $dataItem ) {
 730+ if ( !$this->hasInsertion( $property, $dataItem ) ) {
 731+ $this->addInsertion( $property, $dataItem );
 732+ }
 733+ }
 734+
 735+ foreach ( $set->getDeletions()->getPropertyValues( $property ) as /* SMWDataItem */ $dataItem ) {
 736+ if ( !$this->hasInsertion( $property, $dataItem ) ) {
 737+ $this->addDeletion( $property, $dataItem );
 738+ }
 739+ }
 740+ }
 741+ }
 742+
617743 }
\ No newline at end of file
Index: trunk/extensions/SemanticWatchlist/api/ApiQuerySemanticWatchlist.php
@@ -52,9 +52,12 @@
5353 $this->mergeSets( $resultSets );
5454 }
5555
56 - $this->getResult()->setIndexedTagName( $resultSets, 'set' );
 56+ //$this->getResult()->setIndexedTagName( $resultSets, 'set' );
5757
5858 foreach ( $resultSets as &$set ) {
 59+ if ( !is_object( $set )) {
 60+ var_dump($set);exit;
 61+ }
5962 $set = $set->toArray();
6063
6164 foreach ( $set['changes'] as $propName => $changes ) {
@@ -135,8 +138,37 @@
136139 *
137140 * @param array $sets
138141 */
139 - protected function mergeSets( array &$sets ) {
140 - // TODO
 142+ protected function mergeSets( array &$sets ) {
 143+ if ( count( $sets ) > 1 ) {
 144+ $setsPerEdits = array();
 145+
 146+ // List the sets per edit.
 147+ foreach ( $sets as $set ) {
 148+ if ( !array_key_exists( $set->getEdit()->getId(), $setsPerEdits ) ) {
 149+ $setsPerEdits[$set->getEdit()->getId()] = array();
 150+ }
 151+
 152+ $setsPerEdits[$set->getEdit()->getId()][] = $set;
 153+ }
 154+
 155+ $mergedSets = array();
 156+
 157+ // For all edits with more then one set, merge all sets in the first one,
 158+ // and add it to the $mergedSets list.
 159+ foreach ( $setsPerEdits as $setsForEdit ) {
 160+ $setCount = count( $setsForEdit );
 161+
 162+ if ( $setCount > 1 ) {
 163+ for ( $i = 1; $i < $setCount; $i++ ) {
 164+ $setsForEdit[0]->mergeInChangeSet( $setsForEdit[$i] );
 165+ }
 166+ }
 167+
 168+ $mergedSets[] = $setsForEdit[0];
 169+ }
 170+
 171+ $sets = $mergedSets;
 172+ }
141173 }
142174
143175 /**
@@ -154,7 +186,7 @@
155187 ),
156188 'merge' => array(
157189 ApiBase::PARAM_TYPE => 'boolean',
158 - ApiBase::PARAM_TYPE => false,
 190+ ApiBase::PARAM_DFLT => false,
159191 ),
160192 'limit' => array(
161193 ApiBase :: PARAM_DFLT => 20,
@@ -165,6 +197,7 @@
166198 ),
167199 'continue' => null,
168200 );
 201+
169202 }
170203
171204 /**

Follow-up revisions

RevisionCommit summaryAuthorDate
r88833follow up to r88832, rem debug codejeroendedauw20:41, 25 May 2011

Status & tagging log