? .cvsignore
? browser
? media-popup.patch
? media.interpolateUrl.patch
? patch_101-updated.patch
? images/icons/tango
? images/icons/tango-icon-theme-0.8.90.tar.gz
? test/media.filter.test
? test/media.testlib.inc
Index: media.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/media/media.admin.inc,v
retrieving revision 1.33
diff -u -r1.33 media.admin.inc
--- media.admin.inc	24 Feb 2011 11:09:08 -0000	1.33
+++ media.admin.inc	27 Feb 2011 14:16:11 -0000
@@ -605,3 +605,60 @@
     unset($form_state['values'][media_variable_name('dialog_theme')]);
   }
 }
+
+/**
+ * Confirmation form for rebuliding the file_managed table to include type
+ * in rows where there is no type.
+ */
+function media_admin_rebuild_types_form($form, &$form_state) {
+  $total = db_select('file_managed', 'fm')
+    ->condition('type', NULL)
+    ->countQuery()
+    ->execute()
+    ->fetchField();
+  if ($total == 0) {
+    media_variable_del('show_file_type_rebuild_nag');
+    // @TODO: Make this not sound stupid.
+    drupal_set_message('All files in the system have been assigned types. Media installation complete.');
+    drupal_goto('admin');
+  }
+  $form['total'] = array('#type' => 'value', '#value' => $total);
+  return confirm_form($form, 'Update types for existing files', 'admin/config/media', 'This process is required when installing media on an existing site.  Media needs to scan through existing files and identify the file type. <br/><strong>Update types for ' . $total . ' files?</strogn>');
+}
+
+/**
+ * @see media_admin_rebuild_types_form().
+ */
+function media_admin_rebuild_types_form_submit(&$form, &$form_state) {
+  $total = $form_state['values']['total'];
+
+  $batch = array(
+    'title' => t('Rebuilding type information for ' . $total . ' files'),
+    'operations' => array(
+      array('media_admin_rebuild_types_batch_op', array($total)),
+    ),
+    'finished' => 'media_admin_rebuild_types_batch_complete',
+    'file' => drupal_get_path('module', 'media') . '/media.admin.inc',
+  );
+  batch_set($batch);
+}
+
+/**
+ * Batch operation for fixing the file_managed table for media, adding type values
+ * where no value exists.
+ */
+function media_admin_rebuild_types_batch_op($total, &$context) {
+  $per_run = media_variable_get('media_type_batch_update_per_run', 100);
+  $context['results'] = array_merge($context['results'], media_type_batch_update(FALSE, $per_run));
+  $context['finished'] = count($context['results']) / $total;
+}
+/**
+ * Sets a message informing the user how many file records were updated.
+ */
+function media_admin_rebuild_types_batch_complete($success, $results, $operations) {
+  if ($success) {
+    $message = format_plural(count($results), 'One file identified and given a type.', '@count files identified and given a type.');
+    media_variable_del('show_file_type_rebuild_nag');
+  }
+  drupal_set_message($message);
+}
\ No newline at end of file
Index: media.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/media/media.install,v
retrieving revision 1.19
diff -u -r1.19 media.install
--- media.install	16 Feb 2011 21:37:21 -0000	1.19
+++ media.install	27 Feb 2011 14:16:11 -0000
@@ -1,6 +1,8 @@
 <?php
 // $Id: media.install,v 1.19 2011/02/16 21:37:21 JacobSingh Exp $
 
