xref: /webtrees/app/Report/ReportParserBase.php (revision 6d66a8ecc0f9e1cbe6d48547497df5d2c368439f)
1ef0d468bSGreg Roach<?php
23976b470SGreg Roach
3ef0d468bSGreg Roach/**
4ef0d468bSGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
6ef0d468bSGreg Roach * This program is free software: you can redistribute it and/or modify
7ef0d468bSGreg Roach * it under the terms of the GNU General Public License as published by
8ef0d468bSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9ef0d468bSGreg Roach * (at your option) any later version.
10ef0d468bSGreg Roach * This program is distributed in the hope that it will be useful,
11ef0d468bSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12ef0d468bSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13ef0d468bSGreg Roach * GNU General Public License for more details.
14ef0d468bSGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16ef0d468bSGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Report;
21ef0d468bSGreg Roach
22208e9f76SGreg Roachuse DomainException;
237774ce41SGreg Roachuse Exception;
24*6d66a8ecSGreg Roachuse Fisharebest\Webtrees\Registry;
258792b649SGreg Roachuse XMLParser;
26299d100dSGreg Roach
27b19e047dSGreg Roachuse function call_user_func;
28b6f35a76SGreg Roachuse function fclose;
29b6f35a76SGreg Roachuse function feof;
30b6f35a76SGreg Roachuse function fopen;
31b6f35a76SGreg Roachuse function fread;
32b19e047dSGreg Roachuse function method_exists;
33b6f35a76SGreg Roachuse function sprintf;
34b6f35a76SGreg Roachuse function xml_error_string;
35b6f35a76SGreg Roachuse function xml_get_current_line_number;
36b6f35a76SGreg Roachuse function xml_get_error_code;
37b6f35a76SGreg Roachuse function xml_parse;
38b6f35a76SGreg Roachuse function xml_parser_create;
39b6f35a76SGreg Roachuse function xml_parser_free;
40b6f35a76SGreg Roachuse function xml_parser_set_option;
41b6f35a76SGreg Roachuse function xml_set_character_data_handler;
42b6f35a76SGreg Roachuse function xml_set_element_handler;
43b6f35a76SGreg Roach
44b6f35a76SGreg Roachuse const XML_OPTION_CASE_FOLDING;
45b19e047dSGreg Roach
46ef0d468bSGreg Roach/**
47ef0d468bSGreg Roach * Class ReportParserBase
48ef0d468bSGreg Roach */
49c1010edaSGreg Roachclass ReportParserBase
50c1010edaSGreg Roach{
514e69366aSGreg Roach    /** @var XMLParser (resource before PHP 8.0) The XML parser */
52a6f13a4aSGreg Roach    protected $xml_parser;
53ef0d468bSGreg Roach
54ef0d468bSGreg Roach    /** @var string Text contents of tags */
5577bab461SGreg Roach    protected string $text = '';
56ef0d468bSGreg Roach
57ef0d468bSGreg Roach    /**
58ef0d468bSGreg Roach     * Create a parser for a report
59ef0d468bSGreg Roach     *
60ef0d468bSGreg Roach     * @param string $report The XML filename
617774ce41SGreg Roach     *
627774ce41SGreg Roach     * @throws Exception
63ef0d468bSGreg Roach     */
6476f666f4SGreg Roach    public function __construct(string $report)
65c1010edaSGreg Roach    {
66ef0d468bSGreg Roach        $this->xml_parser = xml_parser_create();
678a4ee39cSGreg Roach
68ef0d468bSGreg Roach        xml_parser_set_option($this->xml_parser, XML_OPTION_CASE_FOLDING, false);
698a4ee39cSGreg Roach
708a4ee39cSGreg Roach        xml_set_element_handler(
718a4ee39cSGreg Roach            $this->xml_parser,
72af14d238SGreg Roach            function ($parser, string $name, array $attrs): void {
738a4ee39cSGreg Roach                $this->startElement($parser, $name, $attrs);
748a4ee39cSGreg Roach            },
75af14d238SGreg Roach            function ($parser, string $name): void {
768a4ee39cSGreg Roach                $this->endElement($parser, $name);
778a4ee39cSGreg Roach            }
788a4ee39cSGreg Roach        );
798a4ee39cSGreg Roach
808a4ee39cSGreg Roach        xml_set_character_data_handler(
818a4ee39cSGreg Roach            $this->xml_parser,
829d454b6bSGreg Roach            function ($parser, string $data): void {
838a4ee39cSGreg Roach                $this->characterData($parser, $data);
848a4ee39cSGreg Roach            }
858a4ee39cSGreg Roach        );
86ef0d468bSGreg Roach
87*6d66a8ecSGreg Roach        $fp = Registry::filesystem()->root()->readStream($report);
887774ce41SGreg Roach
89e364afe4SGreg Roach        while ($data = fread($fp, 4096)) {
90ef0d468bSGreg Roach            if (!xml_parse($this->xml_parser, $data, feof($fp))) {
91208e9f76SGreg Roach                throw new DomainException(sprintf(
92ef0d468bSGreg Roach                    'XML error: %s at line %d',
93ef0d468bSGreg Roach                    xml_error_string(xml_get_error_code($this->xml_parser)),
94ef0d468bSGreg Roach                    xml_get_current_line_number($this->xml_parser)
95ef0d468bSGreg Roach                ));
96ef0d468bSGreg Roach            }
97ef0d468bSGreg Roach        }
98ef0d468bSGreg Roach
997774ce41SGreg Roach        fclose($fp);
1007774ce41SGreg Roach
101ef0d468bSGreg Roach        xml_parser_free($this->xml_parser);
102ef0d468bSGreg Roach    }
103ef0d468bSGreg Roach
104ef0d468bSGreg Roach    /**
105ef0d468bSGreg Roach     * XML handler for an opening (or self-closing) tag.
106ef0d468bSGreg Roach     *
107ef0d468bSGreg Roach     * @param resource $parser The resource handler for the xml parser
108ef0d468bSGreg Roach     * @param string   $name   The name of the xml element parsed
109ef0d468bSGreg Roach     * @param string[] $attrs  An array of key value pairs for the attributes
11018d7a90dSGreg Roach     *
11118d7a90dSGreg Roach     * @return void
112ef0d468bSGreg Roach     */
113af14d238SGreg Roach    protected function startElement($parser, string $name, array $attrs): void
114c1010edaSGreg Roach    {
115a6f13a4aSGreg Roach        $method = $name . 'StartHandler';
116208e9f76SGreg Roach
117ef0d468bSGreg Roach        if (method_exists($this, $method)) {
118b19e047dSGreg Roach            call_user_func([$this, $method], $attrs);
119ef0d468bSGreg Roach        }
120ef0d468bSGreg Roach    }
121ef0d468bSGreg Roach
122ef0d468bSGreg Roach    /**
123ef0d468bSGreg Roach     * XML handler for a closing tag.
124ef0d468bSGreg Roach     *
125ef0d468bSGreg Roach     * @param resource $parser the resource handler for the xml parser
126ef0d468bSGreg Roach     * @param string   $name   the name of the xml element parsed
12718d7a90dSGreg Roach     *
12818d7a90dSGreg Roach     * @return void
129ef0d468bSGreg Roach     */
130af14d238SGreg Roach    protected function endElement($parser, string $name): void
131c1010edaSGreg Roach    {
132a6f13a4aSGreg Roach        $method = $name . 'EndHandler';
133208e9f76SGreg Roach
134ef0d468bSGreg Roach        if (method_exists($this, $method)) {
135b19e047dSGreg Roach            call_user_func([$this, $method]);
136ef0d468bSGreg Roach        }
137ef0d468bSGreg Roach    }
138ef0d468bSGreg Roach
139ef0d468bSGreg Roach    /**
140ef0d468bSGreg Roach     * XML handler for character data.
141ef0d468bSGreg Roach     *
142ef0d468bSGreg Roach     * @param resource $parser The resource handler for the xml parser
143ef0d468bSGreg Roach     * @param string   $data   The name of the xml element parsed
14418d7a90dSGreg Roach     *
14518d7a90dSGreg Roach     * @return void
146ef0d468bSGreg Roach     */
14724f2a3afSGreg Roach    protected function characterData($parser, string $data): void
148c1010edaSGreg Roach    {
149ef0d468bSGreg Roach        $this->text .= $data;
150ef0d468bSGreg Roach    }
151ef0d468bSGreg Roach}
152