langstats.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This script prints a list of most used language terms. The registration of
  5. * frequency for language variables is a very heavy operation.
  6. * To enable, add "$_configuration['language_measure_frequency' ] = 1;" at the
  7. * end of main/inc/conf/configuration.php. Remove when done.
  8. * Add ?output=1 to the URL to generate languag files in /tmp/lang/ with just
  9. * the number of terms you want.
  10. */
  11. /**
  12. * Requires.
  13. */
  14. die();
  15. require_once '../../inc/global.inc.php';
  16. require_once 'langstats.class.php';
  17. /**
  18. * Init.
  19. */
  20. $terms_limit = 10000 + 50;
  21. $x_most_popular = 2000;
  22. $output = false;
  23. $ls = new langstats();
  24. if ($ls === false) {
  25. exit($ls->error);
  26. }
  27. $list = $ls->get_popular_terms($x_most_popular);
  28. if ($_GET['output'] == 1) {
  29. $output = true;
  30. $variables_origin = $ls->get_variables_origin();
  31. }
  32. /**
  33. * Display.
  34. */
  35. if (count($list) == 0) {
  36. echo 'No terms loaded so far';
  37. }
  38. if (count($list) > 0) {
  39. $i = 1;
  40. $j = 1;
  41. $k = 0;
  42. $files = [];
  43. $trans = [];
  44. echo 'Number of records: '.count($list).'<br />';
  45. echo '<table><tr><th>Index</th><th>Registration order</th><th>Term</th>'.($output == 1 ? '<th>Origin</th>' : '').'<th>Count</th></tr>';
  46. foreach ($list as $elem) {
  47. if ($k > $terms_limit) {
  48. break;
  49. }
  50. $fixed_elem = $elem;
  51. if ($output) {
  52. if (empty($variables_origin[$elem['term_name']]) && !empty($variables_origin['lang'.$elem['term_name']])) {
  53. $fixed_elem = ['id' => $elem['id'], 'term_name' => 'lang'.$elem['term_name'], 'term_count' => $elem['term_count']];
  54. }
  55. if (empty($variables_origin[$fixed_elem['term_name']])) {
  56. continue;
  57. }
  58. $files[$variables_origin[$fixed_elem['term_name']]][] = $fixed_elem['term_name'];
  59. $translation = get_lang($fixed_elem['term_name']);
  60. $k += str_word_count($translation);
  61. $trans[$fixed_elem['term_name']] = $translation;
  62. $j++;
  63. }
  64. echo '<tr><td>', $i,
  65. '</td><td>', $fixed_elem['id'],
  66. '</td><td>', $fixed_elem['term_name'];
  67. if ($output) {
  68. echo '</td><td>'.$variables_origin[$fixed_elem['term_name']];
  69. }
  70. echo '</td><td>', $fixed_elem['term_count'], '</td></tr>';
  71. $i++;
  72. }
  73. echo '</table>';
  74. if ($output) {
  75. @mkdir('/tmp/lang');
  76. foreach ($files as $file => $terms) {
  77. @touch('/tmp/lang/'.$file);
  78. file_put_contents('/tmp/lang/'.$file, "<?php".PHP_EOL);
  79. foreach ($terms as $term) {
  80. file_put_contents('/tmp/lang/'.$file, '$'.$term.' = "'.str_replace('"', '\"', $trans[$term]).'";'.PHP_EOL, FILE_APPEND);
  81. }
  82. }
  83. }
  84. }