Added an upgrade hook to populate the author_uid column in logs, pending test by Neal
authorDan
Fri, 18 Dec 2009 20:57:28 -0500
changeset 1179 ac861c01a764
parent 1178 57dfbd2a614d
child 1180 4829d15d2782
Added an upgrade hook to populate the author_uid column in logs, pending test by Neal
install/schemas/upgrade/1.1.6-1.1.7.php
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/install/schemas/upgrade/1.1.6-1.1.7.php	Fri Dec 18 20:57:28 2009 -0500
@@ -0,0 +1,35 @@
+<?php
+
+// Migrate usernames in the logs table
+
+global $db, $session, $paths, $template, $plugins; // Common objects
+
+$q = $db->sql_query('SELECT user_id, username FROM users;');
+if ( !$q )
+  $db->_die();
+
+$map = array();
+while($row = $db->fetchrow())
+{
+  $map[ $row['username'] ] = $row['user_id'];
+}
+$db->free_result();
+
+$q = $db->sql_query('SELECT author FROM logs WHERE author_uid = 1;');
+if ( !$q )
+  $db->_die();
+
+$updated = array();
+
+while ( $row = $db->fetchrow($q) )
+{
+  if ( isset($map[ $row['author'] ]) && !is_valid_ip($row['author']) && !in_array($row['author'], $updated) )
+  {
+    $author = $db->escape($row['author']);
+    $sql = "UPDATE logs SET author_uid = {$map[ $row['author'] ]} WHERE author = '$author';";
+    if ( !$db->sql_query($sql) )
+      $db->_die();
+    $updated[] = $row['author'];
+  }
+}
+