Add method to get entire text layer. This could be useful for search
authorBrian Wolff <bawolff+wn@gmail.com>
Fri, 6 Dec 2013 19:34:49 +0000 (15:34 -0400)
committerNik Everett <neverett@wikimedia.org>
Fri, 13 Dec 2013 23:02:01 +0000 (18:02 -0500)
Change-Id: I22f5fc47aef3cf362cdf630980deea48fe531d45

includes/media/MediaHandler.php

index 1dc74ce..e9d0039 100644 (file)
@@ -426,12 +426,37 @@ abstract class MediaHandler {
         * Currently overloaded by PDF and DjVu handlers
         * @param File $image
         * @param int $page Page number to get information for
-        * @return bool|string Page text or false when no text found.
+        * @return bool|string Page text or false when no text found or if
+        *   unsupported.
         */
        function getPageText( $image, $page ) {
                return false;
        }
 
+       /**
+        * Get the text of the entire document.
+        * @param File $file
+        * @return bool|string The text of the document or false if unsupported.
+        */
+       public function getEntireText( File $file ) {
+               $numPages = $file->pageCount();
+               if ( !$numPages ) {
+                       // Not a multipage document
+                       return $this->getPageText( $file, 1 );
+               }
+               $document = '';
+               for( $i = 1; $i <= $numPages; $i++ ) {
+                       $curPage = $this->getPageText( $file, $i );
+                       if ( is_string( $curPage ) ) {
+                               $document .= $curPage . "\n";
+                       }
+               }
+               if ( $document !== '' ) {
+                       return $document;
+               }
+               return false;
+       }
+
        /**
         * Get an array structure that looks like this:
         *