Index: usernode.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/usernode/usernode.info,v
retrieving revision 1.3
diff -u -r1.3 usernode.info
--- usernode.info	18 Jun 2007 22:54:05 -0000	1.3
+++ usernode.info	10 Dec 2007 19:34:09 -0000
@@ -2,3 +2,4 @@
 name = "Usernode"
 description = "Automatic creation and deletion of usernodes - one node for each user."
 package = "Node Profile"
+core = 6.x
Index: usernode.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/usernode/usernode.install,v
retrieving revision 1.8
diff -u -r1.8 usernode.install
--- usernode.install	12 Oct 2007 14:26:59 -0000	1.8
+++ usernode.install	10 Dec 2007 19:34:09 -0000
@@ -3,29 +3,36 @@
 
 if (!defined('USERNODE_CONTENT_TYPE')) {
   //if the module isn't included yet, we need to make this define so that things are working...
-  define('USERNODE_CONTENT_TYPE', "usernode");
+  define('USERNODE_CONTENT_TYPE', 'usernode');
+}
+
+function usernode_schema() {
+  $schema = array();
+
+  $schema['usernode'] = array(
+    'description' => 'Relates uid in the {user} table with nid in the {node} table.',
+    'fields' => array(
+      'nid' => array(
+        'type' => 'int',
+        'unsigned' => true,
+        'not null' => true,
+        'disp-width' => '10'
+      ),
+      'uid' => array(
+        'type' => 'int',
+        'unsigned' => true,
+        'not null' => true,
+        'disp-width' => '10'
+      )
+    ),
+    'primary key' => array('uid', 'nid')
+  );
+
+  return $schema;
 }
 
 function usernode_install() {
-  switch ($GLOBALS['db_type']) {
-    case 'mysqli':
-    case 'mysql':
-      db_query("CREATE TABLE if not exists {usernode} (
-        nid int(10) unsigned NOT NULL,
-        uid int(10) unsigned NOT NULL,
-        PRIMARY KEY(uid,nid)
-      ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;");
-      break;
-    case 'pgsql':
-      db_query("CREATE TABLE {usernode} (
-        nid int_unsigned NOT NULL,
-        uid int_unsigned NOT NULL,
-        PRIMARY KEY(uid,nid)
-      )");
-      break;
-    default:
-      break;
-  }
+  drupal_install_schema('usernode');
 
   // Prevent the promotion of new usernode objects to the front page by default,
   // by only placing 'status' (the 'Published' option) into the node options.
@@ -38,14 +45,14 @@
   // so let's find and delete the usernodes manually
   $result = db_query("SELECT u.* FROM {users} u ".
                      "JOIN {node} n ON u.uid = n.uid ".
-                     "WHERE n.type = '". USERNODE_CONTENT_TYPE ."'");
+                     "WHERE n.type = '%s'", USERNODE_CONTENT_TYPE);
 
   while ($user = db_fetch_object($result)) {
     usernode_delete_node($user);
   }
 
-  db_query("DROP TABLE {usernode}");
-  db_query("DELETE FROM {node_type} WHERE type = '". USERNODE_CONTENT_TYPE ."'");
+  drupal_uninstall_schema('usernode');
+  db_query("DELETE FROM {node_type} WHERE type = '%s'", USERNODE_CONTENT_TYPE);
   variable_del('node_options_'. USERNODE_CONTENT_TYPE);
 }
 
Index: usernode.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/usernode/usernode.module,v
retrieving revision 1.34
diff -u -r1.34 usernode.module
--- usernode.module	26 Nov 2007 20:26:18 -0000	1.34
+++ usernode.module	10 Dec 2007 19:34:09 -0000
@@ -12,7 +12,9 @@
  * You may change this to your custom nodetype, if you do so you may also remove
  * the next 6 functions, however reimplementing hook_delete would be suggested.
  */
-define('USERNODE_CONTENT_TYPE', "usernode");
+if (!defined('USERNODE_CONTENT_TYPE')) {
+  define('USERNODE_CONTENT_TYPE', 'usernode');
+}
 
 /**
  * Usernode custom nodetype
@@ -26,6 +28,8 @@
       'locked' => TRUE,
       'has_body' => FALSE,
       'has_title' => FALSE,
+      'title_label' => '', // D6 creates a PHP notice without this explicit setting
+      'body_label' => ''
     )
   );
 }
@@ -71,8 +75,7 @@
   }
 
   if ($op == 'update') {
-    if ( (user_access('edit own usernode') && is_usernode($node))
-         || user_access('edit usernodes') ) {
+    if ((user_access('edit own usernode') && is_usernode($node)) || user_access('edit usernodes')) {
       return TRUE;
     }
   }
@@ -80,6 +83,8 @@
 
 /**
  * Implementation of hook_perm().
+ *
+ * @return array
  */
 function usernode_perm() {
   return array('edit usernodes', 'edit own usernode');
@@ -144,82 +149,113 @@
 }
 
 /**
- * Implementation of hook_menu().
+ * Implementation of hook_init().
+ *
+ * Displays an error message for users attempting to delete a usernode.
  */
