langstats.class.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. {
  18. public $db; //database connector
  19. public $error; //errores almacenados
  20. public $db_type = 'sqlite';
  21. public function __construct($file = '')
  22. {
  23. switch ($this->db_type) {
  24. case 'sqlite':
  25. if (!class_exists('SQLite3')) {
  26. $this->error = 'SQLiteNotAvailable';
  27. return false; //cannot use if sqlite not installed
  28. }
  29. if (empty($file)) {
  30. $file = api_get_path(SYS_ARCHIVE_PATH).'/langstasdb';
  31. }
  32. if (is_file($file) && is_writeable($file)) {
  33. $this->db = new SQLite3($file, SQLITE3_OPEN_READWRITE);
  34. } else {
  35. try {
  36. $this->db = new SQLite3($file);
  37. } catch (Exception $e) {
  38. $this->error = 'DatabaseCreateError';
  39. error_log('Exception: '.$e->getMessage());
  40. return false;
  41. }
  42. $err = $this->db->exec(
  43. 'CREATE TABLE lang_freq ('
  44. . ' id integer PRIMARY KEY AUTOINCREMENT, ' //autoincrement in SQLITE
  45. . ' term_name text, term_file text, term_count integer default 0)'
  46. );
  47. if ($err === false) {
  48. $this->error = 'CouldNotCreateTable';
  49. return false;
  50. }
  51. $err = $this->db->exec(
  52. 'CREATE INDEX lang_freq_terms_idx ON lang_freq(term_name, term_file)'
  53. );
  54. if ($err === false) {
  55. $this->error = 'CouldNotCreateIndex';
  56. return false;
  57. }
  58. // Table and index created, move on.
  59. }
  60. break;
  61. case 'mysql': //implementation not finished
  62. if (!function_exists('mysql_connect')) {
  63. $this->error = 'SQLiteNotAvailable';
  64. return false; //cannot use if sqlite not installed
  65. }
  66. $err = Database::query('SELECT * FROM lang_freq');
  67. if ($err === false) { //the database probably does not exist, create it
  68. $err = Database::query(
  69. 'CREATE TABLE lang_freq ('
  70. . ' id int PRIMARY KEY AUTO_INCREMENT, '
  71. . ' term_name text, term_file text default \'\', term_count int default 0)'
  72. );
  73. if ($err === false) {
  74. $this->error = 'CouldNotCreateTable';
  75. return false;
  76. }
  77. } // if no error, we assume the table exists
  78. break;
  79. }
  80. return $this->db;
  81. }
  82. /**
  83. * Add a count for a specific term
  84. * @param string The language term used
  85. * @param string The file from which the language term came from
  86. * @return boolean true
  87. */
  88. public function add_use($term, $term_file = '')
  89. {
  90. $term = $this->db->escapeString($term);
  91. $term_file = $this->db->escapeString($term_file);
  92. $sql = "SELECT id, term_name, term_file, term_count FROM lang_freq WHERE term_name='$term' and term_file='$term_file'";
  93. $ress = $this->db->query($sql);
  94. if ($ress === false) {
  95. $this->error = 'CouldNotQueryTermFromTable';
  96. return false;
  97. }
  98. $i = 0;
  99. while ($row = $ress->fetchArray(SQLITE3_BOTH)) {
  100. $num = $row[3];
  101. $num++;
  102. $res = $this->db->query(
  103. 'UPDATE lang_freq SET term_count = '.$num.' WHERE id = '.$row[0]
  104. );
  105. if ($res === false) {
  106. $this->error = 'CouldNotUpdateTerm';
  107. return false;
  108. } else {
  109. return $row[0];
  110. }
  111. $i++;
  112. }
  113. if ($i == 0) {
  114. //No term found in the table, register as new term
  115. $resi = $this->db->query(
  116. "INSERT INTO lang_freq(term_name, term_file, term_count) VALUES ('$term', '$term_file', 1)"
  117. );
  118. if ($resi === false) {
  119. $this->error = 'CouldNotInsertRow';
  120. return false;
  121. } else {
  122. return $this->db->lastInsertRowID();
  123. }
  124. }
  125. }
  126. /**
  127. * Function to get a list of the X most-requested terms
  128. * @param integer Limit of terms to show
  129. * @return array List of most requested terms
  130. */
  131. public function get_popular_terms($num = 1000)
  132. {
  133. $res = $this->db->query(
  134. 'SELECT * FROM lang_freq ORDER BY term_count DESC LIMIT '.$num
  135. );
  136. $list = array();
  137. while ($row = $res->fetchArray()) {
  138. $list[] = $row;
  139. }
  140. return $list;
  141. }
  142. /**
  143. * Clear all records in lang_freq
  144. * @return boolean true
  145. */
  146. public function clear_all()
  147. {
  148. $res = sqlite_query($this->db, 'DELETE FROM lang_freq WHERE 1=1');
  149. return $list;
  150. }
  151. /**
  152. * Returns an array of all the language variables with their corresponding
  153. * file of origin. This function tolerates a certain rate of error due to
  154. * the duplication of variables in language files.
  155. * @return array variable => origin file
  156. */
  157. public function get_variables_origin()
  158. {
  159. $path = api_get_path(SYS_LANG_PATH).'english/';
  160. $vars = array();
  161. $priority = array('trad4all');
  162. foreach ($priority as $file) {
  163. $list = SubLanguageManager::get_all_language_variable_in_file(
  164. $path.$file.'.inc.php',
  165. true
  166. );
  167. foreach ($list as $var => $trad) {
  168. $vars[$var] = $file.'.inc.php';
  169. }
  170. }
  171. $files = scandir($path);
  172. foreach ($files as $file) {
  173. if (substr($file, 0, 1) == '.' or in_array($file, $priority)) {
  174. continue;
  175. }
  176. $list = SubLanguageManager::get_all_language_variable_in_file(
  177. $path.$file,
  178. true
  179. );
  180. foreach ($list as $var => $trad) {
  181. $vars[$var] = $file;
  182. }
  183. }
  184. return $vars;
  185. }
  186. }