_binary From 9ca45aaeb3068b4063feefbb0b417b658736a4fd Mon Sep 17 00:00:00 2001
From: Vijeth <vijeth.patil@gmail.com>
Date: Sat, 6 Aug 2011 17:40:12 -0700
Subject: [PATCH] vijeth epub patch

---
 configs/config.php                        |    4 +-
 lib/processors/epub_opf_filehandler.php   |   40 +++++++
 lib/processors/epub_processor.php         |  158 +++++++++++++++++++++++++++++
 lib/processors/epub_xhtml_filehandler.php |   36 +++++++
 4 files changed, 237 insertions(+), 1 deletions(-)
 create mode 100644 lib/processors/epub_opf_filehandler.php
 create mode 100644 lib/processors/epub_processor.php
 create mode 100644 lib/processors/epub_xhtml_filehandler.php

diff --git a/configs/config.php b/configs/config.php
index b395fe1..6c3c544 100755
--- a/configs/config.php
+++ b/configs/config.php
@@ -236,7 +236,8 @@ $INDEXED_FILE_TYPES =
             "cfml",
             "csv",
             "doc",
-            "gif",
+            "epub", 
+	    "gif",
             "html",
             "htm",
             "jsp",
@@ -278,6 +279,7 @@ $PAGE_PROCESSORS = array(   "text/html" => "HtmlProcessor",
 
                             "application/msword" => "DocProcessor",
                             "application/vnd.ms-powerpoint" => "PptProcessor",
+ 			    "application/epub+zip" => "EpubProcessor",
 
                             "text/rtf" => "RtfProcessor",  
                             "text/plain" => "TextProcessor", 
diff --git a/lib/processors/epub_opf_filehandler.php b/lib/processors/epub_opf_filehandler.php
new file mode 100644
index 0000000..e18c7ab
--- /dev/null
+++ b/lib/processors/epub_opf_filehandler.php
@@ -0,0 +1,40 @@
+<?php
+class XmlElement {
+  var $name;
+  var $attributes;
+  var $content;
+  var $children;
+};
+
+function xml_to_object($xml) {
+  $parser = xml_parser_create();
+  xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
+  xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
+  xml_parse_into_struct($parser, $xml, $tags);
+  xml_parser_free($parser);
+
+  $elements = array();  // the currently filling [child] XmlElement array
+  $stack = array();
+  foreach ($tags as $tag) {
+    $index = count($elements);
+    if ($tag['type'] == "complete" || $tag['type'] == "open") {
+      $elements[$index] = new XmlElement;
+      $elements[$index]->name = $tag['tag'];
+      $elements[$index]->attributes = $tag['attributes'];
+      $elements[$index]->content = $tag['value'];
+      if ($tag['type'] == "open") {  // push
+        $elements[$index]->children = array();
+        $stack[count($stack)] = &$elements;
+        $elements = &$elements[$index]->children;
+      }
+    }
+    if ($tag['type'] == "close") {  // pop
+      $elements = &$stack[count($stack) - 1];
+      unset($stack[count($stack) - 1]);
+    }
+  }
+  return $elements[0];  // the single top-level element
+}
+
+?>
+
diff --git a/lib/processors/epub_processor.php b/lib/processors/epub_processor.php
new file mode 100644
index 0000000..2aaebeb
--- /dev/null
+++ b/lib/processors/epub_processor.php
@@ -0,0 +1,158 @@
+<?php
+/** 
+ *  SeekQuarry/Yioop --
+ *  Open Source Pure PHP Search Engine, Crawler, and Indexer
+ *
+ *  Copyright (C) 2009, 2010, 2011  Chris Pollett chris@pollett.org
+ *
+ *  LICENSE:
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program 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 General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ *  END LICENSE
+ *
+ * @author Vijeth Patil vijeth.patil@gmail.com
+ * @package seek_quarry
+ * @subpackage processor
+ * @license http://www.gnu.org/licenses/ GPL3
+ * @link http://www.seekquarry.com/
+ * @copyright 2009, 2010, 2011
+ * @filesource
+ */
+
+if(!defined('BASE_DIR')) {echo "BAD REQUEST"; exit();}
+
+/**
+ * Load base class, if needed.
+ */
+require_once BASE_DIR."/lib/processors/text_processor.php";
+
+/**
+ * If XML turns out to be RSS ...
+ */
+require_once BASE_DIR."/lib/processors/rss_processor.php";
+
+/**
+ * If XML turns out to be XHTML ...
+ */
+require_once BASE_DIR."/lib/processors/html_processor.php";
+
+/**
+ * If XML turns out to be OPF
+ */
+require_once BASE_DIR."/lib/processors/epub_opf_filehandler.php";
+
+/**
+ * If XML turns out to be XML/XHTML
+ */
+require_once BASE_DIR."/lib/processors/epub_xhtml_filehandler.php";
+
+/**
+ * Load so can parse urls
+ */
+require_once BASE_DIR."/lib/url_parser.php";
+
+ /**
+ * Used to create crawl summary information 
+ * for XML files (those served as application/epub+zip)
+ *
+ * @author Vijeth Patil
+ * @package seek_quarry
+ * @subpackage processor
+ */
+
+class EpubProcessor extends TextProcessor
+{
+    const MAX_DESCRIPTION_LEN = 2000;
+
+    /**
+     *  Used to extract the title, description and links from
+     *  a string consisting of ebook publication data.
+     *
+     *  @param string $page   epub contents
+     *  @param string $url   the url where the page contents came from,
+     *     used to canonicalize relative links
+     *
+     *  @return array  a summary of the contents of the page
+     *
+     */ 
+    function process($page, $url)
+    { 
+        $summary=NULL;
+        $opfpattern = "/.opf$/i";
+        $htmlpattern= "/.html$/i";
+        $xhtmlpattern= "/.xhtml$/i";
+        $tempfilename="epubzipfilename.zip";
+        $epuburl=0;
+        $epubtitle='';
+        $epublanguage='';
+        $epubxhtmlsummary='';
+        file_put_contents($tempfilename,$page);
+        $zip= new ZipArchive;        
+        if ($zip->open($tempfilename)){    
+            for($i = 0; $i < $zip->numFiles; $i++){
+                $filename[$i]= $zip->getNameIndex($i) ;
+	            if(preg_match($opfpattern,$filename[$i])){
+	                $opfdata = $zip->getFromName($filename[$i]);
+                    $opfsummary=xml_to_object($opfdata);
+                    for($m=0;$m<10;$m++){
+	                    for($n=0;$n<10;$n++){
+		                    if( ($opfsummary->children[$m]->children[$n]->name)=="dc:language"){
+		                        //echo "\nLanguage of the document is:". $opfsummary->children[$m]->children[$n]->content ;
+                               global $epublanguage;
+                               $epublanguage = $opfsummary->children[$m]->children[$n]->content ;
+                            }
+		                    if( ($opfsummary->children[$m]->children[$n]->name)=="dc:title"){
+		                        //echo "\nTitle of the document is:". $opfsummary->children[$m]->children[$n]->content ;
+                                global $epubtitle;
+                                $epubtitle= $opfsummary->children[$m]->children[$n]->content ;
+		                    }
+		                    if( ($opfsummary->children[$m]->children[$n]->name)=="dc:creator"){
+		                        //echo "\nCreator of the document is:". $opfsummary->children[$m]->children[$n]->content ;
+                               global $epubauthor;
+                               $epubauthor = $opfsummary->children[$m]->children[$n]->content ;
+		                    }
+		                    if( ($opfsummary->children[$m]->children[$n]->name)=="dc:identifier"){
+		                        //echo "\nUnique Identifier of the document is:". $opfsummary->children[$m]->children[$n]->content ;
+                                global $epubuniqueidentifier;
+                                $epubuniqueidentifier = $opfsummary->children[$m]->children[$n]->content ;
+		                    }
+	                    }
+                    }
+                }
+			    else if((preg_match($htmlpattern,$filename[$i])) || (preg_match($xhtmlpattern,$filename[$i]))){
+                  // echo $filename[$i]."is getting accessed";
+                   $xhtmldata = $zip->getFromName($filename[$i]);
+                   $xhtmlcontentsummary =processxhtmlfile($xhtmldata,$filename[$i]);
+                  // echo "Hello I am here";                   
+                  // print_r($xhtmlcontentsummary);
+                   global $epubxhtmlsummary;                  
+                   $epubxhtmlsummary.=$xhtmlcontentsummary;
+                }
+            }
+        }
+     
+        //echo $epubtitle;
+        //echo $epublanguage;
+        //echo "bye";
+        //echo $epubxhtmlsummary;
+        $summary[self::TITLE]=$epubtitle;
+        $summary[self::DESCRIPTION]=$epubxhtmlsummary;      
+        $summary[self::LANG]=$epublanguage;
+        $summary[self::LINKS]=$epuburl;
+        $summary[self::PAGE]=$page;    
+        return $summary;
+    }
+}
+?>
diff --git a/lib/processors/epub_xhtml_filehandler.php b/lib/processors/epub_xhtml_filehandler.php
new file mode 100644
index 0000000..00fd7ce
--- /dev/null
+++ b/lib/processors/epub_xhtml_filehandler.php
@@ -0,0 +1,36 @@
+    <?php
+     $datasummary=NULL;
+    
+    function contents($parser, $data){
+      global $datasummary;
+      $datasummary.=$data;
+	}
+    // Omits the start tags
+    function startTag($parser, $data){
+        //echo "<b>";
+    }
+    // Omits the end tags
+    function endTag($parser, $data){
+        //echo "</b><br />";
+    }
+
+    function processxhtmlfile($filedata) {
+	    global $datasummary;
+        $targetfilename="yioopprocessingxhtmlfile";  
+	    file_put_contents($targetfilename,$filedata); 
+        $xml_parser = xml_parser_create();
+	    xml_set_element_handler($xml_parser, "startTag", "endTag");
+	    xml_set_character_data_handler($xml_parser, "contents");
+	    $fp = fopen($targetfilename, "r");
+	    $data = fread($fp, 400);
+        
+	    if(!(xml_parse($xml_parser, $data, feof($fp)))){
+		die("Error on line " . xml_get_current_line_number($xml_parser));
+	    }
+	    xml_parser_free($xml_parser);	   
+	    fclose($fp);
+        unlink($targetfilename);
+	    return $datasummary;
+     }
+    ?>
+
-- 
1.7.4.1

