Index: modules/node/node.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/node/node.module,v
retrieving revision 1.829
diff -u -p -u -p -r1.829 node.module
--- modules/node/node.module	7 Jun 2007 03:45:57 -0000	1.829
+++ modules/node/node.module	11 Jun 2007 20:57:35 -0000
@@ -581,10 +581,10 @@ function node_load($param = array(), $re
   // No db_rewrite_sql is applied so as to get complete indexing for search.
   if ($revision) {
     array_unshift($arguments, $revision);
-    $node = db_fetch_object(db_query('SELECT n.nid, r.vid, n.type, n.status, n.language, n.created, n.changed, n.comment, n.promote, n.sticky, r.timestamp AS revision_timestamp, r.title, r.body, r.teaser, r.log, r.format, u.uid, u.name, u.picture, u.data FROM {node} n INNER JOIN {users} u ON u.uid = n.uid INNER JOIN {node_revisions} r ON r.nid = n.nid AND r.vid = %d WHERE '. $cond, $arguments));
+    $node = db_fetch_object(db_query('SELECT n.nid, r.vid, n.type, n.status, n.language, n.created, n.changed, n.comment, n.promote, n.sticky, n.tnid, n.translate, r.timestamp AS revision_timestamp, r.title, r.body, r.teaser, r.log, r.format, u.uid, u.name, u.picture, u.data FROM {node} n INNER JOIN {users} u ON u.uid = n.uid INNER JOIN {node_revisions} r ON r.nid = n.nid AND r.vid = %d WHERE '. $cond, $arguments));
   }
   else {
-    $node = db_fetch_object(db_query('SELECT n.nid, n.vid, n.type, n.status, n.language, n.created, n.changed, n.comment, n.promote, n.sticky, r.timestamp AS revision_timestamp, r.title, r.body, r.teaser, r.log, r.format, u.uid, u.name, u.picture, u.data FROM {node} n INNER JOIN {users} u ON u.uid = n.uid INNER JOIN {node_revisions} r ON r.vid = n.vid WHERE '. $cond, $arguments));
+    $node = db_fetch_object(db_query('SELECT n.nid, n.vid, n.type, n.status, n.language, n.created, n.changed, n.comment, n.promote, n.sticky, n.tnid, n.translate, r.timestamp AS revision_timestamp, r.title, r.body, r.teaser, r.log, r.format, u.uid, u.name, u.picture, u.data FROM {node} n INNER JOIN {users} u ON u.uid = n.uid INNER JOIN {node_revisions} r ON r.vid = n.vid WHERE '. $cond, $arguments));
   }
 
   if ($node && $node->nid) {
Index: modules/node/node.schema
===================================================================
RCS file: /cvs/drupal/drupal/modules/node/node.schema,v
retrieving revision 1.2
diff -u -p -u -p -r1.2 node.schema
--- modules/node/node.schema	5 Jun 2007 12:13:22 -0000	1.2
+++ modules/node/node.schema	11 Jun 2007 20:57:35 -0000
@@ -4,19 +4,21 @@
 function node_schema() {
   $schema['node'] = array(
     'fields' => array(
-      'nid'      => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
-      'vid'      => array('type' => 'int', 'unsigned' => TRUE, 'not null' => FALSE, 'default' => 0),
-      'type'     => array('type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'default' => ''),
-      'language' => array('type' => 'varchar', 'length' => 12, 'not null' => TRUE, 'default' => ''),
-      'title'    => array('type' => 'varchar', 'length' => 128, 'not null' => TRUE, 'default' => ''),
-      'uid'      => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
-      'status'   => array('type' => 'int', 'not null' => TRUE, 'default' => 1),
-      'created'  => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
-      'changed'  => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
-      'comment'  => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
-      'promote'  => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
-      'moderate' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
-      'sticky'   => array('type' => 'int', 'not null' => TRUE, 'default' => 0)
+      'nid'       => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
+      'vid'       => array('type' => 'int', 'unsigned' => TRUE, 'not null' => FALSE, 'default' => 0),
+      'type'      => array('type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'default' => ''),
+      'language'  => array('type' => 'varchar', 'length' => 12, 'not null' => TRUE, 'default' => ''),
+      'title'     => array('type' => 'varchar', 'length' => 128, 'not null' => TRUE, 'default' => ''),
+      'uid'       => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
+      'status'    => array('type' => 'int', 'not null' => TRUE, 'default' => 1),
+      'created'   => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
+      'changed'   => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
+      'comment'   => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
+      'promote'   => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
+      'moderate'  => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
+      'sticky'    => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
+      'tnid'      => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
+      'translate' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
     ),
     'indexes' => array(
       'nid'                 => array('nid'),
@@ -28,7 +30,8 @@ function node_schema() {
       'node_title_type'     => array('title', array('type', 4)),
       'node_type'           => array(array('type', 4)),
       'status'              => array('status'),
-      'uid'                 => array('uid')
+      'uid'                 => array('uid'),
+      'tnid'                => array('tnid'),
     ),
     'unique keys' => array(
       'nid_vid' => array('nid', 'vid'),
Index: modules/system/system.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.install,v
retrieving revision 1.121
diff -u -p -u -p -r1.121 system.install
--- modules/system/system.install	7 Jun 2007 03:44:13 -0000	1.121
+++ modules/system/system.install	11 Jun 2007 20:57:41 -0000
@@ -3350,6 +3350,16 @@ function system_update_6023() {
 }
 
 /**
+ * Add translation fields to nodes used by translation module.
+ */
+function system_update_6024() {
+  $ret = array();
+  db_add_field($ret, 'node', 'tnid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
+  db_add_field($ret, 'node', 'translate', array('type' => 'int', 'not null' => TRUE, 'default' => 0));
+  return $ret;
+}
+
+/**
  * @} End of "defgroup updates-5.x-to-6.x"
  * The next series of updates should start at 7000.
  */
Index: modules/translation/translation.info
===================================================================
RCS file: modules/translation/translation.info
diff -N modules/translation/translation.info
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ modules/translation/translation.info	11 Jun 2007 20:57:41 -0000
@@ -0,0 +1,7 @@
+; $Id$
+name = Content translation
+description = Allows content to be translated into different languages.
+dependencies[] = locale
+package = Core - optional
+version = VERSION
+core = 6.x
Index: modules/translation/translation.module
===================================================================
RCS file: modules/translation/translation.module
diff -N modules/translation/translation.module
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ modules/translation/translation.module	11 Jun 2007 20:57:41 -0000
@@ -0,0 +1,367 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ *   Manages content translations.
+ *
+ *   Only content types for which the administrator explicitly enabled translations
+ *   could have translations associated. Translations are managed in sets with
+ *   exactly one source content per set. The source content is used to translate
+ *   to different languages, so if the source content is significantly updated,
+ *   the editor can decide to mark all translations outdated.
+ *
+ * @todo
+ *   - What if we want to add an existing node to a translation set?
+ *     hook_nodeapi() update and delete are ready for this, but no UI.
+ *   - How to remove a node from a translation set?
+ *     Again, we have a nice API for this, but no UI yet.
+ */
+
+/**
+ * Identifies a content type which has translation support enabled.
+ */
+define ('TRANSLATION_ENABLED', 2);
+
+/**
+ * Implementation of hook_help().
+ */
+function translation_help($section) {
+  switch ($section) {
+    case 'admin/help#translation':
+      $output = '<p>'. t('The <em>translation</em> module allows content translation for multilingual sites.') .'</p>';
+      $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="@translation">Translation page</a>.', array('@translation' => 'http://drupal.org/handbook/modules/translation/')) .'</p>';
+      return $output;
+    case 'node/%/translate':
+      $output = '<p>'. t('Translations of a piece of content are managed with translation sets. Each translation set has one source content and any number of translations in any of the <a href="!languages">enabled languages</a>. All translations are tracked to be up to date or oudated based on whether the source content was modified significantly.', array('!languages' => url('admin/settings/language'))) .'</p>';
+      return $output;
+  }
+}
+
+/**
+ * Implementation of hook_menu().
+ */
+function translation_menu() {
+  $items = array();
+  $items['node/%node/translate'] = array(
+    'title' => 'Translate',
+    'page callback' => 'translation_node_overview',
+    'page arguments' => array(1),
+    'access callback' => '_translation_tab_access',
+    'access arguments' => array(1),
+    'type' => MENU_LOCAL_TASK,
+    'weight' => 2,
+  );
+  return $items;
+}
+
+/**
+ * Menu access callback.
+ *
+ * Only display translation tab for node types, which have translation enabled
+ * and where the current node is not language neutral (which should span
+ * all languages).
+ */
+function _translation_tab_access($node) {
+  if (!empty($node->language) && translation_supported_type($node->type)) {
+    return user_access('translate content');
+  }
+  return FALSE;
+}
+
+/**
+ * Implementation of hook_perm().
+ */
+function translation_perm() {
+  return array('translate content');
+}
+
+/**
+ * Implementation of hook_form_alter().
+ *
+ * - Add translation option to content type form.
+ * - Alters language fields on node forms when a translation
+ *   is about to be created.
+ */
+function translation_form_alter(&$form, $form_state, $form_id) {
+  if ($form_id == 'node_type_form') {
+    // Add translation option to content type form.
+    $form['workflow']['language']['#options'][TRANSLATION_ENABLED] = t('Enabled, with translation');
+    // Description based on text from locale.module.
+    $form['workflow']['language']['#description'] = t('Enable multilingual support for this content type. If enabled, a language selection field will be added to the editing form, allowing you to select from one of the <a href="!languages">enabled languages</a>. You can also turn on translation for this content type, which lets you have content translated to any of the enabled languages. If disabled, new posts are saved with the default language. Existing content will not be affected by changing this option.', array('!languages' => url('admin/settings/language')));
+  }
+  elseif ($form['#id'] == 'node-form' && translation_supported_type($form['#node']->type)) {
+    $node = $form['#node'];
+    if (!empty($node->translation_source)) {
+      // We are creating a translation. Add values and lock language field.
+      $form['translation_source'] = array('#type' => 'value', '#value' => $node->translation_source);
+      $form['language']['#disabled'] = TRUE;
+    }
+    elseif (!empty($node->nid) && !empty($node->tnid)) {
+      // Disable languages for existing translations, so it is not possible to switch this
+      // node to some language which is already in the translation set. Also remove the
+      // language neutral option.
+      unset($form['language']['#options']['']);
+      foreach (translation_node_get_translations($node->tnid) as $translation) {
+        if ($translation->nid != $node->nid) {
+          unset($form['language']['#options'][$translation->language]);
+        }
+      }
+      // Add translation values and workflow options.
+      $form['translation'] = array(
+        '#type' => 'fieldset',
+        '#title' => t('Translation settings'),
+        '#access' => user_access('translate content'),
+        '#collapsible' => TRUE,
+        '#collapsed' => !$node->translate,
+        '#tree' => TRUE,
+        '#weight' => 30,
+      );
+      $form['translation']['tnid'] = array('#type' => 'value', '#value' => $node->tnid);
+      if ($node->tnid == $node->nid) {
+        // This is the source node of the translation
+        $form['translation']['retranslate'] = array(
+          '#type' => 'checkbox',
+          '#title' => t('Update translations'),
+          '#default_value' => 0,
+          '#description' => t('Mark all translations of this content as outdated.'),
+        );
+        $form['translation']['status'] = array('#type' => 'value', '#value' => 0);
+      }
+      else {
+        $form['translation']['status'] = array(
+          '#type' => 'checkbox',
+          '#title' => t('This translation needs to be updated'),
+          '#default_value' => $node->translate,
+          '#description' => t('When this option is checked, this translation needs to be updated because the source content has changed. Uncheck when the translation is up to date.'),
+        );
+      }
+    }
+  }
+}
+
+/**
+ * Implementation of hook_link().
+ *
+ * Display translation links with native language names, if this node
+ * is part of a translation set.
+ */
+function translation_link($type, $node = NULL, $teaser = FALSE) {
+  $links = array();
+  if ($type == 'node' && ($node->tnid) && $translations = translation_node_get_translations($node->tnid)) {
+    // Do not show link to the same node.
+    unset($translations[$node->language]);
+    $languages = locale_language_list('native');
+    foreach ($translations as $language => $translation) {
+      $links["node_translation_$language"] = array(
+        'title' => $languages[$language],
+        'href' => "node/$translation->nid",
+        'attributes' => array('title' => $translation->title, 'class' => 'translation-link')
+      );
+    }
+  }
+  return $links;
+}
+
+/**
+ * Overview page for a node's translations.
+ *
+ * @param $node
+ *   Node object.
+ */
+function translation_node_overview($node) {
+  if ($node->tnid) {
+    // Already part of a set, grab that set.
+    $tnid = $node->tnid;
+    $translations = translation_node_get_translations($node->tnid);
+  }
+  else {
+    // We have no translation source nid, this could be a new set, emulate that.
+    $tnid = $node->nid;
+    $translations = array($node->language => $node);
+  }
+
+  $header = array(t('Language'), t('Title'), t('Status'), t('Operations'));
+
+  foreach (language_list() as $language) {
+    $options = array();
+    $language_name = $language->name;
+    if (isset($translations[$language->language])) {
+      // Existing translation in the translation set: display status.
+      // We load the full node to check whether the user can edit it.
+      $translation_node = node_load($translations[$language->language]->nid);
+      $title = l($translation_node->title, 'node/'. $translation_node->nid);
+      if (node_access('update', $translation_node)) {
+        $options[] = l(t('edit'), "node/$translation_node->nid/edit");
+      }
+      $status = $translation_node->status ? t('Published') : t('Not published');
+      $status .= $translation_node->translate ? ' - <span class="marker">'. t('outdated') .'</span>' : '';
+      if ($translation_node->nid == $tnid) {
+        $language_name = '<strong>'. $language_name . '</strong> (source)';
+      }
+    }
+    else {
+      // No such translation in the set yet: help user to create it.
+      $title = t('n/a');
+      if (node_access('create', $node)) {
+        $options[] = l(t('add translation'), 'node/add/'. $node->type, array('query' => "translation=$node->nid&language=$language->language"));
+      }
+      $status = t('Not translated');
+    }
+    $rows[] = array($language_name, $title, $status, implode(" | ", $options));
+  }
+
+  drupal_set_title(t('Translations of %title', array('%title' => $node->title)));
+  return theme('table', $header, $rows);
+}
+
+/**
+ * Implementation of hook_nodeapi().
+ *
+ * Manages translation information for nodes.
+ */
+function translation_nodeapi(&$node, $op, $teaser, $page) {
+  // Only act if we are dealing with a content type supporting translations.
+  if (!translation_supported_type($node->type)) {
+    return;
+  }
+ 
+  switch ($op) {
+   case 'prepare':
+      if (empty($node->nid) && isset($_GET['translation']) && isset($_GET['language']) &&
+          ($source_nid = $_GET['translation']) && ($language = $_GET['language']) &&
+          (user_access('translate content'))) {
+        // We are translating a node from a source node, so
+        // load the node to be translated and populate fields.
+        $node->language = $language;
+        $node->translation_source = node_load($source_nid);
+        $node->title = $node->translation_source->title;
+        $node->body = $node->translation_source->body;
+        // Let every module add custom translated fields.
+        node_invoke_nodeapi($node, 'prepare translation');
+      }
+      break;
+
+    case 'insert':
+      if (!empty($node->translation_source)) {
+        if ($node->translation_source->tnid) {
+          // Add node to existing translation set.
+          $tnid = $node->translation_source->tnid;
+        }
+        else {
+          // Create new translation set, using nid from the source node.
+          $tnid = $node->translation_source->nid;
+          db_query("UPDATE {node} SET tnid = %d, translate = %d WHERE nid = %d", $tnid, 0, $node->translation_source->nid);
+        }
+        db_query("UPDATE {node} SET tnid = %d, translate = %d WHERE nid = %d", $tnid, 0, $node->nid);
+      }
+      break;
+
+    case 'update':
+      if (isset($node->translation) && $node->translation && !empty($node->language)) {
+        if ($node->tnid) {
+          // Update translation information.
+          db_query("UPDATE {node} SET tnid = %d, translate = %d WHERE nid = %d", $node->tnid, $node->translation['status'], $node->nid);
+          if (!empty($node->translation['retranslate'])) {
+            // This is the source content, asking to mark all translations outdated.
+            db_query("UPDATE {node} SET translate = 1 WHERE tnid = %d AND nid != %d", $node->tnid, $node->nid);
+          }
+        }
+        else {
+          // The node may have been added to some existing translation set.
+          db_query("UPDATE {node} SET tnid = %d, translate = %d WHERE nid = %d", $node->tnid, $node->translate, $node->nid);
+        }
+      }
+      else {
+        // Remove node from translation set if it was in one.
+        translation_remove_from_set($node);
+      }
+      break;
+
+    case 'delete':
+      translation_remove_from_set($node);
+      break;
+  }
+}
+
+/**
+ * Remove a node from its translation set (if any)
+ * and update the set accordingly.
+ */
+function translation_remove_from_set($node) {
+  if (isset($node->tnid)) {
+    if (db_num_rows(db_query('SELECT tnid FROM {node} WHERE tnid = %d', $node->tnid)) <= 2) {
+      // There would only be one node left in the set: remove the set altogether.
+      db_query('UPDATE {node} SET tnid = 0, translate = 0 WHERE tnid = %d', $node->tnid);
+    }
+    else {
+      db_query('UPDATE {node} SET tnid = 0, translate = 0 WHERE nid = %d', $node->nid);
+
+      // If the node being removed was the source of the translation set,
+      // we pick a new source - preferably one that is up to date.
+      if ($node->tnid == $node->nid) {
+        $new_tnid = db_result(db_query('SELECT nid FROM {node} WHERE tnid = %d ORDER BY translate ASC, nid ASC', $node->tnid));
+        db_query('UPDATE {node} SET tnid = %d WHERE tnid = %d', $new_tnid, $node->tnid);
+      }
+    }
+  }
+}
+
+/**
+ * Get all nodes in a translation set, represented by $tnid.
+ *
+ * @param $tnid
+ *   The translation source nid of the translation set, the identifier
+ *   of the node used to derive all translations in the set.
+ * @return
+ *  Array of partial node objects (nid, title, language) representing
+ *  all nodes in the translation set, in effect all translations
+ *  of node $tnid, including node $tnid itself. Because these are
+ *  not partial nodes, you need to node_load() the full node, if you
+ *  need more properties. The array is indexed by language code.
+ */
+function translation_node_get_translations($tnid) {
+  static $translations = array();
+  
+  if (is_numeric($tnid) && $tnid) {
+    if (!isset($translations[$tnid])) {
+      $translations[$tnid] = array();
+      $result = db_query(db_rewrite_sql('SELECT nid, title, language FROM {node} WHERE tnid = %d'), $tnid);
+      while ($node = db_fetch_object($result)) {
+        $translations[$tnid][$node->language] = $node;
+      }
+    }
+    return $translations[$tnid];
+  }
+}
+
+/**
+ * Return paths of all translations of a node, based on
+ * its Drupal path. 
+ *
+ * @param $path
+ *   A Drupal path, for example node/432.
+ * @return
+ *   An array of paths of all translations of the node
+ *   keyed with language codes.
+ */
+function translation_path_get_translations($path) {
+  $paths = array();
+  // Check for a node related path, and for its translations.
+  if ((preg_match("!^node/([0-9]+)(/|$)!", $path, $matches)) && ($node = node_load($matches[1])) && !empty($node->translation)) {
+    foreach (translation_node_get_translations($node->tnid) as $language => $translation_node) {
+      $paths[$language] = 'node/'. $translation_node->nid . $matches[3];
+    }
+  }
+  return $paths;
+}
+
+/**
+ * Returns whether the given content type has support for translations.
+ *
+ * @return
+ *   Boolean value.
+ */
+function translation_supported_type($type) {
+  return variable_get('language_' . $type, 0) == TRANSLATION_ENABLED;
+}
