From 985af191d001536a784799aea520d5f1d4514f21 Mon Sep 17 00:00:00 2001 From: Yannick Warnier Date: Sun, 26 Aug 2012 18:02:01 -0500 Subject: [PATCH] Improved most used language vars script - refs #5275 --- main/cron/lang/langstats.class.php | 95 ++++++++++++++++++++++-------- main/cron/lang/langstats.php | 64 ++++++++++++++++++-- 2 files changed, 129 insertions(+), 30 deletions(-) diff --git a/main/cron/lang/langstats.class.php b/main/cron/lang/langstats.class.php index fab14bb399..76f522fba7 100644 --- a/main/cron/lang/langstats.class.php +++ b/main/cron/lang/langstats.class.php @@ -17,32 +17,50 @@ class langstats { public $db; //database connector public $error; //errores almacenados + public $db_type = 'sqlite'; public function __construct($file='') { - if (!class_exists('SQLite3')) { - $this->error = 'SQLiteNotAvailable'; - return false; //cannot use if sqlite not installed - } - if (empty($file)) { - $file = api_get_path(SYS_ARCHIVE_PATH).'/langstasdb'; - } - if (is_file($file) && is_writeable($file)) { - $this->db = new SQLite3($file,SQLITE3_OPEN_READWRITE); - } else { - try { - $this->db = new SQLite3($file); - } catch (Exception $e) { - $this->error = 'DatabaseCreateError'; - error_log('Exception: '. $e->getMessage()); - return false; - } - $err = $this->db->exec('CREATE TABLE lang_freq (' - .' id integer PRIMARY KEY AUTOINCREMENT, ' - .' term_name text, term_file text, term_count integer default 0)'); - if ($err === false) { $this->error = 'CouldNotCreateTable'; return false;} - $err = $this->db->exec('CREATE INDEX lang_freq_terms_idx ON lang_freq(term_name, term_file)'); - if ($err === false) { $this->error = 'CouldNotCreateIndex'; return false; } - // Table and index created, move on. + switch ($this->db_type) { + case 'sqlite': + if (!class_exists('SQLite3')) { + $this->error = 'SQLiteNotAvailable'; + return false; //cannot use if sqlite not installed + } + if (empty($file)) { + $file = api_get_path(SYS_ARCHIVE_PATH).'/langstasdb'; + } + if (is_file($file) && is_writeable($file)) { + $this->db = new SQLite3($file,SQLITE3_OPEN_READWRITE); + } else { + try { + $this->db = new SQLite3($file); + } catch (Exception $e) { + $this->error = 'DatabaseCreateError'; + error_log('Exception: '. $e->getMessage()); + return false; + } + $err = $this->db->exec('CREATE TABLE lang_freq (' + .' id integer PRIMARY KEY AUTOINCREMENT, ' + .' term_name text, term_file text, term_count integer default 0)'); + if ($err === false) { $this->error = 'CouldNotCreateTable'; return false;} + $err = $this->db->exec('CREATE INDEX lang_freq_terms_idx ON lang_freq(term_name, term_file)'); + if ($err === false) { $this->error = 'CouldNotCreateIndex'; return false; } + // Table and index created, move on. + } + break; + case 'mysql': //implementation not finished + if (!function_exists('mysql_connect')) { + $this->error = 'SQLiteNotAvailable'; + return false; //cannot use if sqlite not installed + } + $err = Database::query('SELECT * FROM lang_freq'); + if ($err === false) { //the database probably does not exist, create it + $err = Database::query('CREATE TABLE lang_freq (' + .' id int PRIMARY KEY AUTO_INCREMENT, ' + .' term_name text, term_file text default \'\', term_count int default 0)'); + if ($err === false) { $this->error = 'CouldNotCreateTable'; return false;} + } // if no error, we assume the table exists + break; } return $this->db; } @@ -108,4 +126,33 @@ class langstats { $res = sqlite_query($this->db, 'DELETE FROM lang_freq WHERE 1=1'); return $list; } + /** + * Returns an array of all the language variables with their corresponding + * file of origin. This function tolerates a certain rate of error due to + * the duplication of variables in language files. + * @return array variable => origin file + */ + public function get_variables_origin() { + require_once '../../admin/sub_language.class.php'; + $path = api_get_path(SYS_LANG_PATH).'english/'; + $vars = array(); + $priority = array('trad4all', 'notification', 'accessibility'); + foreach ($priority as $file) { + $list = SubLanguageManager::get_all_language_variable_in_file($path.$file.'.inc.php', true); + foreach ($list as $var => $trad) { + $vars[$var] = $file.'.inc.php'; + } + } + $files = scandir($path); + foreach ($files as $file) { + if (substr($file,0,1) == '.' or in_array($file,$priority)) { + continue; + } + $list = SubLanguageManager::get_all_language_variable_in_file($path.$file, true); + foreach ($list as $var => $trad) { + $vars[$var] = $file; + } + } + return $vars; + } } diff --git a/main/cron/lang/langstats.php b/main/cron/lang/langstats.php index 95005cb1cc..1f242bd81a 100644 --- a/main/cron/lang/langstats.php +++ b/main/cron/lang/langstats.php @@ -5,35 +5,87 @@ * frequency for language variables is a very heavy operation. * To enable, add "$_configuration['language_measure_frequency' ] = 1;" at the * end of main/inc/conf/configuration.php. Remove when done. + * Add ?output=1 to the URL to generate languag files in /tmp/lang/ with just + * the number of terms you want. */ /** * Requires */ die(); +$language_file = array( +'accessibility', 'gradebook', 'registration', 'admin', 'group', 'reportlib', +'agenda', 'help', 'reservation', 'announcements', 'hotspot', 'resourcelinker', +'blog', 'import', 'scormbuilder', 'chat', 'scormdocument', 'coursebackup', +'index', 'scorm', 'course_description', 'install', 'shibboleth', +'course_home', 'learnpath', 'slideshow', 'course_info', 'link', 'survey', +'courses', 'md_document', 'tracking', 'create_course', 'md_link', +'trad4all', 'document', 'md_mix', 'userInfo', 'dropbox', 'md_scorm', +'videoconf', 'exercice', 'messages', 'wiki', 'external_module', 'myagenda', +'work', 'forum', 'notebook', 'glossary', 'notification' +); require_once '../../inc/global.inc.php'; require_once 'langstats.class.php'; /** * Init */ +$terms_limit = 10000 + 50; +$x_most_popular = 2000; +$output = false; $ls = new langstats(); if ($ls === false) { exit($ls->error); } -$list = $ls->get_popular_terms(1000); +$list = $ls->get_popular_terms($x_most_popular); +if ($_GET['output'] == 1) { + $output = true; + $variables_origin = $ls->get_variables_origin(); +} /** * Display */ if (count($list)==0) { echo 'No terms loaded so far'; } if (count($list)>0) { - echo 'Number of records: '.count($list).'
'; - echo ''; $i = 1; + $j = 1; + $k = 0; + $files = array(); + $trans = array(); + echo 'Number of records: '.count($list).'
'; + echo '
CountRegistration orderTermCount
'.($output==1?'':'').''; foreach($list as $elem) { + if ($k > $terms_limit) { break; } + $fixed_elem = $elem; + if ($output) { + if (empty($variables_origin[$elem['term_name']]) && !empty($variables_origin['lang'.$elem['term_name']])) { + $fixed_elem = array('id' => $elem['id'], 'term_name' => 'lang'.$elem['term_name'], 'term_count' => $elem['term_count']); + } + if (empty($variables_origin[$fixed_elem['term_name']])) { + continue; + } + $files[$variables_origin[$fixed_elem['term_name']]][] = $fixed_elem['term_name']; + $translation = get_lang($fixed_elem['term_name']); + $k += str_word_count($translation); + $trans[$fixed_elem['term_name']] = $translation; + $j++; + } echo ''; + ''; $i++; } echo '
IndexRegistration orderTermOriginCount
',$i, - '',$elem['id'], - '',$elem['term_name'], - '',$elem['term_count'],'
',$fixed_elem['id'], + '',$fixed_elem['term_name']; + if ($output) { + echo ''.$variables_origin[$fixed_elem['term_name']]; + } + echo '',$fixed_elem['term_count'],'
'; + if ($output) { + @mkdir('/tmp/lang'); + foreach ($files as $file => $terms) { + @touch('/tmp/lang/'.$file); + file_put_contents('/tmp/lang/'.$file,"