Exemple #1
0
 public function save()
 {
     access::verify_csrf();
     if (!identity::active_user()->admin) {
         access::forbidden();
     }
     $locale = Gallery_I18n::instance()->locale();
     $input = Input::instance();
     $key = $input->post("l10n-message-key");
     $root_message = ORM::factory("incoming_translation")->where("key", "=", $key)->where("locale", "=", "root")->find();
     if (!$root_message->loaded()) {
         throw new Exception("@todo bad request data / illegal state");
     }
     $is_plural = Gallery_I18n::is_plural_message(unserialize($root_message->message));
     $is_empty = true;
     if ($is_plural) {
         $plural_forms = l10n_client::plural_forms($locale);
         $translation = array();
         foreach ($plural_forms as $plural_form) {
             $value = $input->post("l10n-edit-plural-translation-{$plural_form}");
             if (null === $value || !is_string($value)) {
                 throw new Exception("@todo bad request data");
             }
             $translation[$plural_form] = $value;
             $is_empty = $is_empty && empty($value);
         }
     } else {
         $translation = $input->post("l10n-edit-translation");
         $is_empty = empty($translation);
         if (null === $translation || !is_string($translation)) {
             throw new Exception("@todo bad request data");
         }
     }
     $entry = ORM::factory("outgoing_translation")->where("key", "=", $key)->where("locale", "=", $locale)->find();
     if ($is_empty) {
         if ($entry->loaded()) {
             $entry->delete();
         }
     } else {
         if (!$entry->loaded()) {
             $entry->key = $key;
             $entry->locale = $locale;
             $entry->message = $root_message->message;
             $entry->base_revision = null;
         }
         $entry->translation = serialize($translation);
         $entry_from_incoming = ORM::factory("incoming_translation")->where("key", "=", $key)->where("locale", "=", $locale)->find();
         if (!$entry_from_incoming->loaded()) {
             $entry->base_revision = $entry_from_incoming->revision;
         }
         $entry->save();
     }
     Gallery_I18n::clear_cache($locale);
     print json_encode(new stdClass());
 }
Exemple #2
0
 static function update_l10n($task)
 {
     $errors = array();
     try {
         $start = microtime(true);
         $data = Cache::instance()->get("update_l10n_cache:{$task->id}");
         if ($data) {
             list($dirs, $files, $cache, $num_fetched) = unserialize($data);
         }
         $i = 0;
         switch ($task->get("mode", "init")) {
             case "init":
                 // 0%
                 $dirs = array("gallery", "modules", "themes", "installer");
                 $files = $cache = array();
                 $num_fetched = 0;
                 $task->set("mode", "find_files");
                 $task->status = t("Finding files");
                 break;
             case "find_files":
                 // 0% - 10%
                 while (($dir = array_pop($dirs)) && microtime(true) - $start < 0.5) {
                     if (in_array(basename($dir), array("tests", "lib"))) {
                         continue;
                     }
                     foreach (glob(DOCROOT . "{$dir}/*") as $path) {
                         $relative_path = str_replace(DOCROOT, "", $path);
                         if (is_dir($path)) {
                             $dirs[] = $relative_path;
                         } else {
                             $files[] = $relative_path;
                         }
                     }
                 }
                 $task->status = t2("Finding files: found 1 file", "Finding files: found %count files", count($files));
                 if (!$dirs) {
                     $task->set("mode", "scan_files");
                     $task->set("total_files", count($files));
                     $task->status = t("Scanning files");
                     $task->percent_complete = 10;
                 }
                 break;
             case "scan_files":
                 // 10% - 70%
                 while (($file = array_pop($files)) && microtime(true) - $start < 0.5) {
                     $file = DOCROOT . $file;
                     switch (pathinfo($file, PATHINFO_EXTENSION)) {
                         case "php":
                             l10n_scanner::scan_php_file($file, $cache);
                             break;
                         case "info":
                             l10n_scanner::scan_info_file($file, $cache);
                             break;
                     }
                 }
                 $total_files = $task->get("total_files");
                 $task->status = t2("Scanning files: scanned 1 file", "Scanning files: scanned %count files", $total_files - count($files));
                 $task->percent_complete = 10 + 60 * ($total_files - count($files)) / $total_files;
                 if (empty($files)) {
                     $task->set("mode", "fetch_updates");
                     $task->status = t("Fetching updates");
                     $task->percent_complete = 70;
                 }
                 break;
             case "fetch_updates":
                 // 70% - 100%
                 // Send fetch requests in batches until we're done
                 $num_remaining = l10n_client::fetch_updates($num_fetched);
                 if ($num_remaining) {
                     $total = $num_fetched + $num_remaining;
                     $task->percent_complete = 70 + 30 * ((double) $num_fetched / $total);
                 } else {
                     Gallery_I18n::clear_cache();
                     $task->done = true;
                     $task->state = "success";
                     $task->status = t("Translations installed/updated");
                     $task->percent_complete = 100;
                 }
         }
         if (!$task->done) {
             Cache::instance()->set("update_l10n_cache:{$task->id}", serialize(array($dirs, $files, $cache, $num_fetched)));
         } else {
             Cache::instance()->delete("update_l10n_cache:{$task->id}");
         }
     } catch (Exception $e) {
         Kohana_Log::add("error", (string) $e);
         $task->done = true;
         $task->state = "error";
         $task->status = $e->getMessage();
         $errors[] = (string) $e;
     }
     if ($errors) {
         $task->log($errors);
     }
 }