Index: includes/locale.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/locale.inc,v
retrieving revision 1.198
diff -u -p -r1.198 locale.inc
--- includes/locale.inc	19 Dec 2008 03:55:23 -0000	1.198
+++ includes/locale.inc	3 Jan 2009 16:58:02 -0000
@@ -125,7 +125,10 @@ function locale_languages_overview_form_
       $language->enabled = 0;
     }
     $language->weight = $form_state['values']['weight'][$langcode];
-    db_query("UPDATE {languages} SET enabled = %d, weight = %d WHERE language = '%s'", $language->enabled, $language->weight, $langcode);
+    db_update('languages')
+      ->fields(array('enabled' => $language->enabled, 'weight' => $language->weight))
+      ->condition('language', $langcode)
+      ->execute();
     $languages[$langcode] = $language;
   }
   drupal_set_message(t('Configuration saved.'));
@@ -204,7 +207,8 @@ function locale_languages_custom_form() 
  *   Language code of the language to edit.
  */
 function locale_languages_edit_form(&$form_state, $langcode) {
-  if ($language = db_fetch_object(db_query("SELECT * FROM {languages} WHERE language = '%s'", $langcode))) {
+  $language = db_query("SELECT * FROM {languages} WHERE language = :language", array(':language' => $langcode))->fetchObject();
+  if ($language) {
     $form = array();
     _locale_languages_common_controls($form, $language);
     $form['submit'] = array(
@@ -295,8 +299,8 @@ function _locale_languages_common_contro
  */
 function locale_languages_predefined_form_validate($form, &$form_state) {
   $langcode = $form_state['values']['langcode'];
-
-  if ($duplicate = db_result(db_query("SELECT COUNT(*) FROM {languages} WHERE language = '%s'", $langcode)) != 0) {
+  $duplicate = db_query("SELECT COUNT(*) FROM {languages} WHERE language = :language", array(':language' => $langcode))->fetchField();
+  if ($duplicate != 0) {
     form_set_error('langcode', t('The language %language (%code) already exists.', array('%language' => $form_state['values']['name'], '%code' => $langcode)));
   }
 
@@ -347,13 +351,17 @@ function locale_languages_edit_form_vali
   if (!empty($form_state['values']['domain']) && !empty($form_state['values']['prefix'])) {
     form_set_error('prefix', t('Domain and path prefix values should not be set at the same time.'));
   }
-  if (!empty($form_state['values']['domain']) && $duplicate = db_fetch_object(db_query("SELECT language FROM {languages} WHERE domain = '%s' AND language <> '%s'", $form_state['values']['domain'], $form_state['values']['langcode']))) {
+
+  $duplicate = db_query("SELECT COUNT(language) FROM {languages} WHERE domain = :domain AND language <> :language", array(':domain' => $form_state['values']['domain'], ':language' => $form_state['values']['langcode']))->fetchField();
+  if (!empty($form_state['values']['domain']) && $duplicate) {
     form_set_error('domain', t('The domain (%domain) is already tied to a language (%language).', array('%domain' => $form_state['values']['domain'], '%language' => $duplicate->language)));
   }
   if (empty($form_state['values']['prefix']) && language_default('language') != $form_state['values']['langcode'] && empty($form_state['values']['domain'])) {
     form_set_error('prefix', t('Only the default language can have both the domain and prefix empty.'));
   }
-  if (!empty($form_state['values']['prefix']) && $duplicate = db_fetch_object(db_query("SELECT language FROM {languages} WHERE prefix = '%s' AND language <> '%s'", $form_state['values']['prefix'], $form_state['values']['langcode']))) {
+
+  $duplicate = db_query("SELECT COUNT(language) FROM {languages} WHERE prefix = :prefix AND language <> :language", array(':prefix' => $form_state['values']['prefix'], ':language' => $form_state['values']['langcode']))->fetchField();
+  if (!empty($form_state['values']['prefix']) && $duplicate) {
     form_set_error('prefix', t('The prefix (%prefix) is already tied to a language (%language).', array('%prefix' => $form_state['values']['prefix'], '%language' => $duplicate->language)));
   }
 }
@@ -362,7 +370,16 @@ function locale_languages_edit_form_vali
  * Process the language editing form submission.
  */
 function locale_languages_edit_form_submit($form, &$form_state) {
-  db_query("UPDATE {languages} SET name = '%s', native = '%s', domain = '%s', prefix = '%s', direction = %d WHERE language = '%s'", $form_state['values']['name'], $form_state['values']['native'], $form_state['values']['domain'], $form_state['values']['prefix'], $form_state['values']['direction'], $form_state['values']['langcode']);
+  db_update('languages')
+    ->fields(array(
+               'name' => $form_state['values']['name'],
+               'native' => $form_state['values']['native'],
+               'domain' => $form_state['values']['domain'],
+               'prefix' => $form_state['values']['prefix'],
+               'direction' => $form_state['values']['direction'])
+             )
+    ->condition('language', $form_state['values']['langcode'])
+    ->execute();
   $default = language_default();
   if ($default->language == $form_state['values']['langcode']) {
     $properties = array('name', 'native', 'direction', 'enabled', 'plurals', 'formula', 'domain', 'prefix', 'weight');
@@ -420,13 +437,20 @@ function locale_languages_delete_form_su
   $languages = language_list();
   if (isset($languages[$form_state['values']['langcode']])) {
     // Remove translations first.
-    db_query("DELETE FROM {locales_target} WHERE language = '%s'", $form_state['values']['langcode']);
+    db_delete('locales_target')
+      ->condition('language', $form_state['values']['langcode'])
+      ->execute();
     cache_clear_all('locale:' . $form_state['values']['langcode'], 'cache');
     // With no translations, this removes existing JavaScript translations file.
     _locale_rebuild_js($form_state['values']['langcode']);
     // Remove the language.
-    db_query("DELETE FROM {languages} WHERE language = '%s'", $form_state['values']['langcode']);
-    db_query("UPDATE {node} SET language = '' WHERE language = '%s'", $form_state['values']['langcode']);
+    db_delete('languages')
+      ->condition('language', $form_state['values']['langcode'])
+      ->execute();
+    db_update('node')
+      ->fields(array('language' => ''))
+      ->condition('language', $form_state['values']['langcode'])
+      ->execute();
     $variables = array('%locale' => $languages[$form_state['values']['langcode']]->name);
     drupal_set_message(t('The language %locale has been removed.', $variables));
     watchdog('locale', 'The language %locale has been removed.', $variables);
@@ -500,7 +524,7 @@ function locale_translate_overview_scree
   // Collect summaries of all source strings in all groups.
   $sums = db_query("SELECT COUNT(*) AS strings, textgroup FROM {locales_source} GROUP BY textgroup");
   $groupsums = array();
-  while ($group = db_fetch_object($sums)) {
+  foreach ($sums as $group) {
     $groupsums[$group->textgroup] = $group->strings;
   }
 
@@ -515,7 +539,7 @@ function locale_translate_overview_scree
 
   // Languages with at least one record in the locale table.
   $translations = db_query("SELECT COUNT(*) AS translation, t.language, s.textgroup FROM {locales_source} s INNER JOIN {locales_target} t ON s.lid = t.lid GROUP BY textgroup, language");
-  while ($data = db_fetch_object($translations)) {
+  foreach ($translations as $data) {
     $ratio = (!empty($groupsums[$data->textgroup]) && $data->translation > 0) ? round(($data->translation/$groupsums[$data->textgroup])*100., 2) : 0;
     $rows[$data->language][$data->textgroup] = $data->translation . '/' . $groupsums[$data->textgroup] . " ($ratio%)";
   }
@@ -778,7 +802,7 @@ function locale_translate_export_po_form
  */
 function locale_translate_edit_form(&$form_state, $lid) {
   // Fetch source string, if possible.
-  $source = db_fetch_object(db_query('SELECT source, textgroup, location FROM {locales_source} WHERE lid = %d', $lid));
+  $source = db_query('SELECT source, textgroup, location FROM {locales_source} WHERE lid = %d', $lid)->fetchObject();
   if (!$source) {
     drupal_set_message(t('String not found.'), 'error');
     drupal_goto('admin/build/translate/search');
@@ -826,7 +850,7 @@ function locale_translate_edit_form(&$fo
 
   // Fetch translations and fill in default values in the form.
   $result = db_query("SELECT DISTINCT translation, language FROM {locales_target} WHERE lid = %d AND language <> '%s'", $lid, $omit);
-  while ($translation = db_fetch_object($result)) {
+  foreach ($result as $translation) {
     $form['translations'][$translation->language]['#default_value'] = $translation->translation;
   }
 
@@ -872,19 +896,30 @@ function locale_translate_edit_form_vali
 function locale_translate_edit_form_submit($form, &$form_state) {
   $lid = $form_state['values']['lid'];
   foreach ($form_state['values']['translations'] as $key => $value) {
-    $translation = db_result(db_query("SELECT translation FROM {locales_target} WHERE lid = %d AND language = '%s'", $lid, $key));
+    $translation = db_query("SELECT translation FROM {locales_target} WHERE lid = :lid AND language = :language", array(
+                              ':lid' => $lid,
+                              ':language' => $key))->fetchField();
     if (!empty($value)) {
       // Only update or insert if we have a value to use.
       if (!empty($translation)) {
-        db_query("UPDATE {locales_target} SET translation = '%s' WHERE lid = %d AND language = '%s'", $value, $lid, $key);
+        db_update('locales_target')
+          ->fields(array('translation' => $value))
+          ->condition('lid', $lid)
+          ->condition('language', $key)
+          ->execute();
       }
       else {
-        db_query("INSERT INTO {locales_target} (lid, translation, language) VALUES (%d, '%s', '%s')", $lid, $value, $key);
+        db_insert('locales_target')
+          ->fields(array('lid' => $lid, 'translation' => $value, 'language' => $key))
+          ->execute();
       }
     }
     elseif (!empty($translation)) {
       // Empty translation entered: remove existing entry from database.
-      db_query("DELETE FROM {locales_target} WHERE lid = %d AND language = '%s'", $lid, $key);
+      db_delete('locales_target')
+        ->condition('lid', $lid)
+        ->condition('language', $key)
+        ->execute();
     }
 
     // Force JavaScript translation file recreation for this language.
@@ -933,8 +968,12 @@ function locale_translate_delete_form(&$
  * Process string deletion submissions.
  */
 function locale_translate_delete_form_submit($form, &$form_state) {
-  db_query('DELETE FROM {locales_source} WHERE lid = %d', $form_state['values']['lid']);
-  db_query('DELETE FROM {locales_target} WHERE lid = %d', $form_state['values']['lid']);
+  db_delete('locales_source')
+    ->condition('lid', $form_state['values']['lid'])
+    ->execute();
+  db_delete('locales_target')
+    ->condition('lid', $form_state['values']['lid'])
+    ->execute();
   // Force JavaScript translation file recreation for all languages.
   _locale_invalidate_js();
   cache_clear_all('locale:', 'cache', TRUE);
@@ -986,7 +1025,9 @@ function locale_add_language($langcode, 
     $direction = isset($predefined[$langcode][2]) ? $predefined[$langcode][2] : LANGUAGE_LTR;
   }
 
-  db_query("INSERT INTO {languages} (language, name, native, direction, domain, prefix, enabled) VALUES ('%s', '%s', '%s', %d, '%s', '%s', %d)", $langcode, $name, $native, $direction, $domain, $prefix, $enabled);
+  db_insert('languages')
+    ->fields(array('language' => $langcode, 'name' => $name, 'native' => $native, 'direction' => $direction, 'domain' => $domain, 'prefix' => $prefix, 'enabled' => $enabled))
+    ->execute();
 
   // Only set it as default if enabled.
   if ($enabled && $default) {
@@ -1031,7 +1072,8 @@ function _locale_import_po($file, $langc
   }
 
   // Check if we have the language already in the database.
-  if (!db_fetch_object(db_query("SELECT language FROM {languages} WHERE language = '%s'", $langcode))) {
+  $language = db_query("SELECT COUNT(language) FROM {languages} WHERE language = :language", array(':language' => $langcode))->fetchField();
+  if (!$langcode) {
     drupal_set_message(t('The language selected for import is not supported.'), 'error');
     return FALSE;
   }
@@ -1280,10 +1322,17 @@ function _locale_import_one_string($op, 
         // Get the plural formula and update in database.
         if (isset($header["Plural-Forms"]) && $p = _locale_import_parse_plural_forms($header["Plural-Forms"], $file->filename)) {
           list($nplurals, $plural) = $p;
-          db_query("UPDATE {languages} SET plurals = %d, formula = '%s' WHERE language = '%s'", $nplurals, $plural, $lang);
+          db_update('languages')
+            ->fields(array(
+                       'plurals' => $nplurals, 'formula' => $plural))
+            ->condition('language', $lang)
+            ->execute();
         }
         else {
-          db_query("UPDATE {languages} SET plurals = %d, formula = '%s' WHERE language = '%s'", 0, '', $lang);
+          db_update('languages')
+            ->fields(array('plurals' => 0, 'formula' => ''))
+            ->condition('language', $lang)
+            ->execute();
         }
         $headerdone = TRUE;
       }
@@ -1344,8 +1393,8 @@ function _locale_import_one_string($op, 
  * @return
  *   The string ID of the existing string modified or the new string added.
  */
-function _locale_import_one_string_db(&$report, $langcode, $source, $translation, $textgroup, $location, $mode, $plid = NULL, $plural = NULL) {
-  $lid = db_result(db_query("SELECT lid FROM {locales_source} WHERE source = '%s' AND textgroup = '%s'", $source, $textgroup));
+function _locale_import_one_string_db(&$report, $langcode, $source, $translation, $textgroup, $location, $mode, $plid = 0, $plural = 0) {
+  $lid = db_query("SELECT lid FROM {locales_source} WHERE source = :source AND textgroup = :textgroup", array(':source' => $source, ':textgroup' => $textgroup))->fetchField();
 
   if (!empty($translation)) {
     // Skip this string unless it passes a check for dangerous code.
@@ -1355,30 +1404,60 @@ function _locale_import_one_string_db(&$
     }
     elseif ($lid) {
       // We have this source string saved already.
-      db_query("UPDATE {locales_source} SET location = '%s' WHERE lid = %d", $location, $lid);
-      $exists = (bool) db_result(db_query("SELECT lid FROM {locales_target} WHERE lid = %d AND language = '%s'", $lid, $langcode));
+      db_update('locales_source')
+        ->fields(array('location' => $location))
+        ->condition('lid', $lid)
+        ->execute();
+      $exists = db_query("SELECT COUNT(lid) FROM {locales_target} WHERE lid = :lid AND language = :language", array(':lid' => $lid, ':language' => $langcode))->fetchField();
       if (!$exists) {
         // No translation in this language.
-        db_query("INSERT INTO {locales_target} (lid, language, translation, plid, plural) VALUES (%d, '%s', '%s', %d, %d)", $lid, $langcode, $translation, $plid, $plural);
+        db_insert('locales_target')
+          ->fields(array(
+                     'lid' => $lid,
+                     'language' => $langcode,
+                     'translation' => $translation,
+                     'plid' => $plid,
+                     'plural' => $plural))
+          ->execute();
         $report['additions']++;
       }
       elseif ($mode == LOCALE_IMPORT_OVERWRITE) {
         // Translation exists, only overwrite if instructed.
-        db_query("UPDATE {locales_target} SET translation = '%s', plid = %d, plural = %d WHERE language = '%s' AND lid = %d", $translation, $plid, $plural, $langcode, $lid);
+        db_update('locales_target')
+          ->fields(array('translation' => $translation, 'plid' => $plid, 'plural' => $plural))
+          ->condition('language', $langcode)
+          ->condition('lid', $lid)
+          ->execute();
         $report['updates']++;
       }
     }
     else {
       // No such source string in the database yet.
-      db_query("INSERT INTO {locales_source} (location, source, textgroup) VALUES ('%s', '%s', '%s')", $location, $source, $textgroup);
-      $lid = db_result(db_query("SELECT lid FROM {locales_source} WHERE source = '%s' AND textgroup = '%s'", $source, $textgroup));
-      db_query("INSERT INTO {locales_target} (lid, language, translation, plid, plural) VALUES (%d, '%s', '%s', %d, %d)", $lid, $langcode, $translation, $plid, $plural);
+      db_insert('locales_source')
+        ->fields(array('location' => $location, 'source' => $source, 'textgroup' => $textgroup))
+        ->execute();
+      $lid = db_query("SELECT lid FROM {locales_source} WHERE source = :source AND textgroup = :textgroup", array(
+                        ':source' => $source, ':textgroup' => $textgroup))
+       ->fetchField();
+      db_insert('locales_target')
+        ->fields(array(
+                   'lid' => $lid,
+                   'language' => $langcode,
+                   'translation' => $translation,
+                   'plid' => $plid,
+                   'plural' => $plural))
+      ->execute();
       $report['additions']++;
     }
   }
   elseif ($mode == LOCALE_IMPORT_OVERWRITE) {
     // Empty translation, remove existing if instructed.
-    db_query("DELETE FROM {locales_target} WHERE language = '%s' AND lid = %d AND plid = %d AND plural = %d", $translation, $langcode, $lid, $plid, $plural);
+    db_delete('locales_target')
+      ->condition('language', $langcode)
+      ->condition('lid', $lid)
+      ->condition('plid', $plid)
+      ->condition('plural', $plural)
+      ->execute();
     $report['deletes']++;
   }
 
@@ -1714,8 +1793,8 @@ function _locale_parse_js_file($filepath
       // Remove the quotes and string concatenations from the string.
       $string = implode('', preg_split('~(?<!\\\\)[\'"]\s*\+\s*[\'"]~s', substr($string, 1, -1)));
 
-      $result = db_query("SELECT lid, location FROM {locales_source} WHERE source = '%s' AND textgroup = 'default'", $string);
-      if ($source = db_fetch_object($result)) {
+      $source = db_query("SELECT lid, location FROM {locales_source} WHERE source = :source AND textgroup = 'default'", array(':source' => $string))->fetchObject();
+      if ($source) {
         // We already have this source string and now have to add the location
         // to the location column, if this file is not yet present in there.
         $locations = preg_split('~\s*;\s*~', $source->location);
@@ -1725,12 +1804,17 @@ function _locale_parse_js_file($filepath
           $locations = implode('; ', $locations);
 
           // Save the new locations string to the database.
-          db_query("UPDATE {locales_source} SET location = '%s' WHERE lid = %d", $locations, $source->lid);
+          db_update('locales_source')
+            ->fields(array('location' => $locations))
+            ->condition('lid', $source->lid)
+            ->execute();
         }
       }
       else {
         // We don't have the source string yet, thus we insert it into the database.
-        db_query("INSERT INTO {locales_source} (location, source, textgroup) VALUES ('%s', '%s', 'default')", $filepath, $string);
+        db_insert('locales_source')
+          ->fields(array('location' => $filepath, 'source' => $string, 'textgroup' => 'default'))
+          ->execute();
       }
     }
   }
@@ -1754,13 +1838,13 @@ function _locale_parse_js_file($filepath
  */
 function _locale_export_get_strings($language = NULL, $group = 'default') {
   if (isset($language)) {
-    $result = db_query("SELECT s.lid, s.source, s.location, t.translation, t.plid, t.plural FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid AND t.language = '%s' WHERE s.textgroup = '%s' ORDER BY t.plid, t.plural", $language->language, $group);
+    $result = db_query("SELECT s.lid, s.source, s.location, t.translation, t.plid, t.plural FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid AND t.language = :language WHERE s.textgroup = :textgroup ORDER BY t.plid, t.plural", array(':language' => $language->language, ':textgroup' => $group));
   }
   else {
-    $result = db_query("SELECT s.lid, s.source, s.location, t.plid, t.plural FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid WHERE s.textgroup = '%s' ORDER BY t.plid, t.plural", $group);
+    $result = db_query("SELECT s.lid, s.source, s.location, t.plid, t.plural FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid WHERE s.textgroup = :textgroup ORDER BY t.plid, t.plural", array(':textgroup' => $group));
   }
   $strings = array();
-  while ($child = db_fetch_object($result)) {
+  foreach ($result as $child) {
     $string = array(
       'comment'     => $child->location,
       'source'      => $child->source,
@@ -2041,7 +2125,7 @@ function _locale_translate_seek() {
     $groups = module_invoke_all('locale', 'groups');
     $header = array(t('Text group'), t('String'), ($limit_language) ? t('Language') : t('Languages'), array('data' => t('Operations'), 'colspan' => '2'));
     $arr = array();
-    while ($locale = db_fetch_object($result)) {
+    foreach ($result as $locale) {
       $arr[$locale->lid]['group'] = $groups[$locale->textgroup];
       $arr[$locale->lid]['languages'][$locale->language] = $locale->translation;
       $arr[$locale->lid]['location'] = $locale->location;
@@ -2140,15 +2224,10 @@ function _locale_rebuild_js($langcode = 
 
   // Construct the array for JavaScript translations.
   // We sort on plural so that we have all plural forms before singular forms.
-  $result = db_query("SELECT s.lid, s.source, t.plid, t.plural, t.translation
-    FROM {locales_source} s
-      LEFT JOIN {locales_target} t ON s.lid = t.lid AND t.language = :language
-    WHERE s.location LIKE '%.js%'
-      AND s.textgroup = 'default'
-    ORDER BY t.plural DESC", array(':language' => $language->language));
+  $result = db_query("SELECT s.lid, s.source, t.plid, t.plural, t.translation FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid AND t.language = :language WHERE s.location LIKE '%.js%' AND s.textgroup = :textgroup ORDER BY t.plural DESC", array(':language' => $language->language, ':textgroup' => 'default'));
 
   $translations = $plurals = array();
-  while ($data = db_fetch_object($result)) {
+  foreach ($result as $data) {
     // Only add this to the translations array when there is actually a translation.
     if (!empty($data->translation)) {
       if ($data->plural) {
@@ -2220,14 +2299,17 @@ function _locale_rebuild_js($langcode = 
   // Save the new JavaScript hash (or an empty value if the file
   // just got deleted). Act only if some operation was executed.
   if ($status) {
-    db_query("UPDATE {languages} SET javascript = '%s' WHERE language = '%s'", $language->javascript, $language->language);
+    db_update('languages')
+      ->field(array('javascript' => $language->javascript))
+      ->condition('language', $language->language)
+      ->execute();
 
     // Update the default language variable if the default language has been altered.
     // This is necessary to keep the variable consistent with the database
     // version of the language and to prevent checking against an outdated hash.
     $default_langcode = language_default('language');
     if ($default_langcode == $language->language) {
-      $default = db_fetch_object(db_query("SELECT * FROM {languages} WHERE language = '%s'", $default_langcode));
+      $default = db_query("SELECT * FROM {languages} WHERE language = :language", array(':language' => $default_langcode))->fetchObject();
       variable_set('language_default', $default);
     }
   }
@@ -2517,12 +2599,12 @@ function locale_batch_by_language($langc
   // Collect all files to import for all enabled modules and themes.
   $files = array();
   $components = array();
-  $query = "SELECT name, filename FROM {system} WHERE status = 1";
+  $query = db_select('system')->fields('system', array('name', 'filename'))->condition('status', 1);
   if (count($skip)) {
-    $query .= " AND name NOT IN (" . db_placeholders($skip, 'varchar') . ")";
+    $query->condition('name', $skip, 'NOT IN');
   }
-  $result = db_query($query, $skip);
-  while ($component = db_fetch_object($result)) {
+  $result = $query->execute();
+  foreach ($result as $component) {
     // Collect all files for all components, names as $langcode.po or
     // with names ending with $langcode.po. This allows for filenames
     // like node-module.de.po to let translators use small files and
@@ -2553,7 +2635,7 @@ function locale_batch_by_component($comp
     $language_list = join('|', array_keys($languages[1]));
     // Collect all files to import for all $components.
     $result = db_query("SELECT name, filename FROM {system} WHERE status = 1");
-    while ($component = db_fetch_object($result)) {
+    foreach ($result as $component) {
       if (in_array($component->name, $components)) {
         // Collect all files for this component in all enabled languages, named
         // as $langcode.po or with names ending with $langcode.po. This allows
