blob: 9eb0032bb5e07b314b76d911f069d09ef0790d40 [file] [log] [blame]
droy81777472007-11-28 21:35:58 +00001<?php
2/*******************************************************************************
droyb9c2cb12008-10-08 13:58:14 +00003 * Copyright (c) 2007,2008 Eclipse Foundation and others.
droy81777472007-11-28 21:35:58 +00004 * 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 * Eclipse Foundation - initial API and implementation
droyb9c2cb12008-10-08 13:58:14 +000011 * Antoine Toulmé - Bug 248917
droy81777472007-11-28 21:35:58 +000012*******************************************************************************/
atoulme425b6c82008-11-24 14:00:27 +000013require(BABEL_BASE_DIR . "classes/system/language.class.php");
14require(BABEL_BASE_DIR . "classes/system/release_train.class.php");
15
droy81777472007-11-28 21:35:58 +000016require(BABEL_BASE_DIR . "classes/string/string.class.php");
17
18class File {
19 public $errStrs;
20
21 public $file_id = 0;
22 public $project_id = '';
droy3bb0c962008-01-17 20:45:01 +000023 public $version = '';
droy81777472007-11-28 21:35:58 +000024 public $name = '';
25 public $is_active = 0;
droyb9c2cb12008-10-08 13:58:14 +000026 public $plugin_id = '';
droy81777472007-11-28 21:35:58 +000027
28
29 function save() {
30 $rValue = false;
droyaead6582008-02-25 19:47:15 +000031 if($this->name != "" && $this->project_id != "" && $this->version != "") {
droy81777472007-11-28 21:35:58 +000032 global $App, $dbh;
33
droy81777472007-11-28 21:35:58 +000034 if($this->file_id == 0) {
droy3bb0c962008-01-17 20:45:01 +000035 $this->file_id = $this->getFileID($this->name, $this->project_id, $this->version);
droy81777472007-11-28 21:35:58 +000036 }
37
38 $sql = "INSERT INTO";
39 $where = "";
40 if($this->file_id > 0) {
41 $sql = "UPDATE";
42 $where = " WHERE file_id = " . $App->sqlSanitize($this->file_id, $dbh);
43 }
44
droy3e7f3a82007-11-29 19:35:51 +000045 $Event = new EventLog("files", "file_id", $this->file_id, $sql);
46
droy81777472007-11-28 21:35:58 +000047 $sql .= " files
48 SET file_id = " . $App->sqlSanitize($this->file_id, $dbh) . ",
49 project_id = " . $App->returnQuotedString($App->sqlSanitize($this->project_id, $dbh)) . ",
droyaead6582008-02-25 19:47:15 +000050 version = " . $App->returnQuotedString($App->sqlSanitize($this->version, $dbh)) . ",
droy81777472007-11-28 21:35:58 +000051 name = " . $App->returnQuotedString($App->sqlSanitize($this->name, $dbh)) . ",
droyb9c2cb12008-10-08 13:58:14 +000052 plugin_id = " . $App->returnQuotedString($App->sqlSanitize($this->plugin_id, $dbh)) . ",
atoulme425b6c82008-11-24 14:00:27 +000053 is_active = " . $this->isActive . $where;
droy81777472007-11-28 21:35:58 +000054 if(mysql_query($sql, $dbh)) {
55 if($this->file_id == 0) {
56 $this->file_id = mysql_insert_id($dbh);
droy3e7f3a82007-11-29 19:35:51 +000057 $Event->key_value = $this->file_id;
droy81777472007-11-28 21:35:58 +000058 }
59 $rValue = true;
droy3e7f3a82007-11-29 19:35:51 +000060 $Event->add();
droy81777472007-11-28 21:35:58 +000061 }
62 else {
droyaead6582008-02-25 19:47:15 +000063 echo $sql;
droy81777472007-11-28 21:35:58 +000064 $GLOBALS['g_ERRSTRS'][1] = mysql_error();
65 }
66 }
droyaead6582008-02-25 19:47:15 +000067 else {
68 echo "ERROR: One missing:Name: " . $this->name . "Project: " . $this->project_id . "Version: " . $this->version;
69 }
droy81777472007-11-28 21:35:58 +000070 return $rValue;
71 }
72
atoulmebe3eae82008-12-05 16:38:20 +000073 static function getFileID($_name, $_project_id, $_version) {
atoulme425b6c82008-11-24 14:00:27 +000074 $rValue = -1;
atoulmebe3eae82008-12-05 16:38:20 +000075 if($_name != "" && $_project_id != "" && $_version != "") {
droy81777472007-11-28 21:35:58 +000076 global $App, $dbh;
77
78 $sql = "SELECT file_id
79 FROM
80 files
81 WHERE name = " . $App->returnQuotedString($App->sqlSanitize($_name, $dbh)) . "
droy3bb0c962008-01-17 20:45:01 +000082 AND project_id = " . $App->returnQuotedString($App->sqlSanitize($_project_id, $dbh)) . "
droyd702c4f2008-02-26 16:26:57 +000083 AND version = '" . $App->sqlSanitize($_version, $dbh) . "'";
droy81777472007-11-28 21:35:58 +000084
85 $result = mysql_query($sql, $dbh);
86 if($result && mysql_num_rows($result) > 0) {
87 $myrow = mysql_fetch_assoc($result);
88 $rValue = $myrow['file_id'];
89 }
90 }
91 return $rValue;
92 }
93
94 function parseProperties($_content) {
95 $rValue = "";
96 if($_content != "") {
droy37f2bc52007-11-29 16:32:21 +000097
droy81777472007-11-28 21:35:58 +000098 global $User, $App;
droy37f2bc52007-11-29 16:32:21 +000099
100 # step 1 - import existing strings. $String->save() will deal with merges
droy4f4c4072008-01-22 16:12:45 +0000101 $previous_line = "";
102 $lines = explode("\n", $_content);
droy81777472007-11-28 21:35:58 +0000103 foreach($lines as $line) {
104 if(strlen($line) > 0 && $line[0] != "#" && $line[0] != ";") {
droye97d6fe2008-06-16 19:31:15 +0000105 # Bug 235553 - don't trim the space at the end of a line!
106 # $line = trim($line);
droy3bb0c962008-01-17 20:45:01 +0000107
droy4f4c4072008-01-22 16:12:45 +0000108 # Does line end with a \ ?
droy87158be2008-01-22 17:02:40 +0000109 if(preg_match("/\\\\$/", $line)) {
droy4f4c4072008-01-22 16:12:45 +0000110 # Line ends with \
111
112 # strip the backslash
droy35eaaaf2008-01-22 16:25:17 +0000113 $previous_line .= $line . "\n";
droy4f4c4072008-01-22 16:12:45 +0000114 }
115 else {
116 if($previous_line != "") {
117 $line = $previous_line . $line;
118 $previous_line = "";
droy81777472007-11-28 21:35:58 +0000119 }
droy4f4c4072008-01-22 16:12:45 +0000120
121 $tags = explode("=", trim($line), 2);
122 if(count($tags) > 1) {
123 if($rValue != "") {
124 $rValue .= ",";
125 }
126 $tags[0] = trim($tags[0]);
droye97d6fe2008-06-16 19:31:15 +0000127 # Bug 235553 - don't trim the space at the end of a line!
droy4f4c4072008-01-22 16:12:45 +0000128 $tags[1] = trim($tags[1]);
129
130 $rValue .= $tags[0];
131
132 $String = new String();
133 $String->file_id = $this->file_id;
134 $String->name = $tags[0];
135 $String->value = $tags[1];
136 $String->userid = $User->userid;
137 $String->created_on = $App->getCURDATE();
138 $String->is_active = 1;
139 $String->save();
140 }
droy81777472007-11-28 21:35:58 +0000141 }
142 }
143 }
droy37f2bc52007-11-29 16:32:21 +0000144
145 # step 2 - remove strings that are no longer in the properties file
146 $String = new String();
147 $aStrings = $String->getActiveStrings($this->file_id);
148 foreach ($aStrings as $String) {
149 $found = false;
150
151 $aStringList = explode(",",$rValue);
152 foreach($aStringList as $strName) {
153 if($strName == $String->name) {
154 $found = true;
155 break;
156 }
157 }
158
159 if(!$found) {
160 $String->deactivate($String->string_id);
161 }
162 }
droy81777472007-11-28 21:35:58 +0000163 }
164 return $rValue;
165 }
atoulme425b6c82008-11-24 14:00:27 +0000166
167 /**
168 * Returns the fragment relative path.
169 */
170 function findFragmentRelativePath() {
171 # strip useless CVS structure before the plugin name (bug 221675 c14):
172 $pattern = '/^([a-zA-Z0-9\/_-])+\/([a-zA-Z0-9_-]+)\.([a-zA-Z0-9_-]+)(.*)\.properties$/i';
173 $replace = '${2}.${3}${4}.properties';
174 $path = preg_replace($pattern, $replace, $this->name);
175
176 # strip source folder (bug 221675) (org.eclipse.plugin/source_folder/org/eclipse/plugin)
177 $pattern = '/^([a-zA-Z0-9_-]+)\.([a-zA-Z0-9_-]+)\.([a-zA-Z0-9\._-]+)(.*)\/(\1)([\.\/])(\2)([\.\/])(.*)\.properties$/i';
178 $replace = '${1}.${2}.${3}/${5}${6}${7}${8}${9}.properties';
179 $path = preg_replace($pattern, $replace, $path);
180
181 return $path;
182 }
183
184 /*
185 * Convert the filename to *_lang.properties, e.g., foo_fr.properties
186 */
atoulmecefb0242008-11-24 16:00:31 +0000187 function appendLangCode($language_iso, $filename = null) {
atoulmedfee65f2008-11-24 14:59:26 +0000188 if (!$filename) {
atoulme786dc552008-11-24 15:50:18 +0000189 $filename = $this->findFragmentRelativePath();
atoulmedfee65f2008-11-24 14:59:26 +0000190 }
atoulme425b6c82008-11-24 14:00:27 +0000191 if (preg_match( "/^(.*)\.properties$/", $filename, $matches)) {
192 $filename = $matches[1] . '_' . $language_iso . '.properties';
193 }
194 return $filename;
195 }
196
197 /**
198 * returns a hash that contains a mapping from the translation keys to the translation values.
199 */
200 function strings4PropertiesFile($language) {
201 $result = array();
202 if (strcmp($language->iso, "en_AA") == 0) {
203 $sql = "SELECT string_id, name, value FROM strings WHERE file_id = " . $this->file_id .
204 " AND is_active AND non_translatable = 0";
205 $strings_result = mysql_query($sql);
206 while (($strings_row = mysql_fetch_assoc($strings_result)) != null) {
207 $result[$strings_row['name']] = $this->project_id . $strings_row['string_id'] . ":" . $strings_row['value'];
atoulmedfee65f2008-11-24 14:59:26 +0000208 }
atoulme425b6c82008-11-24 14:00:27 +0000209 } else {
210 $sql = "SELECT
211 strings.name AS 'key',
212 strings.value AS orig,
213 translations.value AS trans
214 FROM strings, translations
215 WHERE strings.string_id = translations.string_id
216 AND strings.file_id = " . $this->file_id . "
217 AND strings.is_active
218 AND strings.non_translatable = 0
219 AND translations.language_id = " . $language->id . "
220 AND translations.is_active";
221 $strings_result = mysql_query($sql);
222 while (($strings_row = mysql_fetch_assoc($strings_result)) != null) {
223 $result[$strings_row['key']] = $strings_row['trans'];
224 }
225 }
226 return $result;
227 }
droy81777472007-11-28 21:35:58 +0000228}
droyb9c2cb12008-10-08 13:58:14 +0000229?>