Skip to content

Commit

Permalink
Upgrade for the Aggregator
Browse files Browse the repository at this point in the history
Several bugfixes and feature improvements for the Aggregator. When the
result contains a single PDF this is now shown in the browser. If the
Popupviewer is installed it will use this.
  • Loading branch information
gamma committed Apr 14, 2014
1 parent 2dbde4f commit 6792d0c
Show file tree
Hide file tree
Showing 14 changed files with 215 additions and 64 deletions.
18 changes: 11 additions & 7 deletions action/aggregate.php
Expand Up @@ -25,27 +25,31 @@ function register(&$controller) {

function siteexport_aggregate(&$event)
{
global $ID;
global $ID, $INFO;

//if ( $event->data != 'siteexport_aggregate' ) { return true; }
if ( !isset($_REQUEST['siteexport_aggregate']) ) { return true; }
$event->preventDefault();

$exportBase = cleanID($_REQUEST['baseID']);
$functions=& plugin_load('helper', 'siteexport');
$values = $functions->__getOrderedListOfPagesForID($ID, $exportBase);
$values = $functions->__getOrderedListOfPagesForID(getNs($exportBase), $exportBase);

// Generate a TOC that can be exported
$TOC = "<toc merge mergeheader>\n";
$TOC = "~~NOCACHE~~\n<toc merge mergeheader>\n";
foreach( $values as $value ) {
list($id, $title, $sort) = $value;
$TOC .= " * [[{$title}]]\n";
$TOC .= " * [[{$id}|{$title}]]\n";
}

$TOC .= "</toc>\n";
$TOC .= "</toc>";

$info = null;
print p_render('xhtml', p_get_instructions($TOC),$info);
$html = p_render('xhtml', p_get_instructions($TOC),$info);
$html = html_secedit($html,false);
if($INFO['prependTOC']) $html = tpl_toc(true).$html;
echo $html;

$event->preventDefault();
return false;
}

Expand Down
18 changes: 17 additions & 1 deletion action/ajax.php
Expand Up @@ -65,6 +65,7 @@ function ajax_siteexport_provider(&$event, $args) {
case '__siteexport_getsitelist': $this->ajax_siteexport_getsitelist( $event ); break;
case '__siteexport_addsite': $this->ajax_siteexport_addsite( $event ); break;
case '__siteexport_generateurl': $this->ajax_siteexport_generateurl( $event ); break;
case '__siteexport_aggregate': $this->ajax_siteexport_aggregate( $event ); break;
}
}

Expand Down Expand Up @@ -303,6 +304,20 @@ function ajax_siteexport_getsitelist( &$event ) {
return;
}

function ajax_siteexport_aggregate( &$event ) {

// Quick preparations for one page only
if ( $this->filewriter->hasValidCacheFile($_REQUEST, $data) ) {
$this->functions->debug->message("Had a valid cache file and will use it.", null, 2);
print $this->functions->downloadURL();
} else {
// Then go for it!
$this->functions->debug->message("Will create a new cache thing.", null, 2);
$this->ajax_siteexport_addsite( $event );
}

}

