Passed
Push — 1.11.x ( 106594...56cdc0 )
by Angel Fernando Quiroz
08:52
created

main/cron/lang/langstats.php (2 issues)

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
exit();
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');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mkdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

75
        /** @scrutinizer ignore-unhandled */ @mkdir('/tmp/lang');

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
76
        foreach ($files as $file => $terms) {
77
            @touch('/tmp/lang/'.$file);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for touch(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

77
            /** @scrutinizer ignore-unhandled */ @touch('/tmp/lang/'.$file);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
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
}
85