Update tests
[bookreader.git] / BookReaderIA / datanode / BookReaderMeta.inc.php
index fcd1803..abc47f3 100644 (file)
@@ -41,17 +41,15 @@ class BookReaderMeta {
     var $metaDefaults = array(
         'pageProgression' => 'lr',
     );
+    
+    // Stash spot for callback data... where are closures when we need them?
+    static $cbData = NULL;
 
     // Builds metadata object (to be encoded as JSON)
-    function buildMetadata($id, $itemPath, $bookId, $server) {
+    function buildMetadata($id, $itemPath, $subPrefix, $server) {
     
         $response = array();
         
-        if (! $bookId) {
-            $bookId = $id;
-        }
-        $subItemPath = $itemPath . '/' . $bookId;
-        
         if ("" == $id) {
             $this->BRFatal("No identifier specified!");
         }
@@ -68,8 +66,6 @@ class BookReaderMeta {
             $this->BRFatal("Bad id!");
         }
         
-        // XXX check here that subitem is okay
-        
         $filesDataFile = "$itemPath/${id}_files.xml";
         
         if (file_exists($filesDataFile)) {
@@ -78,10 +74,14 @@ class BookReaderMeta {
             $this->BRfatal("File metadata not found!");
         }
         
-        $imageStackInfo = $this->findImageStack($bookId, $filesData);
+        $imageStackInfo = $this->findImageStack($subPrefix, $filesData);
         if ($imageStackInfo['imageFormat'] == 'unknown') {
             $this->BRfatal('Couldn\'t find image stack');
         }
+        // Update subPrefix -> may have been autodetected
+        $subPrefix = $imageStackInfo['subPrefix'];
+        $subItemPath = $itemPath . '/' . $subPrefix;
+
         
         $imageFormat = $imageStackInfo['imageFormat'];
         $archiveFormat = $imageStackInfo['archiveFormat'];
@@ -187,7 +187,7 @@ class BookReaderMeta {
         
         // Internet Archive specific
         $response['itemId'] = $id; // $$$ renamed
-        $response['bookId'] = $bookId;  // $$$ renamed
+        $response['subPrefix'] = $subPrefix;  // $$$ renamed
         $response['itemPath'] = $itemPath;
         $response['zip'] = $imageStackFile;
         $response['server'] = $server;
@@ -263,31 +263,86 @@ class BookReaderMeta {
     
     // Returns { 'imageFormat' => , '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
+        
+        // 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})/";
-            
-        foreach ($filesData->file as $file) {        
-            if (strpos($file['name'], $subPrefix) === 0) { // subprefix matches beginning
-                if (preg_match($imageStackRegex, $file->format, $matches)) {
+
+        // 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'] . '';
                 
-                    // 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']);
-                    }
+                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);
         
-        return array('imageFormat' => 'unknown', 'archiveFormat' => 'unknown', 'imageStackFile' => 'unknown');    
+        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 isValidCallback($identifier) {
@@ -314,7 +369,7 @@ class BookReaderMeta {
         // e.g. http://ia311213.us.archive.org/BookReader/BookReaderImages.php?zip=/0/items/coloritsapplicat00andriala/coloritsapplicat00andriala_jp2.zip&file=coloritsapplicat00andriala_jp2/coloritsapplicat00andriala_0009.jp2&scale=8&rotate=0
         
     
-        $filePath = $this->imageFilePath($leafNum, $metadata['bookId'], $metadata['imageFormat']);
+        $filePath = $this->imageFilePath($leafNum, $metadata['subPrefix'], $metadata['imageFormat']);
         $url = 'http://' . $metadata['server'] . '/BookReader/BookReaderImages.php?zip=' . $metadata['zip'] . '&file=' . $filePath;
         
         if ($scale !== null) {
@@ -331,7 +386,7 @@ class BookReaderMeta {
     function previewURL($page, $metadata) {
         $query = array(
             'id' => $metadata['itemId'],
-            'bookId' => $metadata['bookId'],
+            'subPrefix' => $metadata['subPrefix'],
             'itemPath' => $metadata['itemPath'],
             'server' => $metadata['server'],
             'page' => $page,
@@ -340,8 +395,10 @@ class BookReaderMeta {
         return 'http://' . $metadata['server'] . '/BookReader/BookReaderPreview.php?' . http_build_query($query, '', '&');
     }
     
-    function imageFilePath($leafNum, $bookId, $format) {
-        return sprintf("%s_%s/%s_%04d.%s", $bookId, $format, $bookId, intval($leafNum), $format);
+    function imageFilePath($leafNum, $subPrefix, $format) {
+        $pathParts = pathinfo($subPrefix);
+        $almostIdentifier = $pathParts['basename'];
+        return sprintf("%s_%s/%s_%04d.%s", $almostIdentifier, $format, $almostIdentifier, intval($leafNum), $format);
     }
     
     // Parse date from _meta.xml to integer
@@ -356,18 +413,19 @@ class BookReaderMeta {
     function processRequest($requestEnv) {
         $id = $requestEnv['itemId']; // $$$ renamed
         $itemPath = $requestEnv['itemPath'];
-        $bookId = $requestEnv['bookId']; // $$$ renamed
+        $subPrefix = $requestEnv['subPrefix']; // $$$ renamed
         $server = $requestEnv['server'];
         
         // 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"], "/~testflip") === 0) { // Serving out of home dir
-            $server .= ':80/~testflip';
+        $devHosts = array('testflip', 'rkumar', 'mang');
+        foreach ($devHosts as $host) {
+            if (strpos($_SERVER["REQUEST_URI"], '/~' . $host) === 0) { // Serving out of home dir
+                $server .= ':80/' . $host;
+            }
         }
         
-        $this->emitResponse( $this->buildMetadata($id, $itemPath, $bookId, $server) );
+        $this->emitResponse( $this->buildMetadata($id, $itemPath, $subPrefix, $server) );
     }
     
     function checkPrivs($filename) {