Index: connectors/l10n_localpacks/l10n_localpacks.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/l10n_server/connectors/l10n_localpacks/Attic/l10n_localpacks.module,v
retrieving revision 1.1.2.9.2.5
diff -u -r1.1.2.9.2.5 l10n_localpacks.module
--- connectors/l10n_localpacks/l10n_localpacks.module	16 Aug 2009 15:00:51 -0000	1.1.2.9.2.5
+++ connectors/l10n_localpacks/l10n_localpacks.module	18 Aug 2009 19:34:54 -0000
@@ -38,7 +38,8 @@
   );
   $items['admin/l10n_server/l10n_localpacks/scan'] = array(
     'title' => 'Scan',
-    'page callback' => 'l10n_localpacks_scan',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('l10n_localpacks_scan_form'),
     'access arguments' => array('administer localization community for local packages'),
     'type' => MENU_LOCAL_TASK
   );
@@ -57,7 +58,9 @@
  */
 function l10n_localpacks_cron() {
   if (variable_get('l10n_localpacks_cron', FALSE)) {
-    l10n_localpacks_scan(TRUE);
+    $form_state = array();
+    $form_state['values']['op'] = t('Parse @count releases in a batch process', array('@count' => variable_get('l10n_localpacks_limit', 1)));
+    drupal_execute('l10n_localpacks_scan_form', &$form_state);
   }
 }
 
@@ -117,95 +120,229 @@
 /**
  * Scans files of a project and release picked.
  *
- * @param $automated
- *   TRUE if the execution was automated, so user feedback should
- *   not be provided. FALSE otherwise.
+ * @return array $form
  */
