Index: content_admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/cck/Attic/content_admin.inc,v
retrieving revision 1.28.2.46
diff -u -p -r1.28.2.46 content_admin.inc
--- content_admin.inc	27 Jul 2008 22:50:26 -0000	1.28.2.46
+++ content_admin.inc	22 Aug 2008 20:50:23 -0000
@@ -1320,6 +1320,7 @@ function content_alter_db_field($previou
  *   Additional optional attributes. Recognized attributes:
  *     not null => TRUE|FALSE
  *     default  => NULL|FALSE|value (with or without '', it won't be added)
+ *     index    => TRUE|FALSE
  */
 function content_db_add_column($table, $column, $type, $attributes = array()) {
   switch ($GLOBALS['db_type']) {
@@ -1395,6 +1396,15 @@ function content_db_add_column($table, $
       db_query('ALTER TABLE {'. $table .'} ADD COLUMN '. $column .' '. $type .' '. $not_null .' '. $default);
       break;
   }
+
+  // We only support the simplest possible index right now
+  // TODO: add support for multi-column index, index lengths and index types
+  if ($attributes['index'] === TRUE) {
+    db_query('CREATE INDEX '. $column .' ON  {'. $table .'} ('. $column .')');
+    // For now, we also index the nid column automatically when any other
+    // column is indexed, because it is now likely to be used in views queries
+    db_query('CREATE INDEX nid ON  {'. $table .'} (nid)');
+  }
 }
 
 /**
@@ -1417,6 +1427,7 @@ function content_db_add_column($table, $
  *   Additional optional attributes. Recognized attributes:
  *     not null => TRUE|FALSE
  *     default  => NULL|FALSE|value (with or without '', it won't be added)
+ *     index    => TRUE|FALSE
  */
 function content_db_change_column($table, $column, $column_new, $type, $attributes = array()) {
   switch ($GLOBALS['db_type']) {
@@ -1497,4 +1508,10 @@ function content_db_change_column($table
       db_query('ALTER TABLE {'. $table .'} CHANGE '. $column .' '. $column_new .' '. $type .' '. $not_null .' '. $default);
       break;
   }
+
+  // Recreate the index, if necessary
+  if ($attributes['index'] === TRUE) {
+    db_query('CREATE INDEX '. $column_new .' ON  {'. $table .'} ('. $column_new .')');
+    db_query('CREATE INDEX nid ON  {'. $table .'} (nid)');
+  }
 }
Index: nodereference.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/cck/Attic/nodereference.module,v
retrieving revision 1.39.2.38
diff -u -p -r1.39.2.38 nodereference.module
--- nodereference.module	4 Aug 2008 22:28:16 -0000	1.39.2.38
+++ nodereference.module	22 Aug 2008 20:50:23 -0000
@@ -90,7 +90,7 @@ function nodereference_field_settings($o
 
     case 'database columns':
       $columns = array(
-        'nid' => array('type' => 'int', 'not null' => TRUE, 'default' => '0'),
+        'nid' => array('type' => 'int', 'not null' => TRUE, 'default' => '0', 'index' => TRUE),
       );
       return $columns;
 
@@ -104,6 +104,85 @@ function nodereference_field_settings($o
           'extra' => array('field' => $field),
         ),
       );
+      
+  case 'tables':
+      $tables = content_views_field_tables($field);
+      // Add another copy of the field table, joining on the referenced
+      $tables['node_data_'. $field['field_name'].'_rev'] = array(
+        'name' => $tables['node_data_'. $field['field_name']]['name'],
+        'join' => array(
+          'left' => array(
+            'table' => 'node',
+            'field' => 'nid',
+          ),
+          'right' => array(
+            'field' => $field['field_name'].'_nid',
+          ),
+        ),
+      );
+      return $tables;
+
+    case 'arguments':
+      $arguments = content_views_field_arguments($field);
+      $field_types = _content_field_types();
+      $arguments['content: '. $field['field_name'] .'_rev'] = array(
+        'name' => $field_types[$field['type']]['label'] .': '. $field['widget']['label'] .' rev ('. $field['field_name'] .')',
+        'handler' => 'noderef_views_argument_handler',
+      );
+      return $arguments;
+  }
+}
+
+function noderef_views_argument_handler($op, &$query, $argtype, $arg = '') {
+  if ($op == 'filter') {
+    $field_name = substr($argtype['type'], 9, -4);
+  }
+  else {
+    $field_name = substr($argtype, 9, -4);
+  }
+  $field = content_fields($field_name);
+
+  // The table name used here is the Views alias for the table, not the actual
+  // table name.
+  $table = 'node_data_'. $field['field_name'].'_rev';
+
+  switch ($op) {
+    case 'summary':
+      // To produce summaries we need to join to the referrer nid, rather than the referenced nid
+      $joininfo = array(
+        'left' => array(
+          'table' => 'node',
+          'field' => 'nid',
+        ),
+        'right' => array(
+          'field' => 'nid',
+        ),
+      );
+      $query->add_table($table, FALSE, 1, $joininfo);
+      $query->add_field("title");
+      $query->add_field('nid', $table);
+      $fieldinfo['field'] = "$table.nid";
+      return $fieldinfo;
+
+    case 'sort':
+      break;
+
+    case 'filter':
+      $node = node_load(intval($arg));
+      // We load the node both to retrieve the latest vid, but also to check
+      // access to prevent users accessing nodereferences inside nodes to which
+      // they don't have access (by seeing other nodes, to which they do have access).
+      if ($node && node_access('view', $node)) {
+        $query->ensure_table($table);
+        $query->add_where("$table.vid = %d", $node->vid);
+      }
+      break;
+
+    case 'link':
+      return l($query->title, "$arg/$query->nid");
+
+    case 'title':
+      return content_format($field, $item, 'plain');
   }
 }
 
Index: userreference.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/cck/Attic/userreference.module,v
retrieving revision 1.30.2.30
diff -u -p -r1.30.2.30 userreference.module
--- userreference.module	29 Apr 2008 12:58:56 -0000	1.30.2.30
+++ userreference.module	22 Aug 2008 20:50:23 -0000
@@ -55,7 +55,7 @@ function userreference_field_settings($o
 
     case 'database columns':
       $columns = array(
-        'uid' => array('type' => 'int', 'not null' => FALSE, 'default' => NULL),
+        'uid' => array('type' => 'int', 'not null' => FALSE, 'default' => NULL, 'index' => TRUE),
       );
       return $columns;
 
