summaryrefslogtreecommitdiff
path: root/framework/I18N/core/MessageSource_SQLite.php
blob: 78e4e78d461b534868bf16d27699592eaaf3b02b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php

/**
 * MessageSource_SQLite class file.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the BSD License.
 *
 * Copyright(c) 2004 by Qiang Xue. All rights reserved.
 *
 * To contact the author write to {@link mailto:qiang.xue@gmail.com Qiang Xue}
 * The latest version of PRADO can be obtained from:
 * {@link http://prado.sourceforge.net/}
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @version $Revision: 1.4 $  $Date: 2005/02/25 09:59:40 $
 * @package Prado\I18N\core
 */

namespace Prado\I18N\core;

/**
 * Get the MessageSource class file.
 */
require_once(dirname(__FILE__).'/MessageSource.php');

/**
 * Get the I18N utility file, contains the DSN parser.
 */
require_once(dirname(__FILE__).'/util.php');

/**
 * MessageSource_SQLite class.
 * 
 * Retrive the message translation from a SQLite database.
 *
 * See the MessageSource::factory() method to instantiate this class.
 *
 * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @version v1.0, last update on Fri Dec 24 16:58:58 EST 2004
 * @package Prado\I18N\core
 */
class MessageSource_SQLite extends MessageSource
{
	/**
	 * The SQLite datasource, the filename of the database.
	 * @var string 
	 */
	protected $source;
	
	/**
	 * Constructor.
	 * Create a new message source using SQLite.
	 * @see MessageSource::factory();
	 * @param string SQLite datasource, in PEAR's DB DSN format.
	 */
	function __construct($source)
	{
		$dsn = parseDSN((string)$source);
		$this->source = $dsn['database'];
	}
	
	/**
	 * Get an array of messages for a particular catalogue and cultural 
	 * variant.
	 * @param string the catalogue name + variant
	 * @return array translation messages.
	 */
	protected function &loadData($variant)
	{
		$variant = sqlite_escape_string($variant);
		
		$statement = 
			"SELECT t.id, t.source, t.target, t.comments
				FROM trans_unit t, catalogue c
 				WHERE c.cat_id =  t.cat_id
					AND c.name = '{$variant}' 
				ORDER BY id ASC";
	
		$db = sqlite_open($this->source);
		$rs = sqlite_query($statement, $db);
			
		$result = array();
		
		while($row = sqlite_fetch_array($rs,SQLITE_NUM))
		{
			$source = $row[1];
			$result[$source][] = $row[2]; //target
			$result[$source][] = $row[0]; //id
			$result[$source][] = $row[3]; //comments
		}

		sqlite_close($db);
		
		return $result;
	}
	
	/**
	 * Get the last modified unix-time for this particular catalogue+variant.
	 * We need to query the database to get the date_modified.
	 * @param string catalogue+variant
	 * @return int last modified in unix-time format.
	 */	
	protected function getLastModified($source)
	{
		$source = sqlite_escape_string($source);

		$db = sqlite_open($this->source);
		
		$rs = sqlite_query(
			"SELECT date_modified FROM catalogue WHERE name = '{$source}'",
			$db);
			
		$result = $rs ? (int)sqlite_fetch_single($rs) : 0;
		
		sqlite_close($db);		
	
		return $result;			
	}
	
	/**
	 * Check if a particular catalogue+variant exists in the database.
	 * @param string catalogue+variant
	 * @return boolean true if the catalogue+variant is in the database, 
	 * false otherwise.
	 */
	protected function isValidSource($variant)
	{
		$variant = sqlite_escape_string($variant);
		$db = sqlite_open($this->source);
		$rs = sqlite_query(
			"SELECT COUNT(*) FROM catalogue WHERE name = '{$variant}'",
			$db);
		$result = $rs && (int)sqlite_fetch_single($rs);	
		sqlite_close($db);

		return $result;
	}
	
	/**
	 * Get all the variants of a particular catalogue.
	 * @param string catalogue name
	 * @return array list of all variants for this catalogue. 
	 */
	protected function getCatalogueList($catalogue)
	{
		$variants = explode('_',$this->culture);
		
		$catalogues = array($catalogue);

		$variant = null;
				
		for($i = 0, $k = count($variants); $i < $k; ++$i)
		{						
			if(isset($variants[$i]{0}))
			{
				$variant .= ($variant)?'_'.$variants[$i]:$variants[$i];
				$catalogues[] = $catalogue.'.'.$variant;
			}
		}
		return array_reverse($catalogues);	
	}
	
