Author: david
Date: Tue Oct 25 16:43:32 2011
New Revision: 10186

Log:
Fix migration of digital objects to repository based directory

Modified:
   trunk/lib/model/QubitDigitalObject.php
   trunk/lib/task/migrate/QubitMigrate110.class.php

Modified: trunk/lib/model/QubitDigitalObject.php
==============================================================================
--- trunk/lib/model/QubitDigitalObject.php      Tue Oct 25 13:33:08 2011        
(r10185)
+++ trunk/lib/model/QubitDigitalObject.php      Tue Oct 25 16:43:32 2011        
(r10186)
@@ -1912,4 +1912,51 @@
 
     return true;
   }
+
+  /**
+   * Recursively remove empty directories
+   *
+   * @param string $dir directory name
+   *
+   * @return void
+   */
+  public static function pruneEmptyDirs($dir)
+  {
+    // Remove any extra whitespace or trailing slash
+    $dir = rtrim(trim($dir), '/');
+
+    do
+    {
+      if (sfConfig::get('sf_upload_dir') == $dir || 
sfConfig::get('sf_upload_dir').'/r' == $dir)
+      {
+        return; // Protect uploads/ and uploads/r/
+      }
+
+      if (self::isEmptyDir($dir))
+      {
+        rmdir($dir);
+      }
+      else
+      {
+        return;
+      }
+    } while (strpos($dir, '/') && $dir = substr($dir, 0, strrpos($dir, '/')));
+  }
+
+  /**
+   * Check if directory is empty
+   *
+   * @param string $dir directory name
+   *
+   * @return boolean true if empty
+   */
+  public static function isEmptyDir($dir)
+  {
+    if (is_dir($dir))
+    {
+      $files = scandir($dir);
+
+      return (2 <= count($files)); // Always have "." and ".." dirs
+    }
+  }
 }

Modified: trunk/lib/task/migrate/QubitMigrate110.class.php
==============================================================================
--- trunk/lib/task/migrate/QubitMigrate110.class.php    Tue Oct 25 13:33:08 
2011        (r10185)
+++ trunk/lib/task/migrate/QubitMigrate110.class.php    Tue Oct 25 16:43:32 
2011        (r10186)
@@ -72,7 +72,7 @@
         $this->addThemesMenu();
 
       case 69:
-        //$this->updatePathToAssets();
+        $this->updatePathToAssets();
 
       case 70:
         $this->addRepostioryUploadLimit();
@@ -602,7 +602,7 @@
     // Create "uploads/r" subdirectory
     if (!file_exists(sfConfig::get('sf_upload_dir').'/r'))
     {
-      mkdir(sfConfig::get('sf_upload_dir').'/r');
+      mkdir(sfConfig::get('sf_upload_dir').'/r', 0775);
     }
 
     foreach ($this->data['QubitDigitalObject'] as $key => &$item)
@@ -629,19 +629,18 @@
       if (isset($infoObject['repository_id']))
       {
         $repo = $this->getRowByKeyOrId('QubitRepository', 
$infoObject['repository_id']);
-      }
-      else
-      {
-        $repo = 'null';
-      }
 
-      // Make repository directory
-      if (!file_exists(sfConfig::get('sf_upload_dir').'/r/'.$repo['slug']))
-      {
-        if (!mkdir(sfConfig::get('sf_upload_dir').'/r/'.$repo['slug']))
+        if (!isset($repo['slug']))
         {
+          var_dump('AAAAA');
           continue;
         }
+
+        $repoName = $repo['slug'];
+      }
+      else
+      {
+        $repoName = 'null';
       }
 
       // Update digital object and derivatives paths
@@ -656,36 +655,39 @@
             continue;
           }
 
-          $oldpath = rtrim($item2['path'], '/');
+          $oldpath = $item2['path'];
 
           // Build new path
-          if (preg_match('|/(\d)/(\d)/\d{3,}|', $item2['path'], $matches))
+          if (preg_match('|\d/\d/\d{3,}/$|', $oldpath, $matches))
           {
-            mkdir($newpath = 
sfConfig::get('sf_upload_dir').'/r/'.$repo['slug'].'/'.$matches[1]);
-            mkdir($newpath .= '/'.$matches[2]);
+            $newpath = '/uploads/r/'.$repoName.'/'.$matches[0];
           }
           else
           {
             continue;
           }
 
-          if (file_exists(sfConfig::get('sf_web_dir').$item2['path']))
+          if (!file_exists(sfConfig::get('sf_web_dir').$newpath))
           {
-            if (!rename(sfConfig::get('sf_web_dir').$item2['path'], 
sfConfig::get('sf_web_dir').$newpath))
+            if (!mkdir(sfConfig::get('sf_web_dir').$newpath, 0775, true))
             {
-              continue; // If rename fails, don't update path
+              continue;
             }
           }
 
-          // Delete old dirs, if they are empty
-          $p = strlen($oldpath);
-          while ($p = strrpos($oldpath, '/', $p - strlen($oldpath)))
+          if (file_exists(sfConfig::get('sf_web_dir').$oldpath))
           {
-            break;
+            if (!rename(sfConfig::get('sf_web_dir').$oldpath.$item2['name'], 
sfConfig::get('sf_web_dir').$newpath.$item2['name']))
+            {
+              continue; // If rename fails, don't update path
+            }
           }
 
+          // Delete old dirs, if they are empty
+          
QubitDigitalObject::pruneEmptyDirs(sfConfig::get('sf_web_dir').$oldpath);
+
           // Update path in yaml file
-          // $item2['path'] = str_replace('uploads', 
'uploads/r/'.$repo['slug'], $item2['path']);
+          $item2['path'] = $newpath;
         }
       }
     }

-- 
You received this message because you are subscribed to the Google Groups 
"Qubit Toolkit Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/qubit-commits?hl=en.

Reply via email to