1<?php
2/**
3 *
4 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
5 * @author     Andreas Gohr <andi@splitbrain.org>
6 */
7// must be run within Dokuwiki
8if(!defined('DOKU_INC')) die();
9require_once(DOKU_PLUGIN.'action.php');
10
11require_once DOKU_PLUGIN.'semanticdata/bureaucracy_field.php';
12
13class action_plugin_semanticdata extends DokuWiki_Action_Plugin {
14
15    /**
16     * will hold the semanticdata helper plugin
17     */
18    var $dthlp = null;
19
20    /**
21     * Constructor. Load helper plugin
22     */
23    function action_plugin_semanticdata(){
24        $this->dthlp =& plugin_load('helper', 'semanticdata');
25    }
26
27    /**
28     * Registers a callback function for a given event
29     */
30    function register(&$controller) {
31        $controller->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE', $this, '_handle');
32        $controller->register_hook('HTML_SECEDIT_BUTTON', 'BEFORE', $this, '_editbutton');
33        $controller->register_hook('HTML_EDIT_FORMSELECTION', 'BEFORE', $this, '_editform');
34        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, '_handle_edit_post');
35        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, '_handle_ajax');
36    }
37
38    /**
39     * Handles the page write event and removes the database info
40     * when the plugin code is no longer in the source
41     */
42    function _handle(&$event, $param){
43        $data = $event->data;
44        if(strpos($data[0][1],'dataentry') !== false) return; // plugin seems still to be there
45
46        $store = $this->dthlp->_getTripleStore();
47        $resultFormat = phpSesame::SPARQL_XML; // The expected return type, will return a phpSesame_SparqlRes object (Optional)
48		$lang = "sparql"; // Can also choose SeRQL (Optional)
49		$infer = true; // Can also choose to explicitly disallow inference. (Optional)
50
51		if(!$store) {
52        	msg('Connection to triple store not found',-1);
53        	return;
54        }
55        $id = ltrim($data[1].':'.$data[2],':');
56
57        $sparql =
58			sprintf('DELETE DATA { <%s%s> ?s ?o }',$this->getConf('base_url'),urlencode($id));
59		$result = $store->update($sparql, $resultFormat, $lang, $infer);
60    }
61
62    function _editbutton(&$event, $param) {
63        if ($event->data['target'] !== 'plugin_semanticdata') {
64            return;
65        }
66
67        $event->data['name'] = $this->getLang('dataentry');
68    }
69
70    function _editform(&$event, $param) {
71        global $TEXT;
72        if ($event->data['target'] !== 'plugin_semanticdata') {
73            // Not a data edit
74            return;
75        }
76
77        $event->stopPropagation();
78        $event->preventDefault();
79        unset($event->data['intro_locale']);
80        $event->data['media_manager'] = false;
81
82        echo $this->locale_xhtml('edit_intro' . ($this->getConf('edit_content_only') ? '_contentonly' : ''));
83
84        require_once 'renderer_semanticdata_edit.php';
85        $Renderer = new Doku_Renderer_plugin_semanticdata_edit();
86        $Renderer->form = $event->data['form'];
87
88        // Loop through the instructions
89        $instructions = p_get_instructions($TEXT);
90        foreach ( $instructions as $instruction ) {
91            // Execute the callback against the Renderer
92            call_user_func_array(array(&$Renderer, $instruction[0]),$instruction[1]);
93        }
94    }
95
96    function _handle_edit_post($event) {
97        if (!isset($_POST['data_edit'])) {
98            return;
99        }
100        global $TEXT;
101
102        require_once 'syntax/entry.php';
103        $TEXT = syntax_plugin_semanticdata_entry::editToWiki($_POST['data_edit']);
104    }
105
106    function _handle_ajax($event) {
107        if (strpos($event->data, 'data_page_') !== 0) {
108            return;
109        }
110        $event->preventDefault();
111
112        $type = substr($event->data, 10);
113        $aliases = $this->dthlp->_aliases();
114        if (!isset($aliases[$type])) {
115            echo 'Unknown type';
116            return;
117        }
118        if ($aliases[$type]['type'] !== 'page') {
119            echo 'AutoCompletion is only supported for page types';
120            return;
121        }
122
123        if (substr($aliases[$type]['postfix'], -1, 1) === ':') {
124            // Resolve namespace start page ID
125            global $conf;
126            $aliases[$type]['postfix'] .= $conf['start'];
127        }
128
129        $search = $_POST['search'];
130        $pages = ft_pageLookup($search, false, false);
131
132        $regexp = '/^';
133        if ($aliases[$type]['prefix'] !== '') {
134            $regexp .= preg_quote($aliases[$type]['prefix'], '/');
135        }
136        $regexp .= '([^:]+)';
137        if ($aliases[$type]['postfix'] !== '') {
138            $regexp .= preg_quote($aliases[$type]['postfix'], '/');
139        }
140        $regexp .= '$/';
141
142        $result = array();
143        foreach ($pages as $page => $title) {
144            $id = array();
145            if (!preg_match($regexp, $page, $id)) {
146                // Does not satisfy the postfix and prefix criteria
147                continue;
148            }
149
150            $id = $id[1];
151
152            if ($search !== '' &&
153                stripos($id, cleanID($search)) === false &&
154                stripos($title, $search) === false) {
155                // Search string is not in id part or title
156                continue;
157            }
158
159            if ($title === '') {
160                $title = utf8_ucwords(str_replace('_', ' ', $id));
161            }
162            $result[hsc($id)] = hsc($title);
163        }
164
165        $json = new JSON();
166        echo '(' . $json->encode($result) . ')';
167    }
168}
169