Page MenuHomePhabricator

No OneTemporary

diff --git a/autoid.php b/autoid.php
--- a/autoid.php
+++ b/autoid.php
@@ -1,92 +1,119 @@
<?php
/* Automatic incremented ID based on a prefix and a @@ as a pattern for
* an automatic increased number.
*
* e.g.: neu.txt
* <form>
* Action template template:bug: bugs :
* Thanks "For submitting the bug"
* Textbox "Description" "/^[^\/:]+$/"
* autoid "BUG-ID" "=BUG-@@" "/^[^\/:]+$/" @
* Submit "Send"
* </form>
*
* with template: start.txt
* ====== @@BUG-ID@@ ======
* @@Description@@
*
* The autoiid field BUG-ID contains a template BUG-@@.
* 'BUG-' is used as prefix for all new pages.
* '@@' is replaced with an incremental number based on existing namespace/directory names.
* e.g. BUG-1, BUG-2, BUG-15 esxist then a new page gets BUG-16 as pagename.
*
+* Changes dev@aboehler.at, based on post in DokuWiki Forum (https://forum.dokuwiki.org/thread/7620)
+*
+* - Fixed refresh problem by adding a token to each request and remembering in
+* the metadata if the token has already been used - quite a hack
+*
+* - Fixed renderfield() to really add the hidden element.
+*
+* - Made code a bit more readable
+*
+* @author dev@aboehler.at
*/
class syntax_plugin_bureaucracy_field_autoid extends syntax_plugin_bureaucracy_field {
- function render($params, $form) {
+ function renderfield($params, $form) {
$this->_handlePreload();
- $form->addHidden($params['name'], $this->getParam('value') . '');
+ $form->addHidden($params['name'], $this->getParam('value') . ':' . uniqid());
return;
}
function handle_post($value) {
if (is_null($value))
return true;
- // Save pattern
- $this->opt['autoidpattern'] = $value;
+ // Save pattern and extract auto ID token
+ // This prevents page reloads from recreating the same page again!
+ $value = explode(":", $value);
+ $this->opt['autoidpattern'] = $value[0];
+ $this->opt['autoidtoken'] = $value[1];
// Set value first to ectract the prefix
$result = $this->setVal($value);
// Extract prefix and automatic incremented pagename
$this->getParam('pagename');
return $result;
}
-
+
function getParam($name) {
if (!isset($this->opt[$name]) || $name === 'value' && $this->hidden) {
return null;
}
if ($name === 'pagename') {
+ // Replace only on first invocation
+ if(isset($this->opt['replacementDone']) && ($this->opt['replacementDone'] === true))
+ return parent::getParam($name);
+ global $ID;
+ global $conf;
+
// Pattern e.g. BUG-@@
$value = $this->getParam('value');
if (is_null($value)) {
return null;
}
-
+
// Extract prefix 'BUG-'
$prefix = strstr($this->opt['autoidpattern'], "@@", true);
- // Get all directories starting with the prefix in the current namespace
- global $ID;
- global $conf;
+ // Get all directories starting with the prefix in the current namespace
+
$ns = cleanID(getNS($ID));
$dir = utf8_encodeFN(str_replace(':', '/', $ns));
$data = array();
- $opts2=array();
+ $opts2= array();
$opts2['listdirs'] = true;
$opts2['listfiles'] = false;
$opts2['depth'] = 0;
$opts2['dirmatch'] = "\/" . strtolower($prefix);
search($data, $conf['datadir'] . '/' . $dir, 'search_universal', $opts2, '', 0);
// Get highest ID number of found directories
- $maxid = 1;
+ $maxid = 0;
foreach($data as $i){
$current = substr($i['id'], strlen($prefix));
if (!is_null($current) && is_numeric($current))
$maxid = max(array($maxid, $current));
}
// Construct pagename
- $value = $prefix . ($maxid + 1);
+ if(p_get_metadata($ID, "lastautoidtoken") == $this->opt['autoidtoken'])
+ {
+ $value = $prefix . ($maxid);
+ }
+ else
+ {
+ $value = $prefix . ($maxid + 1);
+ p_set_metadata($ID, array('lastautoidtoken' => $this->opt['autoidtoken']));
+ }
// Set value to be replaced in template
$this->setVal($value);
-
+
if (is_null($value)) {
return null;
}
- global $conf;
+ $this->opt['replacementDone'] = true;
+
if($conf['useslash']) $value = str_replace('/',' ',$value);
return str_replace(':',' ',$value);
}
return $this->opt[$name];
}
}

File Metadata

Mime Type
text/x-diff
Expires
Sun, Dec 22, 9:52 PM (2 d, 9 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
532578
Default Alt Text
(4 KB)

Event Timeline