blob: db8946599600d5d50e0b9f218d838259ab3106e5 [file] [log] [blame]
atoulmee45629f2009-01-21 08:52:55 +00001<?php
2/*******************************************************************************
3 * Copyright (c) 2009 Intalio, Inc.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Antoine Toulme, Intalio Inc. bug 260493: Provide a CSV export and import script
11*******************************************************************************/
12
13if ($argc != 4) {
14 $help = <<<HELP
15Copyright (c) 2009 Intalio, Inc.
16Usage: import_from_csv.php language release_train csv_file
17
18 language: the code of the language to use. See the languages supported by Babel.
19
20 release_train: a release train name.
21
22 csv_file: the path to the csv_file. The path should either be relative to the php file or absolute.
23 The CSV file should be comma separated. It should have this structure: "translation_key", "translation_english_value", "translation_value"
24
25 Example:
26 import_from_csv.php fr_CA europa some_translations_fr.csv
27
28 Authors:
29 This script was written by the Babel project. Please see http://www.eclipse.org/babel for more information.
30
31
32HELP;
33 echo $help;
34 exit;
35}
36
37error_reporting(E_ALL); ini_set("display_errors", true);
38
atoulmec099a0f2009-01-23 17:47:42 +000039
atoulmee45629f2009-01-21 08:52:55 +000040ini_set("memory_limit", "256M");
atoulme3ac52612009-02-02 13:14:39 +000041require_once(dirname(__FILE__) . "/../system/backend_functions.php");
atoulme12882d52009-01-26 18:39:17 +000042require_once(dirname(__FILE__) . "/../system/dbconnection.class.php");
43require_once(dirname(__FILE__) . "/../system/feature.class.php");
44require_once(dirname(__FILE__) . "/../system/user.class.php");
atoulme3e5e9342009-01-23 17:34:30 +000045
atoulmee45629f2009-01-21 08:52:55 +000046$dbc = new DBConnection();
47$dbh = $dbc->connect();
48
49//translations coming from a CSV file are considered to come from professional translators that tested their work in context, and/or in relation with the developers
50// change the variable below if you have doubts on the translation quality.
51$fuzzy = 0;
52
53//user that will be running the translations.
54$USER = getGenieUser()->userid;
55
56$language = $argv[1];
57$release_train_id = $argv[2];
58$csv_file = $argv[3];
59
60$sql = "select language_id from languages where iso_code = '" . addslashes($language) ."'";
kitlo1d027092018-04-19 17:44:07 -040061$lrow = mysqli_fetch_assoc(mysqli_query($dbh, $sql));
atoulmee45629f2009-01-21 08:52:55 +000062if (!$lrow) {
63 echo "This language code is not supported by Babel. Please see the Babel documentation for more information";
64 exit;
65}
66$language_id = $lrow['language_id'];
67
68
69$handle = fopen($csv_file, "r");
70while (($data = fgetcsv($handle)) !== FALSE) {
71 $sql = <<<SQL
droy68864612009-04-14 15:44:05 +000072SELECT s.string_id FROM files AS f INNER JOIN strings AS s ON f.file_id = s.file_id INNER JOIN release_train_projects as v ON (f.project_id = v.project_id AND f.version = v.version) WHERE f.is_active AND s.non_translatable <> 1 AND s.name = '$data[0]' AND s.value = BINARY '$data[1]' AND v.train_id = '$release_train_id'
atoulmee45629f2009-01-21 08:52:55 +000073
74SQL;
kitlo1d027092018-04-19 17:44:07 -040075 $values = mysqli_query($dbh, $sql);
76 $value_row = mysqli_fetch_assoc($values);
atoulmee45629f2009-01-21 08:52:55 +000077 if (!$value_row) {
78 echo "Could not find the matching record for $data[0] with a value of $data[1]";
79 continue;
80 }
81 $string_id = $value_row['string_id'];
82
83 $sql = "select possibly_incorrect from translations where string_id = $string_id and language_id = $language_id";
kitlo1d027092018-04-19 17:44:07 -040084 $tr_row = mysqli_fetch_assoc(mysqli_query($dbh, $sql));
atoulmee45629f2009-01-21 08:52:55 +000085 if ($tr_row) {
86 if ($fuzzy == 1) {
87 if ($tr_row['possibly_incorrect'] == 1) {
88 // replacing a fuzzy translation by a new fuzzy translation. Well.
89 } else {
90 //replacing a non-fuzzy translation by a fuzzy translation: no, thank you
91 echo "The entry " . $data[0] . " is already translated in a non-fuzzy way. Aborting";
92 continue;
93 }
94 } else {
95 // we are not fuzzy, for now let's assume it's ok to override non-fuzzy translations when yours aren't either.
96 }
97 $query = "UPDATE translations set is_active = 0 where string_id = " . $string_id . " and language_id = '" . $language_id . "'";
kitlo1d027092018-04-19 17:44:07 -040098 mysqli_query($dbh, $query);
atoulmee45629f2009-01-21 08:52:55 +000099 }
100 $query = "INSERT INTO translations(string_id, language_id, value, userid, created_on, possibly_incorrect) values('". addslashes($string_id) ."','". addslashes($language_id) ."','" . addslashes($data[2]) . "', '". addslashes($USER) ."', NOW(), $fuzzy)";
kitlo1d027092018-04-19 17:44:07 -0400101 mysqli_query($dbh, $query);
atoulmee45629f2009-01-21 08:52:55 +0000102 echo "Added translation \"$data[2]\" for entry '$data[0]'\n";
103}
104fclose($handle);
105echo "\n\nDone.\n";
106
107?>