From 2fc74843ced0cdff1e6e882f72c6f6f8ddb4d1c6 Mon Sep 17 00:00:00 2001
From: wonder95 <killshot91@gmail.com>
Date: Wed, 14 Sep 2011 17:03:12 -0700
Subject: [PATCH] Adding functionality to pathauto to use a separate index table for bulk aliasing nodes, users, and taxonomy terms.  Adds drush command to fill index table and modifications to queries that pass data to batch API to pull from index table.

---
 pathauto.drush.inc    |  106 +++++++++++++++++++++++++++++++++++++++++++++++++
 pathauto.install      |   50 +++++++++++++++++++++++
 pathauto.pathauto.inc |   12 +++---
 3 files changed, 162 insertions(+), 6 deletions(-)
 create mode 100644 pathauto.drush.inc

diff --git a/pathauto.drush.inc b/pathauto.drush.inc
new file mode 100644
index 0000000..361394b
--- /dev/null
+++ b/pathauto.drush.inc
@@ -0,0 +1,106 @@
+<?php
+
+/**
+ * @file
+ *   drush integration for pathauto.
+ */
+
+/**
+ * Implementation of hook_drush_command().
+ *
+ * In this hook, you specify which commands your
+ * drush module makes available, what it does and
+ * description.
+ *
+ * Notice how this structure closely resembles how
+ * you define menu hooks.
+ *
+ * @See drush_parse_command() for a list of recognized keys.
+ *
+ * @return
+ *   An associative array describing your command(s).
+ */
+function pathauto_drush_command() {
+  $items = array();
+  
+  $items['pathauto-populate-index-table'] = array(
+    // the name of the function implementing your command.
+    'callback' => 'pathauto_populate_index_table',
+    // a short description of your command
+    'description' => dt('Populates index table of node and taxonomy sources to be used when bulk updating url aliases.'),
+    'arguments' => array(
+      'update_types' => 'Comma delimited list of aliases to update (node, term, user).  If no types are specified, all three types will be run.',
+    ),    
+    'aliases' => array('cpit'),
+  );
+ 
+  return $items;
+}
+
+/**
+ * Implementation of hook_drush_help().
+ *
+ * This function is called whenever a drush user calls
+ * 'drush help <name-of-your-command>'
+ *
+ * @param
+ *   A string with the help section (prepend with 'drush:')
+ *
+ * @return
+ *   A string with the help text for your command.
+ */
+function pathauto_drush_help($section) {
+  switch ($section) {
+    case 'drush:pathauto-populate-index-table':
+      return dt("Populates index table for bulk generation of url aliases."); 
+  }
+}
+
+/**
+ * Command callback. Populates index table with alias sources to be used when
+ * bulk generating url aliases.
+ */
+function pathauto_populate_index_table($types = 'all') {
+  // Set $list to contain all three types if no arguments passed.
+  if ($types == 'all') {
+    $list = array('node', 'term', 'user');
+  }
+  // Otherwise create $list from passed arguments.
+  else {
+    $list = explode(',', $types);
+  }
+  
+  foreach ($list as $type) {
+    switch ($type) {
+      case 'node':
+        $table = 'node';
+        $identifier = 'nid';
+        $src_base = 'node/';
+        break;
+      case 'term':
+        $table = 'term_data';
+        $identifier = 'tid';
+        $src_base = 'taxonomy/term/';
+        break;
+      case 'user':
+        $table = 'users';
+        $identifier = 'uid';
+        $src_base = '/user/';
+        break;
+    }
+    
+    $sql = "SELECT " . $identifier . " FROM {{$table}} ORDER BY " . $identifier;
+    $result = db_query($sql);
+    $sql_insert = "INSERT INTO {pathauto_alias_index} VALUES (%d, '%s', '%s', '%s')";
+    $count = 0;
+    while ($data = db_fetch_array($result)) {
+      $$identifier = $identifier;
+      $src = $src_base . $data[$$identifier];
+      db_query($sql_insert, $data[$$identifier], $type, $src, 'N');
+      $count++;
+    }
+    
+  $msg = dt("Index table filled for !type entities with !count records.", array("!type" => $type, "!count" => $count));
+  drush_print("\n" . $msg . "\n");      
+  }
+}
\ No newline at end of file
diff --git a/pathauto.install b/pathauto.install
index 29f42fa..556983e 100644
--- a/pathauto.install
+++ b/pathauto.install
@@ -124,3 +124,53 @@ function pathauto_update_6202() {
   variable_del('pathauto_blog_applytofeeds');
   return array();
 }
