xref: /plugin/addnewpage/syntax.php (revision e815d9c2dc8cc3e4e264a6a353df37e9cc26a3d8)
1<?php
2
3// must be run within Dokuwiki
4if (!defined('DOKU_INC')) die();
5
6if (!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../../') . '/');
7if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
8require_once(DOKU_PLUGIN . 'syntax.php');
9
10/**
11 * Add-New-Page Plugin: a simple form for adding new pages.
12 *
13 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
14 * @author   iDO <ido@idotech.info>
15 * @author   Sam Wilson <sam@samwilson.id.au>
16 */
17class syntax_plugin_addnewpage extends DokuWiki_Syntax_Plugin {
18
19    /**
20     * Get some information about this plugin.
21     *
22     * @return array The info array.
23     */
24    function getInfo() {
25        return array(
26            'author' => 'iDo',
27            'email' => 'ido@idotech.info',
28            'date' => '2013-02-14',
29            'name' => 'addnewpage',
30            'desc' => 'Adds a "new page form" to any wiki page.',
31            'url' => 'https://wiki.dokuwiki.org/plugin:addnewpage',
32        );
33    }
34
35    function getType() { return 'substition'; }
36
37    function getPType() { return 'block'; }
38
39    function getSort() { return 199; }
40
41    function connectTo($mode) {
42        $this->Lexer->addSpecialPattern('\{\{NEWPAGE[^\}]*\}\}', $mode, 'plugin_addnewpage');
43    }
44
45    function handle($match, $state, $pos, &$handler) {
46        $ns = substr($match, 10, -2);  // strip markup
47        return array($ns); // use an array here
48    }
49
50    /**
51     * Create the new-page form.
52     *
53     * @return boolean
54     */
55    function render($mode, &$renderer, $data) {
56        global $lang;
57        $renderer->info['cache'] = false;
58        $data = $data[0]; // get data back from the array
59
60        if ($mode == 'xhtml') {
61            $ns_select = $this->_makecombo($data);
62            if ($ns_select == $this->getLang('nooption')) {
63                $renderer->doc .= (!$this->getConf('addpage_hideACL')) ? $ns_select : '';
64                return true;
65            }
66
67            $button_val = ((@$this->getLang('okbutton')) ? $this->getLang('okbutton') : 'ok');
68            $form = '<div class="addnewpage">'.DOKU_LF
69                .DOKU_TAB.'<form name="addnewpage" method="post" accept-charset="'.$lang['encoding'].'">'.DOKU_LF
70                .DOKU_TAB.DOKU_TAB.$ns_select.DOKU_LF
71                .DOKU_TAB.DOKU_TAB.'<input class="edit" type="text" name="title" size="20" maxlength="255" tabindex="2" />'.DOKU_LF
72                .DOKU_TAB.DOKU_TAB.'<input class="button" type="submit" value="'.$button_val.'" tabindex="3" />'.DOKU_LF
73                .DOKU_TAB.'</form>'.DOKU_LF
74                .'</div>';
75            $renderer->doc .= $form;
76
77            return true;
78        }
79        return false;
80    }
81
82    /**
83     * Parse namespace request
84     *
85     * @author  Samuele Tognini <samuele@cli.di.unipi.it>
86     */
87    function _parse_ns($ns) {
88        global $ID;
89        $ns = preg_replace("/^\.(:|$)/", dirname(str_replace(':', '/', $ID)) . "$1", $ns);
90        $ns = str_replace("/", ":", $ns);
91        $ns = cleanID($ns);
92        return $ns;
93    }
94
95    /**
96     * Create the HTML Select element for namespace selection.
97     *
98     * @global string $ID The page ID
99     * @param string|false $data The destination namespace, or false if none provided.
100     * @return string Select element with appropriate NS selected.
101     */
102    function _makecombo($data) {
103        global $ID;
104
105        $hide = $this->getConf('addpage_hide');
106
107        if (($data != "") && ($hide)) {
108            return '<input type="hidden" name="np_cat" id="np_cat" value="'.$this->_parse_ns($data).'"/>';
109        }
110
111        $ns = explode(':', $ID);
112        array_pop($ns);
113        $ns = implode(':', $ns);
114
115        $r = $this->_getnslist("");
116        $ret = '<select class="edit" id="np_cat" name="np_cat" tabindex="1">';
117
118        $someopt=false;
119
120        if ($this->getConf('addpage_showroot')) {
121            $root_disabled = (auth_quickaclcheck($data.":") < AUTH_CREATE);
122            if ($data=='') {
123                if (!$root_disabled) {
124                    $ret.='<option '.(($ns=='')?'selected ':'').'value="">'.((@$this->getLang('namespaceRoot'))?$this->getLang('namespaceRoot'):'top').'</option>';
125                    $someopt=true;
126                }
127            } else {
128                if (!$root_disabled) {
129                    $ret.='<option '.(($ns==$data)?'selected ':'').'value="'.$data.'">'.$data.'</option>';
130                    $someopt=true;
131                }
132            }
133        }
134
135        foreach ($r as $k => $v) {
136            if ($data != '') {
137                if (strpos(":" . $v, ":" . $data . ":") === false) {
138                    continue;
139                }
140            }
141            if (auth_quickaclcheck($v . ":") < AUTH_CREATE) continue;
142            $vv = explode(':', $v);
143            $vv = str_repeat('&nbsp;&nbsp;', substr_count($v, ':')) . $vv[count($vv) - 1];
144            $ret.='<option '.(($ns == $v) ? 'selected ' : '').'value="'.$v.'">'.$vv.'</option>';
145            $someopt = true;
146        }
147        $ret.='</select>';
148        if (!$someopt) $ret = $this->getLang('nooption');
149
150        return $ret;
151    }
152
153    function _getnslist($tns = '') {
154        require_once(DOKU_INC . 'inc/search.php');
155        global $conf;
156        if ($tns == '') $tns = $conf['datadir'];
157        if (!is_dir($tns)) $tns = str_replace(':', '/', $tns);
158        $data = array();
159        $exclude = $this->getConf('addpage_exclude');
160
161        if ($exclude == "") $exclude = array();
162        else $exclude = @explode(';', strtolower($exclude));
163
164        search($data, $tns, 'search_index', array('ns' => ''));
165
166        $data2 = array();
167        foreach ($data as $k => $v) {
168            if ($v['type'] == 'd') {
169                if (!in_array(strtolower($v['id']), $exclude)) {
170                    array_push($data2, $v['id']);
171                    $r = $this->_getnslist($tns . '/' . $v['id']);
172                    foreach ($r as $vv) {
173                        if (!in_array(strtolower($vv), $exclude)) {
174                            array_push($data2, $v['id'] . ':' . $vv);
175                        }
176                    }
177                }
178            }
179        }
180        return $data2;
181    }
182
183}
184