X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=BookReaderIA%2Fdatanode%2FBookReaderJSIA.php;h=016e988a581c37273ee841f3347a29d070656c59;hb=0afd0adfdd1c1f29f61351a5bdfc03a06bdfe38d;hp=e2c1528384028a029c732f7e4ab89ff40dc3b123;hpb=911f4df4afd8afcc88657384224f3c9bf59a14f9;p=bookreader.git diff --git a/BookReaderIA/datanode/BookReaderJSIA.php b/BookReaderIA/datanode/BookReaderJSIA.php old mode 100755 new mode 100644 index e2c1528..016e988 --- a/BookReaderIA/datanode/BookReaderJSIA.php +++ b/BookReaderIA/datanode/BookReaderJSIA.php @@ -18,15 +18,24 @@ This file is part of BookReader. along with BookReader. If not, see . */ +header('Content-Type: application/javascript'); + $id = $_REQUEST['id']; $itemPath = $_REQUEST['itemPath']; $subPrefix = $_REQUEST['subPrefix']; $server = $_REQUEST['server']; +// $$$mang this code has been refactored into BookReaderMeta.inc.php for use e.g. by +// BookReaderPreview.php and BookReaderImages.php. The code below should be +// taken out and replaced by calls into BookReaderMeta + // Check if we're on a dev vhost and point to JSIA in the user's public_html on the datanode + // $$$ TODO consolidate this logic if (strpos($_SERVER["REQUEST_URI"], "/~mang") === 0) { // Serving out of home dir $server .= ':80/~mang'; +} else if (strpos($_SERVER["REQUEST_URI"], "/~rkumar") === 0) { // Serving out of home dir + $server .= ':80/~rkumar'; } else if (strpos($_SERVER["REQUEST_URI"], "/~testflip") === 0) { // Serving out of home dir $server .= ':80/~testflip'; } @@ -48,28 +57,38 @@ if ("" == $server) { BRFatal("No server specified!"); } -if (!preg_match("|^/[0-3]/items/{$id}$|", $itemPath)) { +if (!preg_match("|^/\d+/items/{$id}$|", $itemPath)) { BRFatal("Bad id!"); } // XXX check here that subitem is okay -$imageFormat = 'unknown'; -$zipFile = "${subItemPath}_jp2.zip"; +$filesDataFile = "$itemPath/${id}_files.xml"; -if (file_exists($zipFile)) { - $imageFormat = 'jp2'; +if (file_exists($filesDataFile)) { + $filesData = simplexml_load_file("$itemPath/${id}_files.xml"); } else { - $zipFile = "${subItemPath}_tif.zip"; - if (file_exists($zipFile)) { - $imageFormat = 'tif'; - } -} // $$$ check here for tar image stack + BRfatal("File metadata not found!"); +} + +$imageStackInfo = findImageStack($subPrefix, $filesData); +if ($imageStackInfo['imageFormat'] == 'unknown') { + BRfatal('Couldn\'t find image stack'); +} + +$imageFormat = $imageStackInfo['imageFormat']; +$archiveFormat = $imageStackInfo['archiveFormat']; +$imageStackFile = $itemPath . "/" . $imageStackInfo['imageStackFile']; if ("unknown" == $imageFormat) { BRfatal("Unknown image format"); } +if ("unknown" == $archiveFormat) { + BRfatal("Unknown archive format"); +} + + $scanDataFile = "${subItemPath}_scandata.xml"; $scanDataZip = "$itemPath/scandata.zip"; if (file_exists($scanDataFile)) { @@ -214,6 +233,16 @@ br.getPageNum = function(index) { } } +// Single images in the Internet Archive scandata.xml metadata are (somewhat incorrectly) +// given a "leaf" number. Some of these images from the scanning process should not +// be displayed in the BookReader (for example colour calibration cards). Since some +// of the scanned images will not be displayed in the BookReader (those marked with +// addToAccessFormats false in the scandata.xml) leaf numbers and BookReader page +// indexes are generally not the same. This function returns the BookReader page +// index given a scanned leaf number. +// +// This function is used, for example, to map between search results (that use the +// leaf numbers) and the displayed pages in the BookReader. br.leafNumToIndex = function(leafNum) { for (var index = 0; index < this.leafMap.length; index++) { if (this.leafMap[index] == leafNum) { @@ -301,7 +330,81 @@ br.getEmbedCode = function() { return ""; } -br.pageW = [ +// getOpenLibraryRecord +br.getOpenLibraryRecord = function(callback) { + // Try looking up by ocaid first, then by source_record + + var jsonURL = 'http://openlibrary.org/query.json?type=/type/edition&*=&ocaid=' + br.bookId; + $.ajax({ + url: jsonURL, + success: function(data) { + if (data && data.length > 0) { + callback(br, data[0]); + } else { + // try sourceid + jsonURL = 'http://openlibrary.org/query.json?type=/type/edition&*=&source_records=ia:' + br.bookId; + $.ajax({ + url: jsonURL, + success: function(data) { + if (data && data.length > 0) { + callback(br, data[0]); + } + }, + dataType: 'jsonp' + }); + } + }, + dataType: 'jsonp' + }); +} + +// getInfoDiv +br.getInfoDiv = function() { + // $$$ it might make more sense to have a URL on openlibrary.org that returns this info + + var escapedTitle = BookReader.util.escapeHTML(this.bookTitle); + var domainRe = /(\w+\.(com|org))/; + var domain = domainRe.exec(this.bookUrl)[1]; + // XXX use different icon for archive.org + var html = [ + '
', + '', escapedTitle, '', + '
', + '
', + '
', + '

', escapedTitle, '

', + // $$$ lookup on OL + // 'by', + // 'Book Author', + '
', + '