+
+/**
+ * Install index table
+ */
+function pathauto_update_6203() {
+  drupal_install_schema('pathauto');
+  return array();
+}
+
+/**
+ * Implementation of hook_schema.
+ */
+function pathauto_schema() {
+  $schema['pathauto_alias_index'] = array(
+    'description' => t('Index table filled by drush command to store nodes that need URL aliases regenerated'),
+    'fields' => array(
+      'id' => array(
+        'description' => t('TODO: please describe this field!'),
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+      ),
+      'src' => array(
+        'description' => t('Source identifier (nid, tid, uid) for entity.'),
+        'type' => 'varchar',
+        'length' => '128',
+        'not null' => TRUE,
+        'default' => '',
+      ),
+      'type' => array(
+        'description' => t('Type of entity the record is for (node, term, user)'),
+        'type' => 'char',
+        'length' => '8',
+        'not null' => TRUE,
+        'default' => '',
+      ),
+      'created' => array(
+        'description' => t('Timestamp'),
+        'type' => 'char',
+        'length' => '1',
+        'not null' => FALSE,
+      ),
+    ),
+    'primary key' => array('id', 'type'),
+    'indexes' => array(
+      'src' => array('src'),
+    ),    
+  );
+  return $schema;
+}
diff --git a/pathauto.pathauto.inc b/pathauto.pathauto.inc
index 4a7b7f8..8698084 100644
--- a/pathauto.pathauto.inc
+++ b/pathauto.pathauto.inc
@@ -54,8 +54,8 @@ function node_pathauto_bulk_update_batch_process(&$context) {
   }
 
   $concat = _pathauto_sql_concat("'node/'", 'n.nid');
-  $sql = "SELECT n.nid FROM {node} n LEFT JOIN {url_alias} ua ON $concat = ua.src WHERE ua.src IS NULL AND n.nid > %d ORDER BY n.nid";
-  $args = array($context['sandbox']['current']);
+  $sql = "SELECT pai.id FROM {pathauto_alias_index} pai LEFT JOIN url_alias ua ON pai.src = ua.src WHERE pai.type = '%s' AND ua.src IS NULL AND pai.id > %d ORDER BY pai.id";
+  $args = array('node', $context['sandbox']['current']);
 
   // Get the total amount of items to process.
   if (!isset($context['sandbox']['total'])) {
@@ -127,8 +127,8 @@ function taxonomy_pathauto_bulk_update_batch_process(&$context) {
 
   $concat = _pathauto_sql_concat("'taxonomy/term/'", 't.tid');
   $forum_vid = variable_get('forum_nav_vocabulary', '');
-  $sql = "SELECT t.tid FROM {term_data} t LEFT JOIN {url_alias} ua ON $concat = ua.src WHERE ua.src IS NULL AND t.tid > %d AND t.vid <> %d ORDER BY t.tid";
-  $args = array($context['sandbox']['current'], $forum_vid);
+  $sql = "SELECT pai.id FROM {pathauto_alias_index} pai LEFT JOIN {url_alias} ua ON pai.src = ua.src INNER JOIN term_data t on pai.id = t.tid WHERE pai.type = '%s' AND ua.src IS NULL AND pai.id > %d AND t.vid <> %d ORDER BY pai.id";
+  $args = array('term', $context['sandbox']['current'], $forum_vid);
 
   // Get the total amount of items to process.
   if (!isset($context['sandbox']['total'])) {
@@ -248,8 +248,8 @@ function user_pathauto_bulk_update_batch_process(&$context) {
   }
 
   $concat = _pathauto_sql_concat("'user/'", 'u.uid');
-  $sql = "SELECT u.uid FROM {users} u LEFT JOIN {url_alias} ua ON $concat = ua.src WHERE ua.src IS NULL AND u.uid > %d ORDER BY u.uid";
-  $args = array($context['sandbox']['current']);
+  $sql = "SELECT pai.id FROM {pathauto_alias_index} pai LEFT JOIN {url_alias} ua ON pai.src = ua.src WHERE pai.type = '%s' AND ua.src IS NULL AND u.uid > %d ORDER BY u.uid";
+  $args = array('user', $context['sandbox']['current']);
 
   // Get the total amount of items to process.
   if (!isset($context['sandbox']['total'])) {
-- 
1.7.4.1

