langstats.class.php 6.9 KB

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