File: file-entities.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 (349 lines) | stat: -rw-r--r-- 9,654 bytes parent folder | download
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php /*
+----------------------------------------------------------------------+
| Copyright (c) 1997-2025 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:    Hartmut Holzgraefe <hholzgra@php.net>                    |
|             Gabor Hojtsy <goba@php.net>                              |
|             André L F S Bacci <ae@php.net>                           |
+----------------------------------------------------------------------+

# Description

This script creates various "file entities", that is, DTD entities that
point to files and file listings, named and composed of:

- dir.dir.file         : pulls in a dir/dir/file.xml
- dir.dif.entities.dir : pulls in XML files from dir/dir/dir/*.xml

In the original file-entities.php.in, the files are created at:

- doc-base/entities/file-entities.ent
- doc-en/reference/entities.*.xml

In new idempotent mode, files are created at:

- doc-base/temp/file-entites.ent
- doc-base/temp/file-entites/dir.dir.ent

The file entity for directories (file listings) are keep as individual
files instead to avoid these libxml errors, in some OS/versions:

- Detected an entity reference loop [1]
- Maximum entity amplification factor exceeded [2]

See LIBXML_LIMITS_HACK below. This workaround creates about a thousand
files per running, that slowsdows even more the manual building on HDD
systems.

[1] https://github.com/php/doc-base/pull/183
[2] https://github.com/php/doc-en/pull/4330

*/

// Setup

ini_set( 'display_errors' , 1 );
ini_set( 'display_startup_errors' , 1 );
error_reporting( E_ALL );
set_time_limit( 0 );
ob_implicit_flush();

const LIBXML_LIMITS_HACK = true;

// Usage

$root = realpain( __DIR__ . "/../.." );
$lang = "";
$chmonly = false;
$debug = false;

array_shift( $argv );
foreach( $argv as $arg )
{
    if ( $arg == "--chmonly" )
    {
        $chmonly = true;
        continue;
    }
    if ( $arg == "--debug" )
    {
        $debug = true;
        continue;
    }
    $lang = $arg;
}

// Main

echo "Creating file-entities.ent... ";

$entities = [];
$mixedCase = [];

generate_file_entities( $root , "en" );
generate_list_entities( $root , "en" );

if ( $lang != "" )
    generate_file_entities( $root , $lang );

pushEntity( "global.function-index", path: realpain( __DIR__ . "/.." ) . "/funcindex.xml" );

if ( ! $chmonly )
    foreach( $entities as $ent )
        if ( str_starts_with( $ent->name , "chmonly." ) )
            $ent->path = '';

$outfile = realpain(  __DIR__ . "/../temp/file-entities.ent" , touch: true );

$file = fopen( $outfile , "w" );
if ( ! $file )
{
    echo "Failed to open $outfile\n.";
    exit( 1 );
}

fputs( $file , "<!-- DON'T TOUCH - AUTOGENERATED BY file-entities.php -->\n\n" );

ksort( $entities );

foreach ( $entities as $ent )
    writeEntity( $file , $ent );

fclose( $file );
echo "done\n";
exit( 0 );



class Entity
{
    function __construct( public string $name , public string $text , public string $path ) {}
}

