Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 45
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 / 45
0.00% covered (danger)
0.00%
0 / 1
156
0.00% covered (danger)
0.00%
0 / 1
 loadByIsbn
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 1
156
1<?php
2
3/**
4 * Syndetics excerpt content loader.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
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   Demian Katz <demian.katz@villanova.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\Excerpts;
31
32/**
33 * Syndetics excerpt content loader.
34 *
35 * @category VuFind
36 * @package  Content
37 * @author   Demian Katz <demian.katz@villanova.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 syndetic excerpts
45     *
46     * @var array
47     */
48    protected $sourceList = [
49        'DBCHAPTER' => [
50            'title' => 'First Chapter or Excerpt',
51            'file' => 'DBCHAPTER.XML',
52            'div' => '<div id="syn_dbchapter"></div>',
53        ],
54    ];
55
56    /**
57     * This method is responsible for connecting to Syndetics and abstracting
58     * excerpts.
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 excerpt data.
72     * @author Joel Timothy Norman <joel.t.norman@wmich.edu>
73     * @author Andrew Nagy <vufind-tech@lists.sourceforge.net>
74     */
75    public function loadByIsbn($key, \VuFindCode\ISBN $isbnObj)
76    {
77        // Initialize return value:
78        $excerpt = [];
79
80        // Find out if there are any excerpts
81        $isbn = $this->getIsbn10($isbnObj);
82        $url = $this->getIsbnUrl($isbn, $key);
83        $result = $this->getHttpClient($url)->send();
84        if (!$result->isSuccess()) {
85            return $excerpt;
86        }
87
88        // Test XML Response
89        if (!($xmldoc = $this->xmlToDOMDocument($result->getBody()))) {
90            throw new \Exception('Invalid XML');
91        }
92
93        $i = 0;
94        foreach ($this->sourceList as $source => $sourceInfo) {
95            $nodes = $xmldoc->getElementsByTagName($source);
96            if ($nodes->length) {
97                // Load excerpts
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                    $excerpt[$i]['Content'] = $sourceInfo['div'];
114                } else {
115                    // Get the marc field for excerpts (520)
116                    $nodes = $xmldoc2->GetElementsbyTagName('Fld520');
117                    if (!$nodes->length) {
118                        // Skip excerpts with missing text
119                        continue;
120                    }
121                    $excerpt[$i]['Content']
122                        = html_entity_decode($xmldoc2->saveXML($nodes->item(0)));
123
124                    // Get the marc field for copyright (997)
125                    $nodes = $xmldoc->GetElementsbyTagName('Fld997');
126                    if ($nodes->length) {
127                        $excerpt[$i]['Copyright'] = html_entity_decode(
128                            $xmldoc2->saveXML($nodes->item(0))
129                        );
130                    } else {
131                        $excerpt[$i]['Copyright'] = null;
132                    }
133
134                    if ($excerpt[$i]['Copyright']) {  //stop duplicate copyrights
135                        $location = strripos(
136                            $excerpt[0]['Content'],
137                            (string)$excerpt[0]['Copyright']
138                        );
139                        if ($location > 0) {
140                            $excerpt[$i]['Content']
141                                = substr($excerpt[0]['Content'], 0, $location);
142                        }
143                    }
144                }
145
146                // change the xml to actual title:
147                $excerpt[$i]['Source'] = $sourceInfo['title'];
148
149                $excerpt[$i]['ISBN'] = $isbn;
150                $excerpt[$i]['username'] = $key;
151
152                $i++;
153            }
154        }
155
156        return $excerpt;
157    }
158}