http://www.mediawiki.org/wiki/Special:Code/MediaWiki/88832

Revision: 88832
Author:   jeroendedauw
Date:     2011-05-25 20:39:54 +0000 (Wed, 25 May 2011)
Log Message:
-----------
implemented set merging to not show duplicate changes on the watchlist

Modified Paths:
--------------
    trunk/extensions/SemanticWatchlist/api/ApiQuerySemanticWatchlist.php
    trunk/extensions/SemanticWatchlist/includes/SWL_ChangeSet.php
    trunk/extensions/SemanticWatchlist/includes/SWL_PropertyChange.php
    trunk/extensions/SemanticWatchlist/specials/SpecialSemanticWatchlist.php

Modified: trunk/extensions/SemanticWatchlist/api/ApiQuerySemanticWatchlist.php
===================================================================
--- trunk/extensions/SemanticWatchlist/api/ApiQuerySemanticWatchlist.php        
2011-05-25 20:35:59 UTC (rev 88831)
+++ trunk/extensions/SemanticWatchlist/api/ApiQuerySemanticWatchlist.php        
2011-05-25 20:39:54 UTC (rev 88832)
@@ -52,9 +52,12 @@
                        $this->mergeSets( $resultSets );
                }
                
-               $this->getResult()->setIndexedTagName( $resultSets, 'set' );
+               //$this->getResult()->setIndexedTagName( $resultSets, 'set' );
                
                foreach ( $resultSets as &$set ) {
+                       if ( !is_object( $set  )) {
+                               var_dump($set);exit;
+                       }
                        $set = $set->toArray();
                        
                        foreach ( $set['changes'] as $propName => $changes ) {
@@ -135,8 +138,37 @@
         * 
         * @param array $sets
         */
-       protected function mergeSets( array &$sets ) {
-               // TODO
+       protected function mergeSets( array &$sets ) {          
+               if ( count( $sets ) > 1 ) {
+                       $setsPerEdits = array();
+                       
+                       // List the sets per edit.
+                       foreach ( $sets as $set ) {
+                               if ( !array_key_exists( 
$set->getEdit()->getId(), $setsPerEdits ) ) {
+                                       $setsPerEdits[$set->getEdit()->getId()] 
= array();
+                               }
+                               
+                               $setsPerEdits[$set->getEdit()->getId()][] = 
$set;
+                       }
+                       
+                       $mergedSets = array();
+                       
+                       // For all edits with more then one set, merge all sets 
in the first one, 
+                       // and add it to the $mergedSets list.
+                       foreach ( $setsPerEdits as $setsForEdit ) {
+                               $setCount = count( $setsForEdit );
+                               
+                               if ( $setCount > 1 ) {
+                                       for ( $i = 1; $i < $setCount; $i++ ) {
+                                               
$setsForEdit[0]->mergeInChangeSet( $setsForEdit[$i] );
+                                       }       
+                               }
+                               
+                               $mergedSets[] = $setsForEdit[0];
+                       }
+                       
+                       $sets = $mergedSets;
+               }
        }
        
        /**
@@ -154,7 +186,7 @@
                        ),
                        'merge' => array(
                                ApiBase::PARAM_TYPE => 'boolean',
-                               ApiBase::PARAM_TYPE => false,
+                               ApiBase::PARAM_DFLT => false,
                        ),
                        'limit' => array(
                                ApiBase :: PARAM_DFLT => 20,
@@ -165,6 +197,7 @@
                        ),
                        'continue' => null,
                );
+               
        }
 
        /**

Modified: trunk/extensions/SemanticWatchlist/includes/SWL_ChangeSet.php
===================================================================
--- trunk/extensions/SemanticWatchlist/includes/SWL_ChangeSet.php       
2011-05-25 20:35:59 UTC (rev 88831)
+++ trunk/extensions/SemanticWatchlist/includes/SWL_ChangeSet.php       
2011-05-25 20:39:54 UTC (rev 88832)
@@ -376,6 +376,8 @@
        /**
         * Adds a SWLPropertyChange to the set for the specified SMWDIProperty.
         * 
+        * @since 0.1
+        * 
         * @param SMWDIProperty $property
         * @param SWLPropertyChange $change
         */
@@ -385,15 +387,39 @@
                                $this->changes->addPropertyObjectChange( 
$property, $change );
                                break;
                        case SWLPropertyChange::TYPE_INSERT:
-                               $this->insertions->addPropertyObjectValue( 
$property, $change->getNewValue() );
+                               $this->addInsertion( $property, 
$change->getNewValue() );
                                break;
                        case SWLPropertyChange::TYPE_DELETE:
-                               $this->deletions->addPropertyObjectValue( 
$property, $change->getOldValue() );
+                               $this->addDeletion(  $property, 
$change->getOldValue()  );
                                break;
                }
        }
        
        /**
+        * Adds a SMWDataItem representing an insertion to the set for the 
specified SMWDIProperty.
+        * 
+        * @since 0.1
+        * 
+        * @param SMWDIProperty $property
+        * @param SMWDataItem $dataItem
+        */
+       public function addInsertion( SMWDIProperty $property, SMWDataItem 
$dataItem ) {
+               $this->insertions->addPropertyObjectValue( $property, $dataItem 
);
+       }
+       
+       /**
+        * Adds a SMWDataItem representing a deletion to the set for the 
specified SMWDIProperty.
+        * 
+        * @since 0.1
+        * 
+        * @param SMWDIProperty $property
+        * @param SMWDataItem $dataItem
+        */
+       public function addDeletion( SMWDIProperty $property, SMWDataItem 
$dataItem ) {
+               $this->deletions->addPropertyObjectValue( $property, $dataItem 
);
+       }
+       
+       /**
         * Returns a list of all properties.
         * 
         * @return array of SMWDIProperty
@@ -613,4 +639,104 @@
                return $this->edit;
        }
        
+       /**
+        * Returns if a certain insertion is present in the set of changes.
+        * 
+        * @since 0.1
+        * 
+        * @param SMWDIProperty $property
+        * @param string $value
+        * 
+        * @return boolean
+        */
+       public function hasInsertion( SMWDIProperty $property, $value ) {
+               $has = false;
+               
+               foreach ( $this->insertions->getPropertyValues( $property ) as 
/* SMWDataItem */ $insertion ) {
+                       if ( $insertion->getSerialization() == $value ) {
+                               $has = true;
+                               break;
+                       }
+               }
+               
+               return $has;
+       }
+       
+       /**
+        * Returns if a certain insertion is present in the set of changes.
+        * 
+        * @since 0.1
+        * 
+        * @param SMWDIProperty $property
+        * @param string $value
+        * 
+        * @return boolean
+        */
+       public function hasDeletion( SMWDIProperty $property, $value ) {
+               $has = false;
+               
+               foreach ( $this->deletions->getPropertyValues( $property ) as 
/* SMWDataItem */ $deletion ) {
+                       if ( $deletion->getSerialization() == $value ) {
+                               $has = true;
+                               break;
+                       }
+               }
+               
+               return $has;            
+       }
+       
+       /**
+        * Returns if a certain change is present in the set of changes.
+        * 
+        * @since 0.1
+        * 
+        * @param SMWDIProperty $property
+        * @param SWLPropertyChange $change
+        * 
+        * @return boolean
+        */
+       public function hasChange( SMWDIProperty $property, SWLPropertyChange 
$change ) {
+               $has = false;
+               
+               foreach ( $this->changes->getPropertyChanges( $property ) as /* 
SWLPropertyChange */ $propChange ) {
+                       if ( $propChange->getSerialization() == 
$change->getSerialization() ) {
+                               $has = true;
+                               break;
+                       }
+               }
+               
+               return $has;                    
+       }
+       
+       /**
+        * Merges in the changes of another change set.
+        * Duplicate changes are detected and only kept as a single change.
+        * This is usefull for merging sets with (possibly overlapping) changes 
belonging to a single edit.
+        * 
+        * @since 0.1
+        * 
+        * @param SWLChangeSet $set
+        */
+       public function mergeInChangeSet( SWLChangeSet $set ) {
+               foreach ( $set->getAllProperties() as $property ) {
+                       foreach ( $set->getChanges()->getPropertyChanges( 
$property ) as /* SWLPropertyChange */ $change ) {
+                               if ( !$this->hasChange( $property, $change ) ) {
+                                       $this->addChange( $property, $change );
+                               }
+                       }
+                       
+                       foreach ( $set->getInsertions()->getPropertyValues( 
$property ) as /* SMWDataItem */ $dataItem ) {
+                               if ( !$this->hasInsertion( $property, $dataItem 
) ) {
+                                       $this->addInsertion( $property, 
$dataItem );
+                               }
+                       }
+       
+                       foreach ( $set->getDeletions()->getPropertyValues( 
$property ) as /* SMWDataItem */ $dataItem ) {
+                               if ( !$this->hasInsertion( $property, $dataItem 
) ) {
+                                       $this->addDeletion( $property, 
$dataItem );
+                               }
+                       }               
+               }
+       }
+       
 }
