Skip to content
Newer
Older
100644 217 lines (163 sloc) 6.92 KB
fba9163 @jdurno Adding files
authored Aug 10, 2012
1 <?php
2 /* Convert an openURL into a Voyager search */
3
4 /*
5 copyright 2011,2012 University of Victoria Libraries
6
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 */
21
22
23 include "../includes/contextobject.inc.php";
24
25 $ctx_obj = new ContextObject();
26
27 //Voyager search URL, search is appended below
28 $voyURL="http://voyager.library.uvic.ca/vwebv/search?";
29
30 //try to determine the format. This depends on metadata in the OpenURL, so it won't always work.
31 //the getFormat() function attempts to guess the format based on clues in the openurl metadata
32 $format = $ctx_obj->getFormat();
33
34 //some books have ISSNs, and we don't want to search on ISSNs for books
35 if ($format !== 'book') {
36 //if there is a issn or eissn just use that
37
38 if($ctx_obj->issn || $ctx_obj->eissn){
39 if($ctx_obj->issn){
40 $withdashissn=$ctx_obj->issn;
41 if(strlen($ctx_obj->issn)<9){
42 $orig_string=$ctx_obj->issn;
43 $insert_string="-";
44 $position=4;
45 $withdashissn=substr_replace($orig_string, $insert_string, $position, 0);
46 }
47 $voyURL.="searchArg1=".$withdashissn."&argType1=phrase&searchCode1=ISBN";
48 } else if($ctx_obj->eissn){
49 $withdasheissn=$ctx_obj->issn;
50 if(strlen($ctx_obj->eissn)<9){
51 $orig_string=$ctx_obj->eissn;
52 $insert_string="-";
53 $position=4;
54 $withdasheissn=substr_replace($orig_string, $insert_string, $position, 0);
55 }
56 $voyURL.="searchArg1=".withdasheissn."&argType1=phrase&searchCode1=ISBN";
57 }
58 $voyURL.="&recCount=25&searchType=2&page.search.search.button=Search";
59 header("Location: $voyURL");
60 exit;
61 }
62
63 }
64
65 // if its a journal JALL (begins with or JKEY^* (contains) just to a journal keyword search and if there is an issn do an issn search
66 // if the item has a jtitle its a journal or if
67 //if the title has the label Journal Title its a journal,
68
69 if($ctx_obj->jtitle){
70 $jtitle.=$ctx_obj->jtitle;
71 $voyURL.="searchArg=".$jtitle."&searchCode=JKEY^*&recCount=25&searchType=1&page.search.search.button=Search";
72 }else if($ctx_obj->labelList[title]=="Journal Title"){
73 $jtitle.=$ctx_obj->title;
74 $voyURL.="searchArg=".$jtitle."&searchCode=JKEY^*&recCount=25&searchType=1&page.search.search.button=Search";
75 } else{
76 // not a journal so do title keyword and if there is one: author
77 //TKEY (contains) do title keyword, author, not isbn,
78
79 if($ctx_obj->btitle){
80 $tempTitle = $ctx_obj->btitle;
81 }elseif($ctx_obj->title){
82 $tempTitle = $ctx_obj->title;
83 } elseif ($ctx_obj->isbn){ //no title, but there is an ISBN? See if you can get a title & author from Worldcat
84 $isbn = $ctx_obj->isbn;
85
86 $voyURL = useWorldCat($isbn);
87
88 //if WorldCat search failed, just use the ISBN as a last-ditch attempt
89 if (!$voyURL) {
90 $voyURL = 'http://voyager.library.uvic.ca/vwebv/search?searchArg1=' . $isbn . '&argType1=phrase&searchCode1=ISBN&recCount=25&searchType=2&page.search.search.button=Search';
91
92 }
93 //send either the WorldCat data or the ISBN, depending on what we got back from WorldCat
94 header("location: $voyURL");
95 exit;
96 }
97
98
99 //split the title on a colon
100 $partsOftempTitle = explode(":", $tempTitle);
101 // now if the first part contains a ( as in something like (3rd ed.) remove that from the title
102 $partsOfTitle = explode("(", $partsOftempTitle[0]);
103 $partsOfTitle2 = array();
104 if (array_key_exists(1, $partsOftempTitle)) {
105 $partsOfTitle2 = explode("(", $partsOftempTitle[1]);
106 }
107
108 $searchTitle = $partsOfTitle[0];
109 $searchTitle = trim($searchTitle);
110 $searchTitle = rtrim($searchTitle,'.');
111 $searchTitle = str_replace('. ', ' ', $searchTitle);
112
113 $searchTitle2 = NULL;
114
115 if (count($partsOfTitle2) > 0) {
116 $searchTitle2 = $partsOfTitle2[0];
117 $searchTitle2 = trim($searchTitle2);
118 $searchTitle2 = rtrim($searchTitle2,'.');
119 $searchTitle2 = str_replace('. ',' ', $searchTitle2);
120 }
121
122
123 $voyURL.="searchArg1=".$searchTitle."&argType1=phrase&searchCode1=TKEY";//TALL
124
125 //track whether we've used the 'combine2' argument; will need to know when we tack on ISBN
126 $used2 = FALSE;
127 //send the part after the semi-colon as a second title phrase search
128 if ($searchTitle2) {
129 $voyURL.="&combine2=and&searchArg2=".$searchTitle2."&argType2=phrase&searchCode2=TKEY";//TALL
130 $used2 = TRUE;
131
132 } elseif($ctx_obj->getAuthors()){
133 $authorName = FALSE;
134 if (!empty($ctx_obj->aulast)) {
135 $authorName = $ctx_obj->aulast;
136 } elseif (count($ctx_obj->au) > 0){
137 $authorName = $ctx_obj->au[0];
138 }
139 //can't use author for books if it's chapter metadata. Use the presence of the 'atitle' parameter to determine this
140 if($authorName && !isset($ctx_obj->atitle)){
141 $voyURL.="&combine2=and&searchArg2=".$authorName."&argType2=all&searchCode2=NKEY";
142 $used2 = TRUE;
143 }
144 }
145
146 //sometimes the ISBN will match when the title won't, so append the ISBN as an OR search
147
148 if (!empty($ctx_obj->isbn)) {
149 if ($used2) {
150 $num = 3;
151 } else {
152 $num = 2;
153 }
154
155 $voyURL .= '&combine' . $num . '=or&searchArg' . $num . '=' . $ctx_obj->isbn . '&argType' . $num . '=phrase&searchCode' . $num . '=ISBN';
156 }
157
158
159
160
161 $voyURL.="&recCount=25&searchType=2&page.search.search.button=Search";
162
163 }
164
165 header("Location: $voyURL");
166
167
168 //get Title and Author info from Worldcat if you have an ISBN
169 function useWorldCat($isbn) {
170
171 $wskey = ''; //need an authorization key
172
173 //construct the query against the basic search API
174 $url = 'http://www.worldcat.org/webservices/catalog/search/opensearch?q=' . $isbn . '&count=1&wskey=' . $wskey;
175
176 $data = get_data($url);
177
178 //parse
179 $bibData = new SimpleXMLElement($data);
180
181 if (isset($bibData->entry[0]->title)) {
182 $bookTitle = $bibData->entry[0]->title;
183 } else {
184 //no title? Useless ...
185 return NULL;
186 }
187
188
189 $authorName = $bibData->entry[0]->author->name;
190
191 $authorBits = explode(',', $authorName);
192 $authorLastName = $authorBits[0];
193
194 //oddly, can't count on colons to separate main & subtitle, so need to do title keyword instead
195 //fortunately, we know the author name info is reliable
196
197 $voyagerURL = 'http://voyager.library.uvic.ca/vwebv/search?searchArg1=' . $bookTitle . '&argType1=all&searchCode1=TKEY&combine2=and&searchArg2=' . $authorLastName . '&argType2=all&searchCode2=NKEY&recCount=25&searchType=2&page.search.search.button=Search';
198
199 return $voyagerURL;
200
201 }
202
203 //wrapper around PHP curl, used by the WorldCat function
204 function get_data($url)
205 {
206 $ch = curl_init();
207 $timeout = 5;
208 curl_setopt($ch,CURLOPT_URL,$url);
209 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
210 curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
211 $data = curl_exec($ch);
212 curl_close($ch);
213 return $data;
214 }
215
216 ?>
Something went wrong with that request. Please try again.