-function l10n_localpacks_scan($automated = FALSE) {
+function l10n_localpacks_scan_form() {
 
   // We look for projects in the working directory.
   $workdir = variable_get('l10n_localpacks_directory', '');
 
-  if (!is_dir($workdir)) {
-    drupal_set_message(t('The configured local packages directory (%workdir) cannot be found. <a href="@configure">Check your configuration</a>.', array('%workdir' => $workdir, '@configure' => url('admin/l10n_server/l10n_localpacks/configure'))));
+  if (!is_dir($workdir) && $workdir != '') {
+    drupal_set_message(t('The configured local packages directory (%workdir) cannot be found. <a href="@configure">Check your configuration</a>.', array('%workdir' => $workdir, '@configure' => url('admin/l10n_server/l10n_localpacks/configure'))), 'error');
   }
-  else {
-    // Packages are always .tar.gz files.
-    $files = file_scan_directory($workdir, '.tar.gz$');
-    if (count($files)) {
-      foreach ($files as $path => $file) {
-
-        if (!l10n_community_is_supported_version($path)) {
-          // Skip files for unsupported versions.
-          continue;
-        }
+  elseif($workdir == '') {
+    drupal_set_message(t('The configured local packages directory is not set. <a href="@configure">Check your configuration</a>.', array('@configure' => url('admin/l10n_server/l10n_localpacks/configure'))), 'error');
+  }
+  $form = array();
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Parse @count releases in a batch process', array('@count' => variable_get('l10n_localpacks_limit', 1))),
+  );
 
-        // Get rid of $workdir prefix on file names, eg.
-        // files/Drupal/drupal-4.6.7.tar.gz or
-        // files/Ubercart/ubercart-5.x-1.0-alpha8.tar.gz.
-        $path = $package = trim(preg_replace('!(^'. preg_quote($workdir, '!') .')(.+)\.tar\.gz!', '\2', $path), '/');
-
-        $project_title = '';
-        if (strpos($path, '/')) {
-          // We have a slash, so this package is in a subfolder.
-          // Eg. Drupal/drupal-4.6.7 or Ubercart/ubercart-5.x-1.0-alpha8.
-          // Grab the directory name as project title.
-          list($project_title, $package) = explode('/', $path);
-        }
-        if (strpos($package, '-')) {
-          // Only remaining are the project uri and release,
-          // eg. drupal-4.6.7 or ubercart-5.x-1.0-alpha8.
-          list($project_uri, $release_version) = explode('-', $package, 2);
+  return $form;
+}
 
-          l10n_localpacks_save_data($project_uri, ($project_title ? $project_title : $project_uri), $release_version, $path .'.tar.gz', filemtime($file->filename));
-        }
-        else {
-          // File name not formatted properly.
-          $result['error'] = t('File name should have project codename and version number included separated with hyphen, such as drupal-5.2.tar.gz.');
-        }
-      }
-    }
+/**
+ * Validate that working directory is available
+ * @param $form
+ * @param $form_state
+ * @return unknown_type
+ */
+function l10n_localpacks_scan_form_validate($form, &$form_state) {
+  // We look for projects in the working directory.
+  // Message is show via l10n_localpacks_scan_form()
+  $workdir = variable_get('l10n_localpacks_directory', '');
+  if (!is_dir($workdir) && $workdir != '') {
+    form_set_error('', '');
+    return;
+  }
+  elseif($workdir == '') {
+    form_set_error('', '');
+    return;
   }
+}
+
+/**
+ *
+ * @param $form
+ * @param $form_state
+ */
+function l10n_localpacks_scan_form_submit($form, &$form_state) {
+  $items = array();
+  $workdir = variable_get('l10n_localpacks_directory', '');
+  $files = file_scan_directory($workdir, '.tar.gz$');
 
-  $user_feedback = FALSE;
+  if ($batch = l10n_localpacks_check_files($files, TRUE, 'l10n_localpacks_batch_scan_finished')) {
+      batch_set($batch);
+  }
+  elseif(!empty($_POST)) {
+    // File name not formatted properly.
+    drupal_set_message(t('File name should have project codename and version number included separated with hyphen, such as drupal-5.2.tar.gz.'), 'error');
+  }
   $results = db_query_range("SELECT * FROM {l10n_community_release} WHERE pid IN (SELECT pid FROM {l10n_community_project} WHERE connector_module = 'l10n_localpacks' AND status = 1) ORDER BY last_parsed ASC", 0, variable_get('l10n_localpacks_limit', 1));
   while ($release = db_fetch_object($results)) {
+    $items[] = $release;
+  }
 
-    // Only parse file if something changed since we last parsed it.
-    $file_name = $workdir .'/'. $release->download_link;
+  if ($batch = l10n_localpacks_check_files($items, FALSE, 'l10n_localpacks_batch_import_finished')) {
+      batch_set($batch);
+  }
+  elseif(!empty($_POST)) {
+    drupal_set_message(t('No (new) local packages found to scan in %workdir.', array('%workdir' => $workdir)));
+  }
+}
 
-    if (file_exists($file_name)) {
-      if (filemtime($file_name) > $release->last_parsed) {
-        include_once drupal_get_path('module', 'l10n_community') .'/extractor.inc';
-        $result = l10n_community_parse_package($file_name, $release->rid);
-
-        // User feedback, if not automated. Log messages are already done.
-        if (isset($result['error']) && !$automated) {
-          $user_feedback = TRUE;
-          drupal_set_message($result['error'], 'error');
-        }
-        elseif (isset($result['message']) && !$automated) {
-          $user_feedback = TRUE;
-          drupal_set_message($result['message']);
+/**
+ *
+ * @param $items
+ * @param $onlyscan
+ * @param $finished
+ */
+function l10n_localpacks_check_files($items, $onlyscan = TRUE, $finished) {
+$files = array();
+$workdir = variable_get('l10n_localpacks_directory', '');
+  if (!$onlyscan) {
+    // We look for projects in the working directory.
+    foreach($items as $release) {
+      // Only parse file if something changed since we last parsed it.
+      $file_name = $workdir .'/'. $release->download_link;
+
+      if (file_exists($file_name)) {
+        if (filemtime($file_name) > $release->last_parsed) {
+          $files[] = array('filename' => $file_name, 'release_rid' => $release->rid);
         }
       }
-      else {
-        if (!$automated) {
-          $user_feedback = TRUE;
-          drupal_set_message(t('@release was already parsed, no need to scan again.', array('@release' => $release->download_link)));
-        }
+    }
+    return l10n_localpacks_import_batch_build($files, $finished);
+  }
+  if ($onlyscan) {
+    foreach ($items as $path => $file) {
+      if (!l10n_community_is_supported_version($path)) {
+        // Skip files for unsupported versions.
+        continue;
       }
+      $files[] = array('file' => $file, 'path' => $path);
     }
-    // Hackish update of last parsed time so other tarballs will get into the queue too.
-    // @todo: work on something better for this.
-    db_query("UPDATE {l10n_community_release} SET last_parsed = %d WHERE rid = %d", time(), $release->rid);
+    return l10n_localpacks_scan_batch_build($files, $finished);
   }
-  if (!$automated && !$user_feedback) {
-    drupal_set_message(t('No (new) local packages found to scan in %workdir.', array('%workdir' => $workdir)));
+  return FALSE;
+}
+
+/**
+ *
+ * @param $files
+ * @param $finished
+ */
+function l10n_localpacks_import_batch_build($files, $finished = NULL) {
+
+  if (count($files)) {
+    $operations = array();
+    foreach ($files as $file) {
+      // We call l10n_localpacks_batch_import for every batch operation.
+      $operations[] = array('l10n_localpacks_batch_import', array($file['filename'], $file['release_rid']));
+    }
+    $batch = array(
+      'operations'    => $operations,
+      'title'         => t('Importing translation packages'),
+      'init_message'  => t('Starting import'),
+      'error_message' => t('Error impoting translation packages'),
+      'file'          => drupal_get_path('module', 'l10n_community') .'/extractor.inc',
+      );
+    if (isset($finished)) {
+      $batch['finished'] = $finished;
+    }
+    return $batch;
   }
+  return FALSE;
+}
 
-  // Ensure that a Drupal page will be displayed with the messages.
-  return '';
+/**
+ *
+ * @param $files
+ * @param $finished
+ */
+function l10n_localpacks_scan_batch_build($files, $finished = NULL) {
+  if (count($files)) {
+    $operations = array();
+    foreach ($files as $file) {
+        // We call l10n_localpacks_batch_scan for every batch operation.
+        $operations[] = array('l10n_localpacks_batch_scan', array($file['file'], $file['path']));
+    }
+    $batch = array(
+      'operations'    => $operations,
+      'title'         => t('Scanning translation packages'),
+      'init_message'  => t('Starting scan'),
+      'error_message' => t('Error scanning translation packages'),
+      );
+    if (isset($finished)) {
+      $batch['finished'] = $finished;
+    }
+    return $batch;
+  }
+  return FALSE;
+}
+
+/**
+ *
+ * @param $file_name
+ * @param $release_rid
+ * @param $context
+ */
+function l10n_localpacks_batch_import($file_name, $release_rid, &$context) {
+  $result = l10n_community_parse_package($file_name, $release_rid);
+  if (isset($result['error'])) {
+    $context['message'] = $result['error'];
+  }
+  elseif (isset($result['message'])) {
+    $context['message'] = $result['message'];
+  }
+  $context['results'][] = $file_name;
+  db_query("UPDATE {l10n_community_release} SET last_parsed = %d WHERE rid = %d", time(), $release_rid);
+}
+
+function l10n_localpacks_batch_scan($file, $path, &$context) {
+  $workdir = variable_get('l10n_localpacks_directory', '');
+  // Get rid of $workdir prefix on file names, eg.
+  // files/Drupal/drupal-4.6.7.tar.gz or
+  // files/Ubercart/ubercart-5.x-1.0-alpha8.tar.gz.
+  $path = $package = trim(preg_replace('!(^'. preg_quote($workdir, '!') .')(.+)\.tar\.gz!', '\2', $path), '/');
+
+  $project_title = '';
+  if (strpos($path, '/')) {
+    // We have a slash, so this package is in a subfolder.
+    // Eg. Drupal/drupal-4.6.7 or Ubercart/ubercart-5.x-1.0-alpha8.
+    // Grab the directory name as project title.
+    list($project_title, $package) = explode('/', $path);
+  }
+  if (strpos($package, '-')) {
+    // Only remaining are the project uri and release,
+    // eg. drupal-4.6.7 or ubercart-5.x-1.0-alpha8.
+    list($project_uri, $release_version) = explode('-', $package, 2);
+    l10n_localpacks_save_data($project_uri, ($project_title ? $project_title : $project_uri), $release_version, $path .'.tar.gz', filemtime($file->filename));
+    $context['results'][] = $file->filename;
+  }
+}
+
+/**
+ *
+ * @param $success
+ * @param $results
+ */
+function l10n_localpacks_batch_scan_finished($success, $results) {
+  if ($success && !empty($_POST)) {
+    drupal_set_message(format_plural(count($results), 'One translation package scanned.', '@count translation packages scanned.'));
+  }
+}
+
+/**
+ *
+ * @param $success
+ * @param $results
+ */
+function l10n_localpacks_batch_import_finished($success, $results) {
+  if ($success && !empty($_POST)) {
+    drupal_set_message(format_plural(count($results), 'One translation package imported.', '@count translation packages imported.'));
+  }
 }
 
 /**
