<?php
/*
  +----------------------------------------------------------------------+
  | PHP Version 4                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 1997-2004 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>                    |
  |             Gabor Hojtsy <goba@php.net>                              |
  +----------------------------------------------------------------------+
  
  $Id: missing-entities.php.in,v 1.17 2005/02/09 23:46:00 hholzgra Exp $
*/

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
// (because the NSGMLS path is relative to that directory!)
$wrkdir = getcwd();
if (strpos($wrkdir, "scripts") !== FALSE) {
    chdir("..");
}

$NSGMLS = "@NSGMLS@"; 
$NSGMLS_OUTPUT = "missing-entities.out"; 

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

// Execute a test of the manual
if("@SP_OPTIONS@" == "" || substr(PHP_OS, 0, 3) == 'WIN') {
    exec(
        "$NSGMLS -f $NSGMLS_OUTPUT -i lang-@LANG@ -D . " .
        "-s @SRCDIR@/dtds/dbxml-4.1.2/phpdocxml.dcl manual.xml"
    );
}
else {
    exec(
        "@SP_OPTIONS@ $NSGMLS -f $NSGMLS_OUTPUT -i lang-@LANG@ -D . " .
        "-s @SRCDIR@/dtds/dbxml-4.1.2/phpdocxml.dcl manual.xml"
    );
}

// Try to open files for rewriting
$ment = fopen("entities/missing-entities.ent", "w");
$mids = fopen("entities/missing-ids.xml", "w");

// Exit if we cannot rewrite the files
if (!$ment || !$mids) {
    die("ERROR: Cannot open files for writing\n");
}

// Write out XML declaration
fwrite($ment, "<" . "?xml version='1.0' encoding='iso-8859-1'?>\n\n");
fwrite($mids, "<" . "?xml version='1.0' encoding='iso-8859-1'?>\n\n");

// Initalize arrays
$missing_ids = array();
$missing_entities = array();

// Open output file
$results = file($NSGMLS_OUTPUT);

// Try to find missing IDs and entities
foreach ($results as $line) {
    
    trim($line);
    
    // missing entity found
    if (strpos($line, "not defined") !== FALSE) {
        $line = preg_replace('!^.[^"]*"!', '<!ENTITY ', $line);
        $line = preg_replace('!".*!', ' "???">', $line);
        $missing_entities[] = $line;
    }
    
    // missing ID found
    else if (strpos($line, "non-existent") !== FALSE) {
        $missing_ids[] = preg_replace("!^.* ID !", " <para id=", $line) . "></para>\n";
    }
}

// Find unique elements (one error message can occur several times)
$missing_ids = array_unique($missing_ids);
$missing_entities = array_unique($missing_entities);

// Sort elements (just to make handwork easier, if needed)
sort($missing_ids);
sort($missing_entities);

// Write out missing entities to file
foreach ($missing_entities as $ent) {
    fwrite($ment, $ent);
}

// That's all for missing entities
fclose($ment);

// If we have missing IDs, write them out as an appendix
if (count($missing_ids) > 0) {

    fwrite($mids, "<appendix id=\"missing-stuff\"><title>&MissingStuff;</title>\n");
    foreach ($missing_ids as $idpara) {
        fwrite($mids, $idpara);
    }
    fwrite($mids, "</appendix>\n");
}

// That's all for missing IDs
fclose($mids);

// print out success info
echo "\nCreated file: entities/missing-entities.ent\n";
if (!empty($missing_entities)) {
    foreach ($missing_entities as $k => $v) {
            echo "* " . preg_replace('@[\s]+@', ' ', $v) . "\n";
    }
} else {
    echo "* No missing entities were found\n";
}

echo "\nCreated file: @LANGDIR@/missing-ids.xml\n";
if (!empty($missing_ids)) {
    foreach ($missing_ids as $k => $v) {
        if (false !== strpos('<para id="zend"', $v)) {
            echo "* " . preg_replace('@[\s]+@', ' ', $v) . "\n";
        } else {
            echo "* Zend id is missing. (Don't worry about this)\n";
            if (count($missing_ids) === 1) {
                echo "* No other missing ids were found\n";
            }
        }
    }
} else {
    echo "* No missing ids found\n";
}

?>
