|
| 1 | +<?php /* |
| 2 | ++----------------------------------------------------------------------+ |
| 3 | +| Copyright (c) 1997-2023 The PHP Group | |
| 4 | ++----------------------------------------------------------------------+ |
| 5 | +| This source file is subject to version 3.01 of the PHP license, | |
| 6 | +| that is bundled with this package in the file LICENSE, and is | |
| 7 | +| available through the world-wide-web at the following url: | |
| 8 | +| https://www.php.net/license/3_01.txt. | |
| 9 | +| If you did not receive a copy of the PHP license and are unable to | |
| 10 | +| obtain it through the world-wide-web, please send a note to | |
| 11 | +| license@php.net, so we can mail you a copy immediately. | |
| 12 | ++----------------------------------------------------------------------+ |
| 13 | +| Authors: André L F S Bacci <ae php.net> | |
| 14 | ++----------------------------------------------------------------------+ |
| 15 | +| Description: Split old DTD .ent file into individual XML files. | |
| 16 | ++----------------------------------------------------------------------+ |
| 17 | +
|
| 18 | +See `entities.php` for detailed rationale. |
| 19 | +
|
| 20 | +Use this for spliting `language-snippets-ent` and possible other DTD |
| 21 | +entities files into individual .xml files. |
| 22 | +
|
| 23 | +After spliting, add generated files under doc-lang/entities/ , and |
| 24 | +the original file, in one go. |
| 25 | +
|
| 26 | +After all DTD .ent files are split or converted, this script can |
| 27 | +be removed. */ |
| 28 | + |
| 29 | +ini_set( 'display_errors' , 1 ); |
| 30 | +ini_set( 'display_startup_errors' , 1 ); |
| 31 | +error_reporting( E_ALL ); |
| 32 | + |
| 33 | +if ( count( $argv ) < 3 ) |
| 34 | + die(" Syntax: php $argv[0] infile outdir [hash user]\n" ); |
| 35 | + |
| 36 | +$infile = $argv[1]; |
| 37 | +$outdir = $argv[2]; |
| 38 | +$hash = $argv[3] ?? ""; |
| 39 | +$user = $argv[4] ?? "_"; |
| 40 | + |
| 41 | +$content = file_get_contents( $infile ); |
| 42 | +$entities = []; |
| 43 | + |
| 44 | +// Parse |
| 45 | + |
| 46 | +$pos1 = 0; |
| 47 | +while ( true ) |
| 48 | +{ |
| 49 | + $pos1 = strpos( $content , "<!ENTITY", $pos1 ); |
| 50 | + if ( $pos1 === false ) break; |
| 51 | + |
| 52 | + $posS = strpos( $content , "'" , $pos1 ); |
| 53 | + $posD = strpos( $content , '"' , $pos1 ); |
| 54 | + |
| 55 | + if ( $posS < $posD ) |
| 56 | + $q = "'"; |
| 57 | + else |
| 58 | + $q = '"'; |
| 59 | + |
| 60 | + $pos1 += 8; |
| 61 | + $pos2 = min( $posS , $posD ) + 1; |
| 62 | + $pos3 = strpos( $content , $q , $pos2 ); |
| 63 | + |
| 64 | + $name = substr( $content , $pos1 , $pos2 - $pos1 - 1 ); |
| 65 | + $text = substr( $content , $pos2 , $pos3 - $pos2 ); |
| 66 | + |
| 67 | + $name = trim( $name ); |
| 68 | + |
| 69 | + $entities[$name] = $text; |
| 70 | +} |
| 71 | + |
| 72 | +// Check |
| 73 | + |
| 74 | +foreach( $entities as $name => $text ) |
| 75 | +{ |
| 76 | + $file = "$outdir/$name.xml"; |
| 77 | + if ( file_exists( $file ) ) |
| 78 | + echo( "Entity name colision, OVERWROTE: $file\n" ); |
| 79 | +} |
| 80 | + |
| 81 | +// Write |
| 82 | + |
| 83 | +foreach( $entities as $name => $text ) |
| 84 | +{ |
| 85 | + $file = "$outdir/$name.xml"; |
| 86 | + |
| 87 | + if ( $hash == "" ) |
| 88 | + $header = '<!-- $Revision$ -->'; |
| 89 | + else |
| 90 | + $header .= "<!-- EN-Revision: $hash Maintainer: $user Status: ready --><!-- CREDITS: $user -->\n"; |
| 91 | + |
| 92 | + file_put_contents( $file , $header . $text ); |
| 93 | +} |
| 94 | + |
| 95 | +// Test |
| 96 | + |
| 97 | +$dom = new DOMDocument(); |
| 98 | +$dom->recover = true; |
| 99 | +$dom->resolveExternals = false; |
| 100 | +libxml_use_internal_errors( true ); |
| 101 | + |
| 102 | +foreach( $entities as $name => $text ) |
| 103 | +{ |
| 104 | + $file = "$outdir/$name.xml"; |
| 105 | + |
| 106 | + $text = file_get_contents( $file ); |
| 107 | + $text = "<frag>$text</frag>"; |
| 108 | + |
| 109 | + $dom->loadXML( $text ); |
| 110 | + $err = libxml_get_errors(); |
| 111 | + libxml_clear_errors(); |
| 112 | + |
| 113 | + foreach( $err as $e ) |
| 114 | + { |
| 115 | + $msg = trim( $e->message ); |
| 116 | + if ( str_starts_with( $msg , "Entity '" ) && str_ends_with( $msg , "' not defined" ) ) |
| 117 | + continue; |
| 118 | + die( "Failed to load $file\n" ); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +$total = count( $entities ); |
| 123 | +print "Generated $total files.\n"; |
0 commit comments