com web/doc: Just Don't Repeat Yourself (DRY): include/lib_re vcheck.inc.php scripts/gen_picture_info.php scrip ts/gen_picture_info_all_lang.php www/revcheck.php

[email protected] Tue, 24 Jun 2014 16:52:55 +0000
Newsgroups php.doc.web
Message-ID <[email protected]>
Commit:    26fba64eedd6faadb62a0411d2bbe4ce1f5061fc
Author:    Sobak <[email protected]>         Tue, 24 Jun 2014 18:52:55 +0200
Parents:   2ac90cbaebdc0100a9aeffa0b1ab60fed537c5d5
Branches:  master

Link:       http://git.php.net/?p=web/doc.git;a=commitdiff;h=26fba64eedd6faadb62a0411d2bbe4ce1f5061fc

Log:
Just Don't Repeat Yourself (DRY)

Changed paths:
  M  include/lib_revcheck.inc.php
  M  scripts/gen_picture_info.php
  M  scripts/gen_picture_info_all_lang.php
  M  www/revcheck.php
diff_26fba64eedd6faadb62a0411d2bbe4ce1f5061fc.txt (text/plain, 13 KB)
diff --git a/include/lib_revcheck.inc.php b/include/lib_revcheck.inc.php
index 2beafd4..b1f9e41 100644
--- a/include/lib_revcheck.inc.php
+++ b/include/lib_revcheck.inc.php
@@ -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;
-}
-
-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';
-
-    $result = $idx->query($sql);
-    $tmp = array();
-    while ($r = $result->fetchArray()) {
-        $tmp[$r['maintainer']] = $r['total'];
+    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 ";
+
+        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';
     }
-    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'; 
-
-    $result = $idx->query($sql);
-    $r = $result->fetchArray();
-    $result = array($r['total'], $r['size']);
-    return $result;
-}
-
-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);
-
-    $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';
+/**
+ * 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 ";
+
+        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';
+        }
+    }
 
-    $result = $idx->query($sql);
+    $result = $idx->query($sql)->fetchArray();
 
-    $r = $result->fetchArray();
-    $result = array($r['total'], $r['size']);
-    return $result;
+    return array($result['total'], $result['size']);
 }
 
 function gen_date($file)
diff --git a/scripts/gen_picture_info.php b/scripts/gen_picture_info.php
index f6b8996..70bafa0 100644
--- a/scripts/gen_picture_info.php
+++ b/scripts/gen_picture_info.php
@@ -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(
diff --git a/scripts/gen_picture_info_all_lang.php b/scripts/gen_picture_info_all_lang.php
index cec70a0..1972b2e 100644
--- a/scripts/gen_picture_info_all_lang.php
+++ b/scripts/gen_picture_info_all_lang.php
@@ -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;
diff --git a/www/revcheck.php b/www/revcheck.php
index 5938952..e690bfe 100644
--- a/www/revcheck.php
+++ b/www/revcheck.php
@@ -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>';