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
32/**
33 * Syndetics TOC content loader.
34 *
35 * @category VuFind
36 * @package  Content
37 * @author   John Jung <jej@uchicago.edu>
38 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
39 * @link     https://vufind.org/wiki/development Wiki
40 */
41class Syndetics extends \VuFind\Content\AbstractSyndetics
42{
43    /**
44     * List of data sources for author notes.
45     *
46     * @var array
47     */
48    protected $sourceList = [
49        'TOC' => [
50            'title' => 'TOC',
51            'file' => 'TOC.XML',
52            'div' => '<div id="syn_toc"></div>',
53        ],
54    ];
55
56    /**
57     * This method is responsible for connecting to Syndetics for tables
58     * of contents.
59     *
60     * It first queries the master url for the ISBN entry seeking an excerpt URL.
61     * If an excerpt URL is found, the script will then use HTTP request to
62     * retrieve the script. The script will then parse the excerpt according to
63     * US MARC (I believe). It will provide a link to the URL master HTML page
64     * for more information.
65     * Configuration:  Sources are processed in order - refer to $sourceList above.
66     *
67     * @param string           $key     API key
68     * @param \VuFindCode\ISBN $isbnObj ISBN object
69     *
70     * @throws \Exception
71     * @return array     Returns array with table of contents data.
72     * @author John Jung <jej@uchicago.edu>
73     */
74    public function loadByIsbn($key, \VuFindCode\ISBN $isbnObj)
75    {
76        // Initialize return value:
77        $toc = [];
78
79        // Find out if there are any tables of contents
80        $isbn = $this->getIsbn10($isbnObj);
81        $url = $this->getIsbnUrl($isbn, $key);
82        $result = $this->getHttpClient($url)->send();
83        if (!$result->isSuccess()) {
84            return $toc;
85        }
86
87        // Test XML Response
88        if (!($xmldoc = $this->xmlToDOMDocument($result->getBody()))) {
89            throw new \Exception('Invalid XML');
90        }
91
92        foreach ($this->sourceList as $source => $sourceInfo) {
93            $nodes = $xmldoc->getElementsByTagName($source);
94            if ($nodes->length) {
95                // Load toc
96                $url = $this->getIsbnUrl($isbn, $key, $sourceInfo['file']);
97                $result2 = $this->getHttpClient($url)->send();
98                if (!$result2->isSuccess()) {
99                    continue;
100                }
101
102                // Test XML Response
103                $xmldoc2 = $this->xmlToDOMDocument($result2->getBody());
104                if (!$xmldoc2) {
105                    throw new \Exception('Invalid XML');
106                }
107
108                // If we have syndetics plus, we don't actually want the content
109                // we'll just stick in the relevant div
110                if ($this->usePlus) {
111                    $toc = $sourceInfo['div'];
112                } else {
113                    // Get the marc field for toc (970)
114                    $nodes = $xmldoc2->GetElementsbyTagName('Fld970');
115
116                    foreach ($nodes as $node) {
117                        $li = '';
118
119                        // Chapter labels.
120                        $nodeList = $node->getElementsByTagName('l');
121                        if ($nodeList->length > 0) {
122                            $li .= sprintf('%s. ', $nodeList->item(0)->nodeValue);
123                        }
124
125                        // Chapter title.
126                        $nodeList = $node->getElementsByTagName('t');
127                        if ($nodeList->length > 0) {
128                            $li .= $nodeList->item(0)->nodeValue;
129                        }
130
131                        $toc[] = preg_replace(
132                            '/<a>|<a [^>]*>|<\/a>/',
133                            '',
134                            html_entity_decode($li)
135                        );
136                    }
137                }
138            }
139        }
140
141        return $toc;
142    }
143}