/** * @since 2012-01-04 * @return array */ public static function getGroupIds(MessageHandle $handle) { $namespace = $handle->getTitle()->getNamespace(); $key = $handle->getKey(); $normkey = strtr(strtolower("{$namespace}:{$key}"), " ", "_"); $index = self::singleton()->retrieve(); if (isset($index[$normkey])) { return (array) $index[$normkey]; } else { return array(); } }
/** * Retrieves a list of groups given MessageHandle belongs to. * @since 2012-01-04 * @param MessageHandle $handle * @return array */ public static function getGroupIds(MessageHandle $handle) { $namespace = $handle->getTitle()->getNamespace(); $key = $handle->getKey(); $normkey = TranslateUtils::normaliseKey($namespace, $key); $value = self::singleton()->get($normkey); if ($value !== null) { return (array) $value; } else { return array(); } }
/** * Returns all translations of a given message. * @param MessageHandle $handle Language code is ignored. * @return array[] * @since 2012-12-18 */ public static function getTranslations(MessageHandle $handle) { $namespace = $handle->getTitle()->getNamespace(); $base = $handle->getKey(); $dbr = wfGetDB(DB_SLAVE); $res = $dbr->select('page', array('page_namespace', 'page_title'), array('page_namespace' => $namespace, 'page_title ' . $dbr->buildLike("{$base}/", $dbr->anyString())), __METHOD__, array('ORDER BY' => 'page_title', 'USE INDEX' => 'name_title')); $titles = array(); foreach ($res as $row) { $titles[] = $row->page_title; } if ($titles === array()) { return array(); } $pageInfo = TranslateUtils::getContents($titles, $namespace); return $pageInfo; }
public function update(MessageHandle $handle, $targetText) { global $wgContLang; if (!$handle->isValid() || $handle->getCode() === '') { return false; } $mkey = $handle->getKey(); $group = $handle->getGroup(); $targetLanguage = $handle->getCode(); $sourceLanguage = $group->getSourceLanguage(); $title = $handle->getTitle(); // Skip definitions to not slow down mass imports etc. // These will be added when the first translation is made if ($targetLanguage === $sourceLanguage) { return false; } $definition = $group->getMessage($mkey, $sourceLanguage); if (!is_string($definition) || !strlen(trim($definition))) { return false; } $dbw = $this->getDB(DB_MASTER); /* Check that the definition exists and fetch the sid. If not, add * the definition and retrieve the sid. If the definition changes, * we will create a new entry - otherwise we could at some point * get suggestions which do not match the original definition any * longer. The old translations are still kept until purged by * rerunning the bootstrap script. */ $conds = array('tms_context' => $title->getPrefixedText(), 'tms_text' => $definition); $sid = $dbw->selectField('translate_tms', 'tms_sid', $conds, __METHOD__); if ($sid === false) { $sid = $this->insertSource($title, $sourceLanguage, $definition); } // Delete old translations for this message if any. Could also use replace $deleteConds = array('tmt_sid' => $sid, 'tmt_lang' => $targetLanguage); $dbw->delete('translate_tmt', $deleteConds, __METHOD__); // Insert the new translation $row = $deleteConds + array('tmt_text' => $targetText); $dbw->insert('translate_tmt', $row, __METHOD__); return true; }
/** * Get the translations in all languages. Cached for performance. * Fuzzy translation are not included. * * @return array Language code => Translation */ public function getTranslations() { static $cache = array(); $key = $this->handle->getTitle()->getPrefixedText(); if (array_key_exists($key, $cache)) { return $cache[$key]; } $data = ApiQueryMessageTranslations::getTranslations($this->handle); $namespace = $this->handle->getTitle()->getNamespace(); $cache[$key] = array(); foreach ($data as $page => $info) { $tTitle = Title::makeTitle($namespace, $page); $tHandle = new MessageHandle($tTitle); $fuzzy = MessageHandle::hasFuzzyString($info[0]) || $tHandle->isFuzzy(); if ($fuzzy) { continue; } $code = $tHandle->getCode(); $cache[$key][$code] = $info[0]; } return $cache[$key]; }
/** * @return string */ public function dialogID() { $hash = sha1($this->handle->getTitle()->getPrefixedDbKey()); return substr($hash, 0, 4); }
public function batchInsertDefinitions(array $batch) { foreach ($batch as $key => $item) { list($title, $language, $text) = $item; $handle = new MessageHandle($title); $context = Title::makeTitle($handle->getTitle()->getNamespace(), $handle->getKey()); $this->sids[$key] = $this->insertSource($context, $language, $text); } wfWaitForSlaves(10); }
/** * Adds tag which identifies the revision of source message at that time. * This is used to show diff against current version of source message * when updating a translation. * Hook: Translate:newTranslation * @param MessageHandle $handle * @param int $revision * @param string $text * @param User $user * @return bool */ public static function updateTransverTag(MessageHandle $handle, $revision, $text, User $user) { if ($user->isAllowed('bot')) { return false; } $group = $handle->getGroup(); $title = $handle->getTitle(); $name = $handle->getKey() . '/' . $group->getSourceLanguage(); $definitionTitle = Title::makeTitleSafe($title->getNamespace(), $name); if (!$definitionTitle || !$definitionTitle->exists()) { return true; } $definitionRevision = $definitionTitle->getLatestRevID(); $dbw = wfGetDB(DB_MASTER); $conds = array('rt_page' => $title->getArticleID(), 'rt_type' => RevTag::getType('tp:transver'), 'rt_revision' => $revision, 'rt_value' => $definitionRevision); $index = array('rt_type', 'rt_page', 'rt_revision'); $dbw->replace('revtag', array($index), $conds, __METHOD__); return true; }
/** * @param MessageHandle $handle * @return TTMServerMessageUpdateJob */ public static function newJob(MessageHandle $handle) { $job = new self($handle->getTitle()); return $job; }
/** * @see schema.xml */ protected function createDocument(MessageHandle $handle, $text, $revId) { $language = $handle->getCode(); $translationTitle = $handle->getTitle(); $title = Title::makeTitle($handle->getTitle()->getNamespace(), $handle->getKey()); $wiki = wfWikiId(); $messageid = $title->getPrefixedText(); $globalid = "{$wiki}-{$messageid}-{$revId}/{$language}"; $doc = new Solarium_Document_ReadWrite(); $doc->wiki = $wiki; $doc->uri = $translationTitle->getCanonicalUrl(); $doc->messageid = $messageid; $doc->globalid = $globalid; $doc->language = $language; $doc->content = $text; $doc->setField('group', $handle->getGroupIds()); return $doc; }
/** * Hook: TranslateEventTranslationEdit * Hook: TranslateEventTranslationReview */ public static function onChange(MessageHandle $handle) { $job = self::newJob($handle->getTitle()); JobQueueGroup::singleton()->push($job); return true; }
/** * @return \Elastica\Document */ protected function createDocument(MessageHandle $handle, $text, $revId) { $language = $handle->getCode(); $localid = $handle->getTitleForBase()->getPrefixedText(); $wiki = wfWikiId(); $globalid = "{$wiki}-{$localid}-{$revId}/{$language}"; $data = array('wiki' => $wiki, 'uri' => $handle->getTitle()->getCanonicalUrl(), 'localid' => $localid, 'language' => $language, 'content' => $text, 'group' => $handle->getGroupIds()); return new \Elastica\Document($globalid, $data); }