Handle directories a little better.

--HG--
extra : rebase_source : 21fb6d3cf27dd6bc9b7949b8ae08fe64f3a7c709
This commit is contained in:
Sébastien Lucas 2013-08-28 14:44:47 +02:00
parent ff30a3b0a3
commit f368795542

View file

@ -26,13 +26,12 @@ class EPub {
* Constructor
*
* @param string $file path to epub file to work on
* @param string $zipClass class to handle zip
* @throws Exception if metadata could not be loaded
*/
public function __construct($file, $zipClass = 'clsTbsZip'){
public function __construct($file){
// open file
$this->file = $file;
$this->zip = new $zipClass();
$this->zip = new clsTbsZip();
if(!$this->zip->Open($this->file)){
throw new Exception('Failed to read epub file');
}
@ -74,7 +73,7 @@ class EPub {
$spine = $this->xpath->query('//opf:spine')->item(0);
$tocid = $spine->getAttribute('toc');
$tochref = $this->xpath->query("//opf:manifest/opf:item[@id='$tocid']")->item(0)->attr('href');
$tocpath = dirname($this->meta).'/'.$tochref;
$tocpath = $this->getFullPath ($tochref);
// read epub toc
if (!$this->zip->FileExists($tocpath)) {
throw new Exception ("Unable to find " . $tocpath);
@ -300,61 +299,6 @@ class EPub {
return $this->getset('dc:description',$description);
}
/**
* Set or get the book's Unique Identifier
*
* @param string Unique identifier
*/
public function Uuid($uuid = false)
{
$nodes = $this->xpath->query('/opf:package');
if ($nodes->length !== 1) {
$error = sprintf('Cannot find ebook identifier');
throw new Exception($error);
}
$identifier = $nodes->item(0)->attr('unique-identifier');
$res = $this->getset('dc:identifier', $uuid, 'id', $identifier);
return $res;
}
/**
* Set or get the book's creation date
*
* @param string Date eg: 2012-05-19T12:54:25Z
*/
public function CreationDate($date = false)
{
$res = $this->getset('dc:date', $date, 'opf:event', 'creation');
return $res;
}
/**
* Set or get the book's modification date
*
* @param string Date eg: 2012-05-19T12:54:25Z
*/
public function ModificationDate($date = false)
{
$res = $this->getset('dc:date', $date, 'opf:event', 'modification');
return $res;
}
/**
* Set or get the book's URI
*
* @param string URI
*/
public function Uri($uri = false)
{
$res = $this->getset('dc:identifier', $uri, 'opf:scheme', 'URI');
return $res;
}
/**
* Set or get the book's ISBN number
*
@ -543,6 +487,53 @@ class EPub {
return $nodes->item(0);
}
public function Combine($a, $b)
{
$isAbsolute = false;
if ($a[0] == "/")
$isAbsolute = true;
if ($b[0] == "/")
throw new InvalidArgumentException("Second path part must not stwar with " . $m_Separator);
$splittedA = split("/", $a);
$splittedB = split("/", $b);
$pathParts = array();
$mergedPath = array_merge($splittedA, $splittedB);
foreach($mergedPath as $item)
{
if ($item == null || $item == "" || $item == ".")
continue;
if ($item == "..")
{
array_pop($pathParts);
continue;
}
array_push($pathParts, $item);
}
$path = implode("/", $pathParts);
if ($isAbsolute)
return("/" . $path);
else
return($path);
}
private function getFullPath ($file, $context = NULL) {
$path = dirname('/'.$this->meta).'/'.$file;
$path = ltrim($path,'\\');
$path = ltrim($path,'/');
if (!empty ($context)) {
$path = $this->combine (dirname ($path), $context);
}
error_log ("FullPath : $path ($file / $context)");
return $path;
}
public function updateForKepub () {
$item = $this->getCoverItem ();
if (!is_null ($item)) {