Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
Syndetics
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 1
132
0.00% covered (danger)
0.00%
0 / 1
 loadByIsbn
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 1
132
1<?php
2
3/**
4 * Syndetics TOC content loader.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) The University of Chicago 2017.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Content
25 * @author   John Jung <jej@uchicago.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development Wiki
28 */
29
30namespace VuFind\Content\TOC;
31
32use function sprintf;
33
34/**
35 * Syndetics TOC content loader.
36 *
37 * @category VuFind
38 * @package  Content
39 * @author   John Jung <jej@uchicago.edu>
40 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
41 * @link     https://vufind.org/wiki/development Wiki
42 */
43class Syndetics extends \VuFind\Content\AbstractSyndetics
44{
45    /**
46     * List of data sources for author notes.
47     *
48     * @var array
49     */
50    protected $sourceList = [
51        'TOC' => [
52            'title' => 'TOC',
53            'file' => 'TOC.XML',
54            'div' => '<div id="syn_toc"></div>',
55        ],
56    ];
57
58    /**
59     * This method is responsible for connecting to Syndetics for tables
60     * of contents.
61     *
62     * It first queries the master url for the ISBN entry seeking an excerpt URL.
63     * If an excerpt URL is found, the script will then use HTTP request to
64     * retrieve the script. The script will then parse the excerpt according to
65     * US MARC (I believe). It will provide a link to the URL master HTML page
66     * for more information.
67     * Configuration:  Sources are processed in order - refer to $sourceList above.
68     *
69     * @param string           $key     API key
70     * @param \VuFindCode\ISBN $isbnObj ISBN object
71     *
72     * @throws \Exception
73     * @return array     Returns array with table of contents data.
74     * @author John Jung <jej@uchicago.edu>
75     */
76    public function loadByIsbn($key, \VuFindCode\ISBN $isbnObj)
77    {
78        // Initialize return value:
79        $toc = [];
80
81        // Find out if there are any tables of contents
82        $isbn = $this->getIsbn10($isbnObj);
83        $url = $this->getIsbnUrl($isbn, $key);
84        $result = $this->getHttpClient($url)->send();
85        if (!$result->isSuccess()) {
86            return $toc;
87        }
88
89        // Test XML Response
90        if (!($xmldoc = $this->xmlToDOMDocument($result->getBody()))) {
91            throw new \Exception('Invalid XML');
92        }
93
94        foreach ($this->sourceList as $source => $sourceInfo) {
95            $nodes = $xmldoc->getElementsByTagName($source);
96            if ($nodes->length) {
97                // Load toc
98                $url = $this->getIsbnUrl($isbn, $key, $sourceInfo['file']);
99                $result2 = $this->getHttpClient($url)->send();
100                if (!$result2->isSuccess()) {
101                    continue;
102                }
103
104                // Test XML Response
105                $xmldoc2 = $this->xmlToDOMDocument($result2->getBody());
106                if (!$xmldoc2) {
107                    throw new \Exception('Invalid XML');
108                }
109
110                // If we have syndetics plus, we don't actually want the content
111                // we'll just stick in the relevant div
112                if ($this->usePlus) {
113                    $toc = $sourceInfo['div'];
114                } else {
115                    // Get the marc field for toc (970)
116                    $nodes = $xmldoc2->GetElementsbyTagName('Fld970');
117
118                    foreach ($nodes as $node) {
119                        $li = '';
120
121                        // Chapter labels.
122                        $nodeList = $node->getElementsByTagName('l');
123                        if ($nodeList->length > 0) {
124                            $li .= sprintf('%s. ', $nodeList->item(0)->nodeValue);
125                        }
126
127                        // Chapter title.
128                        $nodeList = $node->getElementsByTagName('t');
129                        if ($nodeList->length > 0) {
130                            $li .= $nodeList->item(0)->nodeValue;
131                        }
132
133                        $toc[] = preg_replace(
134                            '/<a>|<a [^>]*>|<\/a>/',
135                            '',
136                            html_entity_decode($li)
137                        );
138                    }
139                }
140            }
141        }
142
143        return $toc;
144    }
145}