blob: c45a2949ce781d89b7acc490f78be1e87e16fac5 [file] [log] [blame]
gobrien48275462007-11-29 21:29:17 +00001<?php
2/*******************************************************************************
3 * Copyright (c) 2007 Eclipse Foundation and others.
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 * Paul Colton (Aptana)- initial API and implementation
11 * Eclipse Foundation
droy68ee4942008-02-04 21:24:28 +000012 * Eclipse Contributors (bug 217257)
syoshida4d3138f2015-10-12 17:25:55 +090013 * Satoru Yoshida - [373204] Babel server getting slow
gobrien48275462007-11-29 21:29:17 +000014*******************************************************************************/
15
16require_once("cb_global.php");
17
gobrien1bc0e192008-01-31 15:43:41 +000018
atoulme3e5e9342009-01-23 17:34:30 +000019$string_id = getHTTPParameter("string_id", "POST");
20$translation = getHTTPParameter("translation", "POST");
21$fuzzy_state = getHTTPParameter("fuzzy", "POST");
gobrien1c01c752008-01-10 18:34:31 +000022
23$language_id = $_SESSION["language"];
24$project_id = $_SESSION['project'];
gobrien755df672008-01-18 01:01:33 +000025$version = $_SESSION["version"];
26
gobrien48275462007-11-29 21:29:17 +000027$user_id = $User->userid;
28
droy2da63a12008-03-27 18:13:05 +000029# TODO: refactor these ifs
30$do_nothing = false;
31
droyc62694d2008-05-14 18:51:30 +000032$affected_rows = 0;
33
droy68ee4942008-02-04 21:24:28 +000034if (empty($translation) || (trim($translation) == '')) {
35
droy2da63a12008-03-27 18:13:05 +000036 $do_nothing = true;
droy68ee4942008-02-04 21:24:28 +000037
38} else if($_POST['translate_action'] != "all"){
gobrien34a08cb2008-02-06 18:44:48 +000039 $query = "update
40 translations
gobriend6da6972008-01-26 00:28:20 +000041 set
gobrien34a08cb2008-02-06 18:44:48 +000042 is_active = 0
43 where
44 string_id = '".addslashes($string_id)."'
45 and
46 language_id = '".addslashes($language_id)."'
47 and
48 is_active = 1
49 ";
50 $res = mysql_query($query,$dbh);
51
52 $query = "insert into
53 translations
54 set
55 string_id = '".addslashes($string_id)."',
56 language_id = '".addslashes($language_id)."',
57 value = '".addslashes($translation)."',
58 userid = '".addslashes($user_id)."',
droye3dc1112008-11-19 18:21:28 +000059 possibly_incorrect = '".addslashes($fuzzy_state)."',
gobrien34a08cb2008-02-06 18:44:48 +000060 created_on = NOW()
61 ";
62 $res = mysql_query($query,$dbh);
droyc62694d2008-05-14 18:51:30 +000063 $affected_rows += mysql_affected_rows();
gobrienc5749b72008-02-28 21:43:51 +000064
gobrien198300d2008-03-04 23:22:36 +000065// print $query;
gobriend6da6972008-01-26 00:28:20 +000066}else{
gobrien34a08cb2008-02-06 18:44:48 +000067 //FIND ALL STRINGS THAT ARE THE SAME ACROSS VERSIONS
gobriend6da6972008-01-26 00:28:20 +000068 $query = "select
droya102afc2008-05-21 19:15:42 +000069 s.string_id
gobriend6da6972008-01-26 00:28:20 +000070 from
droya102afc2008-05-21 19:15:42 +000071 strings as s
72 INNER JOIN files AS f on s.file_id = f.file_id
73 INNER JOIN files AS the_file_selected_for_translation on the_file_selected_for_translation.file_id = (select file_id from strings where string_id = '".addslashes($string_id)."')
gobriend6da6972008-01-26 00:28:20 +000074 where
droya102afc2008-05-21 19:15:42 +000075 s.value = (select value from strings where string_id = '".addslashes($string_id)."')
gobriend6da6972008-01-26 00:28:20 +000076 and
droya102afc2008-05-21 19:15:42 +000077 s.name = (select name from strings where string_id = '".addslashes($string_id)."')
78 and f.name = the_file_selected_for_translation.name
79 and f.project_id = the_file_selected_for_translation.project_id
80 and s.is_active = 1";
gobriend6da6972008-01-26 00:28:20 +000081
82 $res = mysql_query($query,$dbh);
gobriend6da6972008-01-26 00:28:20 +000083 while($row = mysql_fetch_assoc($res)){
84 $string_ids[] = $row['string_id'];
85 }
86
87 //GET CURRENT TRANSLATION FOR THIS STRING
droya102afc2008-05-21 19:15:42 +000088 $query= "select value from translations where string_id = '".addslashes($string_id)."' and language_id = '".addslashes($language_id)."' and is_active = 1 order by version limit 1";
gobriend6da6972008-01-26 00:28:20 +000089 $res = mysql_query($query,$dbh);
90 $string_translation = "";
91 while($row = mysql_fetch_assoc($res)){
92 $string_translation = $row['value'];
93 }
94
95 //GET ALL STRINGS WITH SAME TRANSLATIONS
96 if($string_translation){
97 $query = "
98 select
gobrien70d66642008-02-06 18:50:40 +000099 translation_id,string_id,language_id
gobriend6da6972008-01-26 00:28:20 +0000100 from
101 translations
102 where
103 string_id in (".addslashes(implode(',',$string_ids)).")
104 and
105 value = '".addslashes($string_translation)."'
106 and
107 is_active = 1
droya102afc2008-05-21 19:15:42 +0000108 and language_id = '" . addslashes($language_id)."'
gobriend6da6972008-01-26 00:28:20 +0000109 ";
gobrien755df672008-01-18 01:01:33 +0000110
gobriend6da6972008-01-26 00:28:20 +0000111 $res = mysql_query($query,$dbh);
112 while($row = mysql_fetch_assoc($res)){
113 //DE-ACTIVATE ALL OLD TRANSLATIONS
114 $query = "update translations set is_active = 0 where translation_id = '".addslashes($row['translation_id'])."'";
115 $res2 = mysql_query($query,$dbh);
116
117 //INSERT NEW TRANSLATIONS
118 $query = "insert into
119 translations
120 set
121 string_id = '".addslashes($row['string_id'])."',
gobrien70d66642008-02-06 18:50:40 +0000122 language_id = '".addslashes($row['language_id'])."' ,
gobriend6da6972008-01-26 00:28:20 +0000123 value = '".addslashes($translation)."',
124 userid = '".addslashes($user_id)."',
droye3dc1112008-11-19 18:21:28 +0000125 possibly_incorrect = '".addslashes($fuzzy_state)."',
gobriend6da6972008-01-26 00:28:20 +0000126 created_on = NOW()
127 ";
128 $res2 = mysql_query($query,$dbh);
droyc62694d2008-05-14 18:51:30 +0000129 $affected_rows += mysql_affected_rows();
gobriend6da6972008-01-26 00:28:20 +0000130
131 }
gobrien755df672008-01-18 01:01:33 +0000132
gobriend6da6972008-01-26 00:28:20 +0000133 }else{
134 $query = "
135 select
136 strings.string_id
137 from
138 strings
139 left join
140 translations
141 on
142 strings.string_id = translations.string_id
143 and
144 translations.value is NULL
145 where
146 strings.string_id in (".addslashes(implode(',',$string_ids)).")
147 ";
gobrien755df672008-01-18 01:01:33 +0000148
149 $res = mysql_query($query,$dbh);
150
gobriend6da6972008-01-26 00:28:20 +0000151 while($row = mysql_fetch_assoc($res)){
152 $translation_ids[] = $row['string_id'];
153 //INSERT NEW TRANSLATIONS
154 $query = "insert into
155 translations
156 set
157 string_id = '".addslashes($row['string_id'])."',
158 language_id = '".addslashes($language)."' ,
159 value = '".addslashes($translation)."',
droye3dc1112008-11-19 18:21:28 +0000160 possibly_incorrect = '".addslashes($fuzzy_state)."',
gobriend6da6972008-01-26 00:28:20 +0000161 userid = '".addslashes($user_id)."',
162 created_on = NOW()
163 ";
164 $res2 = mysql_query($query,$dbh);
droyc62694d2008-05-14 18:51:30 +0000165 $affected_rows += mysql_affected_rows();
droy2da63a12008-03-27 18:13:05 +0000166 }
167 }
gobriend6da6972008-01-26 00:28:20 +0000168}
droy2da63a12008-03-27 18:13:05 +0000169
170if(!$do_nothing) {
171 # Find all string_id's that have the same binary value as the one we're translating
172 # *and* have no translation yet, and update those too.
droye3dc1112008-11-19 18:21:28 +0000173 if(!$fuzzy_state) {
174 $sql = "SELECT s.string_id, COUNT(t.string_id) AS tr_count
175 FROM strings AS s
176 LEFT JOIN translations AS t ON t.string_id = s.string_id AND t.language_id = '".addslashes($language_id)."'
syoshida4d3138f2015-10-12 17:25:55 +0900177 WHERE s.value = BINARY (select value from strings where string_id = '".addslashes($string_id)."')
droye3dc1112008-11-19 18:21:28 +0000178 AND s.is_active = 1 AND t.value IS NULL GROUP BY s.string_id HAVING tr_count = 0";
droy2da63a12008-03-27 18:13:05 +0000179
droye3dc1112008-11-19 18:21:28 +0000180 $res = mysql_query($sql, $dbh);
181 $str_count = mysql_affected_rows();
droy2da63a12008-03-27 18:13:05 +0000182
droye3dc1112008-11-19 18:21:28 +0000183 while($myrow = mysql_fetch_assoc($res)) {
184 $sql = "insert into
185 translations
186 set
187 string_id = " . $myrow['string_id'] . ",
188 language_id = '".addslashes($language_id)."',
189 value = '".addslashes($translation)."',
190 userid = '".addslashes($user_id)."',
191 created_on = NOW()";
192 mysql_query($sql, $dbh);
193 $affected_rows += mysql_affected_rows();
194 }
droy2da63a12008-03-27 18:13:05 +0000195 }
196}
gobrien5239b3c2008-06-17 06:51:12 +0000197
198$response['translationString'] = htmlspecialchars($translation);
199
200$response['translationArea'] = "<br /><br /><br />
droy749bf842008-07-17 16:10:42 +0000201<center><b>Translated $affected_rows string". ($affected_rows > 1 || $affected_rows == 0 ? "s" : "" ) ." across all Babel projects.
gobrien5239b3c2008-06-17 06:51:12 +0000202</b></center>";
203
204print json_encode($response);
205
206?>