cvs: peardoc /peardoc2 check-ced-location.php
| From: | Martin Jansen | Date: | Mon, 10 Mar 2003 22:22:18 +0000 |
| Subject: | cvs: peardoc /peardoc2 check-ced-location.php | ||
| Groups: | php.pear.doc | ||
| Request: | Send a blank email to pear-doc+get-433@lists.php.net to get a copy of this message | ||
mj Mon Mar 10 17:22:18 2003 EDT
Added files:
/peardoc/peardoc2 check-ced-location.php
Log:
* Add script to validate and correct the link to manual.ced in all the
Docbook files.
Index: peardoc/peardoc2/check-ced-location.php
+++ peardoc/peardoc2/check-ced-location.php
#!/usr/bin/php
<?php
/**
* This is a quick hack to validate and correct the link to the
* file manual.ced, that is used by PSGML when editing peardoc
* files in Emacs, in all XML files that are part of the PEAR
* documentation. The code may of course contain bugs.
*
* (c) Martin Jansen <mj@php.net>, 2003
*
* $Id: check-ced-location.php,v 1.1 2003/03/10 22:22:18 mj Exp $
*/
// Adjust this to your needs
define("BASE_PATH", "/home/martin/cvs/peardoc/peardoc2/");
/**
* Calculate the number of "../" that are necessary to reach
* BASE_PATH
*/
function calculate_steps($path) {
$base = preg_quote(BASE_PATH);
$path = preg_replace("=" . BASE_PATH . "=", "", $path);
return substr_count($path, "/");
}
/**
* Check if a file contains a correct location of manual.ced
*
* @param string filename
* @param int Will contain number of needed steps to reach manual.ced
*/
function check_file($file, &$needed) {
$needed = calculate_steps($file);
$content = file_get_contents($file);
if (preg_match("/sgml-default-dtd-file:\"(.*)\"/", $content, $matches)) {
if ($needed == substr_count($matches[1], "/")) {
return true;
} else {
return false;
}
}
}
/**
* Correct location of manual.ced in a file
*
* @param string filename
* @param int Number of needed steps to reach manual.ced
*/
function fix_file($file, $needed) {
for ($i = 0; $i < $needed; $i++) {
$location .= "../";
}
$location .= "manual.ced";
$content = file_get_contents($file);
$content = preg_replace("/sgml-default-dtd-file:\"(.*)manual.ced\"/",
'sgml-default-dtd-file:"' . $location . '"', $content);
$fp = fopen($file, "w");
if (!$fp) {
return false;
}
fputs($fp, $content);
fclose($fp);
return true;
}
/**
* Recursively check directory
*
* @param string directory
*/
function check_dir($dir = ".") {
$handle = opendir($dir);
while (false !== ($file = readdir ($handle))) {
if ($file == ".." || $file == ".") {
continue;
}
if (preg_match("/\.xml$/", $file)) {
if (!check_file($dir . $file, $needed)) {
echo "Error in " . $dir . $file;
if (fix_file($dir . $file, $needed)) {
echo " ... fixed\n";
} else {
echo " ... unable to fix\n";
}
}
}
if (is_dir($dir . $file)) {
check_dir($dir . $file . "/");
}
}
closedir($handle);
}
check_dir(BASE_PATH . "en/package/database/db/");
?>