Quellcode durchsuchen

#51 - delayed files deletions: comments

Alex Trofimov vor 12 Jahren
Ursprung
Commit
13458636ba

+ 63 - 5
inc/classes/BxDolCmts.php

@@ -360,6 +360,11 @@ class BxDolCmts extends BxDol implements iBxDolReplaceable
         return $this->_sSystem;
     }
 
+    public function getStorageObjectName()
+    {
+        return $this->_sStorageObject;
+    }
+
 	public function getSystemInfo()
     {
         return $this->_aSystem;
@@ -463,6 +468,11 @@ class BxDolCmts extends BxDol implements iBxDolReplaceable
     /** 
      * Database functions
      */
+	public function getQueryObject ()
+    {
+        return $this->_oQuery;
+    }
+
 	public function getCommentsTableName ()
     {
         return $this->_oQuery->getTableName ();
@@ -490,18 +500,66 @@ class BxDolCmts extends BxDol implements iBxDolReplaceable
 
     public function onObjectDelete ($iObjectId = 0)
     {
-        return $this->_oQuery->deleteObjectComments ($iObjectId ? $iObjectId : $this->getId());
+        // delete comments
+        $aFiles = array();
+        $this->_oQuery->deleteObjectComments ($iObjectId ? $iObjectId : $this->getId(), $aFiles);
+
+        // delete files
+        if ($aFiles) {
+            bx_import('BxDolStorage');
+            $oStorage = BxDolStorage::getObjectInstance($this->getStorageObjectName());
+            if ($oStorage)
+                $oStorage->queueFilesForDeletion($aFiles);
+        }
     }
 
-    public function onAuthorDelete ($iAuthorId)
+    public static function onAuthorDelete ($iAuthorId)
     {
-    	foreach($this->_aSystems as $sSystem => $aSystem) {
-            $oQuery = new BxDolCmtsQuery($aSystem);
-            $oQuery->deleteAuthorComments($iAuthorId);
+        bx_import('BxDolStorage');
+        $aSystems = self::getSystems();
+    	foreach($aSystems as $sSystem => $aSystem) {
+            $o = self::getObjectInstance($sSystem, 0);
+            $oQuery = $o->getQueryObject();            
+
+            // delete comments
+            $aFiles = array ();
+            $oQuery->deleteAuthorComments($iAuthorId, $aFiles);
+            
+            // delete files
+            $oStorage = BxDolStorage::getObjectInstance($o->getStorageObjectName());
+            if ($oStorage)
+                $oStorage->queueFilesForDeletion($aFiles);
         }
         return true;
     }
 
+    public static function onModuleUninstall ($sModuleName, &$iFiles = null)
+    {        
+        bx_import('BxDolStorage');
+        $aSystems = self::getSystems();
+    	foreach($aSystems as $sSystem => $aSystem) {
+            if (0 != strncasecmp($sModuleName, $sSystem, strlen($sModuleName)))
+                continue;
+
+            $o = self::getObjectInstance($sSystem);
+            $oQuery = $o->getQueryObject();
+
+            // delete comments            
+            $aFiles = array ();
+            $oQuery->deleteAll($aSystem['system_id'], $aFiles);
+
+            // delete files
+            $oStorage = BxDolStorage::getObjectInstance($o->getStorageObjectName());
+            if ($oStorage && $aFiles)
+                $oStorage->queueFilesForDeletion($aFiles);
+
+            if (null !== $iFiles)
+                $iFiles += count($aFiles);
+        }
+
+        return true;
+    }
+
     /** 
      * Permissions functions
      */

+ 69 - 6
inc/classes/BxDolCmtsQuery.php

@@ -182,15 +182,32 @@ class BxDolCmtsQuery extends BxDolDb
 		return (int)$this->query($sQuery) > 0;
 	}
 
-    function getImages($iSystemId, $iCmtId)
+    function getImages($iSystemId, $iCmtId, $iId = false)
     {
-    	$sQuery = $this->prepare("SELECT * FROM `{$this->_sTableImages2Entries}` WHERE `system_id`=? AND `cmt_id`=?", $iSystemId, $iCmtId);
-    	return $this->getAll($sQuery);
+        $sJoin = '';
+        $sWhere = $this->prepare(" AND `i`.`system_id` = ? ", $iSystemId);
+
+        if (false !== $iCmtId) {
+            $sWhere .= $this->prepare(" AND `i`.`cmt_id` = ? ", $iCmtId);
+        }
+
+        if (false !== $iId) {
+            $sWhere .= $this->prepare(" AND `c`.`cmt_object_id` = ?", $iId);
+            $sJoin .= " INNER JOIN `{$this->_sTable}` AS `c` ON (`i`.`cmt_id` = `c`.`cmt_id`) ";
+        }        
+
+    	return $this->getAll("SELECT * FROM `{$this->_sTableImages2Entries}` AS `i` " . $sJoin . " WHERE 1 " . $sWhere);
     }
 
 	function deleteImages($iSystemId, $iCmtId)
 	{
-		$sQuery = $this->prepare("DELETE FROM `{$this->_sTableImages2Entries}` WHERE `system_id`=? AND `cmt_id`=?", $iSystemId, $iCmtId);
+        $sWhereAddon = '';
+        if (false !== $iCmtId)
+            $sWhereAddon = $this->prepare(" AND `cmt_id` = ? ", $iCmtId);
+
+		$sQuery = $this->prepare("DELETE FROM `{$this->_sTableImages2Entries}` WHERE `system_id` = ?", $iSystemId);
+        $sQuery .= $sWhereAddon;
+
     	return $this->query($sQuery);
 	}
 
@@ -200,8 +217,10 @@ class BxDolCmtsQuery extends BxDolDb
 		return $this->query($sQuery);
     }
 
-    function deleteAuthorComments ($iAuthorId)
+    function deleteAuthorComments ($iAuthorId, &$aFiles = null)
     {
+    	$aSystem = $this->_oMain->getSystemInfo();
+
         $isDelOccured = 0;
         $sQuery = $this->prepare("SELECT `cmt_id`, `cmt_parent_id` FROM {$this->_sTable} WHERE `cmt_author_id` = ? AND `cmt_replies` = 0", $iAuthorId);
         $a = $this->getAll ($sQuery);
@@ -209,8 +228,15 @@ class BxDolCmtsQuery extends BxDolDb
         {
             $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_id` = ?", $r['cmt_id']);
             $this->query ($sQuery);
+
             $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_replies` = `cmt_replies` - 1 WHERE `cmt_id` = ?", $r['cmt_parent_id']);
             $this->query ($sQuery);
+
+            $aFilesMore = $this->convertImagesArray($this->getImages($aSystem['system_id'], $r['cmt_id']));
+            $this->deleteImages($aSystem['system_id'], $r['cmt_id']);
+            if ($aFilesMore && null !== $aFiles) 
+                $aFiles = array_merge($aFiles, $aFilesMore);
+
             $isDelOccured = 1;
         }
         $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_author_id` = 0 WHERE `cmt_author_id` = ? AND `cmt_replies` != 0", $iAuthorId);
@@ -219,11 +245,37 @@ class BxDolCmtsQuery extends BxDolDb
             $this->query ("OPTIMIZE TABLE {$this->_sTable}");
     }
 
-    function deleteObjectComments ($iObjectId)
+    function deleteObjectComments ($iObjectId, &$aFilesReturn = null)
     {
+        $aSystem = $this->_oMain->getSystemInfo();
+        $aFiles = $this->convertImagesArray($this->getImages($aSystem['system_id'], false, $iObjectId));
+
+        if ($aFiles) {
+            $sQuery = $this->prepare("DELETE FROM {$this->_sTableImages2Entries} WHERE `system_id` = ? ", $aSystem['system_id']);
+            $sQuery .= " AND `image_id` IN(" . $this->implode_escape($aFiles) . ") ";
+            $this->query ($sQuery);
+        }
+
         $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_object_id` = ?", $iObjectId);
         $this->query ($sQuery);
         $this->query ("OPTIMIZE TABLE {$this->_sTable}");
+
+        if (null !== $aFilesReturn)
+            $aFilesReturn = $aFiles;
+    }
+
+    function deleteAll ($iSystemId, &$aFiles = null)
+    {
+        // get files
+        if (null !== $aFiles)
+            $aFiles = $this->convertImagesArray($oQuery->getImages($iSystemId));
+
+        // delete files
+        $this->deleteImages($iSystemId);
+
+        // delete comments
+        $sQuery = $this->prepare("TRUNCATE TABLE {$this->_sTable}");
+        $this->query ($sQuery);
     }
 
 	function getObjectTitle($iId)
@@ -237,4 +289,15 @@ class BxDolCmtsQuery extends BxDolDb
         $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldComments}` = ? WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iCount, $iId);
         return $this->query($sQuery);
     }
+
+    protected function convertImagesArray($a) 
+    {
+        if (!$a || !is_array($a))
+            return array();
+
+        $aFiles = array ();
+        foreach ($a as $aFile)
+            $aFiles[] = $aFile['image_id'];
+        return $aFiles;
+    }
 }

+ 4 - 0
inc/classes/BxDolProfile.php

@@ -246,6 +246,10 @@ class BxDolProfile extends BxDol implements iBxDolProfile {
         if ($isStopDeletion)
             return false;
 
+        // delete associated comments
+        bx_import('BxDolCmts');
+        BxDolCmts::onAuthorDelete($ID);
+
         // delete associated content 
         // TODO: remake deletion of associated content
         $this->_oQuery->res("DELETE FROM `sys_acl_levels_members` WHERE `IDMember` = {$ID}");

+ 10 - 0
modules/base/general/classes/BxBaseModGeneralInstaller.php

@@ -63,6 +63,7 @@ class BxBaseModGeneralInstaller extends BxDolStudioInstaller
 
     function uninstall($aParams, $bDisable = false) 
     {
+        // check if module is already waiting while files are deleting
         bx_import('BxDolInstallerUtils');
         if (BxDolInstallerUtils::isModulePendingUninstall($this->_aConfig['home_uri']))
         	return array(
@@ -70,12 +71,21 @@ class BxBaseModGeneralInstaller extends BxDolStudioInstaller
                 'result' => false,
             );
 
+        // queue for deletion storage files
         $bSetModulePendingUninstall = false;
         foreach ($this->_aStorages as $s) {
             if (($o = BxDolStorage::getObjectInstance($s)) && $o->queueFilesForDeletionFromObject())
                 $bSetModulePendingUninstall = true;
         }
 
+        // delete comments and queue for deletion comments attachments
+        bx_import('BxDolCmts');
+        $iFiles = 0;
+        BxDolCmts::onModuleUninstall ($this->_aConfig['name'], $iFiles);
+        if ($iFiles)
+            $bSetModulePendingUninstall = true;
+
+        // if some files were added to the queue, set module as pending uninstall
         if ($bSetModulePendingUninstall) {
             BxDolInstallerUtils::setModulePendingUninstall($this->_aConfig['home_uri']);
         	return array(

+ 2 - 1
template/scripts/BxBaseImageServices.php

@@ -10,7 +10,8 @@
 /**
  * System services related to Images' Processing.
  */
-class BxBaseImageServices extends BxDol {
+class BxBaseImageServices extends BxDol 
+{
     public function __construct()
     {
         parent::__construct();