From 87f2832c2b2bd818bcdbe5075914c59837914739 Mon Sep 17 00:00:00 2001
From: Eric Rasmussen <eric@unl.edu>
Date: Fri, 18 Feb 2011 19:33:29 +0000
Subject: [PATCH] [gh-98] Merging testing into staging (IMCE upgrade)

git-svn-id: file:///tmp/wdn_thm_drupal/branches/drupal-7.x/staging@526 20a16fea-79d4-4915-8869-1ea9d5ebf173
---
 sites/all/modules/imce/imce.info         |  9 ++++---
 sites/all/modules/imce/imce.install      | 32 ++++++++++++++++++------
 sites/all/modules/imce/inc/imce.page.inc | 10 +++-----
 3 files changed, 33 insertions(+), 18 deletions(-)

diff --git a/sites/all/modules/imce/imce.info b/sites/all/modules/imce/imce.info
index 9b64b168..56461245 100644
--- a/sites/all/modules/imce/imce.info
+++ b/sites/all/modules/imce/imce.info
@@ -1,12 +1,13 @@
-; $Id: imce.info,v 1.7.2.1 2010/12/27 20:10:34 ufku Exp $
+; $Id: imce.info,v 1.7.2.2 2011/01/13 09:09:40 ufku Exp $
 name = "IMCE"
 description = "An image/file uploader and browser supporting personal directories and user quota."
 core = "7.x"
 package = "Media"
+configure = "admin/config/media/imce"
 
-; Information added by drupal.org packaging script on 2010-12-27
-version = "7.x-1.1"
+; Information added by drupal.org packaging script on January 1, 1970 - 00:00
+version = "7.x-1.2"
 core = "7.x"
 project = "imce"
-datestamp = "1293481279"
+datestamp = "1296601518"
 
diff --git a/sites/all/modules/imce/imce.install b/sites/all/modules/imce/imce.install
index a5552b3d..458119f9 100644
--- a/sites/all/modules/imce/imce.install
+++ b/sites/all/modules/imce/imce.install
@@ -1,5 +1,5 @@
 <?php
-// $Id: imce.install,v 1.8 2010/08/30 04:06:25 ufku Exp $
+// $Id: imce.install,v 1.8.2.1 2011/01/18 08:56:19 ufku Exp $
 
 /**
  * @file
@@ -50,32 +50,48 @@ function imce_update_7000() {
   if ($ids != '') {
     $ids = explode(',', $ids);
     foreach ($ids as &$id) {
-      $id += '*';
+      $id .= '*';
     }
     variable_set('imce_settings_textarea', implode(', ', $ids));
   }
 }
 
 /**
+ * Migrates imce files from {files} to {file_managed}.
  * Removes {imce_files} in favor of {file_usage}.
  */
 function imce_update_7001(&$sandbox) {
-  if (!db_table_exists('imce_files')) {
+  if (!db_table_exists('imce_files') || !db_table_exists('files')) {
     return;
   }
+  // Initiate progress
   if (!isset($sandbox['progress'])) {
     $sandbox['progress'] = 0;
-    $sandbox['last_fid_processed'] = -1;
-    $sandbox['max'] = db_query("SELECT COUNT(*) FROM {imce_files}")->fetchField();
+    $sandbox['last_fid_processed'] = 0;
+    $sandbox['max'] = db_query("SELECT COUNT(*) FROM {imce_files} i INNER JOIN {files} f ON i.fid = f.fid")->fetchField();
   }
-  $limit = 500;
-  $result = db_query_range('SELECT i.fid FROM {imce_files} i INNER JOIN {file_managed} f ON i.fid = f.fid WHERE i.fid > :fid ORDER BY i.fid', 0, $limit, array(':fid' => $sandbox['last_fid_processed']))->fetchAll();
+  // Prepare variables
+  $limit = 250;
+  $basedir = variable_get('file_directory_path', conf_path() . '/files') . '/';
+  $baselen = strlen($basedir);
+  $scheme = file_default_scheme() . '://';
+  $result = db_query_range('SELECT f.* FROM {imce_files} i INNER JOIN {files} f ON i.fid = f.fid WHERE i.fid > :fid ORDER BY i.fid', 0, $limit, array(':fid' => $sandbox['last_fid_processed']))->fetchAll();
+  // Migrate imce files from {files} to {file_managed}
   foreach ($result as $file) {
+    $relpath = substr($file->filepath, 0, $baselen) == $basedir ? substr($file->filepath, $baselen) : $file->filepath;
+    $file->uri = file_stream_wrapper_uri_normalize($scheme . $relpath);
+    unset($file->filepath);
+    if (!db_query("SELECT 1 FROM {file_managed} WHERE fid = :fid", array(':fid' => $file->fid))->fetchField()) {
+      drupal_write_record('file_managed', $file);
+    }
     file_usage_add($file, 'imce', 'file', $file->fid);
     $sandbox['progress']++;
     $sandbox['last_fid_processed'] = $file->fid;
   }
-  if ($sandbox['#finished'] = empty($sandbox['max']) || ($sandbox['progress'] / $sandbox['max'])) {
+  // Drop {imce_files} if the progress is complete.
+  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
+  if ($sandbox['#finished'] >= 1) {
     db_drop_table('imce_files');
+    return t('Migrated IMCE files.');
   }
 }
\ No newline at end of file
diff --git a/sites/all/modules/imce/inc/imce.page.inc b/sites/all/modules/imce/inc/imce.page.inc
index 072c0f0e..b4dc6d04 100644
--- a/sites/all/modules/imce/inc/imce.page.inc
+++ b/sites/all/modules/imce/inc/imce.page.inc
@@ -1,5 +1,5 @@
 <?php
-// $Id: imce.page.inc,v 1.10.2.3 2010/12/27 20:09:02 ufku Exp $
+// $Id: imce.page.inc,v 1.10.2.4 2011/01/10 13:03:02 ufku Exp $
 
 /**
  * @file
@@ -279,7 +279,9 @@ function imce_fileop_form_validate($form, &$form_state) {
 function imce_upload_submit($form, &$form_state) {
   $form_state['redirect'] = FALSE;
   $imce =& $form_state['build_info']['args'][0]['imce'];
-  $validators = array('imce_validate_all' => array(&$imce));
+  // Need to provide extension validatior, otherwise file_save_upload uses the default.
+  $validators['file_validate_extensions'] = array($imce['extensions'] === '*' ? NULL : $imce['extensions']);
+  $validators['imce_validate_all'] = array(&$imce);
   $diruri = imce_dir_uri($imce);
 
   //save uploaded file.
@@ -613,10 +615,6 @@ function imce_validate_all($file, $imce) {
   if ($imce['quota']) {
     $errors = array_merge($errors, imce_validate_quota($file, $imce['quota'], $imce['dirsize']));
   }
-  //file extension validation
-  if ($imce['extensions'] != '*') {
-    $errors = array_merge($errors, file_validate_extensions($file, $imce['extensions']));
-  }
   //user quota validation. check it if no errors were thrown.
   if (empty($errors) && $imce['tuquota']) {
     $errors = imce_validate_tuquota($file, $imce['tuquota'], file_space_used($imce['uid']));
-- 
GitLab