5 use dokuwiki\Extension\Event
;
6 use dokuwiki\Ui\MediaDiff
;
9 use dokuwiki\Utf8\Sort
;
12 * Manage all builtin AJAX calls
14 * @todo The calls should be refactored out to their own proper classes
20 * Execute the given call
22 * @param string $call name of the ajax call
24 public function __construct($call)
26 $callfn = 'call' . ucfirst($call);
27 if (method_exists($this, $callfn)) {
30 $evt = new Event('AJAX_CALL_UNKNOWN', $call);
31 if ($evt->advise_before()) {
32 echo "AJAX call '" . hsc($call) . "' unknown!\n";
41 * Searches for matching pagenames
43 * @author Andreas Gohr <andi@splitbrain.org>
45 protected function callQsearch()
50 $maxnumbersuggestions = 50;
52 $query = $INPUT->post
->str('q');
53 if (empty($query)) $query = $INPUT->get
->str('q');
54 if (empty($query)) return;
56 $query = urldecode($query);
58 $data = ft_pageLookup($query, true, useHeading('navigation'));
60 if ($data === []) return;
62 echo '<strong>' . $lang['quickhits'] . '</strong>';
65 foreach ($data as $id => $title) {
66 if (useHeading('navigation')) {
71 $name = noNS($id) . ' (' . $ns . ')';
76 echo '<li>' . html_wikilink(':' . $id, $name) . '</li>';
79 if ($counter > $maxnumbersuggestions) {
88 * Support OpenSearch suggestions
90 * @link http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions/1.0
91 * @author Mike Frysinger <vapier@gentoo.org>
93 protected function callSuggestions()
97 $query = cleanID($INPUT->post
->str('q'));
98 if (empty($query)) $query = cleanID($INPUT->get
->str('q'));
99 if (empty($query)) return;
101 $data = ft_pageLookup($query);
102 if ($data === []) return;
103 $data = array_keys($data);
105 // limit results to 15 hits
106 $data = array_slice($data, 0, 15);
107 $data = array_map('trim', $data);
108 $data = array_map('noNS', $data);
109 $data = array_unique($data);
112 /* now construct a json */
114 $query, // the original query
115 $data, // some suggestions
116 [], // no description
120 header('Content-Type: application/x-suggestions+json');
121 echo json_encode($suggestions, JSON_THROW_ON_ERROR
);
125 * Refresh a page lock and save draft
127 * Andreas Gohr <andi@splitbrain.org>
129 protected function callLock()
135 $ID = cleanID($INPUT->post
->str('id'));
136 if (empty($ID)) return;
145 if (!$INFO['writable']) {
146 $response['errors'][] = 'Permission to write this page has been denied.';
147 echo json_encode($response);
151 if (!checklock($ID)) {
153 $response['lock'] = '1';
156 $draft = new Draft($ID, $INFO['client']);
157 if ($draft->saveDraft()) {
158 $response['draft'] = $draft->getDraftMessage();
160 $response['errors'] = array_merge($response['errors'], $draft->getErrors());
162 echo json_encode($response, JSON_THROW_ON_ERROR
);
168 * @author Andreas Gohr <andi@splitbrain.org>
170 protected function callDraftdel()
173 $id = cleanID($INPUT->str('id'));
174 if (empty($id)) return;
176 $client = $INPUT->server
->str('REMOTE_USER');
177 if (!$client) $client = clientIP(true);
179 $draft = new Draft($id, $client);
180 if ($draft->isDraftAvailable() && checkSecurityToken()) {
181 $draft->deleteDraft();
186 * Return subnamespaces for the Mediamanager
188 * @author Andreas Gohr <andi@splitbrain.org>
190 protected function callMedians()
196 $ns = cleanID($INPUT->post
->str('ns'));
197 $dir = utf8_encodeFN(str_replace(':', '/', $ns));
199 $lvl = count(explode(':', $ns));
202 search($data, $conf['mediadir'], 'search_index', ['nofiles' => true], $dir);
203 foreach (array_keys($data) as $item) {
204 $data[$item]['level'] = $lvl +
1;
206 echo html_buildlist($data, 'idx', 'media_nstree_item', 'media_nstree_li');
210 * Return list of files for the Mediamanager
212 * @author Andreas Gohr <andi@splitbrain.org>
214 protected function callMedialist()
219 $NS = cleanID($INPUT->post
->str('ns'));
220 $sort = $INPUT->post
->bool('recent') ?
'date' : 'natural';
221 if ($INPUT->post
->str('do') == 'media') {
224 tpl_mediaContent(true, $sort);
229 * Return the content of the right column
230 * (image details) for the Mediamanager
232 * @author Kate Arzamastseva <pshns@ukr.net>
234 protected function callMediadetails()
236 global $IMG, $JUMPTO, $REV, $fullscreen, $INPUT;
238 require_once(DOKU_INC
. 'lib/exe/mediamanager.php');
241 if ($INPUT->has('image')) $image = cleanID($INPUT->str('image'));
242 if (isset($IMG)) $image = $IMG;
243 if (isset($JUMPTO)) $image = $JUMPTO;
245 if (isset($REV) && !$JUMPTO) $rev = $REV;
248 tpl_mediaFileDetails($image, $rev);
252 * Returns image diff representation for mediamanager
254 * @author Kate Arzamastseva <pshns@ukr.net>
256 protected function callMediadiff()
261 if ($INPUT->has('image')) $image = cleanID($INPUT->str('image'));
262 (new MediaDiff($image))->preference('fromAjax', true)->show();
266 * Manages file uploads
268 * @author Kate Arzamastseva <pshns@ukr.net>
270 protected function callMediaupload()
272 global $NS, $MSG, $INPUT;
275 if (isset($_FILES['qqfile']['tmp_name'])) {
276 $id = $INPUT->post
->str('mediaid', $_FILES['qqfile']['name']);
277 } elseif ($INPUT->get
->has('qqfile')) {
278 $id = $INPUT->get
->str('qqfile');
283 $NS = $INPUT->str('ns');
284 $ns = $NS . ':' . getNS($id);
286 $AUTH = auth_quickaclcheck("$ns:*");
287 if ($AUTH >= AUTH_UPLOAD
) {
288 io_createNamespace("$ns:xxx", 'media');
291 if (isset($_FILES['qqfile']['error']) && $_FILES['qqfile']['error']) unset($_FILES['qqfile']);
294 if (isset($_FILES['qqfile']['tmp_name'])) $res = media_upload($NS, $AUTH, $_FILES['qqfile']);
295 if ($INPUT->get
->has('qqfile')) $res = media_upload_xhr($NS, $AUTH);
300 'link' => media_managerURL(['ns' => $ns, 'image' => $NS . ':' . $id], '&'),
301 'id' => $NS . ':' . $id,
307 foreach ($MSG as $msg) {
308 $error .= $msg['msg'];
311 $result = ['error' => $error, 'ns' => $NS];
314 header('Content-Type: application/json');
315 echo json_encode($result, JSON_THROW_ON_ERROR
);
319 * Return sub index for index view
321 * @author Andreas Gohr <andi@splitbrain.org>
323 protected function callIndex()
329 $ns = cleanID($INPUT->post
->str('idx'));
330 $dir = utf8_encodeFN(str_replace(':', '/', $ns));
332 $lvl = count(explode(':', $ns));
335 search($data, $conf['datadir'], 'search_index', ['ns' => $ns], $dir);
336 foreach (array_keys($data) as $item) {
337 $data[$item]['level'] = $lvl +
1;
340 echo html_buildlist($data, 'idx', [$idx,'formatListItem'], [$idx,'tagListItem']);
344 * List matching namespaces and pages for the link wizard
346 * @author Andreas Gohr <gohr@cosmocode.de>
348 protected function callLinkwiz()
354 $q = ltrim(trim($INPUT->post
->str('q')), ':');
362 $nsd = utf8_encodeFN(str_replace(':', '/', $ns));
365 if ($q !== '' && $ns === '') {
366 // use index to lookup matching pages
367 $pages = ft_pageLookup($id, true);
369 // If 'useheading' option is 'always' or 'content',
370 // search page titles with original query as well.
371 if ($conf['useheading'] == '1' ||
$conf['useheading'] == 'content') {
372 $pages = array_merge($pages, ft_pageLookup($q, true, true));
373 asort($pages, SORT_STRING
);
376 // result contains matches in pages and namespaces
377 // we now extract the matching namespaces to show
381 foreach ($pages as $pid => $title) {
382 if (strpos(getNS($pid), $id) !== false) {
383 // match was in the namespace
384 $dirs[getNS($pid)] = 1; // assoc array avoids dupes
386 // it is a matching page, add it to the result
387 $data[] = ['id' => $pid, 'title' => $title, 'type' => 'f'];
391 foreach (array_keys($dirs) as $dir) {
392 $data[] = ['id' => $dir, 'type' => 'd'];
401 'sneakyacl' => $conf['sneaky_index']
403 if ($id) $opts['filematch'] = '^.*\/' . $id;
404 if ($id) $opts['dirmatch'] = '^.*\/' . $id;
405 search($data, $conf['datadir'], 'search_universal', $opts, $nsd);
411 ['id' => getNS($ns), 'type' => 'u']
416 // fixme sort results in a useful way ?
419 echo $lang['nothingfound'];
423 // output the found data
425 foreach ($data as $item) {
428 if (($item['type'] == 'd' ||
$item['type'] == 'u') && $item['id'] !== '') $item['id'] .= ':';
429 $link = wl($item['id']);
431 echo '<div class="' . (($even > 0) ?
'even' : 'odd') . ' type_' . $item['type'] . '">';
433 if ($item['type'] == 'u') {
434 $name = $lang['upperns'];
436 $name = hsc($item['id']);
439 echo '<a href="' . $link . '" title="' . hsc($item['id']) . '" class="wikilink1">' . $name . '</a>';
441 if (!blank($item['title'])) {
442 echo '<span>' . hsc($item['title']) . '</span>';