function findImageStack($subPrefix, $filesData) { // The order of the image formats determines which will be returned first $imageFormats = array('JP2' => 'jp2', 'TIFF' => 'tif', 'JPEG' => 'jpg'); $imageFormatOrder = array_values($imageFormats); $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})/"; // Strategy: // - Find potential image stacks, regardless of subPrefix // - If not given subPrefix sort based on potential subPrefix and assign based on asciibetical first // - Filter results by subPrefix // - Sort based on image format // - Take best match $imageStacks = array(); foreach ($filesData->file as $file) { if ( preg_match($imageStackRegex, $file->format, $matches) === 1 ) { $imageFormat = $imageFormats[$matches[2]]; $archiveFormat = $archiveFormats[$matches[3]]; $imageStackFile = $file['name'] . ''; if ( preg_match("#(.*)_${imageFormat}\.${archiveFormat}#", $imageStackFile, $matches) === 0) { // stack filename not regular continue; } else { array_push($imageStacks, array( 'imageFormat' => $imageFormat, 'archiveFormat' => $archiveFormat, 'imageStackFile' => $imageStackFile, 'subPrefix' => $matches[1]) ); } } } // print("<pre>"); // print("found subPrefix $subPrefix\n"); // print_r($imageStacks); // die(0); function subPrefixSort($imageStackA, $imageStackB) { return strcmp($imageStackA['subPrefix'], $imageStackB['subPrefix']); } if (! $subPrefix) { usort($imageStacks, 'subPrefixSort'); $subPrefix = $imageStacks[0]['subPrefix']; } self::$cbData = $subPrefix; function subPrefixFilter($imageStack) { return $imageStack['subPrefix'] == BookReaderMeta::$cbData; } $imageStacks = array_filter($imageStacks, 'subPrefixFilter'); function formatSort($imageStackA, $imageStackB) { $formatA = $imageStackA['imageFormat']; $formatB = $imageStackB['imageFormat']; if ($formatA == $formatB) { return 0; } $indexA = array_search($formatA, $imageFormatOrder); $indexB = array_search($formatB, $imageFormatOrder); // We already matched base on format, so both indices should be set if ($indexA == $indexB) { return 0; } return ($indexA < $indexB) ? 1 : -1; } usort($imageStacks, 'formatSort'); // necessary to remap keys if ( count($imageStacks) > 0 ) { return $imageStacks[0]; } else { return array('imageFormat' => 'unknown', 'archiveFormat' => 'unknown', 'imageStackFile' => 'unknown'); } }
function serveLookupRequest($requestEnv) { $brm = new BookReaderMeta(); try { $metadata = $brm->buildMetadata($_REQUEST['id'], $_REQUEST['itemPath'], $_REQUEST['subPrefix'], $_REQUEST['server']); } catch (Exception $e) { $this->BRfatal($e->getMessage()); } $page = $_REQUEST['page']; // Index of image to return $imageIndex = null; // deal with subPrefix if (array_key_exists($_REQUEST, 'subPrefix') && $_REQUEST['subPrefix']) { $parts = explode('/', $_REQUEST['subPrefix']); $bookId = $parts[count($parts) - 1]; } else { $bookId = $_REQUEST['id']; } $pageInfo = $this->parsePageRequest($page, $bookId); $basePage = $pageInfo['type']; $leaf = null; $region = null; switch ($basePage) { case 'title': if (!array_key_exists('titleIndex', $metadata)) { $this->BRfatal("No title page asserted in book"); } $imageIndex = $metadata['titleIndex']; break; /* Old 'cover' behaviour where it would show cover 0 if it exists or return 404. */ /* Old 'cover' behaviour where it would show cover 0 if it exists or return 404. */ case 'cover0': if (!array_key_exists('coverIndices', $metadata)) { $this->BRfatal("No cover asserted in book"); } $imageIndex = $metadata['coverIndices'][0]; // $$$ TODO add support for other covers break; case 'preview': case 'cover': // Show our best guess if cover is requested // Preference is: // Cover page if book was published >= 1923 // Title page // Cover page // Page 0 if (array_key_exists('date', $metadata) && array_key_exists('coverIndices', $metadata)) { if ($brm->parseYear($metadata['date']) >= 1923) { $imageIndex = $metadata['coverIndices'][0]; break; } } if (array_key_exists('titleIndex', $metadata)) { $imageIndex = $metadata['titleIndex']; break; } if (array_key_exists('coverIndices', $metadata)) { $imageIndex = $metadata['coverIndices'][0]; break; } // First page $imageIndex = 0; break; case 'n': // Accessible index page $imageIndex = intval($pageInfo['value']); break; case 'page': // Named page $index = array_search($pageInfo['value'], $metadata['pageNums']); if ($index === FALSE) { // Not found $this->BRfatal("Page not found"); break; } $imageIndex = $index; break; case 'leaf': // Leaf explicitly specified $leaf = $pageInfo['value']; break; default: // Shouldn't be possible $this->BRfatal("Unrecognized page type requested"); break; } if (is_null($leaf)) { // Leaf was not explicitly set -- look it up $leaf = $brm->leafForIndex($imageIndex, $metadata['leafNums']); } $requestEnv = array('zip' => $metadata['zip'], 'file' => $brm->imageFilePath($leaf, $metadata['subPrefix'], $metadata['imageFormat']), 'ext' => 'jpg'); // remove non-passthrough keys from pageInfo unset($pageInfo['type']); unset($pageInfo['value']); // add pageinfo to request $requestEnv = array_merge($pageInfo, $requestEnv); // Return image data - will check privs $this->serveRequest($requestEnv); }
<?php /* Builds metadata about a book on the Internet Archive in json(p) format so that the book can be accessed by other software including the Internet Archive BookReader. Michael Ang <http://github.com/mangtronix> Copyright (c) 2008-2010 Internet Archive. Software license AGPL version 3. This file is part of BookReader. BookReader is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. BookReader is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with BookReader. If not, see <http://www.gnu.org/licenses/>. */ require_once 'BookReaderMeta.inc.php'; $brm = new BookReaderMeta(); $brm->processRequest($_REQUEST);