3 * DokuWiki AJAX call handler
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Andreas Gohr <andi@splitbrain.org>
9 //fix for Opera XMLHttpRequests
10 if(!count($_POST) && $HTTP_RAW_POST_DATA){
11 parse_str($HTTP_RAW_POST_DATA, $_POST);
14 if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__
).'/../../');
15 require_once(DOKU_INC
.'inc/init.php');
16 require_once(DOKU_INC
.'inc/common.php');
17 require_once(DOKU_INC
.'inc/pageutils.php');
18 require_once(DOKU_INC
.'inc/auth.php');
20 session_write_close();
22 header('Content-Type: text/html; charset=utf-8');
25 //call the requested function
26 if(isset($_POST['call']))
27 $call = $_POST['call'];
28 else if(isset($_GET['call']))
29 $call = $_GET['call'];
33 $callfn = 'ajax_'.$call;
35 if(function_exists($callfn)){
38 $evt = new Doku_Event('AJAX_CALL_UNKNOWN', $call);
39 if ($evt->advise_before()) {
40 print "AJAX call '".htmlspecialchars($call)."' unknown!\n";
48 * Searches for matching pagenames
50 * @author Andreas Gohr <andi@splitbrain.org>
52 function ajax_qsearch(){
56 $query = cleanID($_POST['q']);
57 if(empty($query)) $query = cleanID($_GET['q']);
58 if(empty($query)) return;
60 require_once(DOKU_INC
.'inc/html.php');
61 require_once(DOKU_INC
.'inc/fulltext.php');
64 $data = ft_pageLookup($query);
66 if(!count($data)) return;
68 print '<strong>'.$lang['quickhits'].'</strong>';
70 foreach($data as $id){
74 $name = shorten(noNS($id), ' ('.$ns.')',30);
78 print html_wikilink(':'.$id,$name);
85 * Support OpenSearch suggestions
87 * @link http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions/1.0
88 * @author Mike Frysinger <vapier@gentoo.org>
90 function ajax_suggestions() {
94 $query = cleanID($_POST['q']);
95 if(empty($query)) $query = cleanID($_GET['q']);
96 if(empty($query)) return;
98 require_once(DOKU_INC
.'inc/html.php');
99 require_once(DOKU_INC
.'inc/fulltext.php');
100 require_once(DOKU_INC
.'inc/JSON.php');
103 $data = ft_pageLookup($query);
104 if(!count($data)) return;
106 // limit results to 15 hits
107 $data = array_slice($data, 0, 15);
108 $data = array_map('trim',$data);
109 $data = array_map('noNS',$data);
110 $data = array_unique($data);
113 /* now construct a json */
114 $suggestions = array(
115 $query, // the original query
116 $data, // some suggestions
117 array(), // no description
122 header('Content-Type: application/x-suggestions+json');
123 print $json->encode($suggestions);
127 * Refresh a page lock and save draft
129 * Andreas Gohr <andi@splitbrain.org>
131 function ajax_lock(){
134 $id = cleanID($_POST['id']);
135 if(empty($id)) return;
142 if($conf['usedraft'] && $_POST['wikitext']){
143 $client = $_SERVER['REMOTE_USER'];
144 if(!$client) $client = clientIP(true);
146 $draft = array('id' => $id,
147 'prefix' => $_POST['prefix'],
148 'text' => $_POST['wikitext'],
149 'suffix' => $_POST['suffix'],
150 'date' => $_POST['date'],
153 $cname = getCacheName($draft['client'].$id,'.draft');
154 if(io_saveFile($cname,serialize($draft))){
155 echo $lang['draftdate'].' '.strftime($conf['dformat']);
164 * @author Andreas Gohr <andi@splitbrain.org>
166 function ajax_draftdel(){
167 $id = cleanID($_POST['id']);
168 if(empty($id)) return;
170 $client = $_SERVER['REMOTE_USER'];
171 if(!$client) $client = clientIP(true);
173 $cname = getCacheName($client.$id,'.draft');
178 * Return subnamespaces for the Mediamanager
180 * @author Andreas Gohr <andi@splitbrain.org>
182 function ajax_medians(){
184 require_once(DOKU_INC
.'inc/search.php');
185 require_once(DOKU_INC
.'inc/media.php');
188 $ns = cleanID($_POST['ns']);
189 $dir = utf8_encodeFN(str_replace(':','/',$ns));
191 $lvl = count(explode(':',$ns));
194 search($data,$conf['mediadir'],'search_index',array('nofiles' => true),$dir);
195 foreach($data as $item){
196 $item['level'] = $lvl+
1;
197 echo media_nstree_li($item);
198 echo media_nstree_item($item);
204 * Return list of files for the Mediamanager
206 * @author Andreas Gohr <andi@splitbrain.org>
208 function ajax_medialist(){
210 require_once(DOKU_INC
.'inc/media.php');
212 media_filelist($_POST['ns']);
216 * Return sub index for index view
218 * @author Andreas Gohr <andi@splitbrain.org>
220 function ajax_index(){
222 require_once(DOKU_INC
.'inc/search.php');
223 require_once(DOKU_INC
.'inc/html.php');
226 $ns = cleanID($_POST['idx']);
227 $dir = utf8_encodeFN(str_replace(':','/',$ns));
229 $lvl = count(explode(':',$ns));
232 search($data,$conf['datadir'],'search_index',array('ns' => $ns),$dir);
233 foreach($data as $item){
234 $item['level'] = $lvl+
1;
235 echo html_li_index($item);
236 echo '<div class="li">';
237 echo html_list_index($item);
244 * List matching namespaces and pages for the link wizard
246 * @author Andreas Gohr <gohr@cosmocode.de>
248 function ajax_linkwiz(){
251 require_once(DOKU_INC
.'inc/html.php');
253 $q = ltrim($_POST['q'],':');
260 $nsd = utf8_encodeFN(str_replace(':','/',$ns));
261 $idd = utf8_encodeFN(str_replace(':','/',$id));
266 // use index to lookup matching pages
267 require_once(DOKU_INC
.'inc/fulltext.php');
268 require_once(DOKU_INC
.'inc/parserutils.php');
270 $pages = ft_pageLookup($id,false);
272 // result contains matches in pages and namespaces
273 // we now extract the matching namespaces to show
276 $count = count($pages);
277 for($i=0; $i<$count; $i++
){
278 if(strpos(noNS($pages[$i]),$id) === false){
279 // match was in the namespace
280 $dirs[getNS($pages[$i])] = 1; // assoc array avoids dupes
282 // it is a matching page, add it to the result
285 'title' => p_get_first_heading($pages[$i],false),
291 foreach($dirs as $dir => $junk){
300 require_once(DOKU_INC
.'inc/search.php');
308 if($id) $opts['filematch'] = '^.*\/'.$id;
309 if($id) $opts['dirmatch'] = '^.*\/'.$id;
310 search($data,$conf['datadir'],'search_universal',$opts,$nsd);
314 array_unshift($data,array(
321 // fixme sort results in a useful way ?
324 echo $lang['nothingfound'];
328 // output the found data
330 foreach($data as $item){
333 if(($item['type'] == 'd' ||
$item['type'] == 'u') && $item['id']) $item['id'] .= ':';
334 $link = wl($item['id']);
336 echo '<div class="'.(($even > 0)?
'even':'odd').' type_'.$item['type'].'">';
339 if($item['type'] == 'u'){
340 $name = $lang['upperns'];
342 $name = htmlspecialchars($item['id']);
345 echo '<a href="'.$link.'" title="'.htmlspecialchars($item['id']).'" class="wikilink1">'.$name.'</a>';
348 echo '<span>'.htmlspecialchars($item['title']).'</span>';
355 //Setup VIM: ex: et ts=2 enc=utf-8 :