\ No newline at end of file

Modified: trunk/extensions/SemanticWatchlist/includes/SWL_PropertyChange.php
===================================================================
--- trunk/extensions/SemanticWatchlist/includes/SWL_PropertyChange.php  
2011-05-25 20:35:59 UTC (rev 88831)
+++ trunk/extensions/SemanticWatchlist/includes/SWL_PropertyChange.php  
2011-05-25 20:39:54 UTC (rev 88832)
@@ -96,5 +96,16 @@
                }
        }
        
+       /**
+        * Returns a serialized version of the change, suitable to
+        * do equal comparisions but not to unserialize.
+        * 
+        * @return string
+        */
+       public function getSerialization() {
+               return is_null( $this->oldValue ) ? '' : 
$this->oldValue->getSerialization() . '|' .
+                       is_null( $this->newValue ) ? '' : 
$this->newValue->getSerialization();
+       }
+       
 }
        
\ No newline at end of file

Modified: 
trunk/extensions/SemanticWatchlist/specials/SpecialSemanticWatchlist.php
===================================================================
--- trunk/extensions/SemanticWatchlist/specials/SpecialSemanticWatchlist.php    
2011-05-25 20:35:59 UTC (rev 88831)
+++ trunk/extensions/SemanticWatchlist/specials/SpecialSemanticWatchlist.php    
2011-05-25 20:39:54 UTC (rev 88832)
@@ -258,7 +258,8 @@
                        'format' => 'json',
                        'swuserid' => $GLOBALS['wgUser']->getId(),
                        'swlimit' => $limit,
-                       'swcontinue' => $continue
+                       'swcontinue' => $continue,
+                       'swmerge' => '1'
                );
                
                $api = new ApiMain( new FauxRequest( $requestData, true ), true 
);


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

Reply via email to