Published ', this.bookPublished, + //, Publisher name', + '

', + //'

Written in Language

', + '

Other Formats

', + '', + '

More information on ' + domain + '.

', + '
', + '', + '
', + // XXX add link to bug tracker + 'Report a problem', + '|', + 'About the Bookreader', + '
' + ]; + + return html.join('\n'); +} + +br.pageW = [ pageData->page as $page) { @@ -314,7 +417,7 @@ br.pageW = [ ?> ]; -br.pageH = [ +br.pageH = [ '; -br.zip = ''; +br.zip = ''; br.subPrefix = ''; br.server = ''; br.bookTitle= 'title);?>'; br.bookPath = ''; br.bookUrl = ''; br.imageFormat = ''; +br.archiveFormat = ''; {'page-progression'}) { - echo "br.pageProgression = '" . $metaData->{"page-progression"} . "';"; + echo "br.pageProgression = '" . $metaData->{"page-progression"} . "';\n"; } else { // Assume page progression is Left To Right - echo "br.pageProgression = 'lr';"; + echo "br.pageProgression = 'lr';\n"; +} + +$useOLAuth = false; +foreach ($metaData->xpath('//collection') as $collection) { + if('browserlending' == $collection) { + $useOLAuth = true; + } +} + +if ($useOLAuth) { + echo "br.olAuth = true;\n"; +} else { + echo "br.olAuth = false;\n"; } # Special cases @@ -415,13 +532,105 @@ if (typeof(brConfig) != 'undefined') { } } // brConfig -br.cleanupMetadata(); -br.init(); +function OLAuth() { + this.authUrl = 'http://openlibrary.org/ia_auth/' + br.bookId; + this.olConnect = false; + return this; +} + +OLAuth.prototype.init = function() { + var htmlStr = '

Authenticating in-browser loan with openlibrary.org!

'; + htmlStr += '

Please wait...

'; + + this.showPopup("#ddd", "#000", htmlStr); + $.ajax({url:this.authUrl, dataType:'jsonp', jsonpCallback:'olAuth.initCallback'}); +} + +OLAuth.prototype.showPopup = function(bgColor, textColor, msg) { + this.popup = document.createElement("div"); + $(this.popup).css({ + position: 'absolute', + top: '20px', + left: ($('#BookReader').attr('clientWidth')-400)/2 + 'px', + width: '400px', + padding: "20px", + border: "3px double #999999", + zIndex: 3, + backgroundColor: bgColor, + color: textColor + }).appendTo('#BookReader'); + + this.popup.innerHTML = msg; + +} + +OLAuth.prototype.initCallback = function(obj) { + if (false == obj.success) { + $(this.popup).css({ + backgroundColor: "#f00", + color: "#fff" + }); + + this.popup.innerHTML = obj.msg; + return; + } + + //user is authenticated + this.setCookie(obj.token); + this.olConnect = true; + this.startPolling(); + br.init(); +} + +OLAuth.prototype.callback = function(obj) { + if (false == obj.success) { + this.showPopup("#f00", "#fff", obj.msg); + clearInterval(this.poller); + this.ttsPoller = null; + } else { + this.olConnect = true; + this.setCookie(obj.token); + } +} + +OLAuth.prototype.setCookie = function(value) { + var date = new Date(); + date.setTime(date.getTime()+(24*60*60*1000)); //one day expiry + var expiry = date.toGMTString(); + var cookie = 'loan-'+br.bookId+'='+value; + cookie += '; expires='+expiry; + cookie += '; path=/; domain=.archive.org;'; + document.cookie = cookie; +} + +OLAuth.prototype.startPolling = function () { + var self = this; + this.poller=setInterval(function(){ + if (!self.olConnect) { + self.showPopup("#f00", "#fff", 'Cound not connect to Open Library for authentication. Please check to see if you are still connected to the Internet, and then reload this web page.'); + clearInterval(self.poller); + self.ttsPoller = null; + } else { + self.olConnect = false; + //be sure to add random param to authUrl to avoid stale cache + $.ajax({url:self.authUrl+'?rand='+Math.random(), dataType:'jsonp', jsonpCallback:'olAuth.callback'}); + } + },300000); +} + +br.cleanupMetadata(); +if (br.olAuth) { + var olAuth = new OLAuth(); + olAuth.init(); +} else { + br.init(); +} , 'archiveFormat' => '} given a sub-item prefix and loaded xml data +function findImageStack($subPrefix, $filesData) { + + // $$$ The order of the image formats determines which will be returned first + $imageFormats = array('JP2' => 'jp2', 'TIFF' => 'tif', 'JPEG' => 'jpg'); + $archiveFormats = array('ZIP' => 'zip', 'Tar' => 'tar'); + $imageGroup = implode('|', array_keys($imageFormats)); + $archiveGroup = implode('|', array_keys($archiveFormats)); + // $$$ Currently only return processed images + $imageStackRegex = "/Single Page (Processed) (${imageGroup}) (${archiveGroup})/"; + + foreach ($filesData->file as $file) { + if (strpos($file['name'], $subPrefix) === 0) { // subprefix matches beginning + if (preg_match($imageStackRegex, $file->format, $matches)) { + + // Make sure we have a regular image stack + $imageFormat = $imageFormats[$matches[2]]; + if (strpos($file['name'], $subPrefix . '_' . $imageFormat) === 0) { + return array('imageFormat' => $imageFormat, + 'archiveFormat' => $archiveFormats[$matches[3]], + 'imageStackFile' => $file['name']); + } + } + } + } + + return array('imageFormat' => 'unknown', 'archiveFormat' => 'unknown', 'imageStackFile' => 'unknown'); + +} + ?> +