/**
* Add a page to the package (for AJAX calls - Wrapper)
**/
Expand Down Expand Up @@ -504,6 +519,7 @@ function __siteexport_add_site( $ID ) {

// Parse URI PATH and add "html"
$fileName = $this->functions->getSiteName($ID, true);
$this->functions->debug->message("Filename could be:", $fileName, 2);

$this->fileChecked[$url] = $fileName; // 2010-09-03 - One URL to one FileName
$this->functions->settings->depth = str_repeat('../', count(explode('/', $fileName))-1);
Expand All @@ -525,7 +541,7 @@ function __siteexport_add_site( $ID ) {
$fileName = $dirname . '/' . $this->functions->getSiteTitle($ID) . '.' . $extension;
} else if ( !empty($tmpFile[1]) && !strstr($DATA[2], $tmpFile[1]) ) {

$this->functions->debug->message("Will replace old filename '{$fileName}' with {$tmpFile[1]}", null, 1);
$this->functions->debug->message("Will replace old filename '{$fileName}' with {$dirname}/{$tmpFile[1]}", null, 1);
$fileName = $dirname . '/' . $tmpFile[1];
}

Expand Down
8 changes: 6 additions & 2 deletions action/sendfile.php
Expand Up @@ -36,8 +36,9 @@ function siteexport_sendfile(&$event, $args) {
}

$functions = new siteexport_functions();
$functions->settings->pattern = $_REQUEST['siteexport'];
$functions->debug->message("Starting to send a file from siteexporter", null, 2);
$filewriter = new siteexport_zipfilewriter($functions);
$functions->settings->pattern = $_REQUEST['siteexport'];

// Try injecting another name ... can't do, because sendFile sets this right after me and right before sending the actual data.
// header('Content-Disposition: attachment; filename="'. basename($functions->settings->zipFile) .'";');
Expand All @@ -46,9 +47,12 @@ function siteexport_sendfile(&$event, $args) {
$event->data['file'] = $functions->getCacheFileNameForPattern();

$functions->debug->message("fetching cached file from pattern '{$functions->settings->pattern}' with name '{$event->data['file']}'", null, 2);
$functions->debug->message("Event Data Before:", $event->data, 3);

$functions->checkIfCacheFileExistsForFileWithPattern($event->data['file'], $_REQUEST['siteexport']);
$filewriter->getOnlyFileInZip($event->data['file'], $event->data['orig']);

$filewriter->getOnlyFileInZip($event->data);
$functions->debug->message("Event Data After:", $event->data, 3);
}

function siteexport_sendfile_not_found(&$event, $args)
Expand Down
2 changes: 1 addition & 1 deletion helper.php
Expand Up @@ -108,7 +108,7 @@ function __getOrderedListOfPagesForID($ID, $newerThanPage=null)
$newerThanPage = $sortIdentifier;
}

array_push($values, array($site['id'], $functions->getSiteTitle($site['id']), $sortIdentifier));
array_push($values, array(':' . $site['id'], $functions->getSiteTitle($site['id']), $sortIdentifier));
}

