-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor html and text export. Add TEXTNS and HTMLNS syntax
Fixes #135
- Loading branch information
Showing
5 changed files
with
280 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ | |
* @author Gerrit Uitslag <[email protected]> | ||
*/ | ||
|
||
use dokuwiki\Extension\Event; | ||
|
||
/** | ||
* Take care of exporting only pages in selection, and not the bookmanager page itself | ||
*/ | ||
|
@@ -15,20 +17,162 @@ class action_plugin_bookcreator_export extends DokuWiki_Action_Plugin { | |
* @param Doku_Event_Handler $controller | ||
*/ | ||
public function register(Doku_Event_Handler $controller) { | ||
$controller->register_hook('ACTION_EXPORT_POSTPROCESS', 'BEFORE', $this, '_exportOnlySelectedpages'); | ||
$controller->register_hook('ACTION_EXPORT_POSTPROCESS', 'BEFORE', $this, 'replacePageExportBySelection'); | ||
$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'renameDoExportNSAction'); | ||
} | ||
|
||
/** | ||
* The selected pages for the onscreen rendering are rendered plus the remaining part of the bookmanager page | ||
* (i.e. the wiki text below the bookmanager). That remaining part has to be removed. | ||
* Handle export request for exporting namespace renamed as normal html/text | ||
* export, the 'book_ns' is used to recognize the namespace. This way we can use the default handling of text | ||
* and html exports | ||
* | ||
* @param Doku_Event $event | ||
* @param Event $event | ||
*/ | ||
public function renameDoExportNSAction(Event $event) { | ||
// our event? | ||
$allowedEvents = ['export_htmlns', 'export_textns']; | ||
if(!in_array($event->data, $allowedEvents)) return; | ||
|
||
$event->data = substr($event->data, 0, -2); // remove ns from name | ||
} | ||
|
||
/** | ||
* Handle export request for exporting the selection as html or text | ||
* | ||
* @param Event $event | ||
*/ | ||
public function replacePageExportBySelection(Event $event) { | ||
if(!in_array($event->data['mode'], ['text', 'xhtml'])) { | ||
//skip other export modes | ||
return; | ||
} | ||
|
||
global $ID; | ||
global $INPUT; | ||
try{ | ||
if($INPUT->has('selection')) { | ||
//export current list from the bookmanager | ||
$list = json_decode($INPUT->str('selection', '', true), true); | ||
if(!is_array($list) || empty($list)) { | ||
throw new Exception($this->getLang('empty')); | ||
} | ||
} elseif($INPUT->has('savedselection')) { | ||
//export a saved selection of the Bookcreator Plugin | ||
/** @var action_plugin_bookcreator_handleselection $SelectionHandling */ | ||
$SelectionHandling = plugin_load('action', 'bookcreator_handleselection'); | ||
$savedselection = $SelectionHandling->loadSavedSelection($INPUT->str('savedselection')); | ||
$list = $savedselection['selection']; | ||
} elseif($INPUT->has('book_ns')) { | ||
//export triggered with export_textns or export_htmlns | ||
$list = $this->collectPagesOfNS(); | ||
} else { | ||
//export is not from bookcreator | ||
return; | ||
} | ||
|
||
//remove default export version of current page | ||
$event->data['output'] = ''; | ||
|
||
$skippedpages = array(); | ||
foreach($list as $index => $pageid) { | ||
if(auth_quickaclcheck($pageid) < AUTH_READ) { | ||
$skippedpages[] = $pageid; | ||
unset($list[$index]); | ||
} | ||
} | ||
$list = array_filter($list, 'strlen'); //use of strlen() callback prevents removal of pagename '0' | ||
|
||
//if selection contains forbidden pages throw (overridable) warning | ||
if(!$INPUT->bool('book_skipforbiddenpages') && !empty($skippedpages)) { | ||
$msg = hsc(join(', ', $skippedpages)); | ||
throw new Exception(sprintf($this->getLang('forbidden'), $msg)); | ||
} | ||
} catch (Exception $e) { | ||
http_status(400); | ||
print $e->getMessage(); | ||
exit(); | ||
} | ||
|
||
$keep = $ID; | ||
foreach($list as $page) { | ||
$ID = $page; | ||
$event->data['output'] .= p_cached_output(wikiFN($page), $event->data['mode'], $page); | ||
} | ||
$ID = $keep; | ||
} | ||
|
||
|
||
/** | ||
* @return array list of pages from ns after filtering | ||
* @throws Exception | ||
*/ | ||
public function _exportOnlySelectedpages(Doku_Event $event) { | ||
private function collectPagesOfNS(): array | ||
{ | ||
global $INPUT, $conf; | ||
//check input for ns | ||
$pdfnamespace = cleanID($INPUT->str('book_ns')); | ||
if (!@is_dir(dirname(wikiFN($pdfnamespace . ':dummy')))) { | ||
throw new Exception($this->getLang('needns')); | ||
} | ||
|
||
//sort order | ||
$order = $INPUT->str('book_order', 'natural', true); | ||
$sortoptions = array('pagename', 'date', 'natural'); | ||
if (!in_array($order, $sortoptions)) { | ||
$order = 'natural'; | ||
} | ||
|
||
//search depth | ||
$depth = $INPUT->int('book_nsdepth', 0); | ||
if ($depth < 0) { | ||
$depth = 0; | ||
} | ||
|
||
//page search | ||
$result = array(); | ||
$opts = array('depth' => $depth); //recursive all levels | ||
$dir = utf8_encodeFN(str_replace(':', '/', $pdfnamespace)); | ||
search($result, $conf['datadir'], 'search_allpages', $opts, $dir); | ||
|
||
// exclude ids | ||
$excludes = $INPUT->arr('excludes'); | ||
if (!empty($excludes)) { | ||
$result = array_filter($result, function ($item) use ($excludes) { | ||
return !in_array($item['id'], $excludes); | ||
}); | ||
} | ||
// exclude namespaces | ||
$excludesns = $INPUT->arr('excludesns'); | ||
if (!empty($excludesns)) { | ||
$result = array_filter($result, function ($item) use ($excludesns) { | ||
foreach ($excludesns as $ns) { | ||
if (strpos($item['id'], $ns . ':') === 0) return false; | ||
} | ||
return true; | ||
}); | ||
} | ||
|
||
//sorting | ||
if (count($result) > 0) { | ||
if ($order == 'date') { | ||
usort($result, array($this, '_datesort')); | ||
} elseif ($order == 'pagename' || $order == 'natural') { | ||
usort($result, array($this, '_pagenamesort')); | ||
} | ||
} | ||
|
||
$pos = strrpos($event->data['output'],'<!-- END EXPORTED PAGES -->'); | ||
if($pos === false) return; | ||
$list = []; | ||
foreach ($result as $item) { | ||
$list[] = $item['id']; | ||
} | ||
|
||
$event->data['output'] = substr($event->data['output'], 0, $pos); | ||
if ($pdfnamespace !== '') { | ||
if (!in_array($pdfnamespace . ':' . $conf['start'], $list, true)) { | ||
if (file_exists(wikiFN(rtrim($pdfnamespace, ':')))) { | ||
array_unshift($list, rtrim($pdfnamespace, ':')); | ||
} | ||
} | ||
} | ||
return $list; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.