[web-doc] master: Show additions and deletions. Thanks, Lucas Azevedo. Closes GH-30

[email protected] (Nilgün Belma Bugüner) Tue, 11 Jan 2022 04:44:40 +0000
Newsgroups php.doc.web
Message-ID <[email protected]>
Author: Nilgün Belma Bugüner (nilgun)
Date: 2022-01-11T07:44:34+03:00

Commit: https://github.com/php/web-doc/commit/470f728a919d12046d915559067a623480bfd499
Raw diff: https://github.com/php/web-doc/commit/470f728a919d12046d915559067a623480bfd499.diff

Show additions and deletions. Thanks, Lucas Azevedo. Closes GH-30

Changed paths:
  M  include/lib_revcheck.inc.php
  M  scripts/rev.php
  M  www/revcheck.php


Diff:

diff --git a/include/lib_revcheck.inc.php b/include/lib_revcheck.inc.php
index 02f34d8..9a08fa9 100644
--- a/include/lib_revcheck.inc.php
+++ b/include/lib_revcheck.inc.php
@@ -48,7 +48,7 @@ function get_dirs($idx, $lang) {
 // return an array with the outdated files; can be optionally filtered by user or dir
 function get_outdated_files($idx, $lang, $filter = null, $value = null)
 {
-    $sql = "SELECT a.status, a.name AS file, a.maintainer, c.revision AS en_rev, a.revision AS trans_rev, b.path AS dir
+    $sql = "SELECT a.status, a.name AS file, a.maintainer, a.additions, a.deletions, c.revision AS en_rev, a.revision AS trans_rev, b.path AS dir
     FROM translated a, dirs b, enfiles c
     WHERE a.lang = '$lang'
       AND c.name = a.name AND b.id = a.id AND b.id = c.id
@@ -73,7 +73,9 @@ function get_outdated_files($idx, $lang, $filter = null, $value = null)
         'trans_rev' => $r['trans_rev'],
         'status' => $r['status'],
         'maintainer' => $r['maintainer'],
-        'name' => $r['dir']);
+        'name' => $r['dir'],
+        'additions' => $r['additions'],
+        'deletions' => $r['deletions']);
     }
 
     return $tmp;
@@ -252,6 +254,7 @@ function showdiff ()
         $arg_f = escapeshellarg($gitfile);
         $file = `git diff {$arg_h} -- {$arg_f}`;
         chdir( $cwd );
+        if (!$file) return;
         $raw = htmlspecialchars( $file, ENT_XML1, 'UTF-8' );
         $lines = explode ( "\n" , $raw );
         echo "<div style='font: .75rem monospace; overflow-wrap:break-word; line-height: 1.8; border: 1px solid #ccc; border-radius: 4px;'>";
diff --git a/scripts/rev.php b/scripts/rev.php
index 4a685af..662d7f8 100644
--- a/scripts/rev.php
+++ b/scripts/rev.php
@@ -77,6 +77,8 @@
   maintainer TEXT,
   status TEXT,
   syncStatus TEXT,
+  additions INT,
+  deletions INT,
   UNIQUE(lang, id, name)
 );
 
@@ -514,10 +516,21 @@ function captureGitValues( & $output )
             $SQL_BUFF .= "INSERT INTO Untranslated VALUES ($id, '$lang',
             '$en->name', $size);\n";
         } else {
+            $additions = $deletions = 0;
             if ( $en->hash == $trFile->hash ){
                 $trFile->syncStatus = FileStatusEnum::TranslatedOk;
-            } elseif ( strlen( $trFile->hash ) == 40 ) {
+            } elseif ( $trFile->hash != null and strlen( $trFile->hash ) == 40 ) {
                 $trFile->syncStatus = FileStatusEnum::TranslatedOld;
+
+                $cwd = getcwd();
+                chdir( $DOCS . 'en' );
+                $subject = `git diff --numstat $trFile->hash -- {$filename}`;
+                chdir( $cwd );
+                if ( $subject ) {
+                   preg_match('/(\d+)\s+(\d+)/', $subject, $matches);
+                   if ($matches)
+                       [, $additions, $deletions] = $matches;
+                }
             }
             if ( $trFile->completion != null && $trFile->completion != "ready" )
                 $trFile->syncStatus = FileStatusEnum::TranslatedWip;
@@ -531,7 +544,7 @@ function captureGitValues( & $output )
             }
             $SQL_BUFF .= "INSERT INTO translated VALUES ($id, '$lang',
             '$en->name', '$trFile->hash', $size, '$trFile->maintainer',
-            '$trFile->completion', '$trFile->syncStatus');\n";
+            '$trFile->completion', '$trFile->syncStatus', $additions, $deletions);\n";
         }
     }
 }
diff --git a/www/revcheck.php b/www/revcheck.php
index bf1de88..42774f8 100644
--- a/www/revcheck.php
+++ b/www/revcheck.php
@@ -327,6 +327,7 @@
 <table border="0" cellpadding="4" cellspacing="1" style="text-align:center">
 <tr>
 <th rowspan="2">Translated file</th>
+<th rowspan="2">Changes</th>
 <th colspan="2">Revision</th>
 <th rowspan="2">Maintainer</th>
 <th rowspan="2">Status</th>
@@ -335,14 +336,14 @@
 <th>en</th>
 <th>$lang</th>
 </tr>
-<tr><th colspan="5">{$outdated[0]['name']}</th></tr>
+<tr><th colspan="6">{$outdated[0]['name']}</th></tr>
 END_OF_MULTILINE;
         $last_dir = false;
         $prev_name = $outdated[0]['name'];
 
         foreach ($outdated as $r) {
             if ($r['name'] != $prev_name) {
-            echo '<tr><th colspan="5">'.$r['name'].'</th></tr>';
+            echo '<tr><th colspan="6">'.$r['name'].'</th></tr>';
             $prev_name = $r['name'];
         }
 
@@ -357,9 +358,6 @@
             $key = $r['file'];
         //plaintext -color
         $d1 = "?p=plain&amp;lang={$lang}&amp;hbp={$r['trans_rev']}&amp;f=$key&amp;c=on";
-        // GitHub web diff -- May not work with very old commits
-        //$kh = hash( 'sha256' , $key );
-        //"https://github.com/php/doc-en/compare/{$r['trans_rev']}..{$r['en_rev']}#diff-{$kh}";
         //plaintext
         $d2 = "?p=plain&amp;lang={$lang}&amp;hbp={$r['trans_rev']}&amp;f=$key&amp;c=off";
 
@@ -369,9 +367,12 @@
 
         $nm = "<a href='$d2'>{$r['file']}</a> <a href='$d1'>[diff]</a>";
 
+        $ch = "<span style='color: darkgreen;'>+{$r['additions']}</span> <span style='color: firebrick;'>-{$r['deletions']}</span>";
+
         // Write out the line for the current file (get file name shorter)
-        echo '<tr>'.
-        "<td style='white-space:nowrap'>{$nm}</td>".
+        echo '<tr>' .
+        "<td style='white-space:nowrap'>{$nm}</td>" .
+        "<td style='font-size: 14px; text-align: right;'>{$ch}</td>" .
         "<td>{$h1}</td>" .
         "<td>{$h2}</td>" .
         "<td> {$r['maintainer']}</td>" .