-function usernode_menu($may_cache) {
-  global $user;
-
-  if ($may_cache) {
-    // prevent manual adding of usernodes
-    $items[] = array('path' => 'node/add/'. USERNODE_CONTENT_TYPE,
-        'callback' => 'usernode_prevent_manual_adding',
-        'access' => user_access('administer nodes'),
-        'type' => MENU_CALLBACK);
-
-    $items[] = array('path' => 'usernode',
-        'callback' => 'usernode_view_own_usernode',
-        'title' => t('My usernode'),
-        'access' => $user->uid && user_access('access content'),
-        'type' => MENU_SUGGESTED_ITEM);
-    return $items;
-  }
-
-  if (!$may_cache && arg(0) == 'node' && is_numeric(arg(1))) {
+function usernode_init() {
+  if (arg(0) == 'node' && is_numeric(arg(1))) {
     $node = node_load(arg(1));
     if ($node->nid && $node->type == USERNODE_CONTENT_TYPE && arg(2) == 'delete') {
       // somebody tries to delete a usernode!
-      drupal_set_message(t("It's not possible to delete a usernode!"), 'error');
+      drupal_set_message(t('It\'s not possible to delete a usernode!'), 'error');
       drupal_goto('');
     }
   }
 }
 
 /**
+ * Implementation of hook_menu().
+ *
+ * @return array
+ */
+function usernode_menu() {
+  global $user;
+
+  // prevent manual adding of usernodes
+  $items['node/add/'. USERNODE_CONTENT_TYPE] = array(
+    'page callback' => 'usernode_prevent_manual_adding',
+    'access callback' => 'user_access',
+    'access arguments' => array('administer nodes'),
+    'type' => MENU_CALLBACK
+  );
+
+  $items['usernode'] = array(
+    'title' => 'My usernode',
+    'page callback' => 'usernode_view_own_usernode',
+    'access callback' => 'usernode_menu_access',
+    'type' => MENU_SUGGESTED_ITEM
+  );
+  return $items;
+}
+
+/**
+ * Access permission check for the "usernode" path in hook_menu()
+ *
+ * @return bool
+ */
+function usernode_menu_access() {
+  global $user;
+  return !empty($user->uid) && user_access('access content');
+}
+
+/**
  * Delete the associated node(s). Called by hook_user().
  */
 function usernode_delete_node(&$user) {
   $node = usernode_get_node($user);
 
-  db_query('DELETE FROM {node} WHERE nid = %d', $node->nid);
-  db_query('DELETE FROM {node_revisions} WHERE nid = %d', $node->nid);
-  db_query('DELETE FROM {usernode} WHERE nid = %d', $node->nid);
-
-  // Don't call hook_delete as we prevent deletion there
-  node_invoke_nodeapi($node, 'delete');
+  if (!empty($node)) {
+    db_query('DELETE FROM {node} WHERE nid = %d', $node->nid);
+    db_query('DELETE FROM {node_revisions} WHERE nid = %d', $node->nid);
+    db_query('DELETE FROM {usernode} WHERE nid = %d', $node->nid);
+
+    // Don't call hook_delete as we prevent deletion there
+    node_invoke_nodeapi($node, 'delete');
+
+    // Remove this node from the search index if needed.
+    if (function_exists('search_wipe')) {
+      search_wipe($node->nid, 'node');
+    }
+  }
 
   // Clear the cache so an anonymous poster can see the node being deleted.
   cache_clear_all();
-
-  // Remove this node from the search index if needed.
-  if (function_exists('search_wipe')) {
-    search_wipe($node->nid, 'node');
-  }
 }
 
 /**
  * Create an associated node. Called by hook_user().
  */
 function usernode_create_node($user) {
+
   $node = array(
     'type' => USERNODE_CONTENT_TYPE,
     'title' => check_plain($user->name),
-    'user' => $user,
+    'body' => '',
+    'user' => $user->name,
+    'op' => t('Save')
+  );
+  $form_state = array(
+    'values' => array(
+      'title' => check_plain($user->name),
+      'body' => '',
+      'user' => $user->name,
+      'op' => t('Save')
+    )
   );
-  usernode_save_node($node);
+  usernode_save_node($form_state, $node);
 }
 
-/*
- * Saves a usernode by using drupal_execute.
- * This allows modules like auto_nodetitle to work and makes sure all hooks are invoked properly
+/**
+ * Saves a usernode by using drupal_execute().
+ * This allows modules like auto_nodetitle() to work and makes sure all hooks are invoked properly
  */
-function usernode_save_node($node) {
-  $values = array();
-
+function usernode_save_node($form_state, $node) {
   // workaround to disable drupal message "Your usernode has been created"
   $messages = drupal_get_messages();
 
   // create the usernode
-  drupal_execute(USERNODE_CONTENT_TYPE .'_node_form', $values, $node);
+  drupal_execute(USERNODE_CONTENT_TYPE .'_node_form', $form_state, $node);
 
   // write back the old messages
   $_SESSION['messages'] = $messages;
@@ -246,8 +282,7 @@
  * Insert a node/user relationship into the {usernode} table.
  */
 function usernode_insert($node) {
-  db_query("INSERT INTO {usernode} (nid,uid) VALUES(%d,%d)",
-           $node->nid, $node->uid);
+  db_query('INSERT INTO {usernode} (nid, uid) VALUES (%d, %d)', $node->nid, $node->uid);
 }
 
 /**
@@ -258,7 +293,17 @@
   //make sure the node is associated with the updated $user and save it again,
   //so the usernode is updated
   $node->user = $user;
-  usernode_save_node($node);
+  $node = array(
+    'nid' => $node->nid,
+    'type' => USERNODE_CONTENT_TYPE
+  );
+  $form_state = array(
+    'values' => array(
+      'title' => check_plain($user->name),
+      'name' => $user->name
+    )
+  );
+  usernode_save_node($form_state, $node);
 }
 
 /**
@@ -266,7 +311,7 @@
  * Load the user object of the corresponding user into $node->user.
  */
 function usernode_load($node) {
-  $sql = "SELECT uid FROM {usernode} WHERE nid = %d";
+  $sql = 'SELECT uid FROM {usernode} WHERE nid = %d';
   $uid = db_result(db_query($sql, $node->nid));
 
   if ($uid) {
@@ -299,8 +344,8 @@
   static $history = array();
 
   if (!is_object($user)) {
-    if (!$history[$user]) {
-      $history[$user] = db_result(db_query("SELECT nid FROM {usernode} WHERE uid = %d", $user));
+    if (empty($history[$user])) {
+      $history[$user] = db_result(db_query('SELECT nid FROM {usernode} WHERE uid = %d', $user));
     }
     return $history[$user];
   }
@@ -351,18 +396,14 @@
  */
 function usernode_check_all($limit = 100) {
   // find usernodes that have been deleted while the module was deactivated
-  $result = db_query("SELECT un.* FROM {usernode} un ".
-                     "LEFT JOIN {node} n ON un.nid = n.nid ".
-                     "WHERE n.nid IS NULL");
+  $result = db_query('SELECT un.* FROM {usernode} un LEFT JOIN {node} n ON un.nid = n.nid WHERE n.nid IS NULL');
 
   while ($row = db_fetch_object($result)) {
-    db_query("DELETE FROM {usernode} WHERE nid = %d", $row->nid);
+    db_query('DELETE FROM {usernode} WHERE nid = %d', $row->nid);
   }
 
   // create usernodes for all existing users without a usernode
-  $result = db_query_range("SELECT u.* FROM {users} u ".
-                           "LEFT JOIN {usernode} un ON u.uid = un.uid ".
-                           "WHERE un.nid IS NULL AND u.uid != 0", 0, $limit);
+  $result = db_query_range('SELECT u.* FROM {users} u LEFT JOIN {usernode} un ON u.uid = un.uid WHERE un.nid IS NULL AND u.uid != 0', 0, $limit);
   $processed = 0;
   while ($user = db_fetch_object($result)) {
     usernode_create_node($user);
@@ -425,6 +466,8 @@
 /**
  * Implementation of hook_views_tables():
  * Present fields and filters for user data.
+ *
+ * @return array
  */
 function usernode_views_tables() {
   $tables['usernode'] = array(
@@ -637,6 +680,8 @@
 /**
  * Implementation of hook_views_arguments():
  * Present an argument Role ID.
+ *
+ * @return array
  */
 function usernode_views_arguments() {
   $arguments = array(
@@ -662,7 +707,7 @@
 
       $query->add_field('name', 'usernode_role');
       $query->add_field('rid', 'usernode_role');
-      $fieldinfo['field'] = "usernode_role.name";
+      $fieldinfo['field'] = 'usernode_role.name';
       return $fieldinfo;
       break;
 
@@ -677,14 +722,14 @@
       $joininfo['type'] = 'inner';
       $joininfo['extra'] = array('rid' => $rid);
 
-      $query->add_table("usernode_users_roles", true, 1, $joininfo);
+      $query->add_table('usernode_users_roles', true, 1, $joininfo);
       break;
 
     case 'link':
       return l($query->name, "$arg/" . intval($query->rid));
 
     case 'title':
-      $role = db_fetch_object(db_query("SELECT name FROM {role} WHERE rid = '%d'", $query));
+      $role = db_fetch_object(db_query("SELECT name FROM {role} WHERE rid = %d", $query));
       return $role->name;
   }
 }
@@ -710,6 +755,8 @@
 
 /**
  * Callback for usernode_views_tables(): email address as a link.
+ *
+ * @return string
  */
 function usernode_views_handler_field_email($fieldinfo, $fielddata, $value, $data) {
   return l($value, 'mailto:'. $value);
@@ -717,6 +764,8 @@
 
 /**
  * Callback for usernode_views_tables(): render status: active or blocked.
+ *
+ * @return string
  */
 function usernode_views_handler_field_status($fieldinfo, $fielddata, $value, $data) {
   return $value ? t('active') : t('blocked');
