1ef0d468bSGreg Roach<?php 23976b470SGreg Roach 3ef0d468bSGreg Roach/** 4ef0d468bSGreg Roach * webtrees: online genealogy 5*89f7189bSGreg 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 15*89f7189bSGreg 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; 24299d100dSGreg Roach 25b19e047dSGreg Roachuse function call_user_func; 26b6f35a76SGreg Roachuse function fclose; 27b6f35a76SGreg Roachuse function feof; 28b6f35a76SGreg Roachuse function fopen; 29b6f35a76SGreg Roachuse function fread; 30b19e047dSGreg Roachuse function method_exists; 31b6f35a76SGreg Roachuse function sprintf; 32b6f35a76SGreg Roachuse function xml_error_string; 33b6f35a76SGreg Roachuse function xml_get_current_line_number; 34b6f35a76SGreg Roachuse function xml_get_error_code; 35b6f35a76SGreg Roachuse function xml_parse; 36b6f35a76SGreg Roachuse function xml_parser_create; 37b6f35a76SGreg Roachuse function xml_parser_free; 38b6f35a76SGreg Roachuse function xml_parser_set_option; 39b6f35a76SGreg Roachuse function xml_set_character_data_handler; 40b6f35a76SGreg Roachuse function xml_set_element_handler; 41b6f35a76SGreg Roach 42b6f35a76SGreg Roachuse const XML_OPTION_CASE_FOLDING; 43b19e047dSGreg Roach 44ef0d468bSGreg Roach/** 45ef0d468bSGreg Roach * Class ReportParserBase 46ef0d468bSGreg Roach */ 47c1010edaSGreg Roachclass ReportParserBase 48c1010edaSGreg Roach{ 49ef0d468bSGreg Roach /** @var resource The XML parser */ 50a6f13a4aSGreg Roach protected $xml_parser; 51ef0d468bSGreg Roach 52ef0d468bSGreg Roach /** @var string Text contents of tags */ 53ef0d468bSGreg Roach protected $text = ''; 54ef0d468bSGreg Roach 55ef0d468bSGreg Roach /** 56ef0d468bSGreg Roach * Create a parser for a report 57ef0d468bSGreg Roach * 58ef0d468bSGreg Roach * @param string $report The XML filename 597774ce41SGreg Roach * 607774ce41SGreg Roach * @throws Exception 61ef0d468bSGreg Roach */ 6276f666f4SGreg Roach public function __construct(string $report) 63c1010edaSGreg Roach { 64ef0d468bSGreg Roach $this->xml_parser = xml_parser_create(); 658a4ee39cSGreg Roach 66ef0d468bSGreg Roach xml_parser_set_option($this->xml_parser, XML_OPTION_CASE_FOLDING, false); 678a4ee39cSGreg Roach 688a4ee39cSGreg Roach xml_set_element_handler( 698a4ee39cSGreg Roach $this->xml_parser, 70af14d238SGreg Roach function ($parser, string $name, array $attrs): void { 718a4ee39cSGreg Roach $this->startElement($parser, $name, $attrs); 728a4ee39cSGreg Roach }, 73af14d238SGreg Roach function ($parser, string $name): void { 748a4ee39cSGreg Roach $this->endElement($parser, $name); 758a4ee39cSGreg Roach } 768a4ee39cSGreg Roach ); 778a4ee39cSGreg Roach 788a4ee39cSGreg Roach xml_set_character_data_handler( 798a4ee39cSGreg Roach $this->xml_parser, 809d454b6bSGreg Roach function ($parser, string $data): void { 818a4ee39cSGreg Roach $this->characterData($parser, $data); 828a4ee39cSGreg Roach } 838a4ee39cSGreg Roach ); 84ef0d468bSGreg Roach 85e364afe4SGreg Roach $fp = fopen($report, 'rb'); 868a4ee39cSGreg Roach 877774ce41SGreg Roach if ($fp === false) { 887774ce41SGreg Roach throw new Exception('Cannot open ' . $report); 897774ce41SGreg Roach } 907774ce41SGreg Roach 91e364afe4SGreg Roach while ($data = fread($fp, 4096)) { 92ef0d468bSGreg Roach if (!xml_parse($this->xml_parser, $data, feof($fp))) { 93208e9f76SGreg Roach throw new DomainException(sprintf( 94ef0d468bSGreg Roach 'XML error: %s at line %d', 95ef0d468bSGreg Roach xml_error_string(xml_get_error_code($this->xml_parser)), 96ef0d468bSGreg Roach xml_get_current_line_number($this->xml_parser) 97ef0d468bSGreg Roach )); 98ef0d468bSGreg Roach } 99ef0d468bSGreg Roach } 100ef0d468bSGreg Roach 1017774ce41SGreg Roach fclose($fp); 1027774ce41SGreg Roach 103ef0d468bSGreg Roach xml_parser_free($this->xml_parser); 104ef0d468bSGreg Roach } 105ef0d468bSGreg Roach 106ef0d468bSGreg Roach /** 107ef0d468bSGreg Roach * XML handler for an opening (or self-closing) tag. 108ef0d468bSGreg Roach * 109ef0d468bSGreg Roach * @param resource $parser The resource handler for the xml parser 110ef0d468bSGreg Roach * @param string $name The name of the xml element parsed 111ef0d468bSGreg Roach * @param string[] $attrs An array of key value pairs for the attributes 11218d7a90dSGreg Roach * 11318d7a90dSGreg Roach * @return void 114ef0d468bSGreg Roach */ 115af14d238SGreg Roach protected function startElement($parser, string $name, array $attrs): void 116c1010edaSGreg Roach { 117a6f13a4aSGreg Roach $method = $name . 'StartHandler'; 118208e9f76SGreg Roach 119ef0d468bSGreg Roach if (method_exists($this, $method)) { 120b19e047dSGreg Roach call_user_func([$this, $method], $attrs); 121ef0d468bSGreg Roach } 122ef0d468bSGreg Roach } 123ef0d468bSGreg Roach 124ef0d468bSGreg Roach /** 125ef0d468bSGreg Roach * XML handler for a closing tag. 126ef0d468bSGreg Roach * 127ef0d468bSGreg Roach * @param resource $parser the resource handler for the xml parser 128ef0d468bSGreg Roach * @param string $name the name of the xml element parsed 12918d7a90dSGreg Roach * 13018d7a90dSGreg Roach * @return void 131ef0d468bSGreg Roach */ 132af14d238SGreg Roach protected function endElement($parser, string $name): void 133c1010edaSGreg Roach { 134a6f13a4aSGreg Roach $method = $name . 'EndHandler'; 135208e9f76SGreg Roach 136ef0d468bSGreg Roach if (method_exists($this, $method)) { 137b19e047dSGreg Roach call_user_func([$this, $method]); 138ef0d468bSGreg Roach } 139ef0d468bSGreg Roach } 140ef0d468bSGreg Roach 141ef0d468bSGreg Roach /** 142ef0d468bSGreg Roach * XML handler for character data. 143ef0d468bSGreg Roach * 144ef0d468bSGreg Roach * @param resource $parser The resource handler for the xml parser 145ef0d468bSGreg Roach * @param string $data The name of the xml element parsed 14618d7a90dSGreg Roach * 14718d7a90dSGreg Roach * @return void 148ef0d468bSGreg Roach */ 149af14d238SGreg Roach protected function characterData($parser, $data): void 150c1010edaSGreg Roach { 151ef0d468bSGreg Roach $this->text .= $data; 152ef0d468bSGreg Roach } 153ef0d468bSGreg Roach} 154