xref: /plugin/addnewpage/syntax.php (revision 447a4886ab918d7d041a43f27a58d40e5969cfa1)
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, Sam Wilson, Michael Braun',
27            'email' => '',
28            'date' => '2013-03-12',
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="get" action="'.DOKU_BASE.DOKU_SCRIPT.'" 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 type="hidden" name="do" value="edit" />'.DOKU_LF
73                .DOKU_TAB.DOKU_TAB.'<input type="hidden" name="id" />'.DOKU_LF
74                .DOKU_TAB.DOKU_TAB.'<input class="button" type="submit" value="'.$button_val.'" tabindex="3" />'.DOKU_LF
75                .DOKU_TAB.'</form>'.DOKU_LF
76                .'</div>';
77            $renderer->doc .= $form;
78
79            return true;
80        }
81        return false;
82    }
83
84    /**
85     * Parse namespace request
86     *
87     * @author Samuele Tognini <samuele@cli.di.unipi.it>
88     * @author Michael Braun <michael-dev@fami-braun.de>
89     */
90    function _parse_ns($ns) {
91        global $ID;
92        if ($ns == "@PAGE@") return $ID;
93        if ($ns == "@NS@") return getNS($ID);
94        $ns = preg_replace("/^\.(:|$)/", dirname(str_replace(':', '/', $ID)) . "$1", $ns);
95        $ns = str_replace("/", ":", $ns);
96        $ns = cleanID($ns);
97        return $ns;
98    }
99
100    /**
101     * Create the HTML Select element for namespace selection.
102     *
103     * @global string $ID The page ID
104     * @param string|false $data The destination namespace, or false if none provided.
105     * @return string Select element with appropriate NS selected.
106     */
107    function _makecombo($data) {
108        global $ID;
109
110        $hide = $this->getConf('addpage_hide');
111
112        if (($data != "") && ($hide)) {
113            return '<input type="hidden" name="np_cat" id="np_cat" value="'.$this->_parse_ns($data).'"/>';
114        }
115
116        $ns = explode(':', $ID);
117        array_pop($ns);
118        $ns = implode(':', $ns);
119
120        $r = $this->_getnslist("");
121        $ret = '<select class="edit" id="np_cat" name="np_cat" tabindex="1">';
122
123        $someopt=false;
124
125        if ($this->getConf('addpage_showroot')) {
126            $root_disabled = (auth_quickaclcheck($data.":") < AUTH_CREATE);
127            if ($data=='') {
128                if (!$root_disabled) {
129                    $ret.='<option '.(($ns=='')?'selected ':'').'value="">'.((@$this->getLang('namespaceRoot'))?$this->getLang('namespaceRoot'):'top').'</option>';
130                    $someopt=true;
131                }
132            } else {
133                if (!$root_disabled) {
134                    $ret.='<option '.(($ns==$data)?'selected ':'').'value="'.$data.'">'.$data.'</option>';
135                    $someopt=true;
136                }
137            }
138        }
139
140        foreach ($r as $k => $v) {
141            if ($data != '') {
142                if (strpos(":" . $v, ":" . $data . ":") === false) {
143                    continue;
144                }
145            }
146            if (auth_quickaclcheck($v . ":") < AUTH_CREATE) continue;
147            $vv = explode(':', $v);
148            $vv = str_repeat('&nbsp;&nbsp;', substr_count($v, ':')) . $vv[count($vv) - 1];
149            $ret.='<option '.(($ns == $v) ? 'selected ' : '').'value="'.$v.'">'.$vv.'</option>';
150            $someopt = true;
151        }
152        $ret.='</select>';
153        if (!$someopt) $ret = $this->getLang('nooption');
154
155        return $ret;
156    }
157
158    function _getnslist($tns = '') {
159        require_once(DOKU_INC . 'inc/search.php');
160        global $conf;
161        if ($tns == '') $tns = $conf['datadir'];
162        if (!is_dir($tns)) $tns = str_replace(':', '/', $tns);
163        $data = array();
164        $exclude = $this->getConf('addpage_exclude');
165
166        if ($exclude == "") $exclude = array();
167        else $exclude = @explode(';', strtolower($exclude));
168
169        search($data, $tns, 'search_index', array('ns' => ''));
170
171        $data2 = array();
172        foreach ($data as $k => $v) {
173            if ($v['type'] == 'd') {
174                if (!in_array(strtolower($v['id']), $exclude)) {
175                    array_push($data2, $v['id']);
176                    $r = $this->_getnslist($tns . '/' . $v['id']);
177                    foreach ($r as $vv) {
178                        if (!in_array(strtolower($vv), $exclude)) {
179                            array_push($data2, $v['id'] . ':' . $vv);
180                        }
181                    }
182                }
183            }
184        }
185        return $data2;
186    }
187
188}
189