xref: /plugin/addnewpage/syntax.php (revision 05e1bc90dba3c07e47bea709f5fa361b5e9cb28a)
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	if ($ns == "@PAGE@") return $ID;
90	if ($ns == "@NS@") return getNS($ID);
91        $ns = preg_replace("/^\.(:|$)/", dirname(str_replace(':', '/', $ID)) . "$1", $ns);
92        $ns = str_replace("/", ":", $ns);
93        $ns = cleanID($ns);
94        return $ns;
95    }
96
97    /**
98     * Create the HTML Select element for namespace selection.
99     *
100     * @global string $ID The page ID
101     * @param string|false $data The destination namespace, or false if none provided.
102     * @return string Select element with appropriate NS selected.
103     */
104    function _makecombo($data) {
105        global $ID;
106
107        $hide = $this->getConf('addpage_hide');
108
109        if (($data != "") && ($hide)) {
110            return '<input type="hidden" name="np_cat" id="np_cat" value="'.$this->_parse_ns($data).'"/>';
111        }
112
113        $ns = explode(':', $ID);
114        array_pop($ns);
115        $ns = implode(':', $ns);
116
117        $r = $this->_getnslist("");
118        $ret = '<select class="edit" id="np_cat" name="np_cat" tabindex="1">';
119
120        $someopt=false;
121
122        if ($this->getConf('addpage_showroot')) {
123            $root_disabled = (auth_quickaclcheck($data.":") < AUTH_CREATE);
124            if ($data=='') {
125                if (!$root_disabled) {
126                    $ret.='<option '.(($ns=='')?'selected ':'').'value="">'.((@$this->getLang('namespaceRoot'))?$this->getLang('namespaceRoot'):'top').'</option>';
127                    $someopt=true;
128                }
129            } else {
130                if (!$root_disabled) {
131                    $ret.='<option '.(($ns==$data)?'selected ':'').'value="'.$data.'">'.$data.'</option>';
132                    $someopt=true;
133                }
134            }
135        }
136
137        foreach ($r as $k => $v) {
138            if ($data != '') {
139                if (strpos(":" . $v, ":" . $data . ":") === false) {
140                    continue;
141                }
142            }
143            if (auth_quickaclcheck($v . ":") < AUTH_CREATE) continue;
144            $vv = explode(':', $v);
145            $vv = str_repeat('&nbsp;&nbsp;', substr_count($v, ':')) . $vv[count($vv) - 1];
146            $ret.='<option '.(($ns == $v) ? 'selected ' : '').'value="'.$v.'">'.$vv.'</option>';
147            $someopt = true;
148        }
149        $ret.='</select>';
150        if (!$someopt) $ret = $this->getLang('nooption');
151
152        return $ret;
153    }
154
155    function _getnslist($tns = '') {
156        require_once(DOKU_INC . 'inc/search.php');
157        global $conf;
158        if ($tns == '') $tns = $conf['datadir'];
159        if (!is_dir($tns)) $tns = str_replace(':', '/', $tns);
160        $data = array();
161        $exclude = $this->getConf('addpage_exclude');
162
163        if ($exclude == "") $exclude = array();
164        else $exclude = @explode(';', strtolower($exclude));
165
166        search($data, $tns, 'search_index', array('ns' => ''));
167
168        $data2 = array();
169        foreach ($data as $k => $v) {
170            if ($v['type'] == 'd') {
171                if (!in_array(strtolower($v['id']), $exclude)) {
172                    array_push($data2, $v['id']);
173                    $r = $this->_getnslist($tns . '/' . $v['id']);
174                    foreach ($r as $vv) {
175                        if (!in_array(strtolower($vv), $exclude)) {
176                            array_push($data2, $v['id'] . ':' . $vv);
177                        }
178                    }
179                }
180            }
181        }
182        return $data2;
183    }
184
185}
186