|
| 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: Convert old style .ent into new style .ent XML bundle. | |
| 16 | ++----------------------------------------------------------------------+ |
| 17 | +
|
| 18 | +See `entities.php` source for detailed rationale. |
| 19 | +
|
| 20 | +Use this for converting bundled entities files that use <!ENTITY> into |
| 21 | +XML version used by `entities.php`. |
| 22 | +
|
| 23 | +After converting, add the generated entities in an global.ent or |
| 24 | +manual.ent file, and delete the previous one. |
| 25 | +
|
| 26 | +After all old style .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 ) < 2 ) |
| 34 | + die(" Syntax: php $argv[0] infile\n" ); |
| 35 | + |
| 36 | +$infile = $argv[1]; |
| 37 | + |
| 38 | +$content = file_get_contents( $infile ); |
| 39 | + |
| 40 | +$pos1 = 0; |
| 41 | +while ( true ) |
| 42 | +{ |
| 43 | + $pos1 = strpos( $content , "<!ENTITY", $pos1 ); |
| 44 | + if ( $pos1 === false ) break; |
| 45 | + |
| 46 | + $posS = strpos( $content , "'" , $pos1 ); |
| 47 | + $posD = strpos( $content , '"' , $pos1 ); |
| 48 | + |
| 49 | + if ( $posS < $posD ) |
| 50 | + $q = "'"; |
| 51 | + else |
| 52 | + $q = '"'; |
| 53 | + |
| 54 | + $pos1 += 8; |
| 55 | + $pos2 = min( $posS , $posD ) + 1; |
| 56 | + $pos3 = strpos( $content , $q , $pos2 ); |
| 57 | + |
| 58 | + $name = substr( $content , $pos1 , $pos2 - $pos1 - 1 ); |
| 59 | + $text = substr( $content , $pos2 , $pos3 - $pos2 ); |
| 60 | + |
| 61 | + // weird &ugly; ass, namespace corret, DOMDocumentFragment -> DOMNodeList (ampunstand intended) |
| 62 | + |
| 63 | + $name = trim( $name ); |
| 64 | + $text = str_replace( "&" , "&" , $text ); |
| 65 | + |
| 66 | + $frag = "<entities xmlns='http://docbook.org/ns/docbook' xmlns:xlink='http://www.w3.org/1999/xlink'>\n"; |
| 67 | + $frag .= " <entity name='$name'>$text</entity>\n"; |
| 68 | + $frag .= '</entities>'; |
| 69 | + |
| 70 | + $dom = new DOMDocument( '1.0' , 'utf8' ); |
| 71 | + $dom->recover = true; |
| 72 | + $dom->resolveExternals = false; |
| 73 | + libxml_use_internal_errors( true ); |
| 74 | + |
| 75 | + $dom->loadXML( $frag , LIBXML_NSCLEAN ); |
| 76 | + $dom->normalizeDocument(); |
| 77 | + |
| 78 | + libxml_clear_errors(); |
| 79 | + |
| 80 | + $text = $dom->saveXML( $dom->getElementsByTagName( "entity" )[0] ); |
| 81 | + $text = str_replace( "&" , "&" , $text ); |
| 82 | + |
| 83 | + echo "$text\n"; |
| 84 | +} |
0 commit comments