Skip to content

Commit

Permalink
Add of cache for i18n and css with a temp folder.
Browse files Browse the repository at this point in the history
Add of about file for epub with a Mediawiki:Wsexport about file on each wiki.
Small bug fixed.
  • Loading branch information
Tpt committed Jan 22, 2012
1 parent 5a35132 commit 769ddf5
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 35 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -6,7 +6,8 @@ Wikisource export is a tool for exporting Wikisource page in many format like ep
Installation
============

You have only to download the files and use it with PHP 5.3 or more and for ODT export with XLS.
You have to download the files and use it with PHP 5.3 or more and for ODT export with XLS.
You have to create a temp folder in the root of directories.

Composition
===========
Expand Down
5 changes: 5 additions & 0 deletions book/Book.php
Expand Up @@ -48,4 +48,9 @@ class Book extends Page {
* @type array
*/
public $pictures = array();

/**
* list of contributors of the book array('PSEUDO' => array('flags' => array(), 'count' => integer))
*/
public $credits = array();
}
16 changes: 8 additions & 8 deletions book/BookProvider.php
Expand Up @@ -91,7 +91,7 @@ public function get($title, $isMetadata = false) {
$this->curl_async->waitForKey($keyCreditImage);
}

$credit_html = $this->mergeCredit();
$book->credits = $this->mergeCredit();

}
$book->pictures = $pictures;
Expand Down Expand Up @@ -271,10 +271,7 @@ protected function mergeCredit() {
}
}
uasort($credit, "cmp_credit");
$html = "<ul>\n";
foreach ($credit as $name => $value)
$html .= "\t<li>" . $name . "</li>\n";
$this->book->credits_html = $html;
return $credit;
}
}

