/[drupal]/contributions/modules/bookpost/bookpost.module
ViewVC logotype

Contents of /contributions/modules/bookpost/bookpost.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Wed Nov 5 17:41:44 2008 UTC (12 months, 3 weeks ago) by rubinsztajn
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +2 -2 lines
File MIME type: text/x-php
#329638: fixed type to strip spaces from user supplied ISBN's.  Thanks kalle.
1 <?php
2 // $Id: bookpost.module,v 1.2 2008/10/07 19:42:35 rubinsztajn Exp $
3
4 /**
5 * @file
6 * Aaron Rubinstein
7 * Module for collecting book information and covers from the Open Library.
8 * This modules filters ISBN's or OpenLibrary IDs supplied by users inside two
9 * curly braces {{}} and returns book information and
10 * covers if available from the Open Library.
11 * Based on John Miedema's OpenBook plugin for Wordpress
12 * @see http://wordpress.org/extend/plugins/openbook-book-data/
13 * @see http://openlibrary.org
14 */
15
16 //Implementation of hook_filter_tips
17 function bookpost_filter_tips($delta, $format, $long = FALSE) {
18 switch($delta) {
19 case 0:
20 if ($long) {
21 return t('Placing an 10 or 13-digit ISBN or Open Library ID between curly braces, {{0312305389}}, will return book information and cover from the Open Library');
22 }
23 else {
24 return t('An ISBN or Open Library ID between curly braces {{}} will return book info and cover');
25 }
26 break;
27 }
28 }
29
30 //Implementation of hook_filter
31 function bookpost_filter($op, $delta = 0, $format = -1, $text ='') {
32 switch ($op) {
33 case 'list':
34 return array(0 => t('Book Post Filter'));
35
36 case 'description':
37 return t('Replaces ISBN or Open Library ID, e.g. {{0738531367}}, {{OL1880549M}}, with book information and cover');
38
39 case 'no cache':
40 return TRUE;
41
42 case 'prepare':
43 return $text;
44
45 case 'process':
46 preg_match_all('/{{(.*)}}/', $text, $match);
47 $tag = $match[0];
48 $book = array();
49 $counter=0;
50 for($i=0;$i<count($match[1]);$i++) {
51 $book[$counter] = retrieve_bookdata($match[1][$i]);
52 $counter++;
53 }
54 $text = str_replace($tag, $book, $text);
55 return $text;
56
57 case 'settings':
58 $form['bookpost_filter'] = array(
59 '#type' => 'fieldset',
60 '#title' => t('Book Post Filter'),
61 '#collapsible' => TRUE,
62 '#collapsed' => FALSE,
63 );
64 $form['bookpost_filter']['worldcat'] = array(
65 '#type' => 'checkbox',
66 '#title' => t('Link to WorldCat'),
67 '#default_value' => variable_get('worldcat', 0),
68 );
69 $form['bookpost_filter']['librarything'] = array(
70 '#type' => 'checkbox',
71 '#title' => t('Link to LibraryThing'),
72 '#default_value' => variable_get('librarything', 0),
73 );
74 $form['bookpost_filter']['googlebooks'] = array(
75 '#type' => 'checkbox',
76 '#title' => t('Link to Google Book Search'),
77 '#default_value' => variable_get('googlebooks', 0),
78 );
79 return $form;
80 }
81 }
82
83 function retrieve_bookdata($id) {
84 if(preg_match('/^OL/', $id)) {
85 $bookkey = '/b/'.$id;
86 }
87 else {
88 $isbn = $id;
89
90 //clean ISBN of dashes and spaces
91 $isbn = str_replace("-", "", $isbn);
92 $isbn = str_replace(" ", "", $isbn);
93
94 $path = drupal_get_path('module', 'bookpost');
95 //link to WorldCat, LibraryThing, and Google books if selected
96 if(variable_get('worldcat', 0)) {
97 $worldcat = '<a href="http://worldcat.org/isbn/' . $isbn . '">Find on WorldCat</a><p />';
98 }
99 if(variable_get('librarything', 0)) {
100 $librarything = '<a href="http://librarything.com/isbn/' . $isbn . '">Find on LibraryThing</a><p />';
101 }
102 if(variable_get('googlebooks', 0)) {
103 $googlebooks = '<a href="http://books.google.com/books?vid=ISBN' . $isbn . '">Find on Google Books</a><br />';
104 }
105 //query OpenLibrary for OL ids that match the ISBN
106 $url_bookkeys = "http://openlibrary.org/api/search?q={%22query%22:%22(isbn_10:(".$isbn.")%20OR%20isbn_13:(".$isbn."))%22}&text=true";
107 $bookkeys = get_url_contents($url_bookkeys, $curltimeout);
108 $obj = json_decode($bookkeys);
109 $bookkeyresult = $obj->{'result'};
110
111 //determine number of versions and choose first available
112 //version if none specified
113 $bookversioncount = count($bookkeyresult);
114 if($bookversion == "") $bookversion = 1;
115 elseif ($bookversion > $bookversioncount) $bookversion = $bookversioncount;
116 $bookversion = $bookversion -1;
117
118 $bookkey = $bookkeyresult[$bookversion];
119 }
120
121 $bookpage = "http://openlibrary.org" . $bookkey;
122 $curltimeout = 10;
123 $url = "http://openlibrary.org/api/get?key=".$bookkey."&text=true";
124 $bookdata = get_url_contents($url, $curltimeout);
125
126 $obj = json_decode($bookdata);
127 $bookdataresult = $obj->{'result'};
128
129 //title
130 $title = $bookdataresult->{'title'};
131 $subtitle = $bookdataresult->{'subtitle'};
132 $title=ucwords($title);
133
134 //authors -- handle multiple
135 $authors = $bookdataresult->{'authors'};
136 if(is_array($authors)) {
137 for($i=0;$i<count($authors);$i++) {
138 $authorkey = $authors[$i]->{'key'};
139 $url_author = "http://openlibrary.org/api/get?key=".$authorkey."&text=true";
140
141 $authordata = get_url_contents($url_author, $curltimeout);
142 $obj = json_decode($authordata);
143 $authorresult = $obj->{'result'};
144 $name = $authorresult->{'name'};
145 if($i==0) $authorlist = $name;
146 else $authorlist = $authorlist.",".$name;
147 }
148 }
149 $authors = $authorlist;
150 if ($authors=="") {
151 $authors = $bookdataresult->{'by_statement'};
152 if ($authors=="") {
153 $authors=$bookdataresult->{'contributions'};
154 if(is_array($authors)) $authors=implode(",", $authors);
155 }
156 }
157
158 $authors = ucwords($authors);
159
160 //publishers
161 $publishers = $bookdataresult->{'publishers'};
162 if(is_array($publishers)) {
163 for($i=0;$i<count($publishers);$i++) {
164 $publisher = $publishers[$i];
165 if($i==0) $publisherlist = $publisher;
166 else $publisherlist = $publisherlist . ", " . $publisher;
167 }
168 }
169 $publishers = ucwords($publisherlist);
170
171 //coverimage
172 $olnumber_begin = stripos($bookkey,"/b/") + 3;
173 $olnumber = substr($bookkey, $olnumber_begin);
174
175
176 //test if Open Library returns a 1 pixel x 1 pixel image (i.e. no cover is available for the title
177 $path = drupal_get_path('module', 'bookpost');
178 $size = getimagesize("http://covers.openlibrary.org/b/olid/$olnumber-M.jpg");
179 if($size[0] == 1) {
180 $coverimage = $path . '/images/openlibrarylogo-en.jpg';
181 }
182 else {
183 $coverimage = "http://covers.openlibrary.org/b/olid/" .$olnumber . "-M.jpg";
184 }
185 //pagination
186 $pages = $bookdataresult->{'number_of_pages'};
187
188 //publication date
189 $date = $bookdataresult->{'publish_date'};
190
191 //link to fulltext if available
192 $link = $bookdataresult->{'ocaid'};
193 if($link) {
194 $fulltext = '<a href="http://openlibrary.org/details/' . $link . '"><img src="' . $path .'/images/readonline.jpg" border="0" /></a><br />';
195 }
196
197 //build HTML
198 $html_bookdata = "";
199
200 //coverimage
201 $html_coverimage = "<img src='" . $coverimage . "' alt='' border=0 style='float:left;padding-right:15px;padding-bottom:10px;' />";
202 $html_coverimage = "<a href='" . $bookpage . "'>" . $html_coverimage . "</a>";
203
204 //title
205 $html_title = '<a href="' . $bookpage . '" ><i>' . $title . '</i></a>';
206
207 //author
208 $html_authors = $authors;
209
210 //publishers
211 $html_publishers = $publishers;
212
213 //assemble text
214 $html_text = "Title: <b>" . $html_title . "</b><br /> " . "Author(s): <b> ". $html_authors . "</b><br />" . "Publisher: " .$html_publishers . '<br />' . "Pages: " . $pages . '<br />' . "Date: " .$date . '<p />' .$fulltext . $worldcat . $librarything . $googlebooks;
215
216 //div id for themeing
217 $html_bookdata = '<div class="bookpost" style="width:500px;height:275px;">' . $html_coverimage . $html_text . '</div>';
218
219 return $html_bookdata;
220
221 }
222
223 //this method replaces file_get_contents, which is sometimes disallowed on servers
224 function get_url_contents($url, $curltimeout) {
225
226 // Establish a cURL handle.
227 $ch = curl_init($url);
228
229 // Set our options
230 curl_setopt($ch, CURLOPT_HEADER, false); //false=do not include headers
231 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //true=return as string
232 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $curltimeout); //timeout for when OL is down
233 curl_setopt($ch, CURLOPT_TIMEOUT, $curltimeout); //timeout for when OL is down
234
235 // Execute the request
236 $output = curl_exec($ch);
237
238 // Close the cURL session.
239 curl_close($ch);
240
241 if ($output == "") throw new Exception("Open Library Data Unavailable");
242
243 return $output;
244 }

  ViewVC Help
Powered by ViewVC 1.1.2