Przeglądaj źródła

Improved most used language vars script - refs #5275

Yannick Warnier 12 lat temu
rodzic
commit
985af191d0
2 zmienionych plików z 129 dodań i 30 usunięć
  1. 71 24
      main/cron/lang/langstats.class.php
  2. 58 6
      main/cron/lang/langstats.php

+ 71 - 24
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;
+  }
 } 

+ 58 - 6
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).'<br />';
-  echo '<table><tr><th>Count</th><th>Registration order</th><th>Term</th><th>Count</th></tr>';
   $i = 1;
+  $j = 1;
+  $k = 0;
+  $files = array();
+  $trans = array();
+  echo 'Number of records: '.count($list).'<br />';
+  echo '<table><tr><th>Index</th><th>Registration order</th><th>Term</th>'.($output==1?'<th>Origin</th>':'').'<th>Count</th></tr>';
   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 '<tr><td>',$i,
-      '</td><td>',$elem['id'],
-      '</td><td>',$elem['term_name'],
-      '</td><td>',$elem['term_count'],'</td></tr>';
+      '</td><td>',$fixed_elem['id'],
+      '</td><td>',$fixed_elem['term_name'];
+    if ($output) {
+      echo '</td><td>'.$variables_origin[$fixed_elem['term_name']];
+    }
+    echo '</td><td>',$fixed_elem['term_count'],'</td></tr>';
     $i++;
   }
   echo '</table>';
+  if ($output) {
+    @mkdir('/tmp/lang');
+    foreach ($files as $file => $terms) {
+      @touch('/tmp/lang/'.$file);
+      file_put_contents('/tmp/lang/'.$file,"<?php".PHP_EOL);
+      foreach ($terms as $term) {
+        file_put_contents('/tmp/lang/'.$file,'$'.$term.' = "'.str_replace('"','\"',$trans[$term]).'";'.PHP_EOL, FILE_APPEND);
+      }
+    }  
+  }
 }