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

View File

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

View File

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

View File

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