langstats.class.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This class takes the creation and querying of an SQLite DB in charge. The
  5. * goal of this DB is to get stats on the usage of language vars for a common
  6. * user.
  7. * @package chamilo.cron.lang
  8. */
  9. /**
  10. * This class takes the creation and querying of an SQLite DB in charge. The
  11. * goal of this DB is to get stats on the usage of language vars for a common
  12. * user. This class requires the SQLite extension of PHP to be installed. The
  13. * check for the availability of sqlite_open() should be made before calling
  14. * the constructor (preferrably)
  15. */
  16. class langstats {
  17. public $db; //database connector
  18. public $error; //errores almacenados
  19. public $db_type = 'sqlite';
  20. public function __construct($file='') {
  21. switch ($this->db_type) {
  22. case 'sqlite':
  23. if (!class_exists('SQLite3')) {
  24. $this->error = 'SQLiteNotAvailable';
  25. return false; //cannot use if sqlite not installed
  26. }
  27. if (empty($file)) {
  28. $file = api_get_path(SYS_ARCHIVE_PATH).'/langstasdb';
  29. }
  30. if (is_file($file) && is_writeable($file)) {
  31. $this->db = new SQLite3($file,SQLITE3_OPEN_READWRITE);
  32. } else {
  33. try {
  34. $this->db = new SQLite3($file);
  35. } catch (Exception $e) {
  36. $this->error = 'DatabaseCreateError';
  37. error_log('Exception: '. $e->getMessage());
  38. return false;
  39. }
  40. $err = $this->db->exec('CREATE TABLE lang_freq ('
  41. .' id integer PRIMARY KEY AUTOINCREMENT, '
  42. .' term_name text, term_file text, term_count integer default 0)');
  43. if ($err === false) { $this->error = 'CouldNotCreateTable'; return false;}
  44. $err = $this->db->exec('CREATE INDEX lang_freq_terms_idx ON lang_freq(term_name, term_file)');
  45. if ($err === false) { $this->error = 'CouldNotCreateIndex'; return false; }
  46. // Table and index created, move on.
  47. }
  48. break;
  49. case 'mysql': //implementation not finished
  50. if (!function_exists('mysql_connect')) {
  51. $this->error = 'SQLiteNotAvailable';
  52. return false; //cannot use if sqlite not installed
  53. }
  54. $err = Database::query('SELECT * FROM lang_freq');
  55. if ($err === false) { //the database probably does not exist, create it
  56. $err = Database::query('CREATE TABLE lang_freq ('
  57. .' id int PRIMARY KEY AUTO_INCREMENT, '
  58. .' term_name text, term_file text default \'\', term_count int default 0)');
  59. if ($err === false) { $this->error = 'CouldNotCreateTable'; return false;}
  60. } // if no error, we assume the table exists
  61. break;
  62. }
  63. return $this->db;
  64. }
  65. /**
  66. * Add a count for a specific term
  67. * @param string The language term used
  68. * @param string The file from which the language term came from
  69. * @return boolean true
  70. */
  71. public function add_use($term, $term_file='') {
  72. $term = $this->db->escapeString($term);
  73. $term_file = $this->db->escapeString($term_file);
  74. $sql = "SELECT id, term_name, term_file, term_count FROM lang_freq WHERE term_name='$term' and term_file='$term_file'";
  75. $ress = $this->db->query($sql);
  76. if ($ress === false) {
  77. $this->error = 'CouldNotQueryTermFromTable';
  78. return false;
  79. }
  80. $i = 0;
  81. while ($row = $ress->fetchArray(SQLITE3_BOTH)) {
  82. $num = $row[3];
  83. $num++;
  84. $res = $this->db->query('UPDATE lang_freq SET term_count = '.$num.' WHERE id = '.$row[0]);
  85. if ($res === false) {
  86. $this->error = 'CouldNotUpdateTerm';
  87. return false;
  88. } else {
  89. return $row[0];
  90. }
  91. $i++;
  92. }
  93. if ($i == 0) {
  94. //No term found in the table, register as new term
  95. $resi = $this->db->query("INSERT INTO lang_freq(term_name, term_file, term_count) VALUES ('$term', '$term_file', 1)");
  96. if ($resi === false) {
  97. $this->error = 'CouldNotInsertRow';
  98. return false;
  99. } else {
  100. return $this->db->lastInsertRowID();
  101. }
  102. }
  103. }
  104. /**
  105. * Function to get a list of the X most-requested terms
  106. * @param integer Limit of terms to show
  107. * @return array List of most requested terms
  108. */
  109. public function get_popular_terms($num=1000) {
  110. $res = $this->db->query('SELECT * FROM lang_freq ORDER BY term_count DESC LIMIT '.$num);
  111. $list = array();
  112. while ($row = $res->fetchArray()) {
  113. $list[] = $row;
  114. }
  115. return $list;
  116. }
  117. /**
  118. * Clear all records in lang_freq
  119. * @return boolean true
  120. */
  121. public function clear_all() {
  122. $res = sqlite_query($this->db, 'DELETE FROM lang_freq WHERE 1=1');
  123. return $list;
  124. }
  125. /**
  126. * Returns an array of all the language variables with their corresponding
  127. * file of origin. This function tolerates a certain rate of error due to
  128. * the duplication of variables in language files.
  129. * @return array variable => origin file
  130. */
  131. public function get_variables_origin() {
  132. require_once '../../admin/sub_language.class.php';
  133. $path = api_get_path(SYS_LANG_PATH).'english/';
  134. $vars = array();
  135. $priority = array('trad4all', 'notification', 'accessibility');
  136. foreach ($priority as $file) {
  137. $list = SubLanguageManager::get_all_language_variable_in_file($path.$file.'.inc.php', true);
  138. foreach ($list as $var => $trad) {
  139. $vars[$var] = $file.'.inc.php';
  140. }
  141. }
  142. $files = scandir($path);
  143. foreach ($files as $file) {
  144. if (substr($file,0,1) == '.' or in_array($file,$priority)) {
  145. continue;
  146. }
  147. $list = SubLanguageManager::get_all_language_variable_in_file($path.$file, true);
  148. foreach ($list as $var => $trad) {
  149. $vars[$var] = $file;
  150. }
  151. }
  152. return $vars;
  153. }
  154. }