From a62729e35bb7b2f588cfc4632cbcc82dbb32c01b Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Thu, 28 Jun 2012 17:44:54 -0400
Subject: [PATCH] Fix AttachmentMigrater::queueAttachments

Remove clobber between queue() method and queue instance property (which is
the queue list). Also properly limit the number of attachments queued for
migration as indicated by the initial call to the queueAttachments() method.
Lastly, fix an issue where the first $limit items of the database cannot be
found on or read from the disk. Recurse through the queueAttachments()
method until the entire database is scanned or $limit items are placed in
the queue.
---
 include/class.migrater.php | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/include/class.migrater.php b/include/class.migrater.php
index 72679537e..04f38be2a 100644
--- a/include/class.migrater.php
+++ b/include/class.migrater.php
@@ -114,7 +114,7 @@ class AttachmentMigrater {
      */
     function do_batch($time=30, $max=0) {
 
-        if(!$this->queueAttachments() || !$this->getQueueLength())
+        if(!$this->queueAttachments($max) || !$this->getQueueLength())
             return 0;
 
         $count = 0;
@@ -123,7 +123,7 @@ class AttachmentMigrater {
             if($this->next() && $max && ++$count>=$max)
                 break;
 
-        return $this->queueAttachments();
+        return $this->queueAttachments($max);
 
     }
 
@@ -131,7 +131,7 @@ class AttachmentMigrater {
         return $this->skipList;
     }
 
-    function queue($fileinfo) {
+    function enqueue($fileinfo) {
         $this->queue[] = $fileinfo;
     }
 
@@ -159,8 +159,14 @@ class AttachmentMigrater {
         }
         # Get the mime/type of each file
         # XXX: Use finfo_buffer for PHP 5.3+
-        if(function_exists('mime_content_type')) //XXX: function depreciated in newer versions of PHP!!!!!
+        if(function_exists('mime_content_type')) { 
+            //XXX: function depreciated in newer versions of PHP!!!!!
             $info['type'] = mime_content_type($info['path']);
+        } elseif (function_exists('finfo_file')) { // PHP 5.3.0+
+            $finfo = finfo_open(FILEINFO_MIME_TYPE);
+            $info['type'] = finfo_file($finfo, $info['path']);
+        }
+        # TODO: Add extension-based mime-type lookup
 
         if (!($fileId = AttachmentFile::save($info))) {
             return $this->skip($info['attachId'],
@@ -194,8 +200,8 @@ class AttachmentMigrater {
 
         $sql='SELECT attach_id, file_name, file_key, Ti.created'
             .' FROM '.TICKET_ATTACHMENT_TABLE.' TA'
-            .' JOIN '.TICKET_TABLE.' Ti ON Ti.ticket_id=TA.ticket_id'
-            .' WHERE NOT file_id '; //XXX: ignore orphaned attachments?
+            .' INNER JOIN '.TICKET_TABLE.' Ti ON (Ti.ticket_id=TA.ticket_id)'
+            .' WHERE NOT file_id ';
 
         if(($skipList=$this->getSkipList()))
             $sql.= ' AND attach_id NOT IN('.implode(',', db_input($skipList)).')';
@@ -253,10 +259,10 @@ class AttachmentMigrater {
             # anyway.
             $info['size'] = @filesize($info['path']);
             # Coroutines would be nice ..
-            $this->queue($info);
+            $this->enqueue($info);
         }
 
-        return $this->getQueueLength();
+        return $this->queueAttachments($limit);
     }
 
     function skip($attachId, $error) {
-- 
GitLab