1<?php 2/** 3 * 4 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 5 * @author Andreas Gohr <andi@splitbrain.org> 6 */ 7 8/** 9 * Class action_plugin_data 10 */ 11class action_plugin_data extends DokuWiki_Action_Plugin { 12 13 /** 14 * will hold the data helper plugin 15 * @var helper_plugin_data 16 */ 17 var $dthlp = null; 18 19 /** 20 * Constructor. Load helper plugin 21 */ 22 function __construct(){ 23 $this->dthlp = plugin_load('helper', 'data'); 24 } 25 26 /** 27 * Registers a callback function for a given event 28 */ 29 function register(Doku_Event_Handler $controller) { 30 $controller->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE', $this, '_handle'); 31 $controller->register_hook('HTML_SECEDIT_BUTTON', 'BEFORE', $this, '_editbutton'); 32 $controller->register_hook('HTML_EDIT_FORMSELECTION', 'BEFORE', $this, '_editform'); 33 $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, '_handle_edit_post'); 34 $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, '_handle_ajax'); 35 } 36 37 /** 38 * Handles the page write event and removes the database info 39 * when the plugin code is no longer in the source 40 * 41 * @param Doku_Event $event 42 * @param null $param 43 */ 44 function _handle(Doku_Event $event, $param){ 45 $data = $event->data; 46 if(strpos($data[0][1],'dataentry') !== false) return; // plugin seems still to be there 47 48 $sqlite = $this->dthlp->_getDB(); 49 if(!$sqlite) return; 50 $id = ltrim($data[1].':'.$data[2],':'); 51 52 // get page id 53 $res = $sqlite->query('SELECT pid FROM pages WHERE page = ?',$id); 54 $pid = (int) $sqlite->res2single($res); 55 if(!$pid) return; // we have no data for this page 56 57 $sqlite->query('DELETE FROM data WHERE pid = ?',$pid); 58 $sqlite->query('DELETE FROM pages WHERE pid = ?',$pid); 59 } 60 61 /** 62 * @param Doku_Event $event 63 * @param null $param 64 */ 65 function _editbutton($event, $param) { 66 if ($event->data['target'] !== 'plugin_data') { 67 return; 68 } 69 70 $event->data['name'] = $this->getLang('dataentry'); 71 } 72 73 /** 74 * @param Doku_Event $event 75 * @param null $param 76 */ 77 function _editform(Doku_Event $event, $param) { 78 global $TEXT; 79 if ($event->data['target'] !== 'plugin_data') { 80 // Not a data edit 81 return; 82 } 83 84 $event->stopPropagation(); 85 $event->preventDefault(); 86 unset($event->data['intro_locale']); 87 $event->data['media_manager'] = false; 88 89 echo $this->locale_xhtml('edit_intro' . ($this->getConf('edit_content_only') ? '_contentonly' : '')); 90 91 require_once 'renderer_data_edit.php'; 92 $Renderer = new Doku_Renderer_plugin_data_edit(); 93 $Renderer->form = $event->data['form']; 94 95 // Loop through the instructions 96 $instructions = p_get_instructions($TEXT); 97 foreach ( $instructions as $instruction ) { 98 // Execute the callback against the Renderer 99 call_user_func_array(array($Renderer, $instruction[0]),$instruction[1]); 100 } 101 } 102 103 /** 104 * @param Doku_Event $event 105 */ 106 function _handle_edit_post(Doku_Event $event) { 107 if (!isset($_POST['data_edit'])) { 108 return; 109 } 110 global $TEXT; 111 112 require_once 'syntax/entry.php'; 113 $TEXT = syntax_plugin_data_entry::editToWiki($_POST['data_edit']); 114 } 115 116 /** 117 * @param Doku_Event $event 118 */ 119 function _handle_ajax(Doku_Event $event) { 120 if ($event->data !== 'data_page') { 121 return; 122 } 123 124 $event->stopPropagation(); 125 $event->preventDefault(); 126 127 $type = substr($_REQUEST['aliastype'], 10); 128 $aliases = $this->dthlp->_aliases(); 129 130 if (!isset($aliases[$type])) { 131 echo 'Unknown type'; 132 return; 133 } 134 135 if ($aliases[$type]['type'] !== 'page') { 136 echo 'AutoCompletion is only supported for page types'; 137 return; 138 } 139 140 if (substr($aliases[$type]['postfix'], -1, 1) === ':') { 141 // Resolve namespace start page ID 142 global $conf; 143 $aliases[$type]['postfix'] .= $conf['start']; 144 } 145 146 $search = $_REQUEST['search']; 147 148 $c_search = $search; 149 $in_ns = false; 150 if (!$search) { 151 // No search given, so we just want all pages in the prefix 152 $c_search = $aliases[$type]['prefix']; 153 $in_ns = true; 154 } 155 $pages = ft_pageLookup($c_search, $in_ns, false); 156 157 $regexp = '/^'; 158 if ($aliases[$type]['prefix'] !== '') { 159 $regexp .= preg_quote($aliases[$type]['prefix'], '/'); 160 } 161 $regexp .= '([^:]+)'; 162 if ($aliases[$type]['postfix'] !== '') { 163 $regexp .= preg_quote($aliases[$type]['postfix'], '/'); 164 } 165 $regexp .= '$/'; 166 167 $result = array(); 168 foreach ($pages as $page => $title) { 169 $id = array(); 170 if (!preg_match($regexp, $page, $id)) { 171 // Does not satisfy the postfix and prefix criteria 172 continue; 173 } 174 175 $id = $id[1]; 176 177 if ($search !== '' && 178 stripos($id, cleanID($search)) === false && 179 stripos($title, $search) === false) { 180 // Search string is not in id part or title 181 continue; 182 } 183 184 if ($title === '') { 185 $title = utf8_ucwords(str_replace('_', ' ', $id)); 186 } 187 $result[hsc($id)] = hsc($title); 188 } 189 190 $json = new JSON(); 191 header('Content-Type: application/json'); 192 echo $json->encode($result); 193 } 194} 195