Expand Down Expand Up @@ -390,11 +387,14 @@ protected function deprecatedNodes($oldName, $newName, $style) {
$nodes = $this->xPath->query('//html:' . $oldName); //The getElementsByTagName doesn't find all tags
foreach($nodes as $oldNode) {
$newNode = $this->xPath->document->createElement($newName);
foreach($oldNode->childNodes as $child){
foreach($oldNode->childNodes as $child) {
$newNode->appendChild($child);
}
foreach($oldNode->attributes as $attrName => $attrNode) {
$newNode->setAttribute($attrName, $attrNode);
foreach($oldNode->attributes as $attribute) {
if($attribute->name == 'value')
$newNode->nodeValue = $attribute->value;
else
$newNode->setAttribute($attribute->name, $attribute->value);
}
$newNode->setAttribute('style', $style . ' ' . $newNode->getAttribute('style'));
$oldNode->parentNode->replaceChild($newNode, $oldNode);
Expand Down
61 changes: 61 additions & 0 deletions book/Refresh.php
@@ -0,0 +1,61 @@
<?php
/**
* @author Thomas Pellissier Tanon
* @copyright 2012 Thomas Pellissier Tanon
* @licence http://www.gnu.org/licenses/gpl.html GNU General Public Licence
*/

class Refresh {

protected $lang = '';
protected $api = null;

public function refresh($lang = '') {
$this->api = new Api($lang);
$this->lang = $this->api->lang;

global $wsexportConfig;
if(@mkdir($wsexportConfig['tempPath'].'/'.$this->lang)) {}

$this->getI18n();
$this->getEpubCssWikisource();
$this->getAboutXhtmlWikisource();
}

protected function getI18n() {
global $wsexportConfig;
$ini = parse_ini_file($wsexportConfig['basePath'] . '/book/i18n.ini');
try {
$response = $this->api->get('http://' . $this->lang . '.wikisource.org/w/index.php?title=MediaWiki:Wsexport_i18n.ini&action=raw&ctype=text/plain');
$temp = parse_ini_string($response);
if($ini != false)
$ini = array_merge($ini, $temp);
} catch(Exception $e) {
}
$this->setTempFileContent('i18n', serialize($ini));
}

protected function getEpubCssWikisource() {
try {
$content = $this->api->get('http://' . $this->lang . '.wikisource.org/w/index.php?title=MediaWiki:Epub.css&action=raw&ctype=text/css');
if($content != '')
$this->setTempFileContent('epub.css', $content);
} catch(Exception $e) {
}
}

protected function getAboutXhtmlWikisource() {
try {
$content = $this->api->get('http://' . $this->lang . '.wikisource.org/w/index.php?title=MediaWiki:Wsexport_about&action=render');
if($content != '')
$this->setTempFileContent('about.xhtml', $content);
} catch(Exception $e) {
}
}

protected function setTempFileContent($name, $content) {
global $wsexportConfig;
return file_put_contents($wsexportConfig['tempPath'].'/'.$this->lang.'/'.$name, $content);
}
}

72 changes: 47 additions & 25 deletions book/formats/Epub2Generator.php
Expand Up @@ -42,7 +42,7 @@ public function getMimeType() {
* @todo images, cover, about...
*/
public function create(Book $book) {
$css = $this->getCssWikisource($book->lang);
$css = $this->getTempFile($book->lang, 'epub.css');
$this->i18n = $this->getI18n($book->lang);
setLocale(LC_TIME, $book->lang . '_' . strtoupper($book->lang));
$wsUrl = wikisourceUrl($book->lang, $book->title);
Expand All @@ -58,6 +58,7 @@ public function create(Book $book) {
if($book->cover != '')
$zip->addContentFile('OPS/cover.xhtml', $this->getXhtmlCover($book));
$zip->addContentFile('OPS/title.xhtml', $this->getXhtmlTitle($book));
$zip->addContentFile('OPS/about.xhtml', $this->getXhtmlAbout($book));
$zip->addFile(dirname(__FILE__).'/images/Accueil_scribe.png', 'OPS/Accueil_scribe.png');
$zip->addContentFile('OPS/' . $book->title . '.xhtml', $book->content->saveXML());
if(!empty($book->chapters)) {
Expand Down Expand Up @@ -132,8 +133,8 @@ protected function getOpfContent(Book $book, $wsUrl) {
}
if($this->withCss)
$content.= '<item id="mainCss" href="main.css" media-type="text/css" />';
//$content.= '<item id="about" href="about.xhtml" media-type="application/xhtml+xml" /> //TODO: about...
$content.= '</manifest>
$content.= '<item id="about" href="about.xhtml" media-type="application/xhtml+xml" />
</manifest>
<spine toc="ncx">';
if($book->cover != '')
$content.= '<itemref idref="cover" linear="yes" />';
Expand All @@ -144,8 +145,8 @@ protected function getOpfContent(Book $book, $wsUrl) {
$content.= '<itemref idref="' . $chapter->title . '" linear="yes" />';
}
}
//$content.= '<itemref idref="about" linear="no" />
$content.= '</spine>
$content.= '<itemref idref="about" linear="yes" />
</spine>
<guide>';
if($book->cover != '')
$content.= '<reference type="cover" title="' . $this->i18n['cover'] . '" href="cover.xhtml" />';
Expand All @@ -156,8 +157,8 @@ protected function getOpfContent(Book $book, $wsUrl) {
if(isset($book->chapters[0])) {
$content.= '<reference type="text" title="' . $book->chapters[0]->name . '" href="' . $book->chapters[0]->title . '.xhtml" />';
}
//$content.= '<reference type="copyright-page" title="' . $this->i18n['about'] . '" href="about.xml"/>
$content.= '</guide>
$content.= '<reference type="copyright-page" title="' . $this->i18n['about'] . '" href="about.xhtml"/>
</guide>
</package>';
return $content;
}
Expand Down Expand Up @@ -193,13 +194,13 @@ protected function getNcxToc(Book $book) {
$order++;
}
}
/* $content.= '<navPoint id="about" playOrder="' . $order . '">
$content.= '<navPoint id="about" playOrder="' . $order . '">
<navLabel>
<text>' . $this->i18n['about'] . '</text>
</navLabel>
<content src="about.xhtml"/>
</navPoint> */
$content.= '</navMap>
</navPoint>
</navMap>
</ncx>';
return $content;
}
Expand Down Expand Up @@ -250,6 +251,27 @@ protected function getXhtmlTitle(Book $book) {
return $content;
}

protected function getXhtmlAbout(Book $book) {
$list = '';
foreach($book->credits as $name => $value)
$list .= '<li>' . $name . "</li>\n";
$about = $this->getTempFile($book->lang, 'about.xhtml');
if($about == '')
$about = $list;
else
$about = str_replace('{CONTRIBUTORS}', '<ul>'.$list.'</ul>', $about);
$content = '<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="' . $book->lang . '">
<head>
<title>' . $this->i18n['about'] . '</title>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
</head>
<body>' . $about . '</body>
</html>';
return $content;
}

protected function encodeTitles(Book $book) {
$this->linksList[] = $book->title . '.xhtml';
foreach($book->chapters as $chapter) {
Expand Down Expand Up @@ -355,26 +377,26 @@ protected function setCssLink(DOMDocument $dom) {
$node->appendChild($link);
}

protected function getCssWikisource($lang) {
try {
$api = new Api($lang);
return $api->get('http://' . $lang . '.wikisource.org/w/index.php?title=MediaWiki:Epub.css&action=raw&ctype=text/css');
} catch(Exception $e) {
protected function getTempFile($lang, $name) {
global $wsexportConfig;
$path = $wsexportConfig['tempPath'].'/'.$lang.'/'.$name;
if(file_exists($path))
return file_get_contents($path);
else
return '';
}
}

protected function getI18n($lang) {
$ini = parse_ini_file(dirname(__FILE__) . '/i18n.ini');
try {
$api = new Api($lang);
$response = $api->get('http://' . $lang . '.wikisource.org/w/index.php?title=MediaWiki:Wsexport_i18n.ini&action=raw&ctype=text/plain');
$temp = parse_ini_string($response);
if($ini != false)
$ini = array_merge($ini, $temp);
} catch(Exception $e) {
$content = $this->getTempFile($lang, 'i18n');
if($content == '') {
global $wsexportConfig;
include $wsexportConfig['basePath'].'/book/Refresh.php';
$refresh = new Refresh();
$refresh->refresh();
return unserialize($this->getTempFile($lang, 'i18n'));
} else {
return unserialize($content);
}
return $ini;
}
}

1 change: 1 addition & 0 deletions book/formats/i18n.ini → book/i18n.ini
Expand Up @@ -2,3 +2,4 @@ title_page = "Title page"
cover = "Cover"
exported_from_wikisource_the = "Exported from Wikisource the %d"
about = "About"

3 changes: 3 additions & 0 deletions book/init.php
Expand Up @@ -4,6 +4,9 @@
* @copyright 2011 Thomas Pellissier Tanon
* @licence http://www.gnu.org/licenses/gpl.html GNU General Public Licence
*/
global $wsexportConfig;
$basePath = $wsexportConfig['basePath'];

include($basePath . '/utils/utils.php');
include($basePath . '/utils/HttpException.php');
include($basePath . '/utils/Api.php');
Expand Down
12 changes: 11 additions & 1 deletion http/book.php
@@ -1,8 +1,18 @@
<?php
$basePath = '..';
$wsexportConfig = array(
'basePath' => '..',
'tempPath' => '../temp'
);

include('../book/init.php');

try {
if(isset($_GET['refresh'])) {
include $wsexportConfig['basePath'].'/book/Refresh.php';
$refresh = new Refresh();
$refresh->refresh();
exit();
}
if(!isset($_GET['page']) || $_GET['page'] == '')
include 'help/book.php';
$title = htmlspecialchars(urldecode($_GET['page']));
Expand Down

0 comments on commit 769ddf5

Please sign in to comment.