Index: content_admin.inc =================================================================== RCS file: /cvs/drupal/contributions/modules/cck/Attic/content_admin.inc,v retrieving revision 1.28.2.48 diff -u -p -r1.28.2.48 content_admin.inc --- content_admin.inc 3 Sep 2008 13:45:05 -0000 1.28.2.48 +++ content_admin.inc 19 Sep 2008 04:24:22 -0000 @@ -1344,6 +1344,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']) { @@ -1419,6 +1420,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)'); + } } /** @@ -1441,6 +1451,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']) { @@ -1521,4 +1532,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/contributions/modules/cck/Attic/nodereference.module,v retrieving revision 1.39.2.40 diff -u -p -r1.39.2.40 nodereference.module --- nodereference.module 4 Sep 2008 23:01:27 -0000 1.39.2.40 +++ nodereference.module 19 Sep 2008 04:24:22 -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,87 @@ 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': + $nid = $query; + $node = node_load($nid); + return check_plain($node->title); } } Index: userreference.module =================================================================== RCS file: /cvs/drupal/contributions/modules/cck/Attic/userreference.module,v retrieving revision 1.30.2.32 diff -u -p -r1.30.2.32 userreference.module --- userreference.module 3 Sep 2008 13:45:05 -0000 1.30.2.32 +++ userreference.module 19 Sep 2008 04:24:22 -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;