1<?php
2
3/**
4 * This is the Dokuwiki export for FINDOLOGIC.
5 * If any bugs occur, please submit a new issue
6 * @see https://github.com/findologic/dokuwiki-plugin-findologic-xml-export/issues/new
7 * @author Dominik Brader <support@findologic.com>
8 */
9
10require_once(__DIR__ . '/DokuwikiXMLExport.php');
11
12class OutputXMLHelper
13{
14    /**
15     * Default DokuWiki include path
16     */
17    const DOKUWIKI_INC = 'DOKU_INC';
18    /**
19     * Name of count _GET parameter
20     */
21    const COUNT_NAME = 'count';
22    /**
23     * Name of start _GET parameter
24     */
25    const START_NAME = 'start';
26    /**
27     * Header if export succeeds
28     */
29    const EXPORT_HEADER = 'Content-type: text/xml';
30    /**
31     * Default value for count _GET parameter
32     */
33    const DEFAULT_COUNT_VALUE = 20;
34    /**
35     * Default value for start _GET parameter
36     */
37    const DEFAULT_START_VALUE = 0;
38    /**
39     * Gets set when an error appears.
40     * An error may appear if _GET parameters are not valid.
41     */
42    const EXPORT_ERROR_HEADER = 'Status: 400 Bad Request';
43    /**
44     * This error message will be outputted if an error occurs.
45     */
46    const EXPORT_ERROR_MESSAGE = 'start and count values are not valid';
47    /**
48     * Error code that will be returned if an error occurs.
49     */
50    const EXPORT_ERROR_CODE = 400;
51
52    /**
53     * Error level for DokuWiki functions.
54     */
55    const ERROR_LEVEL = -1;
56
57    /**
58     * Validates count and start values
59     *
60     * @param $start int start value
61     * @param $count int count value
62     * @param $type int ID of the type to check.
63     * @return bool true if parameters are valid, else false
64     */
65    public function paramsValid($start, $count, $type)
66    {
67        $start = filter_var($start, $type);
68        $count = filter_var($count, $type);
69        return (is_int($count) && is_int($start) && $start >= 0 && $count > 0);
70    }
71
72    /**
73     * Gets the value of _GET param converted to HTML entities, or the default value if _GET param was not set.
74     *
75     * @param $paramName string Name of the URL parameter
76     * @param $defaultValue string Default value if _GET parameter is not set
77     * @param $getParam array _GET param
78     * @return string value of the _GET parameter or default value if _GET parameter is not set
79     */
80    public function getUrlParam($paramName, $defaultValue, $getParam)
81    {
82        if (isset($getParam[$paramName])) {
83            return htmlspecialchars($getParam[$paramName]);
84        } else {
85            return $defaultValue;
86        }
87    }
88
89    /**
90     * Returns generated XML from export
91     *
92     * @param $start int start value
93     * @param $count int count value
94     * @return string Generated XML
95     */
96    public function getXml($start, $count)
97    {
98        global $conf;
99        $dokuwikiXmlExport = new DokuwikiXMLExport($conf);
100        return $dokuwikiXmlExport->generateXMLExport($start, $count);
101    }
102
103    /**
104     * Sets the export error header and prints an error message
105     */
106    public function throwError() {
107        header(self::EXPORT_ERROR_HEADER, true, self::EXPORT_ERROR_CODE);
108        msg(self::EXPORT_ERROR_MESSAGE, self::ERROR_LEVEL);
109        html_msgarea();
110    }
111
112    /**
113     * Sets export header and prints the XML.
114     *
115     * @param $start int start value
116     * @param $count int count value
117     */
118    public function printXml($start, $count) {
119        header(self::EXPORT_HEADER);
120        echo $this->getXml($start, $count);
121    }
122}