File: RevcheckRun.php

package info (click to toggle)
php-doc 20250827~git.abe740d%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 71,968 kB
  • sloc: xml: 985,760; php: 25,504; javascript: 671; sh: 177; makefile: 37
file content (237 lines) | stat: -rw-r--r-- 8,584 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
/**
 *  +----------------------------------------------------------------------+
 *  | Copyright (c) 1997-2023 The PHP Group                                |
 *  +----------------------------------------------------------------------+
 *  | This source file is subject to version 3.01 of the PHP license,      |
 *  | that is bundled with this package in the file LICENSE, and is        |
 *  | available through the world-wide-web at the following url:           |
 *  | https://www.php.net/license/3_01.txt.                                |
 *  | If you did not receive a copy of the PHP license and are unable to   |
 *  | obtain it through the world-wide-web, please send a note to          |
 *  | license@php.net, so we can mail you a copy immediately.              |
 *  +----------------------------------------------------------------------+
 *  | Authors:     André L F S Bacci <ae php.net>                          |
 *  +----------------------------------------------------------------------+
 *  | Description: Calculate translation sync/diff status from two         |
 *  |              directories.                                            |
 *  +----------------------------------------------------------------------+
 */

require_once __DIR__ . '/all.php';

class RevcheckRun
{
    public string $sourceDir;
    public string $targetDir;
    public RevcheckFileList $sourceFiles;
    public RevcheckFileList $targetFiles;

    public array $filesOk = [];
    public array $filesOld = [];
    public array $filesRevtagProblem = [];
    public array $filesUntranslated = [];
    public array $filesNotInEn = [];
    public array $filesWip = [];

    public array $qaList = [];
    public RevcheckData $revData;
    private int $slowPathCount = 0;

    function __construct( string $sourceDir , string $targetDir , bool $writeResults = false )
    {
        $this->sourceDir = $sourceDir;
        $this->targetDir = $targetDir;

        // Load respective file trees
        $this->sourceFiles = new RevcheckFileList( $sourceDir );
        $this->targetFiles = new RevcheckFileList( $targetDir );

        // Source files get info from version control
        GitLogParser::parseDir( $sourceDir , $this->sourceFiles );

        // Target files get info from revtags
        RevtagParser::parseDir( $targetDir , $this->targetFiles );

        // match and mix
        $this->parseTranslationXml();
        $this->calculateStatus();

        // fs output
        if ( $writeResults )
        {
            QaFileInfo::cacheSave( $this->qaList );
            $this->saveRevcheckData();
        }

        if ( $this->slowPathCount > 1000 )
            fprintf( STDERR , "Warn: Slow path called {$this->slowPathCount} times.\n" );
    }

