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

Revision: 88642
Author:   jeroendedauw
Date:     2011-05-23 14:49:40 +0000 (Mon, 23 May 2011)
Log Message:
-----------
some schedma changes and work on proper clean up when deleting a watchlist group

Modified Paths:
--------------
    trunk/extensions/SemanticWatchlist/SemanticWatchlist.hooks.php
    trunk/extensions/SemanticWatchlist/SemanticWatchlist.sql
    trunk/extensions/SemanticWatchlist/api/ApiDeleteWatchlistGroup.php

Modified: trunk/extensions/SemanticWatchlist/SemanticWatchlist.hooks.php
===================================================================
--- trunk/extensions/SemanticWatchlist/SemanticWatchlist.hooks.php      
2011-05-23 14:29:25 UTC (rev 88641)
+++ trunk/extensions/SemanticWatchlist/SemanticWatchlist.hooks.php      
2011-05-23 14:49:40 UTC (rev 88642)
@@ -25,8 +25,8 @@
      * @return true
      */
        public static function onDataUpdate( SMWStore $store, SMWSemanticData 
$newData ) {
-               $changes = SWLChangeSet::newFromSemanticData( 
$store->getSemanticData( $newData->getSubject() ), $newData );
-               $groups = SWLGroups::getMatchingWatchGroups( 
$changes->getTitle() );
+               $changeSet = SWLChangeSet::newFromSemanticData( 
$store->getSemanticData( $newData->getSubject() ), $newData );
+               $groups = SWLGroups::getMatchingWatchGroups( 
$changeSet->getTitle() );
                
                $wasInserted = $changes->writeToStore( $groups ) != 0;
                

Modified: trunk/extensions/SemanticWatchlist/SemanticWatchlist.sql
===================================================================
--- trunk/extensions/SemanticWatchlist/SemanticWatchlist.sql    2011-05-23 
14:29:25 UTC (rev 88641)
+++ trunk/extensions/SemanticWatchlist/SemanticWatchlist.sql    2011-05-23 
14:49:40 UTC (rev 88642)
@@ -30,19 +30,19 @@
   change_new_value         BLOB                NULL -- The new value of the 
property (null for a deletion)
 ) /*$wgDBTableOptions*/;
 
