File: xml-check.php

package info (click to toggle)
php-doc 20100521-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 59,992 kB
  • ctags: 4,085
  • sloc: xml: 796,833; php: 21,338; cpp: 500; sh: 117; makefile: 58; awk: 28
file content (60 lines) | stat: -rw-r--r-- 3,093 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/php
<?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:    Jakub Vrana <vrana@php.net>                              |
  +----------------------------------------------------------------------+

  $Id: xml-check.php 293138 2010-01-05 10:21:11Z rquadling $
*/

if (!isset($_SERVER["argv"][1])) {
	echo "Purpose: Check XML syntax of single PHP manual file.\n";
	echo "Usage: xml-check.php filename.xml [xmllint-command]\n";
	echo "Note: Links are not checked.\n";
	exit();
}

$file = file_get_contents($_SERVER["argv"][1]);
$xmllint = (isset($_SERVER["argv"][2]) ? $_SERVER["argv"][2] : "xmllint");
$root = str_replace('\\', '/', dirname(__FILE__));
$root = substr($root, 0, strrpos($root, '/'));
$realpath = str_replace('\\', '/', realpath($_SERVER["argv"][1]));
$example_filename = "$root/xml-check.xml";
$rootpos = strlen($root) + 1;
$language = substr($realpath, $rootpos, strpos($realpath, '/', $rootpos) - $rootpos);
$header = preg_replace('~.*(<!DOCTYPE.*)<book.*~s', '\\1', file_get_contents("$root/manual.xml.in"));
$header = str_replace(array('@srcdir@', '@LANGDIR@', "\n"), array('.', $language, ''), $header); // \n to preserve line numbers

$file = preg_replace('~(<?xml[^>]*>)(.*)~s', "\\1$header<book>" . (!preg_match("~<chapter|<reference|<appendix~", $file) ? "<chapter id='example'><title>Example</title>\\2\n</chapter>" : "\\2") . "\n</book>\n", $file);
$fp = fopen($example_filename, "wb");
fwrite($fp, $file);
fclose($fp);

passthru("$xmllint --noent --noout --valid $example_filename 2> $example_filename.out"); // xmllint outputs to stderr which is not catched by shell_exec, 2> &1 doesn't work on Windows
$errors = file_get_contents("$example_filename.out");
$errors = preg_replace("~.*validity error : IDREF attribute linkend references an unknown ID.*\n.*\n.*\n~", "", $errors);
$errors = str_replace($example_filename, $_SERVER["argv"][1], $errors);

if (empty($errors)) {
	echo "Success: Your file passed this XML check, do consider running 'make test' as well.\n";
} else {
	echo "Errors: The following XML error exist:\n";
	echo $errors;
}

//~ unlink("$example_filename");
unlink("$example_filename.out");
?>