* @package wombat * @subpackage nls */ patI18n::load('Module', 'Wombat'); /** * Import to-be-translated messages * * Importer for translation module MySQL * * @version 1.0.1 * @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(patI18n_Module_Wombat::TABLE), 'value' => 0, 'relation' => 'NOT' ); } return $this->table->save(patI18n_Module_Wombat::TABLE, 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); if (empty($singular)) { $singular = 0; } $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(patI18n_Module_Wombat::TABLE, $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) { // old record is required! $old = $this->table->get(patI18n_Module_Wombat::TABLE, $id); if (1 != count($old)) { return; } // check for changes if ($string == $old[0]['trans_' . $lang]) { return; } $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) { $string = trim($string); $clause = array(); $clause[] = array( 'field' => 'md5', 'value' => md5($string) ); $clause[] = array( 'field' => 'domain', 'value' => $domain ); $option = array(); $ids = $this->table->getIds(patI18n_Module_Wombat::TABLE, null, $clause, $option); if (empty($ids)) { return null; } return $ids[0]; } /** * Get Id for String * * Public interface to seek message id * * @param string $string * @param string $domain * @return string|null */ public function getId($string, $domain = null) { $string = trim($string); if (empty($string)) { return null; } $domain = $this->resolveDomain($domain); return $this->getMsgId($string, $domain); } /** * Get All Translations * * Look up message in database and return all translation texts * * @param string $id * @return array */ public function getAllTranslations($id) { if (empty($id)) { return array(); } $msg = $this->table->get(patI18n_Module_Wombat::TABLE, $id); if (1 != count($msg)) { return array(); } $trans = array(); $msg = $msg[0]; foreach ($msg as $k => $v) { if (0 != strncmp('trans_', $k, 6)) { continue; } $k = substr($k, 6); $trans[$k] = $v; } return $trans; } /** * Store URL Info for Message * * This allows to make use of Reference-fatures using translatable elements * * @param string $id of message * @param string $urlId id of URL * @param string $path extra path info */ public function setUrl($id, $urlId, $path = '') { $uPrimary = $this->table->getIdentifier('url'); $save = array( $uPrimary => $urlId, 'path' => $path ); $this->saveMsg($id, $save); } /** * Store Namespace Info for Message * * Store namespace and xid for message * * @param string $id of message * @param string $namespace * @param string $xid */ public function setXid($id, $namespace, $xid) { $save = array( 'xnamespace' => $namespace, 'xid' => $xid ); $this->saveMsg($id, $save); } }