From 4a16fdd8724cec61e38525e24a655d5c50d5e60f Mon Sep 17 00:00:00 2001 From: Giuseppe Di Terlizzi Date: Wed, 8 May 2019 18:30:57 +0200 Subject: [PATCH] Added "autoindex" option for enable the indexing after upload of file in Media Manager --- action/media.php | 153 +++++++++++++++++++++++++++++++++++++++++++ action/search.php | 17 +++-- conf/default.php | 12 +++- conf/metadata.php | 12 +++- lang/en/settings.php | 11 +++- 5 files changed, 196 insertions(+), 9 deletions(-) create mode 100644 action/media.php diff --git a/action/media.php b/action/media.php new file mode 100644 index 0000000..a7cb9c4 --- /dev/null +++ b/action/media.php @@ -0,0 +1,153 @@ + + * @author @license GPL 2 (http://www.gnu.org/licenses/gpl.html) + */ + +if(!defined('DOKU_INC')) die(); + +class action_plugin_docsearch_media extends DokuWiki_Action_Plugin { + + private $backupConfig; + + public function register(Doku_Event_Handler $controller) { + + if ($this->getConf('autoIndex')) { + $controller->register_hook('MEDIA_UPLOAD_FINISH', 'AFTER', $this, 'handle_media_event', 'upload'); + $controller->register_hook('MEDIA_DELETE_FILE', 'AFTER', $this, 'handle_media_event', 'delete'); + } + + } + + + public function handle_media_event(Doku_Event &$event, $param) { + + global $ACT; + global $conf; + + // load the plugin converter settings. + $converter_conf = DOKU_INC . 'lib/plugins/docsearch/conf/converter.php'; + $conf['docsearch'] = confToHash($converter_conf); + + // no converters == no work ;-) + if(empty($conf['docsearch'])) { + return; + } + + $conf['docsearchext'] = array_keys($conf['docsearch']); + + // build the data pathes + + // the base "data" dir + $base = ''; + + if($conf['savedir'][0] === '.') { + $base = DOKU_INC; + } + + $base .= $conf['savedir'] . '/'; + + // build the important pathes + $input = $conf['mediadir']; + $output = $base . 'docsearch/pages'; + $index = $base . 'docsearch/index'; + $cache = $base . 'docsearch/cache'; + $meta = $base . 'docsearch/meta'; + $locks = $base . 'docsearch/locks'; + + // create output dir + io_mkdir_p($output); + io_mkdir_p($index); + io_mkdir_p($cache); + io_mkdir_p($meta); + io_mkdir_p($locks); + + // backup original DokuWiki config + $this->backupConfig = $conf; + + // change the data folders + $conf['datadir'] = $output; + $conf['indexdir'] = $index; + $conf['cachedir'] = $cache; + $conf['metadir'] = $meta; + $conf['lockdir'] = $locks; + + // dbglog("Data: " . print_r($event->data, true)); + // dbglog("Param: $param"); + + if ($param == 'delete') { + + $page = $event->data['id']; + + $Indexer = idx_get_indexer(); + $result = $Indexer->deletePage($page); + + // remove meta file and converted file + @unlink(metaFN($page,'.indexed')); + @unlink(metaFN($page,'.meta')); + @unlink(wikiFN($page)); + + } + + if ($param == 'upload') { + + $path_parts = pathinfo($event->data[1]); + $extension = $path_parts['extension']; + + if (! $extension) { + return; + } + + // unknown extension -> return + if (!in_array($extension, $conf['docsearchext'])) { + return; + } + + // prepare folder and paths + $file = $event->data[1]; + $id = $event->data[2]; + $out = $output . '/' . str_replace(':', '/', $id) . '.txt'; + + io_mkdir_p(dirname($out)); + + // dbglog("indexing: $id"); + // dbglog("output: $out"); + + // prepare command + $cmd = $conf['docsearch'][$extension]; + $cmd = str_replace('%in%', escapeshellarg($file), $cmd); + $cmd = str_replace('%out%', escapeshellarg($out), $cmd); + + // dbglog("CMD: $cmd"); + + // Run command + $exitCode = 0; + system($cmd, $exitCode); + + if ($exitCode != 0) { + dbglog("Command failed: $cmd"); + } + + // check file encoding for bad utf8 characters - if a bad thing is found convert assuming latin1 as source encoding + $text = io_readFile($out); + + if (!utf8_check($text)) { + $text = utf8_encode($text); + io_saveFile($out, $text); + } + + // add the page to the index + $ID = cleanID($id); + idx_addPage($ID); + + } + + // restore original config + $conf = $this->backupConfig; + + } + +} + diff --git a/action/search.php b/action/search.php index 06ee9f0..626c8b1 100644 --- a/action/search.php +++ b/action/search.php @@ -3,6 +3,7 @@ * Script to search in uploaded pdf documents * * @author Dominik Eckelmann + * @author Giuseppe Di Terlizzi * @author @license GPL 2 (http://www.gnu.org/licenses/gpl.html) */ @@ -51,8 +52,9 @@ function display(Doku_Event &$event, $param) { $conf = $this->backupConfig; - echo '

' . hsc($this->getLang('title')) . '

'; - echo '
'; + echo '
'; + echo '

' . hsc($this->getLang('title')) . ':

'; + echo '
'; $num = 0; foreach($searchResults as $id => $data) { @@ -62,8 +64,8 @@ function display(Doku_Event &$event, $param) { $usages = array(); } - echo '' . hsc($id) . ': '; - echo '' . hsc($data['hits']) . ' ' . hsc($lang['hits']) . ''; + echo '
' . hsc($id) . '
'; + echo '
' . hsc($data['hits']) . ' ' . hsc($lang['hits']) . ''; if(!empty($usages)) { echo ''; echo ', ' . hsc($this->getLang('usage')) . ' '; @@ -73,16 +75,19 @@ function display(Doku_Event &$event, $param) { echo ''; } + echo '
'; + if(isset($data['snippet'])) { - echo '
'; + echo '
'; echo $data['snippet']; - echo '
'; + echo ''; } echo '
'; $num++; } + echo '
'; echo '
'; } } diff --git a/conf/default.php b/conf/default.php index e2ed743..6fb64b8 100644 --- a/conf/default.php +++ b/conf/default.php @@ -1,4 +1,14 @@ + * @author Giuseppe Di Terlizzi + */ + $conf['showSnippets'] = 15; -$conf['showUsage'] = 5; +$conf['showUsage'] = 5; +$conf['autoIndex'] = 0; diff --git a/conf/metadata.php b/conf/metadata.php index 32e1ca1..0266ab2 100644 --- a/conf/metadata.php +++ b/conf/metadata.php @@ -1,4 +1,14 @@ + * @author Giuseppe Di Terlizzi + */ + $meta['showSnippets'] = array('numeric'); -$meta['showUsage'] = array('numeric'); +$meta['showUsage'] = array('numeric'); +$meta['autoIndex'] = array('onoff'); diff --git a/lang/en/settings.php b/lang/en/settings.php index 1ceddfb..38d00e0 100644 --- a/lang/en/settings.php +++ b/lang/en/settings.php @@ -1,4 +1,13 @@ + * @author Giuseppe Di Terlizzi + */ $lang['showSnippets'] = 'Show snippets on first # of results'; -$lang['showUsage'] = 'Show file usage on first # of results'; +$lang['showUsage'] = 'Show file usage on first # of results'; +$lang['autoindex'] = 'Enable auto indexing after upload of file in Media Manager';