Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 34
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 / 34
0.00% covered (danger)
0.00%
0 / 1
90
0.00% covered (danger)
0.00%
0 / 1
 loadByIsbn
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
90
1<?php
2
3/**
4 * Syndetics author notes 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\AuthorNotes;
31
32/**
33 * Syndetics author notes 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 data sources for author notes.
45     *
46     * @var array
47     */
48    protected $sourceList = [
49        'ANOTES' => [
50            'title' => 'Author Notes',
51            'file' => 'ANOTES.XML',
52            'div' => '<div id="syn_anotes"></div>',
53        ],
54    ];
55
56    /**
57     * This method is responsible for connecting to Syndetics and abstracting
58     * author notes.
59     *
60     * It first queries the master url for the ISBN entry seeking a note URL.
61     * If a note URL is found, the script will then use HTTP request to
62     * retrieve the script. The script will then parse the note 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 author note 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        $anotes = [];
79
80        // Find out if there are any notes
81        $isbn = $this->getIsbn10($isbnObj);
82        $url = $this->getIsbnUrl($isbn, $key);
83        $result = $this->getHttpClient($url)->send();
84        if (!$result->isSuccess()) {
85            return $anotes;
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 notes
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                    $anotes[$i]['Content'] = $sourceInfo['div'];
114                } else {
115                    // Get the marc field for author notes (980)
116                    $nodes = $xmldoc2->GetElementsbyTagName('Fld980');
117                    if (!$nodes->length) {
118                        // Skip fields with missing text
119                        continue;
120                    }
121                    // Decode the content and strip unwanted <a> tags:
122                    $anotes[$i]['Content'] = preg_replace(
123                        '/<a>|<a [^>]*>|<\/a>/',
124                        '',
125                        html_entity_decode($xmldoc2->saveXML($nodes->item(0)))
126                    );
127                }
128
129                // change the xml to actual title:
130                $anotes[$i]['Source'] = $sourceInfo['title'];
131
132                $anotes[$i]['ISBN'] = $isbn;
133                $anotes[$i]['username'] = $key;
134
135                $i++;
136            }
137        }
138
139        return $anotes;
140    }
141}