Passed
Push — master ( 119e27...c32e95 )
by Gaetano
08:58
created

error()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * Script used to convert docbook source to human-readable docs
4
 *
5
 * @copyright (c) 2007-2022 G. Giunta
6
 *
7
 * @todo rename to something akin to xsltproc
8
 */
9
10
if ($_SERVER['argc'] < 4) {
11
    error("Usage: php convert.php docbook.xml stylesheet.xsl output-dir|output_file");
12
}
13
14
$doc = $_SERVER['argv'][1];
15
$xss = $_SERVER['argv'][2];
16
$target = $_SERVER['argv'][3];
17
18
if (!file_exists($doc))
19
  error("KO: file $doc cannot be found");
20
if (!file_exists($xss))
21
  error("KO: file $xss cannot be found");
22
23
info("Starting xsl conversion process...");
24
25
// Replace tokens in the existing xslt file
26
$docbookFoXslPath = realpath('./build/vendor/docbook/docbook-xsl/fo/docbook.xsl');
27
$docbookChunkXslPath = realpath('./build/vendor/docbook/docbook-xsl/xhtml/chunk.xsl');
28
file_put_contents(
29
    $xss,
30
    str_replace(
31
        array('%fo-docbook.xsl%', '%docbook-chunk.xsl%'),
32
        array($docbookFoXslPath, $docbookChunkXslPath),
33
        file_get_contents($xss)
34
    )
35
);
36
37
// Load the XML source
38
$xml = new DOMDocument;
39
$xml->load($doc);
40
$xsl = new DOMDocument;
41
$xsl->load($xss);
42
43
// Configure the transformer
44
$processor = new XSLTProcessor;
45
if (version_compare(PHP_VERSION, '5.4', "<")) {
46
    if (defined('XSL_SECPREF_WRITE_FILE')) {
47
        ini_set("xsl.security_prefs", XSL_SECPREF_CREATE_DIRECTORY | XSL_SECPREF_WRITE_FILE);
48
    }
49
} else {
50
    // the php online docs only mention setSecurityPrefs, but somehow some installs have setSecurityPreferences...
51
    if (method_exists('XSLTProcessor', 'setSecurityPrefs')) {
52
        $processor->setSecurityPrefs(XSL_SECPREF_CREATE_DIRECTORY | XSL_SECPREF_WRITE_FILE);
53
    } else {
54
        $processor->setSecurityPreferences(XSL_SECPREF_CREATE_DIRECTORY | XSL_SECPREF_WRITE_FILE);
0 ignored issues
show
Bug introduced by
The method setSecurityPreferences() does not exist on XSLTProcessor. Did you maybe mean setSecurityPrefs()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
        $processor->/** @scrutinizer ignore-call */ 
55
                    setSecurityPreferences(XSL_SECPREF_CREATE_DIRECTORY | XSL_SECPREF_WRITE_FILE);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
    }
56
}
57
if (is_dir($target)) {
58
    if (!$processor->setParameter('', 'base.dir', $target)) {
59
        error("KO setting param base.dir");
60
    }
61
}
62
63
// attach the xsl rules
64
$processor->importStyleSheet($xsl);
65
66
$out = $processor->transformToXML($xml);
67
68
// bring back the xsl file to its pristine state
69
file_put_contents(
70
    $xss,
71
    str_replace(
72
        array($docbookFoXslPath, $docbookChunkXslPath),
73
        array('%fo-docbook.xsl%', '%docbook-chunk.xsl%'),
74
        file_get_contents($xss)
75
    )
76
);
77
78
if (!is_dir($target)) {
79
    if (!file_put_contents($target, $out)) {
80
        error("KO saving output to '{$target}'");
81
    }
82
}
83
84
info("OK");
85
86
// *** functions ***
87
88
function info($msg)
89
{
90
    echo "$msg\n";
91
}
92
93
function error($msg, $errcode = 1)
94
{
95
    fwrite(STDERR, "$msg\n");
96
    exit($errcode);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
97
}
98