    private function calculateStatus()
    {
        // Most of status are marked $sourceFiles,
        // except NotInEnTree, that are marked on $targetFiles.
        // $revData contains all status

        foreach( $this->sourceFiles->iterator() as $source )
        {
            $target = $this->targetFiles->get( $source->file );

            // Untranslated

            if ( $target == null )
            {
                $source->status = RevcheckStatus::Untranslated;
                $this->filesUntranslated[] = $source;
                $this->addData( $source , null );
                continue;
            }

            // RevTagProblem

            if ( $target->revtag == null || strlen( $target->revtag->revision ) != 40 )
            {
                $source->status = RevcheckStatus::RevTagProblem;
                $this->filesRevtagProblem[] = $source;
                $this->addData( $source , $target->revtag );
                continue;
            }

            $daysOld = ( strtotime( "now" ) - $source->date ) / 86400;
            $daysOld = (int)$daysOld;

            // TODO Make QA related state detect changes and autogenerate
            // TODO Move all QA related code outside of RevcheckRun
            {
                $sourceHash = $source->isSyncHash( $target->revtag->revision ) ? $source->hashDiff : $source->hashLast;
                $targetHash = $target->revtag->revision;

                $qaInfo = new QaFileInfo( $sourceHash , $targetHash , $this->sourceDir , $this->targetDir , $source->file , $daysOld );
                $this->qaList[ $source->file ] = $qaInfo;
            }

            // TranslatedOk

            if ( $target->revtag->status == "ready" && $source->isSyncHash( $target->revtag->revision ) )
            {
                $source->status = RevcheckStatus::TranslatedOk;
                $this->filesOk[] = $source;
                $this->addData( $source , $target->revtag );
                continue;
            }

            // TranslatedOld
            // TranslatedWip

            if ( $target->revtag->status == "ready" )
            {
                $source->status = RevcheckStatus::TranslatedOld;
                $this->filesOld[] = $source;
                $this->addData( $source , $target->revtag );
            }
            else
            {
                $source->status = RevcheckStatus::TranslatedWip;
                $this->filesWip[] = $source;
                $this->addData( $source , $target->revtag );
            }
        }

        // NotInEnTree

        foreach( $this->targetFiles->iterator() as $target )
        {
            $source = $this->sourceFiles->get( $target->file );
            if ( $source == null )
            {
                $target->status = RevcheckStatus::NotInEnTree;
                $this->filesNotInEn[] = $target;
                $this->addData( $target , $target->revtag );
            }
        }

        asort( $this->revData->fileDetail );
    }

    private function addData( RevcheckFileItem $info , RevtagInfo|null $revtag = null ) : void
    {
        $file = new RevcheckDataFile;

        $file->path = dirname( $info->file );
        $file->name = basename( $info->file );
        $file->size = $info->size;
        $file->days = floor( ( time() - $info->date ) / 86400 );
        $file->status = $info->status;
        $file->hashLast = $info->hashLast;
        $file->hashDiff = $info->hashDiff;

        $this->revData->addFile( $info->file , $file );

        if ( $revtag != null )
        {
            $file->hashRvtg = $revtag->revision;
            $file->maintainer = $revtag->maintainer;
            $file->completion = $revtag->status;

            $translator = $this->revData->getTranslator( $revtag->maintainer );

            switch( $info->status )
            {
                case RevcheckStatus::TranslatedOk:
                    $translator->countOk++;
                    break;
                case RevcheckStatus::TranslatedOld:
                    $translator->countOld++;
                    break;
                default:
                    $translator->countOther++;
                    break;
            }

            switch( $info->status ) // adds,dels
            {
                case RevcheckStatus::TranslatedOld:
                case RevcheckStatus::TranslatedWip:
                    $this->slowPathCount++;
                    GitSlowUtils::parseAddsDels( $this->sourceDir , $file );
            }
        }
    }

    private function parseTranslationXml() : void
    {
        $this->revData = new RevcheckData;
        $this->revData->lang = $this->targetDir;
        $this->revData->date = date("r");

        $dom = XmlUtil::loadFile( $this->targetDir . '/translation.xml' );

        $tag = $dom->getElementsByTagName( 'intro' )[0] ?? null;
        if ( $tag == null )
            $intro = "No intro available for the {$this->targetDir} translation of the manual.";
        else
        {
            $intro = "";
            foreach( $tag->childNodes as $node )
                $intro .= $dom->saveXML( $node );
        }
        $this->revData->intro = $intro;

        $persons = $dom->getElementsByTagName( 'person' );
        foreach( $persons as $person )
        {
            $nick = $person->getAttribute( 'nick' );
            $translator = $this->revData->getTranslator( $nick );
            $translator->name = $person->getAttribute( 'name' );
            $translator->email = $person->getAttribute( 'email' );
            $translator->vcs = $person->getAttribute( 'vcs' ) ?? "";
        }
    }

    private function saveRevcheckData()
    {
        $json = json_encode( $this->revData , JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT );
        file_put_contents( __DIR__ . "/../../../.revcheck.json" , $json );
    }
}