Just Don't Repeat Yourself (DRY)

This commit is contained in:
Sobak
2014-06-24 18:52:55 +02:00
parent 2ac90cbaeb
commit 26fba64eed
4 changed files with 87 additions and 297 deletions

View File

@ -187,135 +187,50 @@ function get_misstags($idx, $lang)
return $tmp;
}
// Return a string
function translator_get_wip($idx, $lang)
{
$sql = 'SELECT
COUNT(name) AS total,
person as nick
FROM
wip
WHERE
lang="' . $lang . '"
GROUP BY
nick
ORDER BY
nick';
$result = $idx->query($sql);
$tmp = array();
while ($r = $result->fetchArray()) {
$tmp[$r['nick']] = $r['total'];
/**
* Returns translators' stats of specified $lang
* Replaces old translator_get_wip(), translator_get_old(),
* translator_get_critical() and translator_get_uptodate() functions
*
* @param string $status one of [uptodate, old, critical, wip]
* @return array
*/
function get_translators_stats($idx, $lang, $status) {
if ($status == 'wip') { // special case, ehh; does anyone still use this status?
$sql = "SELECT COUNT(name) AS total, person AS maintainer
FROM wip
WHERE lang = '$lang'
GROUP BY maintainer";
}
return $tmp;
}
else {
$sql = "SELECT COUNT(a.name) AS total, a.maintainer
FROM files a
LEFT JOIN files b ON a.name = b.name AND a.dir = b.dir
WHERE a.lang = '$lang' AND b.lang = 'en' AND a.size IS NOT NULL AND ";
function translator_get_old($idx, $lang)
{
$sql = 'SELECT
COUNT(a.name) AS total,
a.maintainer as maintainer
FROM
files a
LEFT JOIN
files b
ON
a.name = b.name
AND
a.dir = b.dir
WHERE
a.lang="' . $lang . '"
AND
b.lang="en"
AND
b.revision != a.revision
AND
b.size - a.size < ' . ALERT_SIZE . '
AND
(b.mdate - a.mdate) / 86400 < ' . ALERT_DATE . '
AND
a.size is not NULL
GROUP BY
a.maintainer';
if ($status == 'uptodate') {
$sql .= 'a.revision = b.revision';
}
elseif ($status == 'old') {
$sql .= 'b.revision != a.revision AND b.size - a.size < ' . ALERT_SIZE . ' AND (b.mdate - a.mdate) / 86400 < ' . ALERT_DATE;
}
elseif ($status == 'critical') {
$sql .= 'b.revision != a.revision AND (b.size - a.size >= ' . (1024 * ALERT_SIZE) . ' OR (b.mdate - a.mdate) / 86400 >= ' . ALERT_DATE . ')';
}
$sql .= ' GROUP BY a.maintainer';
}
$result = $idx->query($sql);
$tmp = array();
while ($r = $result->fetchArray()) {
$tmp[$r['maintainer']] = $r['total'];
}
return $tmp;
}
function translator_get_critical($idx, $lang)
{
$sql = 'SELECT
COUNT(a.name) AS total,
a.maintainer as maintainer
FROM
files a
LEFT JOIN
files b
ON
a.name = b.name
AND
a.dir = b.dir
WHERE
a.lang="' . $lang . '"
AND
b.lang="en"
AND (
b.revision != a.revision
AND (
b.size - a.size >= ' . (1024 * ALERT_SIZE) . '
OR
(b.mdate - a.mdate) / 86400 >= ' . ALERT_DATE . '
)
)
AND
a.size is not NULL
GROUP BY
a.maintainer
ORDER BY
a.maintainer';
$result = $idx->query($sql);
$tmp = array();
while ($r = $result->fetchArray()) {
$tmp[$r['maintainer']] = $r['total'];
}
return $tmp;
}
function translator_get_uptodate($idx, $lang)
{
$sql = 'SELECT
COUNT(a.name) AS total,
a.maintainer as maintainer
FROM
files a
LEFT JOIN
files b
ON
a.name = b.name
AND
a.dir = b.dir
WHERE
a.lang="' . $lang . '"
AND
b.lang="en"
AND
a.revision = b.revision
GROUP BY
a.maintainer
ORDER BY
a.maintainer';
$result = $idx->query($sql);
$tmp = array();
while ($r = $result->fetchArray()) {
$tmp[$r['maintainer']] = $r['total'];
}
return $tmp;
}
function get_translators($idx, $lang)
{
$sql = "SELECT nick, name, mail, svn FROM translators WHERE lang = '$lang' ORDER BY nick COLLATE NOCASE";
@ -327,172 +242,47 @@ function get_translators($idx, $lang)
return $persons;
}
// Return an array
function get_stats_uptodate($idx, $lang)
{
$sql = 'SELECT
COUNT(a.name) as total,
SUM(c.size) as size
FROM
files a
LEFT JOIN
files c
ON
c.name = a.name
AND
c.dir = a.dir
WHERE
a.lang="' . $lang . '"
AND
c.lang="en"
AND
a.revision = c.revision';
/**
* Returns statistics of specified $lang
* Replaces old get_stats_uptodate(), get_stats_old(),
* get_stats_critical(), get_stats_wip(), get_stats_notrans()
* and get_stats_notag() functions
*
* @param string $status one of [uptodate, old, critical, wip, notrans, norev]
* @return array
*/
function get_stats($idx, $lang, $status) {
if ($status == 'wip') { // special case, ehh; does anyone still use this status?
$sql = "SELECT COUNT(*) AS total, 0 AS size
FROM wip
WHERE lang = '$lang'";
}
else {
$sql = "SELECT COUNT(a.name) AS total, SUM(b.size) AS size
FROM files a
LEFT JOIN files b ON a.name = b.name AND a.dir = b.dir
WHERE a.lang = '$lang' AND b.lang = 'en' AND ";
$result = $idx->query($sql);
$r = $result->fetchArray();
$result = array($r['total'], $r['size']);
return $result;
}
if ($status == 'uptodate') {
$sql .= 'a.revision = b.revision';
}
elseif ($status == 'old') {
$sql .= 'b.revision != a.revision AND b.size - a.size < ' . ALERT_SIZE . ' AND (b.mdate - a.mdate) / 86400 < ' . ALERT_DATE . ' AND a.size IS NOT NULL';
}
elseif ($status == 'critical') {
$sql .= 'b.revision != a.revision AND (b.size - a.size >= ' . (1024 * ALERT_SIZE) . ' OR (b.mdate - a.mdate) / 86400 >= ' . ALERT_DATE . ') AND a.revision != "n/a" AND a.size IS NOT NULL';
}
elseif ($status == 'norev') {
$sql .= '(a.revision IS NULL OR a.revision = "n/a") AND a.size IS NOT NULL';
}
elseif ($status == 'notrans') {
$sql .= 'a.revision IS NULL AND a.size IS NULL';
}
}
function get_stats_critical($idx, $lang)
{
$sql = 'SELECT
COUNT(a.name) as total,
sum(b.size) as size
FROM
files a,
dirs d
LEFT JOIN
files b
ON
a.dir = b.dir
AND
a.name = b.name
WHERE
a.lang="' . $lang .'"
AND
b.lang="en"
AND (
b.revision != a.revision
AND
(
(b.size - a.size) >= ' . ALERT_SIZE . '
OR
(b.mdate - a.mdate) / 86400 >= ' . ALERT_DATE . '
)
)
AND
a.revision != "n/a"
AND
a.size is not NULL
AND
a.dir = d.id';
$result = $idx->query($sql)->fetchArray();
$result = $idx->query($sql);
$r = $result->fetchArray();
$result = array($r['total'], $r['size']);
return $result;
}
// Return an array
function get_stats_old($idx, $lang)
{
$sql = 'SELECT
COUNT(a.name) as total,
sum(b.size) as size
FROM
files a,
dirs d
LEFT JOIN
files b
ON
a.dir = b.dir
AND
a.name = b.name
WHERE
a.lang="' . $lang .'"
AND
b.lang="en"
AND
b.revision != a.revision
AND
(b.size - a.size) < ' . ALERT_SIZE . '
AND
(b.mdate - a.mdate) / 86400 <= ' . ALERT_DATE . '
AND
a.size is not NULL
AND
a.dir = d.id';
$result = $idx->query($sql);
$r = $result->fetchArray();
$result = array($r['total'], $r['size']);
return $result;
}
// Returns number of untranslated files for specified $lang
function get_stats_notrans($idx, $lang)
{
$sql = "SELECT COUNT(a.name) AS total, SUM(b.size) as size
FROM files a, dirs d
LEFT JOIN files b ON a.dir = b.dir AND a.name = b.name
WHERE a.lang = '$lang' AND b.lang='en' AND a.revision IS NULL AND a.size IS NULL AND a.dir = d.id";
$result = $idx->query($sql);
$r = $result->fetchArray();
return array($r['total'], $r['size']);
}
function get_stats_wip($idx, $lang)
{
$sql = 'SELECT
COUNT(*) as total,
0 as size
FROM
wip
WHERE
lang = "' . $lang . '"';
$result = $idx->query($sql);
$r = $result->fetchArray();
return array($r['total'], $r['size']);
}
// Return an array
function get_stats_notag($idx, $lang)
{
$sql = 'SELECT
COUNT(a.name) as total,
sum(b.size) as size
FROM
files a,
dirs d
LEFT JOIN
files b
ON
a.dir = b.dir
AND
a.name = b.name
WHERE
a.lang="' . $lang .'"
AND
b.lang="en"
AND
(a.revision is NULL OR a.revision = "n/a")
AND
a.size is not NULL
AND
a.dir = d.id';
$result = $idx->query($sql);
$r = $result->fetchArray();
$result = array($r['total'], $r['size']);
return $result;
return array($result['total'], $result['size']);
}
function gen_date($file)

View File

@ -29,19 +29,19 @@ echo "Graphs generated in {$time}s\n";
function generate_image($lang, $idx) {
global $LANGUAGES;
$up_to_date = get_stats_uptodate($idx, $lang);
$up_to_date = get_stats($idx, $lang, 'uptodate');
$up_to_date = $up_to_date[0];
//
$critical = @get_stats_critical($idx, $lang);
$critical = @get_stats($idx, $lang, 'critical');
$critical = $critical[0];
//
$old = @get_stats_old($idx, $lang);
$old = @get_stats($idx, $lang, 'old');
$old = $old[0];
//
$missing = get_stats_notrans($idx, $lang);
$missing = get_stats($idx, $lang, 'notrans');
$missing = $missing[0];
//
$no_tag = @get_stats_notag($idx, $lang);
$no_tag = @get_stats($idx, $lang, 'norev');
$no_tag = $no_tag[0];
$data = array(

View File

@ -12,7 +12,7 @@ sort($language);
$files_EN = count_en_files($idx);
foreach ($language as $lang) {
$tmp = get_stats_uptodate($idx, $lang);
$tmp = get_stats($idx, $lang, 'uptodate');
$percent_tmp[] = round($tmp[0] * 100 / $files_EN);
$legend_tmp[] = $lang;

View File

@ -51,10 +51,10 @@ switch($tool) {
echo '<p>Error: no translators info found in database.</p>';
}
else {
$uptodate = translator_get_uptodate($dbhandle, $lang);
$old = translator_get_old($dbhandle, $lang);
$critical = translator_get_critical($dbhandle, $lang);
$wip = translator_get_wip($dbhandle, $lang);
$uptodate = get_translators_stats($dbhandle, $lang, 'uptodate');
$old = get_translators_stats($dbhandle, $lang, 'old');
$critical = get_translators_stats($dbhandle, $lang, 'critical');
$wip = get_translators_stats($dbhandle, $lang, 'wip');
foreach($translators as $nick =>$data) {
$files_w[$nick] = array('uptodate' => '', 'old' =>'', 'critical' => '', 'norev' => '', 'wip' => '');
@ -204,12 +204,12 @@ TRANSLATORS_HEAD;
REV_WIP => array(0,0)
);
$file_summary_array[REV_WIP] = get_stats_wip($dbhandle, $lang);
$file_summary_array[REV_CRITICAL] = get_stats_critical($dbhandle, $lang);
$file_summary_array[REV_UPTODATE] = get_stats_uptodate($dbhandle, $lang);
$file_summary_array[REV_OLD] = get_stats_old($dbhandle, $lang);
$file_summary_array[REV_NOREV] = get_stats_notag($dbhandle, $lang);
$file_summary_array[REV_NOTRANS] = get_stats_notrans($dbhandle, $lang);
$file_summary_array[REV_WIP] = get_stats($dbhandle, $lang, 'wip');
$file_summary_array[REV_CRITICAL] = get_stats($dbhandle, $lang, 'critical');
$file_summary_array[REV_UPTODATE] = get_stats($dbhandle, $lang, 'uptodate');
$file_summary_array[REV_OLD] = get_stats($dbhandle, $lang, 'old');
$file_summary_array[REV_NOREV] = get_stats($dbhandle, $lang, 'norev');
$file_summary_array[REV_NOTRANS] = get_stats($dbhandle, $lang, 'notrans');
echo '<table border="0" cellpadding="4" cellspacing="1" style="text-align:center;">';
echo '<tr><th>File status type</th><th>Number of files</th><th>Percent of files</th><th>Size of files (kB)</th><th>Percent of size</th></tr>';