This repository has been archived by the owner on Dec 28, 2024. It is now read-only.
forked from splitbrain/dokuwiki-plugin-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.php
218 lines (180 loc) · 6.96 KB
/
action.php
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/**
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Andreas Gohr <[email protected]>
*/
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
require_once(DOKU_PLUGIN.'action.php');
require_once DOKU_PLUGIN.'data/bureaucracy_field.php';
class action_plugin_data extends DokuWiki_Action_Plugin {
/**
* will hold Modes the caching works for
*/
var $supportedModes = array('xhtml', 'i');
/**
* will hold the data helper plugin
*/
var $dthlp = null;
/**
* Constructor. Load helper plugin
*/
function action_plugin_data(){
$this->dthlp =& plugin_load('helper', 'data');
}
/**
* Registers a callback function for a given event
*/
function register(&$controller) {
$controller->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE', $this, '_handle');
$controller->register_hook('HTML_SECEDIT_BUTTON', 'BEFORE', $this, '_editbutton');
$controller->register_hook('HTML_EDIT_FORMSELECTION', 'BEFORE', $this, '_editform');
$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, '_handle_edit_post');
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, '_handle_ajax');
$controller->register_hook('PARSER_CACHE_USE','BEFORE', $this, '_cache_prepare');
}
/**
* Handles the page write event and removes the database info
* when the plugin code is no longer in the source
*/
function _handle(&$event, $param){
$data = $event->data;
if(strpos($data[0][1],'dataentry') !== false) return; // plugin seems still to be there
$sqlite = $this->dthlp->_getDB();
if(!$sqlite) return;
$id = ltrim($data[1].':'.$data[2],':');
// get page id
$res = $sqlite->query('SELECT pid FROM pages WHERE page = ?',$id);
$pid = (int) $sqlite->res2single($res);
if(!$pid) return; // we have no data for this page
$sqlite->query('DELETE FROM data WHERE pid = ?',$pid);
$sqlite->query('DELETE FROM pages WHERE pid = ?',$pid);
}
function _editbutton(&$event, $param) {
if ($event->data['target'] !== 'plugin_data') {
return;
}
$event->data['name'] = $this->getLang('dataentry');
}
function _editform(&$event, $param) {
global $TEXT;
if ($event->data['target'] !== 'plugin_data') {
// Not a data edit
return;
}
$event->stopPropagation();
$event->preventDefault();
unset($event->data['intro_locale']);
$event->data['media_manager'] = false;
echo $this->locale_xhtml('edit_intro' . ($this->getConf('edit_content_only') ? '_contentonly' : ''));
require_once 'renderer_data_edit.php';
$Renderer = new Doku_Renderer_plugin_data_edit();
$Renderer->form = $event->data['form'];
// Loop through the instructions
$instructions = p_get_instructions($TEXT);
foreach ( $instructions as $instruction ) {
// Execute the callback against the Renderer
call_user_func_array(array(&$Renderer, $instruction[0]),$instruction[1]);
}
}
function _handle_edit_post($event) {
if (!isset($_POST['data_edit'])) {
return;
}
global $TEXT;
require_once 'syntax/entry.php';
$TEXT = syntax_plugin_data_entry::editToWiki($_POST['data_edit']);
}
function _handle_ajax($event) {
if (strpos($event->data, 'data_page_') !== 0) {
return;
}
$event->preventDefault();
$type = substr($event->data, 10);
$aliases = $this->dthlp->_aliases();
if (!isset($aliases[$type])) {
echo 'Unknown type';
return;
}
if ($aliases[$type]['type'] !== 'page') {
echo 'AutoCompletion is only supported for page types';
return;
}
if (substr($aliases[$type]['postfix'], -1, 1) === ':') {
// Resolve namespace start page ID
global $conf;
$aliases[$type]['postfix'] .= $conf['start'];
}
$search = $_POST['search'];
$pages = ft_pageLookup($search, false, false);
$regexp = '/^';
if ($aliases[$type]['prefix'] !== '') {
$regexp .= preg_quote($aliases[$type]['prefix'], '/');
}
$regexp .= '([^:]+)';
if ($aliases[$type]['postfix'] !== '') {
$regexp .= preg_quote($aliases[$type]['postfix'], '/');
}
$regexp .= '$/';
$result = array();
foreach ($pages as $page => $title) {
$id = array();
if (!preg_match($regexp, $page, $id)) {
// Does not satisfy the postfix and prefix criteria
continue;
}
$id = $id[1];
if ($search !== '' &&
stripos($id, cleanID($search)) === false &&
stripos($title, $search) === false) {
// Search string is not in id part or title
continue;
}
if ($title === '') {
$title = utf8_ucwords(str_replace('_', ' ', $id));
}
$result[hsc($id)] = hsc($title);
}
$json = new JSON();
echo '(' . $json->encode($result) . ')';
}
/**
* prepare the cache object for default _useCache action
*/
function _cache_prepare(&$event, $param) {
global $ID;
global $conf;
$cache =& $event->data;
// we're only interested in instructions of the current page
// without the ID check we'd get the cache objects for included pages as well
if(!isset($cache->page) && ($cache->page != $ID)) return;
if(!isset($cache->mode) || !in_array($cache->mode, $this->supportedModes)) return;
$sqlite = $this->dthlp->_getDB();
if(!$sqlite) return;
$cache->depends['files'][] = $sqlite->dbfile;
// get additional depends
$depends = p_get_metadata($ID, 'relation haspart');
if(empty($depends)) return;
$key = '';
foreach(array_keys($depends) as $page) {
if(strpos($page,'/') || cleanID($page) != $page) {
continue;
} else {
$file = wikiFN($page);
if(!in_array($cache->depends['files'], array($file)) && @file_exists($file)) {
$cache->depends['files'][] = $file;
$key .= '#' . $page . '|ACL' . auth_quickaclcheck($page);
}
}
}
// empty $key implies no includes, so nothing to do
if(empty($key)) return;
// mark the cache as being modified by the include plugin
$cache->dataplugin = true;
// set new cache key & cache name
// now also dependent on included page ids and their ACL_READ status
$cache->key .= $key;
$cache->cache = getCacheName($cache->key, $cache->ext);
}
}