123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- #!/bin/php
- <?php
- /*
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- const N="\n";
- const SNAME='vertrad.php';
- const SVERS='0.0.1';
- const RED="\33[1;31m";
- const NORM="\033[0m";
- $opts=[
- 'files'=>[],
- 'prompt'=>false
- ];
- $help=
- 'SYNOPSIS
- '.SNAME.' [options] <file[s]>
- DESCRIPTION
- This is '.SNAME.' v'.SVERS.', a CLI php script that ...
- OPTIONS
- -p, --prompt
- Prompt for editing on each suspect pair of msgid-msgstr.
- -h, --help
- Show this help text and exit.
- ...
- NOTES
- ...
- EXIT VALUES
- 0: regular run
- 1: fatal error
- DISCLAIMER AND LICENSE
- This program comes with ABSOLUTELY NO WARRANTY; for details see the source.
- This is free software, and you are welcome to redistribute it under certain
- conditions; see <http://www.gnu.org/licenses/> for details.'.N;
- for ($i=1; $i<$argc; $i++) {
- if ($argv[$i][0]=='-') {
- switch ($argv[$i]) {
- case '--makereadme':
- file_put_contents(__DIR__.'/README.md','```text'.N.$help.'```'.N);
- exit(0);
- break;
- case '-p':
- case '--prompt':
- $opts['prompt']=true;
- break;
- case '-h':
- case '--help':
- echo($help);
- exit(0);
- break;
- default:
- fwrite(STDERR,'Error: «'.$argv[$i].'» is not a valid option.'.N);
- exit(1);
- }
- } elseif (is_file($argv[$i]) && is_readable($argv[$i])) {
- $opts['files'][]=$argv[$i];
- } else {
- fwrite(STDERR,'Error: «'.$argv[$i].'» is not a readable file.'.N);
- exit(1);
- }
- }
- //print_r($opts); exit(0);
- if (count($opts['files'])==0) {
- fwrite(STDERR,'Error: you have to specify at least one file.'.N);
- exit(1);
- }
- $ts=0;
- foreach ($opts['files'] as $file) {
- $cont=@file($file,FILE_IGNORE_NEW_LINES);
- if ($cont!==false) {
- $s=0;
- $i=0;
- $c=count($cont);
- while ($i<$c) {
- if (preg_match('/^msgid "/u',$cont[$i])===1) {
- $msgid=$cont[$i];
- $i++;
- $msgstr=$cont[$i];
- if ($msgstr!='msgstr ""') {
- $show=false;
- if (preg_match_all('/\\\"/u',$msgid)!=preg_match_all('/\\\"/u',$msgstr)) $show=true;
- if (preg_match_all('/</u',$msgid)!=preg_match_all('/</u',$msgstr)) $show=true;
- if (preg_match_all('/>/u',$msgid)!=preg_match_all('/>/u',$msgstr)) $show=true;
- if (preg_match_all('/\'/u',$msgid)!=preg_match_all('/\'/u',$msgstr)) $show=true;
- if ($show) {
- $s++;
- echo('«'.$file.'»:'.$i.N.hil($msgid).N.hil($msgstr).N);
- if ($opts['prompt']) {
- $inp='+';
- while (!in_array($inp,['y','n','e','']))
- $inp=strtolower(prompt('Do you want to edit it? [(y)es/(N)o/(e)xit] '));
- if ($inp=='y')
- //system('nano +'.($i+1).' '.escapeshellarg($file).' > `tty`');
- system('kate -l '.($i+1).' '.escapeshellarg($file));
- elseif ($inp=='e')
- exit(0);
- }
- echo(N);
- }
- }
- }
- $i++;
- }
- //echo('«'.$file.'» total suspects: '.$s.N);
- $summ[]='«'.$file.'» total suspects: '.$s;
- $ts+=$s;
- } else {
- fwrite(STDERR,'Error: could not open «'.$file.'».'.N);
- exit(1);
- }
- }
- foreach ($summ as $row)
- echo($row.N);
- echo('Grand total of suspects: '.$ts.N);
- exit(0);
- function hil($msg) {
- $msg=preg_replace('/\\\"/u',RED.'\\\"'.NORM,$msg);
- $msg=preg_replace('/</u',RED.'<'.NORM,$msg);
- $msg=preg_replace('/>/u',RED.'>'.NORM,$msg);
- $msg=preg_replace('/\'/u',RED.'\''.NORM,$msg);
- return($msg);
- }
- function prompt($prompt) {
- echo($prompt);
- $line=stream_get_line(STDIN, 1024, PHP_EOL);
- return(trim($line));
- }
- ?>
|