Author: jstephens
Date: 2007-11-01 22:49:47 +0100 (Thu, 01 Nov 2007)
New Revision: 8478
Log:
Fixed to get rid of entities which PHP-DOM insists on importing if
they're defined externally
Modified:
trunk/ndbapi/fix-indexes
Modified: trunk/ndbapi/fix-indexes
===================================================================
--- trunk/ndbapi/fix-indexes 2007-11-01 20:42:48 UTC (rev 8477)
+++ trunk/ndbapi/fix-indexes 2007-11-01 21:49:47 UTC (rev 8478)
Changed blocks: 1, Lines Added: 84, Lines Deleted: 38; 4676 bytes
@@ -1,91 +1,137 @@
#!/usr/bin/php
<?php
- # fix-indexes - Jon Stephens (jon@stripped)
- # Version 1.0 - 01 November 2007
+# fix-indexes - Jon Stephens (jon@stripped)
+# Version 1.0 - 01 November 2007
- # Function: Traverses all *.xml files in the directory in which the
- # script is invoked and removes all <literal> elements contained
- # within <primary> and <secondary> elements found in those files
+# Function: Traverses all *.xml files in the directory in which the
+# script is invoked and removes all <literal> elements contained within
+# <primary> or <secondary> elements found in those files.
- # Purpose: Clean up sub-optimal indexing in DocBook XML files
+# Purpose: Cleans up sub-optimal indexing in DocBook XML files.
- # Requires: PHP 5 with DOM and SimpleXML extensions
- # (tested with PHP 5.2.0 on SuSE Linux 10.2)
+# Requires: PHP 5 with DOM and SimpleXML extensions
+# (tested with PHP 5.2.0 on SuSE Linux 10.2).
- # Invoke with --debug for some debugging output
+# Invoke with --debug for some debugging output.
+# (Warning: This is fairly verbose, especially with large files.)
- $__DEBUG__ = $argc > 0 && $argv[1] == '--debug' ? TRUE : FALSE;
+# Now includes fix to get rid of entities which PHP-DOM insists
+# on importing if they're defined externally.
+ $__DEBUG__ = isset($argv[1]) && $argv[1] == '--debug' ? TRUE : FALSE;
+
function remove_tags($el)
-{
- global $__DEBUG__;
+ {
+ global $__DEBUG__;
- $tag = $el->tagName;
+ $tag = $el->tagName;
- if($__DEBUG__)
- echo "Processing $tag element; ";
+ if($__DEBUG__)
+ echo "Processing $tag element; ";
- $simple = simpleXML_import_DOM($el);
- $xml = $simple->asXML();
+ $simple = simpleXML_import_DOM($el);
+ $xml = $simple->asXML();
- if($__DEBUG__)
- echo "original text in element: $xml;";
+ if($__DEBUG__)
+ echo "original text in element: $xml;";
- $text = preg_replace('/<\/?(literal|' . $tag . ')>/', '', $xml);
+ $text = preg_replace('/<\/?(literal|' . $tag . ')>/', '', $xml);
- if($__DEBUG__)
- echo " text after processing: $text\n";
+ if($__DEBUG__)
+ echo " text after processing: $text\n";
- $owner = $el->ownerDocument;
+ $owner = $el->ownerDocument;
- $text_node = $owner->createTextNode($text);
+ $text_node = $owner->createTextNode($text);
- $new_el = $owner->createElement($tag);
- $new_el->appendChild($text_node);
+ $new_el = $owner->createElement($tag);
+ $new_el->appendChild($text_node);
- return $new_el;
-}
+ return $new_el;
+ }
-
$files = scandir(".");
$files_processed = 0;
- $tags_processed = 0;
foreach($files as $file)
{
if(is_file($file) && strpos($file, '.xml'))
{
if($__DEBUG__)
- echo "\nProcessing $file... ";
+ echo "\nProcessing $file...\n";
$files_processed++;
+ $tags = 0;
$doc = DOMDocument::load($file);
$primaries = $doc->getElementsByTagName('primary');
$secondaries = $doc->getElementsByTagName('secondary');
- $tags = count($primaries) + count($secondaries);
- $tags_processed += $tags;
-
- if($__DEBUG__)
- echo "$tags tags to be processed in this file...\n";
-
$index_sets = array($primaries, $secondaries);
foreach($index_sets as $indexes)
+ {
foreach($indexes as $index)
+ {
$index->parentNode->replaceChild(remove_tags($index), $index);
+ $tags++;
+ }
+ }
+ if($__DEBUG__)
+ echo "$tags tags processed in $file...\n";
+
+ $tags_processed += $tags;
+
$doc->save($file);
unset($doc);
+
+ if($__DEBUG__)
+ echo "Removing included entity definitions...\n";
+
+ $lines = file($file);
+ $count = count($lines);
+
+ if($__DEBUG__)
+ echo "$file contains $count lines...\n";
+
+ $start = FALSE;
+ $end = FALSE;
+
+ for($i = 0; $i < $count; $i++)
+ {
+
+ $end = strpos($lines[$i], ']>') !== FALSE;
+
+ if($start)
+ {
+ if($end)
+ {
+ $lines[$i] = " %all.entities;\n$lines[$i]";
+ break;
+ }
+ else
+ {
+ if($__DEBUG__)
+ echo $lines[$i];
+
+ unset($lines[$i]);
+ }
+ }
+ else
+ $start = strpos($lines[$i], '<!ENTITY % all.entities SYSTEM') !== FALSE;
+ }
+
+ file_put_contents($file, $lines);
}
}
echo "\nProcessed $tags_processed tags
in $files_processed XML files.\nDone.\n";
-?>
\ No newline at end of file
+?>
+
| Thread |
|---|
| • svn commit - mysqldoc@docsrva: r8478 - trunk/ndbapi | jon | 1 Nov |