+define('MEDIA_UPDATE_RECORDS_ON_INSTALL', 200);
+
 /**
  * @file
  * Install, update and uninstall functions for the Media module.
@@ -56,7 +58,6 @@
     'media_preview' => 'media_large_icon',
     //@TODO: We need a real "original" formatter
     'media_original' => 'file_generic',
-    //@TODO: Non-sequiter small == medium.  Why medium if there is no small?
     'media_small'   => 'hidden',
     'media_large'   => 'file_generic',
   );
@@ -142,6 +143,15 @@
   foreach ($roles as $rid => $role) {
     user_role_grant_permissions($rid, array('view media'));
   }
+
+  // Updates the type field for the first MEDIA_UPDATE_RECORDS_ON_INSTALL files.
+  if (count(media_type_batch_update(FALSE, MEDIA_UPDATE_RECORDS_ON_INSTALL)) == MEDIA_UPDATE_RECORDS_ON_INSTALL) {
+    // Okay, so there is a tiny logic error here.  If the user has exactly 
+    // MEDIA_UPDATE_RECORDS_ON_INSTALL files, this will return true but doesn't seem worth another query.
+    // This variable will cause a persistant nag message to appear on every page
+    // for the administrator, urging them to finish the process.
+    media_variable_set('show_file_type_rebuild_nag', TRUE);
+  }
 }
 
 /**
Index: media.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/media/media.module,v
retrieving revision 1.74
diff -u -r1.74 media.module
--- media.module	24 Feb 2011 11:09:08 -0000	1.74
+++ media.module	27 Feb 2011 14:16:12 -0000
@@ -68,6 +68,15 @@
     'file' => 'media.admin.inc',
   );
   // For managing different types of media and the fields associated with them.
+  $items['admin/config/media/rebuild_types'] = array(
+    'title' => 'Rebuild type information for media',
+    'description' => 'In case there are files in file_managed w/o a type, this function rebuilds them',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('media_admin_rebuild_types_form'),
+    'access arguments' => array('administer media'),
+    'file' => 'media.admin.inc',
+  );
+  // For managing different types of media and the fields associated with them.
   $items['admin/config/media/types'] = array(
     'title' => 'Media Types',
     'description' => 'Manage files used on your site.',
@@ -428,6 +437,11 @@
  * in the URL.
  */
 function media_page_alter(&$page) {
+
+  if (user_access('administer media') && media_variable_get('show_file_type_rebuild_nag') && $_GET['q'] != 'admin/config/media/rebuild_types') {
+    drupal_set_message(t('Media module install is not complete.  <a href="@type_rebuild_link"> Finish the install </a> ', array('@type_rebuild_link' => url('admin/config/media/rebuild_types'))), 'warning');
+  }
+  
   if (isset($_GET['render']) && $_GET['render'] == 'media-popup') {
     $page['#theme'] = 'media_dialog_page';
     // temporary fix while awaiting fix for 914786
Index: media.types.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/media/media.types.inc,v
retrieving revision 1.16
diff -u -r1.16 media.types.inc
--- media.types.inc	29 Nov 2010 19:29:17 -0000	1.16
+++ media.types.inc	27 Feb 2011 14:16:12 -0000
@@ -253,7 +253,6 @@
 
 /**
  * Implement hook_media_format_form_prepare_alter
- * @return unknown_type
  */
 function media_media_format_form_prepare_alter(&$form, &$form_state, $media) {
   switch($media->type) {
@@ -268,4 +267,48 @@
       );
       break;
   }
+}
+
+/**
+ * Adds a value for the type column in files_managed.
+ *
+ * If $update_existing is TRUE, will update the type of files with an existing type value.
+ *
+ * @param boolean $update_existing
+ * @param integer $no_to_update
+ * @param integer $offset
+ *
+ * @return array
+ *  A list of updated file ids
+ */
+function media_type_batch_update($update_existing = FALSE, $no_to_update = NULL, $offset = 0) {
+  $results = array();
+  
+  $query = db_select('file_managed', 'fm')
+    ->fields('fm', array('fid'));
+
+  if (!$update_existing) {
+    $query->condition('type', NULL);
+  }
+
+  if ($no_to_update) {
+    $query->range($offset, $no_to_update);
+  }
+  elseif ($offset) {
+    $query->range($offset);
+  }
+
+  $fids = $query->execute()->fetchCol();
+  foreach ($fids as $fid) {
+    $file = file_load($fid);
+    if (!$file->fid) {
+      throw new Exception('Unable to continue, file was not loaded.');
+    }
+    $file->type = media_get_type($file);
+    file_save($file);
+    $results[] = $fid;
+  }
+
+  return $results;
+
 }
\ No newline at end of file
Index: includes/media.variables.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/media/includes/media.variables.inc,v
retrieving revision 1.15
diff -u -r1.15 media.variables.inc
--- includes/media.variables.inc	24 Feb 2011 11:09:08 -0000	1.15
+++ includes/media.variables.inc	27 Feb 2011 14:16:12 -0000
@@ -149,6 +149,10 @@
       'type_icon_directory' => drupal_get_path('module', 'media') . '/images/types',
       'icon_base_directory' => drupal_get_path('module', 'media') . '/images/icons',
       'icon_set' => 'default',
+      
+       // This is set in media_enable.  It will show a persistant dsm on every page
+       // until the user runs the batch operation provided by media_admin_rebuild_types_form
+      'show_file_type_rebuild_nag' => FALSE,
     );
   }
 
