summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2009-03-26 04:36:00 +0000
committerBharat Mediratta <bharat@menalto.com>2009-03-26 04:36:00 +0000
commit78943402b27bc5afb0736089b25a020cd33024b6 (patch)
treed9e5311d7b2a301e76374e585df5acf3872ba380
parent75d98701ae64100a0e444a0caaa14efbc1b2028d (diff)
Convert the L10n scanner from a library to a helper.
In order to make the class static, I had to remove the index cache. I'll restore that and cache the index keys in the task context in a subsequent change. For now, I've put in a @todo to add the caching back in.
-rw-r--r--core/controllers/admin_languages.php6
-rw-r--r--core/helpers/l10n_scanner.php (renamed from core/libraries/L10n_Scanner.php)62
2 files changed, 22 insertions, 46 deletions
diff --git a/core/controllers/admin_languages.php b/core/controllers/admin_languages.php
index 1439f9c8..72a01bc3 100644
--- a/core/controllers/admin_languages.php
+++ b/core/controllers/admin_languages.php
@@ -42,10 +42,10 @@ class Admin_Languages_Controller extends Admin_Controller {
}
public function fetch_updates() {
- // TODO: Convert this to AJAX / progress bar.
+ // @todo Convert this to AJAX / progress bar.
$form = $this->_translation_updates_form();
if ($form->validate()) {
- L10n_Scanner::instance()->update_index();
+ l10n_scanner::update_index();
l10n_client::fetch_updates();
message::success(t("Translations installed/updated"));
}
@@ -95,7 +95,7 @@ class Admin_Languages_Controller extends Admin_Controller {
$this->index($form);
}
}
-
+
private function _languages_form() {
$all_locales = locale::available();
$installed_locales = locale::installed();
diff --git a/core/libraries/L10n_Scanner.php b/core/helpers/l10n_scanner.php
index 6352a319..81764900 100644
--- a/core/libraries/L10n_Scanner.php
+++ b/core/helpers/l10n_scanner.php
@@ -21,36 +21,12 @@
/**
* Scans all source code for messages that need to be localized.
*/
-class L10n_Scanner_Core {
+class l10n_scanner_Core {
// Based on Drupal's potx module, originally written by:
// G‡bor Hojtsy http://drupal.org/user/4166
- private static $_instance;
-
- private $_index_keys;
-
- private function __construct() {}
-
- static function instance() {
- if (self::$_instance == null) {
- self::$_instance = new L10n_Scanner_Core();
- }
-
- return self::$_instance;
- }
-
// TODO(andy_st): Report progress via callback
- function update_index() {
- // Load the current index into memory
- $this->_index_keys = array();
- foreach (Database::instance()
- ->select("key")
- ->from("incoming_translations")
- ->where("locale", "root")
- ->get() as $row) {
- $this->_index_keys[$row->key] = true;
- }
-
+ static function update_index() {
// Index all files
$dir = new L10n_Scanner_File_Filter_Iterator(
new RecursiveIteratorIterator(
@@ -58,27 +34,27 @@ class L10n_Scanner_Core {
new RecursiveDirectoryIterator(DOCROOT))));
foreach ($dir as $file) {
if (pathinfo($file->getFilename(), PATHINFO_EXTENSION) == "php") {
- $this->_scan_php_file($file, $this);
+ l10n_scanner::_scan_php_file($file);
} else {
- $this->_scan_info_file($file, $this);
+ l10n_scanner::_scan_info_file($file);
}
}
}
- function process_message($message) {
+ static function process_message($message) {
+ // @todo this is O(N) queries over the number of messages. Precache all message keys
+ // in the task context and then do lookups over that to get it down to O(1).
$key = I18n::get_message_key($message);
- if (!isset($this->_index_keys[$key])) {
- $entry = ORM::factory("incoming_translation");
+ $entry = ORM::factory("incoming_translation", array("key" => $key));
+ if (!$entry->loaded) {
$entry->key = $key;
$entry->message = serialize($message);
$entry->locale = "root";
$entry->save();
-
- $this->_index_keys[$key] = true;
}
}
- private function _scan_php_file($file, &$message_handler) {
+ private static function _scan_php_file($file) {
$code = file_get_contents($file);
$raw_tokens = token_get_all($code);
unset($code);
@@ -101,24 +77,24 @@ class L10n_Scanner_Core {
unset($raw_tokens);
if (!empty($func_token_list["t"])) {
- $this->_parse_t_calls($tokens, $func_token_list["t"], $message_handler);
+ l10n_scanner::_parse_t_calls($tokens, $func_token_list["t"]);
}
if (!empty($func_token_list["t2"])) {
- $this->_parse_plural_calls($tokens, $func_token_list["t2"], $message_handler);
+ l10n_scanner::_parse_plural_calls($tokens, $func_token_list["t2"]);
}
}
- private function _scan_info_file($file, &$message_handler) {
+ private static function _scan_info_file($file) {
$code = file_get_contents($file);
if (preg_match("#name\s*?=\s*(.*?)\ndescription\s*?=\s*(.*)\n#", $code, $matches)) {
unset($matches[0]);
foreach ($matches as $string) {
- $message_handler->process_message($string);
+ l10n_scanner::process_message($string);
}
}
}
- private function _parse_t_calls(&$tokens, &$call_list, &$message_handler) {
+ private static function _parse_t_calls(&$tokens, &$call_list) {
foreach ($call_list as $index) {
$function_name = $tokens[$index++];
$parens = $tokens[$index++];
@@ -129,7 +105,7 @@ class L10n_Scanner_Core {
if (in_array($next_token, array(")", ","))
&& (is_array($first_param) && ($first_param[0] == T_CONSTANT_ENCAPSED_STRING))) {
$message = self::_escape_quoted_string($first_param[1]);
- $message_handler->process_message($message);
+ l10n_scanner::process_message($message);
} else {
// t() found, but inside is something which is not a string literal.
// TODO(andy_st): Call status callback with error filename/line.
@@ -138,7 +114,7 @@ class L10n_Scanner_Core {
}
}
- private function _parse_plural_calls(&$tokens, &$call_list, &$message_handler) {
+ private static function _parse_plural_calls(&$tokens, &$call_list) {
foreach ($call_list as $index) {
$function_name = $tokens[$index++];
$parens = $tokens[$index++];
@@ -153,14 +129,14 @@ class L10n_Scanner_Core {
&& is_array($second_param) && $second_param[0] == T_CONSTANT_ENCAPSED_STRING) {
$singular = self::_escape_quoted_string($first_param[1]);
$plural = self::_escape_quoted_string($first_param[1]);
- $message_handler->process_message(array("one" => $singular, "other" => $plural));
+ l10n_scanner::process_message(array("one" => $singular, "other" => $plural));
} else {
// t2() found, but inside is something which is not a string literal.
// TODO(andy_st): Call status callback with error filename/line.
}
}
}
- }
+ }
/**
* Escape quotes in a strings depending on the surrounding