<?php
/*  
  +----------------------------------------------------------------------+
  | PHP Version 4                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 1997-2010 The PHP Group                                |
  +----------------------------------------------------------------------+
  | This source file is subject to version 3.0 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:           |
  | http://www.php.net/license/3_0.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>                    |
  +----------------------------------------------------------------------+
  
  $Id: test_missing-entities.php.in 293138 2010-01-05 10:21:11Z rquadling $
*/

/*

ATTENTION: don't use this script!!!
           its only meant to test the creation of missings
           together with xmllint.
           For testing you must change following lines from manual.xml.in

<!-- Autogenerated missing entites and IDs to make build work -->
<!ENTITY % missing-entities  SYSTEM "entities/missing-entities.ent">
%missing-entities;

<!ENTITY missing-ids SYSTEM "entities/missing-ids.xml">
]>

to:
<!-- Autogenerated missing entites and IDs to make build work -->
<!ENTITY % missing-entities  SYSTEM "entities/test_missing-entities.ent">
%missing-entities;

<!ENTITY missing-ids SYSTEM "entities/test_missing-ids.xml">
]>
          The results are _only_ usefull, if all entities are valid,
          this means any entitie must have the trailing ;

*/

##########################
# Function declacarions  #
##########################

function test_manual ($XMLLINT) {
// Execute a test of the manual
    exec(
        "$XMLLINT --noout --noent --valid manual.xml 2>&1",
        $results,
        $retcode
    );
    return $results;
}  // end test_manual

function catch_output ($results, $what="") {

// catch the output from xmllint


    $missings = array();

    switch ($what) {
    
    case "entities":
        foreach ($results as $line) {
    
        // missing entitiy found, replace each with "???"
            if (strpos($line, "not defined") !== FALSE) {
                $line = preg_replace('!^.[^\']*\'!', '<!ENTITY ', $line);
                $line = preg_replace('!\'.*!', ' "???">', $line);
                $missings[] = $line;
            }
        }
        break;
    
    case "ids":
        foreach ($results as $line) {
    
        // missing ID found
            if (strpos($line, "an unknown ID") !== FALSE) {
                $missings[] = preg_replace("!^.* ID !", " <para id=", $line) . "></para>\n";
            }
        }    
        break;         
    }


    // Sort elements (just to make handwork easier, if needed)
    $missings = array_unique($missings);
    sort($missings);
    return $missings;
} // catch_output

function write_file ($missings, $what="") {

// Try to open file for rewriting
   
    if ($what === "entities") {

        $handle = fopen(MISSING_ENT, "w");

        // Exit if we cannot rewrite the files
        if (!$handle) {
            exit ("ERROR: Cannot open ".MISSING_ENT." for writing\n");
        }

        // Write out XML declaration

        fwrite($handle, XML_DECL);

        // Write out missings to file
   
        foreach ($missings as $ent) {
            fwrite($handle, $ent."\n");
        }
        fclose($handle);
        
        // print out success info
        echo  MISSING_ENT." created\n";
    } 
    if ($what === "ids") {
            $handle = fopen(MISSING_ID, "w");

            // Exit if we cannot rewrite the files
            if (!$handle) {
                exit ("ERROR: Cannot open ".MISSING_ID." for writing\n");
            }
            // Write out XML declaration and appendix part
            fwrite($handle, XML_DECL);
            fwrite($handle, APPENDIX_START);
            
            foreach ($missings as $idpara) {
                fwrite($handle, $idpara);
            }
            fwrite($handle, APPENDIX_END);
            fclose($handle);

            // print out success info
            echo MISSING_ID." created\n";
    }
}  //end write_file


######################################################################
#                                                                    #
#  main part                                                         #
#                                                                    #
######################################################################

// define some constants

define ('XML_DECL', "<" . "?xml version='1.0' encoding='iso-8859-1'?>\n\n");
define ('APPENDIX_START', "<appendix id=\"missing-stuff\"><title>&MissingStuff;</title>\n");
define ('APPENDIX_END', "</appendix>\n");
define ('MISSING_ENT', "entities/test_missing-entities.ent");
define ('MISSING_ID', "entities/test_missing-ids.xml");
set_time_limit(0);

// Print out info for the viewer and log files
echo "\ntesting the manual for missing elements...\n";

// If we are in a scripts dir, go one dir up
$wrkdir = getcwd();
if (strpos($wrkdir, "scripts") !== FALSE) {
    chdir("..");
}

$XMLLINT = "@XMLLINT@"; 

// Support for Windows systems
if (substr(PHP_OS, 0, 3) == 'WIN') {
    $XMLLINT = str_replace("/", "\\", "$XMLLINT");
}

$results  = test_manual($XMLLINT);
$missings = catch_output ($results,"entities");
write_file ($missings, "entities");

unset ($results, $missings);

$results  = test_manual($XMLLINT);
$missings = catch_output ($results,"ids");
write_file ($missings, "ids");
?>