function pushEntity( string $name , string $text = '' , string $path = '' )
{
    global $entities;
    global $mixedCase;

    $name = str_replace( '_' , '-' , $name );
    $path = str_replace( '\\' , '/' , $path );
    $ent = new Entity( $name , $text , $path );
    $entities[ $name ] = $ent;

    if ( ( $text == "" && $path == "" ) || ( $text != "" && $path != "" ) )
    {
        echo "Something went wrong on file-entities.php.\n";
        exit( 1 );
    }

    $lname = strtolower( $name );
    if ( isset( $mixedCase[ $lname ] ) && $mixedCase[ $lname ] != $name )
    {
        echo <<<END
        \n\n
        BROKEN BUILD on case insensitive file systems!

        Detected file entities names, distinct only by case:
        - {$mixedCase[ $lname ]}
        - $name

        This may PERMANENTLY BRICK manual build on Windows machines!

        If you are seeing this message building doc-en, avoid committing any changes
        on repository, and if it's already committed, revert and send a heads up on
        mail lists, on how to fix the issue (refer to this message).

        If you are seeing this message building a translation, this means that the
        translation may have files or directories that differ from doc-en only by
        upper or lower case letters. Find these differences and fix them at the git
        level ('git mv"). After, delete the files and 'git restore' them, to see if
        the 'git mv' worked.

        This message only may be visible on non-Windows machines. Mixed cases inside
        a repository, or between repositories, may only cause difficult to debug build
        failures on Windows, without any other information. There is no easy fix for
        this than a complete new checkout of the affected repository.

        See: https://github.com/php/doc-en/pull/4330#issuecomment-2557306828\n\n
        END;
        exit( 1 );
    }
    $mixedCase[ $lname ] = $name;
}

function generate_file_entities( string $root , string $lang )
{
    $path = "$root/$lang";
    $test = realpain( $path );
    if ( $test === false || is_dir( $path ) == false )
    {
        echo "Language directory not found: $path\n.";
        exit( 1 );
    }
    $path = $test;

    file_entities_recurse( $path , array() );
}

function file_entities_recurse( string $langroot , array $dirs )
{
    $dir = rtrim( "$langroot/" . implode( '/' , $dirs ) , "/" );
    $files = scandir( $dir );
    $subdirs = [];

    foreach( $files as $file )
    {
        if ( $file == "" )
            continue;
        if ( $file[0] == "." )
            continue;
        if ( $file == "entities" && count( $dirs ) == 0 )
            continue;

        $path = "$dir/$file";

        if ( is_dir ( $path ) )
        {
            $subdirs[] = $file;
            continue;
        }
        if ( str_ends_with( $file , ".xml" ) )
        {
            $name = implode( '.' , $dirs ) . "." . basename( $file , ".xml" );
            $name = trim( $name , "." );
            pushEntity( $name , path: $path );
        }
    }

    foreach( $subdirs as $subdir )
    {
        $recurse = $dirs;
        $recurse[] = $subdir;
        file_entities_recurse( $langroot , $recurse );
    }
}

function generate_list_entities( string $root , string $lang )
{
    $path = "$root/$lang";
    $test = realpain( $path );
    if ( $test === false || is_dir( $path ) == false )
    {
        echo "Language directory not found: $path\n.";
        exit( 1 );
    }
    $path = $test;

    $dirs = [ "reference" ];
    list_entities_recurse( $path , $dirs );
}

function list_entities_recurse( string $root , array $dirs )
{
    $list = [];

    $dir = rtrim( "$root/" . implode( '/' , $dirs ) , "/" );
    $files = scandir( $dir );
    $subdirs = [];

    foreach( $files as $file )
    {
        if ( $file == "" )
            continue;
        if ( $file[0] == "." )
            continue;

        $path = "$dir/$file";

        if ( is_dir ( $path ) )
        {
            $subdirs[] = $file;
            continue;
        }

        if ( str_ends_with( $file , ".xml" ) )
        {
            $name = implode( '.' , $dirs ) . "." . basename( $file , ".xml" );
            $name = trim( $name , "." );
            $name = str_replace( '_' , '-' , $name );
            $list[ $name ] = "&{$name};";
        }
    }
    ksort( $list );

    $copy = $dirs;
    $last = array_pop( $copy );
    $copy[] = "entities";
    $copy[] = $last;

    $name = implode( "." , $copy );
    $text = implode( "\n" , $list );

    if ( $text != "" )
    {
        if ( LIBXML_LIMITS_HACK )
        {
            static $entityDir = "";
            if ( $entityDir == "" )
                $entityDir = realpain( __DIR__ . "/../temp/file-entities" , mkdir: true );

            $path = $entityDir . "/" . implode( '.' , $dirs ) . ".ent";
            file_put_contents( $path , $text );
            pushEntity( $name , path: $path );
        }
        else
            pushEntity( $name , text: $text );
    }

    foreach( $subdirs as $subdir )
    {
        $recurse = $dirs;
        $recurse[] = $subdir;
        list_entities_recurse( $root , $recurse );
    }
}

function writeEntity( $file , Entity $ent )
{
    $name = $ent->name;
    $text = $ent->text;
    $path = $ent->path;

    if ( $path == "" )
        $line = "<!ENTITY $name '$text'>\n";
    else
        $line = "<!ENTITY $name SYSTEM '$path'>\n";

    fwrite( $file , $line );
}

function realpain( string $path , bool $touch = false , bool $mkdir = false ) : string
{
    // pain is real

    // care for external XML tools (realpath() everywhere)
    // care for Windows builds (foward slashes everywhere)
    // avoid `cd` and chdir() like the plague

    $path = str_replace( "\\" , '/' , $path );

    if ( $mkdir && ! file_exists( $path ) )
        mkdir( $path , recursive: true );

    if ( $touch && ! file_exists( $path ) )
        touch( $path );

    $res = realpath( $path );
    if ( is_string( $res ) )
        $path = str_replace( "\\" , '/' , $res );

    return $path;
}