if ( $newerThanPage != null ) {
Expand Down
40 changes: 26 additions & 14 deletions inc/filewriter.php
Expand Up @@ -93,7 +93,7 @@ private function __writeFileToZip($FILE, $NAME, $ZIP) {
return false;
}

$zip = new ZipArchive;
$zip = new ZipArchive();
if ( !$zip ) {
$this->functions->debug->runtimeException("Can't create new instance of 'ZipArchive'. Please make sure that you have the ziplib extension for PHP installed.");
return false;
Expand Down Expand Up @@ -125,7 +125,7 @@ private function __writeFileToZip($FILE, $NAME, $ZIP) {
*/
function fileExistsInZip($NAME)
{
$zip = new ZipArchive;
$zip = new ZipArchive();
$code = $zip->open($this->functions->settings->zipFile, ZipArchive::CREATE);
if ($code === TRUE) {
return !($zip->statName($NAME) === FALSE);
Expand All @@ -140,11 +140,15 @@ function fileExistsInZip($NAME)
*/
function hasValidCacheFile($requestData, $depends=array())
{
$pattern = $this->functions->requestParametersToCacheHash($requestData);
return $this->hasValidCacheFileForPattern($pattern, $depends);
}

private function hasValidCacheFileForPattern($pattern, $depends=array())
{
$this->functions->debug->message("HASH-Pattern for CacheFile: ", $pattern, 2);
$this->functions->settings->hasValidCacheFile = false; // reset the cache settings
$HASH = $this->functions->requestParametersToCacheHash($requestData);
$this->functions->debug->message("HASH for CacheFile: ", $HASH, 2);

$cacheFile = $this->functions->getCacheFileNameForPattern($HASH);
$cacheFile = $this->functions->getCacheFileNameForPattern($pattern);

$mtime = @filemtime($cacheFile); // 0 if not exists

Expand Down Expand Up @@ -180,34 +184,42 @@ function hasValidCacheFile($requestData, $depends=array())
return $this->functions->settings->hasValidCacheFile = true;
}

public function getOnlyFileInZip(&$filename = null, &$headerFileName = null) {
if ( is_null($filename) ) $filename = $this->functions->settings->zipFile;
public function getOnlyFileInZip(&$data = null) {

if ( is_null($data['file']) ) $data['file'] = $this->functions->settings->zipFile;

$zip = new ZipArchive();
if ( !$zip->open($filename) ) {
$code = $zip->open($data['file']);
if ( $code !== TRUE ) {
$this->functions->debug->message("Can't open the zip-file.", $data['file'], 2);
return false;
}

if ( $zip->numFiles != 1 ) {
$this->functions->debug->message("More than one ({$zip->numFiles}) file in zip.", $data['file'], 2);
return false;
}

$stat = $zip->statIndex( 0 );
$this->functions->debug->message("Stat.", $stat, 3);
if ( substr($stat['name'], -3) != 'pdf' ) {
$this->functions->debug->message("The file was not a PDF ({$stat['name']}).", $stat['name'], 2);
return false;
}

$data['mime'] = 'application/pdf';
$data['download'] = 0;

// Extract single file.
$folder = dirname($filename);
$folder = dirname($data['file']);

$headerFileName = utf8_basename($stat['name']);
$data['orig'] = utf8_basename($stat['name']);
$zip->extractTo($folder, $stat['name']);
$zip->close();

sleep(1);
$filename .= '.' . cleanID($headerFileName); // Wee need the other file for cache reasons.
@rename($folder.'/'.$headerFileName, $filename);
$data['file'] .= '.' . cleanID($data['orig']); // Wee need the other file for cache reasons.
@rename($folder.'/'.$data['orig'], $data['file']);
return true;
}
}
Expand Down
23 changes: 14 additions & 9 deletions inc/functions.php
Expand Up @@ -28,14 +28,22 @@ public function getPluginName()

public function downloadURL()
{
return ml($this->settings->origZipFile, array('cache' => 'nocache', 'siteexport' => $this->settings->pattern), true, '&');
$params = array('cache' => 'nocache', 'siteexport' => $this->settings->pattern);

if ( $this->debug->debugLevel() < 5 ) {
// If debug, then debug!
$params['debug'] = $this->debug->debugLevel();
}

return ml($this->settings->origZipFile, $params, true, '&');
}

public function checkIfCacheFileExistsForFileWithPattern($file, $pattern)
{
if ( !@file_exists($file) )
{
// If the cache File does not exist, move the newly created one over ...
$this->debug->message("'{$file}' does not exist. Checking original ZipFile", null, 3 );
$newCacheFile = mediaFN($this->getSpecialExportFileName($this->settings->origZipFile, $pattern));

if ( !@file_exists($newCacheFile) )
Expand All @@ -45,6 +53,8 @@ public function checkIfCacheFileExistsForFileWithPattern($file, $pattern)

$status = io_rename($newCacheFile, $file);
$this->debug->message("had to move another original file over. Did it work? " . ($status ? 'Yes, it did.' : 'No, it did not.'), null, 2 );
} else {
$this->debug->message("The file does exist!", $file, 2 );
}
}

Expand Down Expand Up @@ -280,13 +290,13 @@ public function getSpecialExportFileName($FILE, $PATTERN=null) {
}

if ( empty($PATTERN) && empty($this->settings->pattern) ){
$this->debug("Generating an internal md5 pattern. This will go wrong - and won't cache properly.");
$this->debug->message("Generating an internal md5 pattern. This will go wrong - and won't cache properly.", null, 3);
$PATTERN = md5(microtime(false));
}

// Set Pattern Global for other stuff
if ( empty($this->settings->pattern) ) {
$this->settings['pattern'] = $PATTERN;
$this->settings->pattern = $PATTERN;
} else {
$PATTERN = $this->settings->pattern;
}
Expand Down Expand Up @@ -499,11 +509,6 @@ function removeWikiVariables(&$removeArray, $advanced=false, $isString=false) {
}
unset($removeArray['customoptionname']);
unset($removeArray['customoptionvalue']);


if ( !empty( $removeArray['debug'] ) && intval($removeArray['debug']) >= 0 && intval($removeArray['debug']) <= 5) {
$this->debug->setDebugLevel(intval($removeArray['debug']));
}
}

if ( $advanced ) {
Expand Down Expand Up @@ -533,7 +538,7 @@ function removeWikiVariables(&$removeArray, $advanced=false, $isString=false) {
unset($removeArray['startcounter']);
unset($removeArray['pattern']);
unset($removeArray['TOCMapWithoutTranslation']);
unset($removeArray['disableCache']);
// unset($removeArray['disableCache']);
unset($removeArray['debug']);
}

Expand Down
2 changes: 1 addition & 1 deletion inc/httpproxy.php
Expand Up @@ -10,7 +10,7 @@
* professionals can help explain the functionality of a particular
* procedure, but they will not modify these examples to provide added
* functionality or construct procedures to meet your specific needs.
* Copyright i-net software 1998-2010
* Copyright © i-net software 1998-2010
*/

/** ********************************************************************
Expand Down
2 changes: 1 addition & 1 deletion inc/pdfgenerator.php
Expand Up @@ -89,7 +89,7 @@ function arrangeHtml(&$html, $norendertags = '' )
'<div class="wrap_pagebreak"></div>' => '<pagebreak />',
'<sup>' => '<sup class="sup">',
'<sub>' => '<sub class="sub">',
'<code>' => '<code class="code">',
'<code>' => '<code class="code">'
);
$html = str_replace(array_keys($standardReplacer), array_values($standardReplacer), $html);

Expand Down
7 changes: 6 additions & 1 deletion inc/settings.php
Expand Up @@ -34,8 +34,13 @@ class settings_plugin_siteexport_settings extends DokuWiki_Plugin
function settings_plugin_siteexport_settings($functions) {
global $ID;

$functions->debug->setDebugLevel($this->getConf('debugLevel'));
$functions->debug->setDebugFile ($this->getConf('debugFile'));
if ( !empty( $_REQUEST['debug'] ) && intval($_REQUEST['debug']) >= 0 && intval($_REQUEST['debug']) <= 5) {
$functions->debug->setDebugLevel(intval($_REQUEST['debug']));
} else
{
$functions->debug->setDebugLevel($this->getConf('debugLevel'));
}

if ( empty($_REQUEST['pattern']) )
{
Expand Down
2 changes: 1 addition & 1 deletion plugin.info.txt
Expand Up @@ -3,7 +3,7 @@
base siteexport
author i-net software
email tools@inetsoftware.de
date 2013-11-20
date 2014-04-14
name Site Export
desc exports the dokuwiki site in the given format
url http://www.inetsoftware.de/other-products/dokuwiki-plugins/site-export
20 changes: 20 additions & 0 deletions renderer/pdf.php
Expand Up @@ -221,6 +221,26 @@ public function startSectionEdit($start, $type, $title = null) {
return "";
}

/**
* Wrap centered media in a div to center it
*/
function _media ($src, $title=NULL, $align=NULL, $width=NULL,
$height=NULL, $cache=NULL, $render = true) {

$out = '';
if($align == 'center'){
$out .= '<div align="center" style="text-align: center">';
}

$out .= parent::_media ($src, $title, $align, $width, $height, $cache, $render);

if($align == 'center'){
$out .= '</div>';
}

return $out;
}

function internalmedia ($src, $title=NULL, $align=NULL, $width=NULL,
$height=NULL, $cache=NULL, $linking=NULL) {
global $ID;
Expand Down

0 comments on commit 6792d0c

Please sign in to comment.