	/**
	 * Retrive catalogue details, array($cat_id, $variant, $count).
	 * @param string catalogue
	 * @return array catalogue details, array($cat_id, $variant, $count). 
	 */
	private function getCatalogueDetails($catalogue='messages')
	{
		if(empty($catalogue))
			$catalogue = 'messages';

		$variant = $catalogue.'.'.$this->culture;
		
		$name = sqlite_escape_string($this->getSource($variant));	
			
		$db = sqlite_open($this->source);
		
		$rs = sqlite_query("SELECT cat_id
					FROM catalogue WHERE name = '{$name}'", $db);
		
		if(sqlite_num_rows($rs) != 1)
			return false;
		
		$cat_id = (int)sqlite_fetch_single($rs);
		
		//first get the catalogue ID
		$rs = sqlite_query(
			"SELECT count(msg_id)
				FROM trans_unit
				WHERE cat_id = {$cat_id}", $db);

		$count = (int)sqlite_fetch_single($rs);

		sqlite_close($db);	
		
		return array($cat_id, $variant, $count);
	}
	
	/**
	 * Update the catalogue last modified time.
	 * @return boolean true if updated, false otherwise. 
	 */
	private function updateCatalogueTime($cat_id, $variant, $db)
	{
		$time = time();
		
		$result = sqlite_query("UPDATE catalogue 
							SET date_modified = {$time}
							WHERE cat_id = {$cat_id}", $db);

		if(!empty($this->cache))		
			$this->cache->clean($variant, $this->culture);	
		
		return $result;
	}
		
	/**
	 * Save the list of untranslated blocks to the translation source. 
	 * If the translation was not found, you should add those
	 * strings to the translation source via the <b>append()</b> method.
	 * @param string the catalogue to add to
	 * @return boolean true if saved successfuly, false otherwise.
	 */
	function save($catalogue='messages')
	{
		$messages = $this->untranslated;
		
		if(count($messages) <= 0) return false;				
		
		$details = $this->getCatalogueDetails($catalogue);	
		
		if($details)
			list($cat_id, $variant, $count) = $details;
		else
			return false;
		
		if($cat_id <= 0) return false;
		$inserted = 0;
		
		$db = sqlite_open($this->source);
		$time = time();

		foreach($messages as $message)
		{
			$message = sqlite_escape_string($message);
			$statement = "INSERT INTO trans_unit
				(cat_id,id,source,date_added) VALUES
				({$cat_id}, {$count},'{$message}',$time)";
			if(sqlite_query($statement, $db))
			{
				$count++; $inserted++;			
			}
		}
		if($inserted > 0)
			$this->updateCatalogueTime($cat_id, $variant, $db);
	
		sqlite_close($db);	
		
		return $inserted > 0;		
	}	
		
	/**
	 * Update the translation.
	 * @param string the source string.
	 * @param string the new translation string.
	 * @param string comments
	 * @param string the catalogue of the translation.
	 * @return boolean true if translation was updated, false otherwise. 
	 */	
	function update($text, $target, $comments, $catalogue='messages')
	{
		$details = $this->getCatalogueDetails($catalogue);
		if($details)
			list($cat_id, $variant, $count) = $details;
		else
			return false;
		
		$comments = sqlite_escape_string($comments);
		$target = sqlite_escape_string($target);
		$text = sqlite_escape_string($text);
		
		$time = time();
		
		$db = sqlite_open($this->source);
		
		$statement = "UPDATE trans_unit SET
						target = '{$target}',
						comments = '{$comments}',
						date_modified = '{$time}'
					WHERE cat_id = {$cat_id} 
						AND source = '{$text}'";
		
		$updated = false;
		
		if(sqlite_query($statement, $db))
			$updated = $this->updateCatalogueTime($cat_id, $variant, $db);
		
		sqlite_close($db);			
				
		return $updated;
	}	
	
	/**
	 * Delete a particular message from the specified catalogue.
	 * @param string the source message to delete.
	 * @param string the catalogue to delete from.
	 * @return boolean true if deleted, false otherwise. 
	 */
	function delete($message, $catalogue='messages')
	{
		$details = $this->getCatalogueDetails($catalogue);
		if($details)
			list($cat_id, $variant, $count) = $details;
		else
			return false;
			
		$db = sqlite_open($this->source);
		$text = sqlite_escape_string($message);
		
		$statement = "DELETE FROM trans_unit WHERE
						cat_id = {$cat_id} AND source = '{$message}'";
		$deleted = false;
				
		if(sqlite_query($statement, $db))
			$deleted = $this->updateCatalogueTime($cat_id, $variant, $db);		
			
		sqlite_close($db);	
				
		return $deleted;
	}	
	
	/**
	 * Returns a list of catalogue as key and all it variants as value.
	 * @return array list of catalogues 
	 */
	function catalogues()
	{
		$db = sqlite_open($this->source);
		$statement = 'SELECT name FROM catalogue ORDER BY name';
		$rs = sqlite_query($statement, $db);
		$result = array();
		while($row = sqlite_fetch_array($rs,SQLITE_NUM))
		{
			$details = explode('.',$row[0]);
			if(!isset($details[1])) $details[1] = null;
			
			$result[] = $details;
		}
		sqlite_close($db);
		return $result;
	}	
}