Author: david
Date: Fri Feb 25 18:11:11 2011
New Revision: 9002

Log:
Optimize propel:generate-slug.  Use internal hash to lookup duplicate keys, and 
abandon ORM update.

Modified:
   trunk/lib/task/propelGenerateSlugsTask.class.php

Modified: trunk/lib/task/propelGenerateSlugsTask.class.php
==============================================================================
--- trunk/lib/task/propelGenerateSlugsTask.class.php    Fri Feb 25 16:58:03 
2011        (r9001)
+++ trunk/lib/task/propelGenerateSlugsTask.class.php    Fri Feb 25 18:11:11 
2011        (r9002)
@@ -63,6 +63,13 @@
       'information_object' => 'QubitInformationObject'
     );
 
+    // Create hash of slugs already in database
+    $sql = "SELECT slug FROM slug ORDER BY slug";
+    foreach ($conn->query($sql, PDO::FETCH_NUM) as $row)
+    {
+      $slugs[$row[0]] = true;
+    }
+
     foreach ($tables as $table => $classname)
     {
       $this->logSection('propel', "Generate $table slugs...");
@@ -88,9 +95,57 @@
 
       foreach($conn->query($sql, PDO::FETCH_NUM) as $row)
       {
-        $obj = QubitObject::getById($row[0]);
-        $obj->slug = QubitSlug::slugify($row[1]);
-        $obj->insertSlug($conn);
+        // Get unique slug
+        if (null !== $row[1])
+        {
+          $slug = QubitSlug::slugify($row[1]);
+
+          // Truncate at 250 chars
+          if (250 < strlen($slug))
+          {
+            $slug = substr($slug, 0, 250);
+          }
+
+          $count = 0;
+          $suffix = '';
+
+          while (isset($slugs[$slug.$suffix]))
+          {
+            $count++;
+            $suffix = '-'.$count;
+          }
+
+          $slug .= $suffix;
+        }
+        else 
+        {
+          $slug = QubitSlug::random();
+
+          while (isset($slugs[$slug]))
+          {
+            $slug = QubitSlug::random();
+          }
+        }
+
+        $slugs[$slug] = true; // Lookup table
+        $newRows[] = array($row[0], $slug); // To add to slug table
+      }
+
+      // Do inserts
+      $inc = 1000;
+      for ($i = 0; $i < count($newRows); $i += $inc)
+      {
+        $sql = "INSERT INTO slug (object_id, slug) VALUES ";
+        
+        $last = min($i+$inc, count($newRows));
+        for ($j = $i; $j < $last; $j++)
+        {
+          $sql .= '('.$newRows[$j][0].', \''.$newRows[$j][1].'\'), ';
+        }
+        
+        $sql = substr($sql, 0, -2).';';
+
+        $conn->exec($sql);
       }
     }
 

-- 
You received this message because you are subscribed to the Google Groups 
"Qubit Toolkit Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/qubit-commits?hl=en.

Reply via email to