blob: 590e06a65465b06dfcc342ef475964997320c4e2 [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 ";
kitlo2c8d8a92018-04-19 13:25:09 -040050 $res = mysqli_query($dbh, $query);
gobrien34a08cb2008-02-06 18:44:48 +000051
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 ";
kitlo2c8d8a92018-04-19 13:25:09 -040062 $res = mysqli_query($dbh, $query);
kitlo1d027092018-04-19 17:44:07 -040063 $affected_rows += mysqli_affected_rows($dbh);
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
kitlo2c8d8a92018-04-19 13:25:09 -040082 $res = mysqli_query($dbh, $query);
kitlo1d027092018-04-19 17:44:07 -040083 while($row = mysqli_fetch_assoc($res)){
gobriend6da6972008-01-26 00:28:20 +000084 $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";
kitlo2c8d8a92018-04-19 13:25:09 -040089 $res = mysqli_query($dbh, $query);
gobriend6da6972008-01-26 00:28:20 +000090 $string_translation = "";
kitlo1d027092018-04-19 17:44:07 -040091 while($row = mysqli_fetch_assoc($res)){
gobriend6da6972008-01-26 00:28:20 +000092 $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
kitlo2c8d8a92018-04-19 13:25:09 -0400111 $res = mysqli_query($dbh, $query);
kitlo1d027092018-04-19 17:44:07 -0400112 while($row = mysqli_fetch_assoc($res)){
gobriend6da6972008-01-26 00:28:20 +0000113 //DE-ACTIVATE ALL OLD TRANSLATIONS
114 $query = "update translations set is_active = 0 where translation_id = '".addslashes($row['translation_id'])."'";
kitlo2c8d8a92018-04-19 13:25:09 -0400115 $res2 = mysqli_query($dbh, $query);
gobriend6da6972008-01-26 00:28:20 +0000116
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 ";
kitlo2c8d8a92018-04-19 13:25:09 -0400128 $res2 = mysqli_query($dbh, $query);
kitlo1d027092018-04-19 17:44:07 -0400129 $affected_rows += mysqli_affected_rows($dbh);
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
kitlo2c8d8a92018-04-19 13:25:09 -0400149 $res = mysqli_query($dbh, $query);
gobrien755df672008-01-18 01:01:33 +0000150
kitlo1d027092018-04-19 17:44:07 -0400151 while($row = mysqli_fetch_assoc($res)){
gobriend6da6972008-01-26 00:28:20 +0000152 $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 ";
kitlo2c8d8a92018-04-19 13:25:09 -0400164 $res2 = mysqli_query($dbh, $query);
kitlo1d027092018-04-19 17:44:07 -0400165 $affected_rows += mysqli_affected_rows($dbh);
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
kitlo2c8d8a92018-04-19 13:25:09 -0400180 $res = mysqli_query($dbh, $sql);
kitlo1d027092018-04-19 17:44:07 -0400181 $str_count = mysqli_affected_rows($dbh);
droy2da63a12008-03-27 18:13:05 +0000182
kitlo1d027092018-04-19 17:44:07 -0400183 while($myrow = mysqli_fetch_assoc($res)) {
droye3dc1112008-11-19 18:21:28 +0000184 $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()";
kitlo2c8d8a92018-04-19 13:25:09 -0400192 mysqli_query($dbh, $sql);
kitlo1d027092018-04-19 17:44:07 -0400193 $affected_rows += mysqli_affected_rows($dbh);
droye3dc1112008-11-19 18:21:28 +0000194 }
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?>