-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmedia.php
More file actions
153 lines (108 loc) · 4.11 KB
/
Copy pathmedia.php
File metadata and controls
153 lines (108 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?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;
}
}