/*
*
/*
* Note: Edits to this file must pass through github. To submit a patch to this
* file please contact mang at archive dot org or http://github.com/mangtronix
* Direct changes to this file may get clobbered when the code is synchronized
* from github.
*/
class BookReader
{
// Operators recognized in BookReader download URLs
public static $downloadOperators = array('page');
// Returns true if can display the book in item with a given prefix (typically the item identifier)
public static function canDisplay($item, $prefix, $checkOldScandata = false)
{
// A "book" is an image stack and scandata.
// 1. Old items may have scandata.xml or scandata.zip and itemid_{imageformat}.{zip,tar}
// 2. Newer items may have multiple {arbitraryname}_scandata.xml and {arbitraryname}_{imageformat}.{zip,tar}
$foundScandata = false;
$foundImageStack = false;
$targetScandata = $prefix . "_scandata.xml";
// $$$ TODO add support for jpg and tar stacks
// https://bugs.edge.launchpad.net/gnubook/+bug/323003
// https://bugs.edge.launchpad.net/gnubook/+bug/385397
$imageFormatRegex = '@' . preg_quote($prefix, '@') . '_(jp2|tif|jpg)\.(zip|tar)$@';
$baseLength = strlen($item->metadataGrabber->mainDir . '/');
foreach ($item->getFiles() as $location => $fileInfo) {
$filename = substr($location, $baseLength);
if ($checkOldScandata) {
if ($filename == 'scandata.xml' || $filename == 'scandata.zip') {
$foundScandata = $filename;
}
}
if ($filename == $targetScandata) {
$foundScandata = $filename;
}
if (preg_match($imageFormatRegex, $filename)) {
$foundImageStack = $filename;
}
}
if ($foundScandata && $foundImageStack) {
return true;
}
return false;
}
// Finds the prefix to use for the book given the part of the URL trailing after /stream/
public static function findPrefix($urlPortion)
{
if (!preg_match('#[^/&?]+#', $urlPortion, $matches)) {
// URL portion was empty or started with /, &, or ? -- no item identifier
return false;
}
$prefix = $matches[0]; // item identifier
// $$$ Currently swallows the rest of the URL.
// If we want to support e.g. /stream/itemid/subdir/prefix/page/23 will need to adjust.
if (preg_match('#[^/&?]+/([^&?]+)#', $urlPortion, $matches)) {
// Match is everything after item identifier and slash, up to end or ? or &
// e.g. itemid/{match/these/parts}?foo=bar
$prefix = $matches[1]; // sub prefix --
}
return $prefix;
}
// $$$ would be cleaner to use different templates instead of the uiMode param
//
// @param subprefix Optional prefix to display a book inside an item (e.g. if does not match identifier)
public static function draw($server, $mainDir, $identifier, $subPrefix, $title,
$coverLeaf=null, $titleStart='Internet Archive', $uiMode='full')
{
// Set title to default if not set
if (!$title) {
$title = 'BookReader';
}
$id = $identifier;
// manually update with Launchpad version number at each checkin so that browsers
// do not use old cached version
// see https://bugs.launchpad.net/gnubook/+bug/330748
$version = "imageurls";
if (BookReader::getDevHost($server)) {
// on dev host - add time to force reload
$version .= '_' . time();
}
if ("" == $id) {
echo "No identifier specified!";
die(-1);
}
$metaURL = BookReader::jsMetadataURL($server, $identifier, $mainDir, $subPrefix);
?>