* @package wombat * @subpackage nls */ patI18n::load('Module', 'Wombat'); /** * Import to-be-translated messages * * Importer for translation module MySQL * * @version 1.0.0 * @package wombat * @subpackage nls */ class patI18n_Importer_Wombat extends patI18n_Module_Wombat implements patI18n_Importer { const MODE_IMPORT = 'import'; const MODE_ORPHANCHECK = 'orphancheck'; /** * Current opatration mode * * @var $mode */ private $mode; /** * constructor * * Start table access */ public function __construct() { $this->mode = self::MODE_IMPORT; $this->initTable(); } /** * Mark message or messages as orphans * * @param string $id or -1 for all * @param string text domain or null for all */ public function markOrphan($id = -1, $domain = null) { $save = array( 'orphan' => 1 ); if (0 < $id) { $this->saveMsg($id, $save); return; } $clause = array(); if ($domain) { $clause[] = array( 'field' => 'domain', 'value' => $domain ); } else { $clause[] = array( 'field' => $this->table->getIdentifier('nlsmsg'), 'value' => 0, 'relation' => 'NOT' ); } return $this->table->save('nlsmsg', null, $save, $clause); } /** * Set opratation mode * * @param string $mode either MODE_IMPORT or MODE_ORPHANCHECK */ public function setMode($mode = self::MODE_IMPORT) { $this->mode = $mode; } /** * Add new message * * Check for existing message id and add new one if it does not exit yet. * In case of a message in plural form, provide the id of the corresponding * singular message. * * @param string $string new message string * @param string|null $singular optional id of singular form * @param string|null $domain optional text domain * @return string $id */ public function addMsg($string, $singular = null, $domain = null) { $string = trim($string); if (empty($string)) { return null; } $domain = $this->resolveDomain($domain); $id = $this->getMsgId($string, $domain); // just return the id if message already exists if ($id) { if (self::MODE_ORPHANCHECK == $this->mode) { $save = array( 'orphan' => 0 ); $this->saveMsg($id, $save); } return $id; } if (empty($singular)) { $singular = 0; } $save = array( 'singular' => $singular, 'md5' => md5($string), 'msg' => $string, 'msg_org' => $string, 'domain' => $domain, 'changed' => gmdate('Y-m-d H:i:s'), 'created' => gmdate('Y-m-d H:i:s'), 'orphan' => 0 ); return $this->saveMsg('__new', $save); } /** * Update existing message * * Replace existing message string with new one. Keep all translations * and and store old message string in msg_org * * @param string $old old message string * @param string $new new message string * @param string|null $singular optional id of singular form * @param string|null $domain optional text domain * @return string $id */ public function updateMsg($old, $new, $singular = null, $domain = null) { $new = trim($new); if (empty($new)) { return null; } $old = trim($old); $domain = $this->resolveDomain($domain); $id = $this->getMsgId($old, $domain); $save = array( 'singular' => $singular, 'md5' => md5($new), 'msg' => $new, 'msg_org' => $old, 'changed' => gmdate('Y-m-d H:i:s'), 'orphan' => 0 ); if (empty($id)) { $save['msg_org'] = $new; $save['domain'] = $domain; return $this->saveMsg('__new', $save);; } if ($old == $new) { return $id; } return $this->saveMsg($id,$save); } /** * Save message date * * Actually save row in database. * * @param string $id * @param array $save * @return string message id */ private function saveMsg($id, $save) { return $this->table->save('nlsmsg', $id, $save); } /** * Add translated message for id * * See whether translation for this message id already exists. * Add translation if there is no translation yet. * * Optionally provide $min parameter for plural form. * * @param string $id message id * @param string $string translated message string * @param string $lang language code of translation * @param int $min minimum count for plural forms, or 0 for singular form */ public function addTranslation($id, $string, $lang, $min = 0) { $save = array( 'trans_' . $lang => $string, 'changed_' . $lang => gmdate('Y-m-d H:i:s'), 'min_' . $lang => $min ); $this->saveMsg($id, $save); } /** * Find message id * * Look up message id in database and return message id string on * success * * @param string $string * @param string $domain * @return string|null */ private function getMsgId($string, $domain = null) { $clause = array(); $clause[] = array( 'field' => 'md5', 'value' => md5($string) ); $clause[] = array( 'field' => 'domain', 'value' => $domain ); $ids = $this->table->getIds('nlsmsg', null, $clause); if (empty($ids)) { return null; } return $ids[0]; } }