Skip to content

Commit

Permalink
Merge remote-tracking branch 'klapin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
samwilson committed May 16, 2014
2 parents b3bb16b + e6a7406 commit 41f7d8f
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 113 deletions.
4 changes: 2 additions & 2 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ jQuery(document).ready(function() {
// For when the user deletes the text
$submit.attr("disabled", "disabled");
}
});
}).keyup();

// Change the form's page-ID field on submit
$editform = jQuery(".addnewpage form").submit(function(e) {
jQuery(".addnewpage form").submit(function(e) {

// Build the new page ID and save in hidden form field
var ns = jQuery(this).find("[name='np_cat']");
Expand Down
297 changes: 186 additions & 111 deletions syntax.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
<?php

// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();

if (!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../../') . '/');
if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
require_once(DOKU_PLUGIN . 'syntax.php');
if(!defined('DOKU_INC')) die();

/**
* Add-New-Page Plugin: a simple form for adding new pages.
Expand All @@ -17,65 +13,98 @@
class syntax_plugin_addnewpage extends DokuWiki_Syntax_Plugin {

/**
* Get some information about this plugin.
*
* @return array The info array.
* Syntax Type
*/
function getInfo() {
return array(
'author' => 'iDo, Sam Wilson, Michael Braun',
'email' => '',
'date' => '2013-06-20',
'name' => 'addnewpage',
'desc' => 'Adds a "new page form" to any wiki page.',
'url' => 'https://wiki.dokuwiki.org/plugin:addnewpage',
);
public function getType() {
return 'substition';
}

function getType() { return 'substition'; }

function getPType() { return 'block'; }
/**
* Paragraph Type
*/
public function getPType() {
return 'block';
}

function getSort() { return 199; }
/**
* @return int
*/
public function getSort() {
return 199;
}

function connectTo($mode) {
/**
* @param string $mode
*/
public function connectTo($mode) {
$this->Lexer->addSpecialPattern('\{\{NEWPAGE[^\}]*\}\}', $mode, 'plugin_addnewpage');
}

// @codingStandardsIgnoreStart
function handle($match, $state, $pos, &$handler) {
// @codingStandardsIgnoreEnd
$ns = substr($match, 10, -2); // strip markup
return array($ns);
/**
* Handler to prepare matched data for the rendering process
*
* Handled syntax options:
* {{NEWPAGE}}
* {{NEWPAGE>your:namespace}}
* {{NEWPAGE#newtpl1,newtpl2}}
* {{NEWPAGE#newtpl1|Title1,newtpl2|Title1}}
* {{NEWPAGE>your:namespace#newtpl1|Title1,newtpl2|Title1}}
*
* @param string $match The text matched by the patterns
* @param int $state The lexer state for the match
* @param int $pos The character position of the matched text
* @param Doku_Handler $handler The Doku_Handler object
* @return array Return an array with all data you want to use in render
*/
public function handle($match, $state, $pos, Doku_Handler $handler) {
$options = substr($match, 9, -2); // strip markup
$options = explode('#', $options, 2);

$namespace = trim(ltrim($options[0], '>'));
$templates = explode(',', $options[1]);
$templates = array_map('trim', $templates);
return array(
'namespace' => $namespace,
'newpagetemplates' => $templates
);
}

/**
* Create the new-page form.
*
* @return boolean
*
* @param $mode string output format being rendered
* @param $renderer Doku_Renderer the current renderer object
* @param $data array data created by handler()
* @return boolean rendered correctly?
*/
function render($mode, &$renderer, $data) {
public function render($mode, Doku_Renderer $renderer, $data) {
global $lang;
$renderer->info['cache'] = false;
$data = $data[0]; // get data back from the array

if ($mode == 'xhtml') {
$ns_select = $this->_makecombo($data);
if ($ns_select == $this->getLang('nooption')) {
$renderer->doc .= (!$this->getConf('addpage_hideACL')) ? $ns_select : '';
if($mode == 'xhtml') {
$disablecache = null;
$namespaceinput = $this->_htmlNamespaceInput($data['namespace'], $disablecache);
if($namespaceinput === false) {
if($this->getConf('addpage_hideACL')) {
$renderer->doc .= '';
} else {
$renderer->doc .= $this->getLang('nooption');
}
return true;
}
if($disablecache) $renderer->info['cache'] = false;

$button_val = ((@$this->getLang('okbutton')) ? $this->getLang('okbutton') : 'ok');
$form = '<div class="addnewpage">'.DOKU_LF
.DOKU_TAB.'<form name="addnewpage" method="get" action="'.DOKU_BASE.DOKU_SCRIPT.'" accept-charset="'.$lang['encoding'].'">'.DOKU_LF
.DOKU_TAB.DOKU_TAB.$ns_select.DOKU_LF
.DOKU_TAB.DOKU_TAB.'<input class="edit" type="text" name="title" size="20" maxlength="255" tabindex="2" />'.DOKU_LF
.DOKU_TAB.DOKU_TAB.'<input type="hidden" name="do" value="edit" />'.DOKU_LF
.DOKU_TAB.DOKU_TAB.'<input type="hidden" name="id" />'.DOKU_LF
.DOKU_TAB.DOKU_TAB.'<input class="button" type="submit" value="'.$button_val.'" tabindex="3" />'.DOKU_LF
.DOKU_TAB.'</form>'.DOKU_LF
.'</div>';
$newpagetemplateinput = $this->_htmlTemplateInput($data['newpagetemplates']);

$form = '<div class="addnewpage">' . DOKU_LF
. DOKU_TAB . '<form name="addnewpage" method="get" action="' . DOKU_BASE . DOKU_SCRIPT . '" accept-charset="' . $lang['encoding'] . '">' . DOKU_LF
. DOKU_TAB . DOKU_TAB . $namespaceinput . DOKU_LF
. DOKU_TAB . DOKU_TAB . '<input class="edit" type="text" name="title" size="20" maxlength="255" tabindex="2" />' . DOKU_LF
. $newpagetemplateinput
. DOKU_TAB . DOKU_TAB . '<input type="hidden" name="do" value="edit" />' . DOKU_LF
. DOKU_TAB . DOKU_TAB . '<input type="hidden" name="id" />' . DOKU_LF
. DOKU_TAB . DOKU_TAB . '<input class="button" type="submit" value="' . $this->getLang('okbutton') . '" tabindex="4" />' . DOKU_LF
. DOKU_TAB . '</form>' . DOKU_LF
. '</div>';
$renderer->doc .= $form;

return true;
Expand All @@ -89,10 +118,10 @@ function render($mode, &$renderer, $data) {
* @author Samuele Tognini <[email protected]>
* @author Michael Braun <[email protected]>
*/
function _parse_ns($ns) {
protected function _parse_ns($ns) {
global $ID;
if ($ns == "@PAGE@") return $ID;
if ($ns == "@NS@") return getNS($ID);
if($ns == "@PAGE@") return $ID;
if($ns == "@NS@") return getNS($ID);
$ns = preg_replace("/^\.(:|$)/", dirname(str_replace(':', '/', $ID)) . "$1", $ns);
$ns = str_replace("/", ":", $ns);
$ns = cleanID($ns);
Expand All @@ -101,110 +130,156 @@ function _parse_ns($ns) {

/**
* Create the HTML Select element for namespace selection.
*
* @global string $ID The page ID
*
* @param string|false $dest_ns The destination namespace, or false if none provided.
* @param bool $disablecache reference indicates if caching need to be disabled
* @global string $ID The page ID
* @return string Select element with appropriate NS selected.
*/
function _makecombo($dest_ns) {
protected function _htmlNamespaceInput($dest_ns, &$disablecache) {
global $ID;
$disablecache = false;

// If a NS has been provided:
// Whether to hide the NS selection (otherwise, show only subnamespaces).
$hide = $this->getConf('addpage_hide');

// Whether the user can create pages in the provided NS (or root, if no
// destination NS has been set.
$can_create = (auth_quickaclcheck($dest_ns.":") >= AUTH_CREATE);
$can_create = (auth_quickaclcheck($dest_ns . ":") >= AUTH_CREATE);

if (!empty($dest_ns) && $hide) {
if ($can_create) {
return '<input type="hidden" name="np_cat" id="np_cat" value="'.$this->_parse_ns($dest_ns).'"/>';
//namespace given, but hidden
if($hide && !empty($dest_ns)) {
if($can_create) {
return '<input type="hidden" name="np_cat" id="np_cat" value="' . $this->_parse_ns($dest_ns) . '"/>';
} else {
return $this->getLang('nooption');
return false;
}
}

$ns = explode(':', $ID);
array_pop($ns);
$ns = implode(':', $ns);
//show select of given namespace
$currentns = getNS($ID);

$r = $this->_getnslist("");
$ret = '<select class="edit" id="np_cat" name="np_cat" tabindex="1">';

// Whether the NS select element has any options
$someopt=false;
$someopt = false;

// Show root namespace if requested and allowed
if ($this->getConf('addpage_showroot') && $can_create) {
if (empty($dest_ns)) {
if($this->getConf('addpage_showroot') && $can_create) {
if(empty($dest_ns)) {
// If no namespace has been provided, add an option for the root NS.
$option_text = ((@$this->getLang('namespaceRoot'))?$this->getLang('namespaceRoot'):'top');
$ret.='<option '.(($ns=='')?'selected ':'').'value="">'.$option_text.'</option>';
$someopt=true;
$ret .= '<option ' . (($currentns == '') ? 'selected ' : '') . 'value="">' . $this->getLang('namespaceRoot') . '</option>';
$someopt = true;
} else {
// If a namespace has been provided, add an option for it.
$ret.='<option '.(($ns==$dest_ns)?'selected ':'').'value="'.$dest_ns.'">'.$dest_ns.'</option>';
$someopt=true;
$ret .= '<option ' . (($currentns == $dest_ns) ? 'selected ' : '') . 'value="' . formText($dest_ns) . '">' . formText($dest_ns) . '</option>';
$someopt = true;
}
}

foreach ($r as $k => $v) {
if ($data != '') {
if (strpos(":" . $v, ":" . $data . ":") === false) {
continue;
}
}
if (auth_quickaclcheck($v . ":") < AUTH_CREATE) continue;
$vv = explode(':', $v);
$vv = str_repeat('&nbsp;&nbsp;', substr_count($v, ':')) . $vv[count($vv) - 1];
$ret.='<option '.(($ns == $v) ? 'selected ' : '').'value="'.$v.'">'.$vv.'</option>';
$subnamespaces = $this->_getnslist($dest_ns);
foreach($subnamespaces as $ns) {
if(auth_quickaclcheck($ns . ":") < AUTH_CREATE) continue;
$nsparts = explode(':', $ns);
$nsparts = str_repeat('&nbsp;&nbsp;', substr_count($ns, ':')) . $nsparts[count($nsparts) - 1];
$ret .= '<option ' . (($currentns == $ns) ? 'selected ' : '') . 'value="' . $ns . '">' . $nsparts . '</option>';
$someopt = true;
$disablecache = true;
}
$ret.='</select>';
if (!$someopt) $ret = $this->getLang('nooption');
$ret .= '</select>';

return $ret;
if($someopt) {
return $ret;
} else {
return false;
}
}

/**
* Get a list of namespaces below the given namespace.
* Recursively fetches subnamespaces.
*
* Includes inc/search.php
* @global array $conf Site configuration variables
* @uses utf8_encodeFN
* @param string $tns The top namespace
*
* @param string $topns The top namespace
* @return array Multi-dimensional array of all namespaces below $tns
*/
function _getnslist($tns = '') {
require_once(DOKU_INC . 'inc/search.php');
protected function _getnslist($topns = '') {
global $conf;
if ($tns == '') $tns = $conf['datadir'];
if (!is_dir($tns)) $tns = utf8_encodeFN(str_replace(':', '/', $tns));
$data = array();
$exclude = $this->getConf('addpage_exclude');

if ($exclude == "") $exclude = array();
else $exclude = @explode(';', strtolower($exclude));

search($data, $tns, 'search_index', array('ns' => ''));

$data2 = array();
foreach ($data as $k => $v) {
if ($v['type'] == 'd') {
if (!in_array(strtolower($v['id']), $exclude)) {
array_push($data2, $v['id']);
$r = $this->_getnslist($tns . '/' . $v['id']);
foreach ($r as $vv) {
if (!in_array(strtolower($vv), $exclude)) {
array_push($data2, $v['id'] . ':' . $vv);
}
}

$topns = utf8_encodeFN(str_replace(':', '/', $topns));

$excludes = $this->getConf('addpage_exclude');
if($excludes == "") {
$excludes = array();
} else {
$excludes = @explode(';', strtolower($excludes));
}
$searchdata = array();
search($searchdata, $conf['datadir'], 'search_namespaces', array(), $topns);

$namespaces = array();
foreach($searchdata as $ns) {
foreach($excludes as $exclude) {
if(strpos($ns['id'], $exclude) === 0) {
continue 2;
}
}
$namespaces[] = $ns['id'];
}
return $data2;

return $namespaces;
}

/**
* Create html for selection of namespace templates
*
* @param array $newpagetemplates array of namespace templates
* @return string html of select or hidden input
*/
public function _htmlTemplateInput($newpagetemplates) {
$cnt = count($newpagetemplates);
if($cnt < 1 || $cnt == 1 && $newpagetemplates[0] == '') {
$input = '';

} else {
if($cnt == 1) {
list($template, ) = $this->_parseNStemplatepage($newpagetemplates[0]);
$input = '<input type="hidden" name="newpagetemplate" value="' . formText($template) . '" />';
} else {
$first = true;
$input = '<select name="newpagetemplate" tabindex="3">';
foreach($newpagetemplates as $template) {
$p = ($first ? ' selected="selected"' : '');
$first = false;

list($template, $name) = $this->_parseNStemplatepage($template);
$p .= ' value="'.formText($template).'"';
$input .= "<option $p>".formText($name)."</option>";
}
$input .= '</select>';
}
$input = DOKU_TAB . DOKU_TAB . $input . DOKU_LF;
}
return $input;
}

/**
* Parses and resolves the namespace template page
*
* @param $nstemplate
* @return array
*/
protected function _parseNStemplatepage($nstemplate) {
global $ID;

@list($template, $name) = explode('|', $nstemplate, 2);

$exist = null;
resolve_pageid(getNS($ID), $template, $exist); //get absolute id

if (is_null($name)) $name = $template;

return array($template, $name);
}

}

0 comments on commit 41f7d8f

Please sign in to comment.