--- Sets of changes, as in the set you get when editing a page.
-CREATE TABLE IF NOT EXISTS /*$wgDBprefix*/swl_sets (
-  set_id                   SMALLINT unsigned   NOT NULL auto_increment PRIMARY 
KEY,
-  set_user_name            VARCHAR(255)        NOT NULL, -- The person that 
made the modification (account name or ip)
-  set_page_id              INT(10) unsigned    NOT NULL, -- The id of the page 
the modification was on  
-  set_time                 CHAR(14) binary     NOT NULL default '' -- The time 
the chages where made  
+-- Individual edits to pages.
+CREATE TABLE IF NOT EXISTS /*$wgDBprefix*/swl_edits (
+  edit_id                  SMALLINT unsigned   NOT NULL auto_increment PRIMARY 
KEY,
+  edit_user_name           VARCHAR(255)        NOT NULL, -- The person that 
made the modification (account name or ip)
+  edit_page_id             INT(10) unsigned    NOT NULL, -- The id of the page 
the modification was on  
+  edit_time                CHAR(14) binary     NOT NULL default '' -- The time 
the chages where made  
 ) /*$wgDBTableOptions*/;
 
--- Links edits to watchlist groups.
-CREATE TABLE IF NOT EXISTS /*$wgDBprefix*/swl_edits_per_group (
-  epg_group_id             SMALLINT unsigned   NOT NULL, -- Foreign key: 
swl_groups.group_id
-  epg_edit_id              INT(10) unsigned    NOT NULL, -- Edit ID
-  PRIMARY KEY  (epg_group_id,epg_edit_id)
+-- Links change sets their edits.
+CREATE TABLE IF NOT EXISTS /*$wgDBprefix*/swl_sets_per_edit (
+  spe_set_id               SMALLINT unsigned   NOT NULL, -- Foreign key: 
swl_sets.set_id
+  spe_edit_id              INT(10) unsigned    NOT NULL, -- Edit ID
+  PRIMARY KEY  (spe_set_id,spe_edit_id)
 ) /*$wgDBTableOptions*/;
 
 -- Links change sets to watchlist groups.

Modified: trunk/extensions/SemanticWatchlist/api/ApiDeleteWatchlistGroup.php
===================================================================
--- trunk/extensions/SemanticWatchlist/api/ApiDeleteWatchlistGroup.php  
2011-05-23 14:29:25 UTC (rev 88641)
+++ trunk/extensions/SemanticWatchlist/api/ApiDeleteWatchlistGroup.php  
2011-05-23 14:49:40 UTC (rev 88642)
@@ -29,27 +29,117 @@
                
                $everythingOk = true;
                
+               foreach ( $params['ids'] as $id ) {
+                       $everythingOk = $this->deleteGroup( $id ) && 
$everythingOk;
+               }
+               
+               $this->getResult()->addValue(
+                       null,
+                       'success',
+                       $everythingOk
+               );
+       }
+       
+       /**
+        * Delete the group with specified ID, and
+        * all linked data not used by other groups.
+        * 
+        * @since 0.1
+        * 
+        * @param integer $groupId
+        * 
+        * @return boolean Sucess indicator
+        */
+       protected function deleteGroup( $groupId ) {
+               $everythingOk = true;
+               
+               $dbr = wfGetDB( DB_SLAVE );
+               
+               // Find all edits linked to this group.
+               $editsForGroup = $dbr->select(
+                       array( 'swl_sets_per_group', 'swl_sets_per_edit', 
'swl_edits' ),
+                       array( 'spe_edit_id' ),
+                       array(
+                               'spg_group_id' => $id,
+                       ),
+                       '',
+                       array(),
+                       array(
+                               'swl_sets_per_edit' => array( 'INNER JOIN', 
array( 'spe_set_id=spg_set_id' ) ),
+                       )
+               );
+               
+               $editsToDelete = array();
+               
+               // For each linked edit, find all linked groups, and save those 
with only one (this one).
+               foreach ( $editsForGroup as $edit ) {
+                       $groupsForEdit = $dbr->select(
+                               array( 'swl_sets_per_group', 
'swl_sets_per_edit', 'swl_groups' ),
+                               array( 'spg_group_id' ),
+                               array(
+                                       'spe_edit_id' => $edit->edit_id,
+                               ),
+                               '',
+                               array(),
+                               array(
+                                       'swl_sets_per_edit' => array( 'INNER 
JOIN', array( 'spe_set_id=spg_set_id' ) ),
+                               )
+                       );
+                       
+                       if ( $dbr->numRows( $groupsForEdit ) < 2 ) {
+                               $editsToDelete[] = $edit->edit_id;
+                       }
+               }
+               
                $dbw = wfGetDB( DB_MASTER );
                $dbw->begin();
                
-               foreach ( $params['ids'] as $id ) {
-                       $result = $dbw->delete(
-                               'swl_groups',
-                               array( 'group_id' => $id )
+               // Delete all edits and sets per edits only linked to this 
group.
+               foreach ( $editsToDelete as $editId ) {
+                       $dbw->delete(
+                               'swl_edits',
+                               array( 'edit_id' => $editId )
                        );
+                       
+                       $dbw->delete(
+                               'swl_sets_per_edit',
+                               array( 'spe_edit_id' => $editId )
+                       );
+               }
+               
+               // Delete sets per group links for this group. 
+               $result = $dbw->delete(
+                       'swl_sets_per_group',
+                       array( 'spg_group_id' => $id )
+               );
 
-                       if ( $result === false ) {
-                               $everythingOk = false;
-                       }
+               if ( $result === false ) {
+                       $everythingOk = false;
                }
                
-               $dbw->commit();
+               // Delete users per group links for this group.
+               $result = $dbw->delete(
+                       'swl_users_per_group',
+                       array( 'upg_group_id' => $id )
+               );
+
+               if ( $result === false ) {
+                       $everythingOk = false;
+               }
                
-               $this->getResult()->addValue(
-                       null,
-                       'success',
-                       $everythingOk
+               // Delete the actual group.
+               $result = $dbw->delete(
+                       'swl_groups',
+                       array( 'group_id' => $id )
                );
+
+               if ( $result === false ) {
+                       $everythingOk = false;
+               }
+               
+               $dbw->commit();
+
+               return $everythingOk; 
        }
 
        public function getAllowedParams() {


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

Reply via email to