Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 153 additions & 0 deletions action/media.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php
/**
* Add or remove documents from DokuWiki index
*
* @author Giuseppe Di Terlizzi <giuseppe.diterlizzi@gmail.com>
* @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;

}

}

17 changes: 11 additions & 6 deletions action/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Script to search in uploaded pdf documents
*
* @author Dominik Eckelmann <eckelmann@cosmocode.de>
* @author Giuseppe Di Terlizzi <giuseppe.diterlizzi@gmail.com>
* @author @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
*/

Expand Down Expand Up @@ -51,8 +52,9 @@ function display(Doku_Event &$event, $param) {

$conf = $this->backupConfig;

echo '<h2>' . hsc($this->getLang('title')) . '</h2>';
echo '<div class="search_result">';
echo '<div class="search_docsearch">';
echo '<h2>' . hsc($this->getLang('title')) . ':</h2>';
echo '<dl class="search_results">';

$num = 0;
foreach($searchResults as $id => $data) {
Expand All @@ -62,8 +64,8 @@ function display(Doku_Event &$event, $param) {
$usages = array();
}

echo '<a href="' . ml($id) . '" title="" class="wikilink1">' . hsc($id) . '</a>: ';
echo '<span class="search_cnt">' . hsc($data['hits']) . ' ' . hsc($lang['hits']) . '</span>';
echo '<dt><a href="' . ml($id) . '" title="" class="wikilink1">' . hsc($id) . '</a></dt>';
echo '<dd class="meta"><span class="hits">' . hsc($data['hits']) . ' ' . hsc($lang['hits']) . '</span>';
if(!empty($usages)) {
echo '<span class="usage">';
echo ', ' . hsc($this->getLang('usage')) . ' ';
Expand All @@ -73,16 +75,19 @@ function display(Doku_Event &$event, $param) {
echo '</span>';
}

echo '</dd>';

if(isset($data['snippet'])) {
echo '<div class="search_snippet">';
echo '<dd class="snippet">';
echo $data['snippet'];
echo '</div>';
echo '</dd>';
}

echo '<br />';
$num++;
}

echo '</dl>';
echo '</div>';
}
}
12 changes: 11 additions & 1 deletion conf/default.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
<?php

/**
* Default settings for docsearch plugin
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
*
* @author Dominik Eckelmann <eckelmann@cosmocode.de>
* @author Giuseppe Di Terlizzi <giuseppe.diterlizzi@gmail.com>
*/

$conf['showSnippets'] = 15;
$conf['showUsage'] = 5;
$conf['showUsage'] = 5;
$conf['autoIndex'] = 0;
12 changes: 11 additions & 1 deletion conf/metadata.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
<?php

/**
* Metadata settings for docsearch plugin
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
*
* @author Dominik Eckelmann <eckelmann@cosmocode.de>
* @author Giuseppe Di Terlizzi <giuseppe.diterlizzi@gmail.com>
*/

$meta['showSnippets'] = array('numeric');
$meta['showUsage'] = array('numeric');
$meta['showUsage'] = array('numeric');
$meta['autoIndex'] = array('onoff');
11 changes: 10 additions & 1 deletion lang/en/settings.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
<?php

/**
* English translation settings for docsearch plugin
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
*
* @author Dominik Eckelmann <eckelmann@cosmocode.de>
* @author Giuseppe Di Terlizzi <giuseppe.diterlizzi@gmail.com>
*/
$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';