xref: /plugin/addnewpage/syntax.php (revision 5ad1ba2306c9b9680ba6a1af8f483fe88b305f2d)
1*5ad1ba23SRobert McLeod<?php
2*5ad1ba23SRobert McLeodif(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
3*5ad1ba23SRobert McLeodif(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
4*5ad1ba23SRobert McLeodrequire_once(DOKU_PLUGIN.'syntax.php');
5*5ad1ba23SRobert McLeod
6*5ad1ba23SRobert McLeodclass syntax_plugin_addnewpage extends DokuWiki_Syntax_Plugin {
7*5ad1ba23SRobert McLeod    function getInfo(){
8*5ad1ba23SRobert McLeod        return array(
9*5ad1ba23SRobert McLeod            'author' => 'iDo',
10*5ad1ba23SRobert McLeod            'email'  => 'ido@idotech.info',
11*5ad1ba23SRobert McLeod            'date'   => '20/12/2006',
12*5ad1ba23SRobert McLeod            'name'   => 'addnewpage',
13*5ad1ba23SRobert McLeod            'desc'   => 'This add a "new page form" in your page. \\ Syntax : {{NEWPAGE[>namespace]}}  where [>namespace] is optional.',
14*5ad1ba23SRobert McLeod            'url'    => 'http://wiki.splitbrain.org/plugin:addnewpage',
15*5ad1ba23SRobert McLeod        );
16*5ad1ba23SRobert McLeod    }
17*5ad1ba23SRobert McLeod
18*5ad1ba23SRobert McLeod
19*5ad1ba23SRobert McLeod    function getType(){
20*5ad1ba23SRobert McLeod        return 'substition';
21*5ad1ba23SRobert McLeod    }
22*5ad1ba23SRobert McLeod
23*5ad1ba23SRobert McLeod    function getSort(){
24*5ad1ba23SRobert McLeod        return 199;
25*5ad1ba23SRobert McLeod    }
26*5ad1ba23SRobert McLeod
27*5ad1ba23SRobert McLeod    function connectTo($mode) {
28*5ad1ba23SRobert McLeod      $this->Lexer->addSpecialPattern('\{\{NEWPAGE[^\}]*\}\}', $mode, 'plugin_addnewpage');
29*5ad1ba23SRobert McLeod    }
30*5ad1ba23SRobert McLeod
31*5ad1ba23SRobert McLeod    function handle($match, $state, $pos, &$handler){
32*5ad1ba23SRobert McLeod      $ns = substr($match, 10, -2);  // strip markup
33*5ad1ba23SRobert McLeod      return array($ns); // use an array here
34*5ad1ba23SRobert McLeod	}
35*5ad1ba23SRobert McLeod
36*5ad1ba23SRobert McLeod
37*5ad1ba23SRobert McLeod	function render($mode, &$renderer, $data) {
38*5ad1ba23SRobert McLeod    	global $lang;
39*5ad1ba23SRobert McLeod		$renderer->info['cache'] = false;
40*5ad1ba23SRobert McLeod		$data = $data[0]; // get data back from the array
41*5ad1ba23SRobert McLeod
42*5ad1ba23SRobert McLeod		if ($mode == 'xhtml') {
43*5ad1ba23SRobert McLeod			$cmb=$this->_makecombo($data);
44*5ad1ba23SRobert McLeod			if ($cmb==$this->getLang('nooption')) {
45*5ad1ba23SRobert McLeod				$renderer->doc .=(!$this->getConf('addpage_hideACL'))?$cmb:'';
46*5ad1ba23SRobert McLeod				return true;
47*5ad1ba23SRobert McLeod			}
48*5ad1ba23SRobert McLeod
49*5ad1ba23SRobert McLeod		    $renderer->doc .= '<div class="addnewpage_form" id="addnewpage_form" align="left">';
50*5ad1ba23SRobert McLeod		    $renderer->doc .= '<form name="editform" id="editform" method="post" action="" accept-charset="'.$lang['encoding'].'" onsubmit="setName();return true;">';
51*5ad1ba23SRobert McLeod		    $renderer->doc .= $cmb;
52*5ad1ba23SRobert McLeod		    $renderer->doc .= '<input class="edit" type="text" name="title" id="addnewpage_title" size="20" maxlength="255" tabindex="2" />';
53*5ad1ba23SRobert McLeod			$renderer->doc .= '<input type="hidden" name="do" id="do" value="edit" />';
54*5ad1ba23SRobert McLeod		    $renderer->doc .= '<input class="button" type="submit" value="'.((@$this->getLang('okbutton'))?$this->getLang('okbutton'):'ok').'" tabindex="3" />';
55*5ad1ba23SRobert McLeod		    $renderer->doc .= '</form>';
56*5ad1ba23SRobert McLeod		    $renderer->doc .= '</div>';
57*5ad1ba23SRobert McLeod
58*5ad1ba23SRobert McLeod			return true;
59*5ad1ba23SRobert McLeod		}
60*5ad1ba23SRobert McLeod		return false;
61*5ad1ba23SRobert McLeod	}
62*5ad1ba23SRobert McLeod	/**
63*5ad1ba23SRobert McLeod	* Parse namespace request
64*5ad1ba23SRobert McLeod	*
65*5ad1ba23SRobert McLeod	* @author  Samuele Tognini <samuele@cli.di.unipi.it>
66*5ad1ba23SRobert McLeod	*/
67*5ad1ba23SRobert McLeod	function _parse_ns ($ns) {
68*5ad1ba23SRobert McLeod		global $ID;
69*5ad1ba23SRobert McLeod		$ns=preg_replace("/^\.(:|$)/",dirname(str_replace(':','/',$ID))."$1",$ns);
70*5ad1ba23SRobert McLeod		$ns=str_replace("/",":",$ns);
71*5ad1ba23SRobert McLeod		$ns = cleanID($ns);
72*5ad1ba23SRobert McLeod		return $ns;
73*5ad1ba23SRobert McLeod	}
74*5ad1ba23SRobert McLeod	function _makecombo($data) {
75*5ad1ba23SRobert McLeod		global $ID;
76*5ad1ba23SRobert McLeod
77*5ad1ba23SRobert McLeod		$hide=$this->getConf('addpage_hide');
78*5ad1ba23SRobert McLeod
79*5ad1ba23SRobert McLeod		if (($data != "") && ($hide))
80*5ad1ba23SRobert McLeod			return '<input type="hidden" name="np_cat" id="np_cat" value="'. $this->_parse_ns($data) .'"/>';
81*5ad1ba23SRobert McLeod
82*5ad1ba23SRobert McLeod		$ns=explode(':',$ID);
83*5ad1ba23SRobert McLeod		array_pop($ns);
84*5ad1ba23SRobert McLeod		$ns=implode(':',$ns);
85*5ad1ba23SRobert McLeod
86*5ad1ba23SRobert McLeod		$r=$this->_getnslist("");
87*5ad1ba23SRobert McLeod
88*5ad1ba23SRobert McLeod
89*5ad1ba23SRobert McLeod
90*5ad1ba23SRobert McLeod		$ret='<select class="edit" id="np_cat" name="np_cat"  tabindex="1">';
91*5ad1ba23SRobert McLeod
92*5ad1ba23SRobert McLeod		$someopt=false;
93*5ad1ba23SRobert McLeod
94*5ad1ba23SRobert McLeod		if ($this->getConf('addpage_showroot')) {
95*5ad1ba23SRobert McLeod
96*5ad1ba23SRobert McLeod			$root_disabled=(auth_quickaclcheck($data.":") < AUTH_CREATE) ?true:false;
97*5ad1ba23SRobert McLeod
98*5ad1ba23SRobert McLeod
99*5ad1ba23SRobert McLeod			if ($data=='') {
100*5ad1ba23SRobert McLeod				if (!$root_disabled) {
101*5ad1ba23SRobert McLeod					$ret.='<option '.(($ns=='')?'selected="true"':'').' value="">'.((@$this->getLang('namespaceRoot'))?$this->getLang('namespaceRoot'):'top').'</option>';
102*5ad1ba23SRobert McLeod					$someopt=true;
103*5ad1ba23SRobert McLeod					}
104*5ad1ba23SRobert McLeod			} else {
105*5ad1ba23SRobert McLeod				if (!$root_disabled) {
106*5ad1ba23SRobert McLeod					$ret.='<option '.(($ns==$data)?'selected="true"':'').' value="'.$data.'">'.$data.'</option>';
107*5ad1ba23SRobert McLeod					$someopt=true;
108*5ad1ba23SRobert McLeod				}
109*5ad1ba23SRobert McLeod
110*5ad1ba23SRobert McLeod			}
111*5ad1ba23SRobert McLeod		}
112*5ad1ba23SRobert McLeod		foreach ($r as $k => $v) {
113*5ad1ba23SRobert McLeod			if ($data != '')
114*5ad1ba23SRobert McLeod				if (strpos(":".$v,":".$data.":")===false) continue;
115*5ad1ba23SRobert McLeod
116*5ad1ba23SRobert McLeod			if(auth_quickaclcheck($v.":") < AUTH_CREATE)continue;
117*5ad1ba23SRobert McLeod			$vv=explode(':',$v);
118*5ad1ba23SRobert McLeod			$vv=str_repeat('&nbsp;&nbsp;',substr_count($v, ':')).$vv[count($vv)-1];
119*5ad1ba23SRobert McLeod			$ret.='<option '.(($ns==$v)?'selected="true"':'').' value="'.$v.'">'.$vv.'</option>';
120*5ad1ba23SRobert McLeod			$someopt=true;
121*5ad1ba23SRobert McLeod		}
122*5ad1ba23SRobert McLeod		$ret.='</select>';
123*5ad1ba23SRobert McLeod		if (!$someopt) $ret = $this->getLang('nooption');
124*5ad1ba23SRobert McLeod
125*5ad1ba23SRobert McLeod		return $ret;
126*5ad1ba23SRobert McLeod	}
127*5ad1ba23SRobert McLeod	function _getnslist ($tns='') {
128*5ad1ba23SRobert McLeod		require_once(DOKU_INC.'inc/search.php');
129*5ad1ba23SRobert McLeod		global $conf;
130*5ad1ba23SRobert McLeod
131*5ad1ba23SRobert McLeod		if ($tns=='')
132*5ad1ba23SRobert McLeod			$tns = $conf['datadir'];
133*5ad1ba23SRobert McLeod
134*5ad1ba23SRobert McLeod		if (!is_dir($tns))
135*5ad1ba23SRobert McLeod			$tns  = str_replace(':','/',$tns);
136*5ad1ba23SRobert McLeod
137*5ad1ba23SRobert McLeod		$data = array();
138*5ad1ba23SRobert McLeod
139*5ad1ba23SRobert McLeod		$exclude=$this->getConf('addpage_exclude');
140*5ad1ba23SRobert McLeod
141*5ad1ba23SRobert McLeod		if ($exclude=="")
142*5ad1ba23SRobert McLeod			$exclude=array();
143*5ad1ba23SRobert McLeod		else
144*5ad1ba23SRobert McLeod			$exclude=@explode(';',strtolower($exclude));
145*5ad1ba23SRobert McLeod
146*5ad1ba23SRobert McLeod		search($data,$tns,'search_index',array('ns' => ''));
147*5ad1ba23SRobert McLeod
148*5ad1ba23SRobert McLeod		$data2 = array();
149*5ad1ba23SRobert McLeod		foreach($data as $k => $v) {
150*5ad1ba23SRobert McLeod			if ($v['type']=='d') {
151*5ad1ba23SRobert McLeod				if (!in_array(strtolower($v['id']),$exclude)) {
152*5ad1ba23SRobert McLeod					array_push($data2,$v['id']);
153*5ad1ba23SRobert McLeod					$r=$this->_getnslist($tns.'/'.$v['id']);
154*5ad1ba23SRobert McLeod					foreach ($r as $vv) {
155*5ad1ba23SRobert McLeod						if (!in_array(strtolower($vv),$exclude))
156*5ad1ba23SRobert McLeod							array_push($data2,$v['id'].':'.$vv);
157*5ad1ba23SRobert McLeod					}
158*5ad1ba23SRobert McLeod				}
159*5ad1ba23SRobert McLeod			}
160*5ad1ba23SRobert McLeod		}
161*5ad1ba23SRobert McLeod		return $data2;
162*5ad1ba23SRobert McLeod	}
163*5ad1ba23SRobert McLeod}
164*5ad1ba23SRobert McLeod?>
165