Not logged in. · Lost password · Register
Forum: General Help and Support Development RSS
[SOLVED] Plugin is registered but would not be called
Avatar
roga #1
Member since Sep 2018 · 35 posts · Location: Zürich, Switzerland
Group memberships: Members
Show profile · Link to this post
Subject: [SOLVED] Plugin is registered but would not be called
Hi,

I have to modify a Plugin and for this, I first have to investigate how does it work. My Knowledge in php is low and I don't know how Plugins in dokuwiki works exactly.

In the Code below I added two lines for debugging.

The first below entry function Register()

dbglog("PLUGIN REGEX_TEMPLATE REGISTERED!");

The second below entry function regex_template()

dbglog("PLUGIN REGEX_TEMPLATE CALLED!");

Then I tried to use the Plugin ...

Now my Question: The debug.log only shows me entries for PLUGIN REGEX_TEMPLATE REGISTERED! but I never see an entry for PLUGIN REGEX_TEMPLATE CALLED!

This means, that the Plugin would not be invoked?

Why?

Any help are welcome.
Kind regards,
Roland


<?php
/**
 * Regex Template plugin, Choose template from regex on $ID
 * @author     Cédric Villemain <cedric.villemain@dalibo.com>
 */

if(!defined('DOKU_INC')) die();

if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'action.php');

class action_plugin_regextemplate extends DokuWiki_Action_Plugin {

  /**
   * return some info
   */
  function getInfo(){
          return confToHash(dirname(__FILE__).'/plugin.info.txt');
  }

  /**
   * register the eventhandlers
   */
  function register(Doku_Event_Handler $controller){

    $controller->register_hook('COMMON_PAGETPL_LOAD', 'BEFORE', $this, 'regex_template', array());
    dbglog("PLUGIN REGEX_TEMPLATE REGISTERED!");
  }

  function regex_template(Doku_Event $event, $param) {
        global $ID;
        global $conf;
        global $INFO;
        dbglog("PLUGIN REGEX_TEMPLATE CALLED!");
        $path = dirname(wikiFN($ID));

        if(@file_exists($path.'/_template.txt')){
          $tpl = io_readFile($path.'/_template.txt');
        }else{
          // search upper namespaces for templates
          $len = strlen(rtrim($conf['datadir'],'/'));
          while (strlen($path) >= $len){
                if(@file_exists($path.'/__template.txt')){
                  $tpl = io_readFile($path.'/__template.txt');
                  break;
                }
                $path = substr($path, 0, strrpos($path, '/'));
          }
        }

        $array_regex = (array)explode("\n",$conf['plugin']['regex_template']['reg_tpl_regex']);
        reset($array_regex);
        foreach ($array_regex as $regex) {
          list($pattern, $replacement) = explode(',', $regex);
          $my_new_ID = @preg_replace($pattern, $replacement, $event->data['id']);
          $my_new_file = wikiFN($my_new_ID);
          if(@file_exists($my_new_file)){
                $tpl = io_readFile($my_new_file);
                break;
          }
        }

        //if(!$tpl) return '';
        if($tpl) $event->data['tpl'] = $tpl;

        // replace placeholders
        $tpl = str_replace('@ID@',$ID,$tpl);
        $tpl = str_replace('@NS@',getNS($ID),$tpl);
        $tpl = str_replace('@PAGE@',strtr(noNS($ID),'_',' '),$tpl);
        $tpl = str_replace('@USER@',$_SERVER['REMOTE_USER'],$tpl);
        $tpl = str_replace('@NAME@',$INFO['userinfo']['name'],$tpl);
        $tpl = str_replace('@MAIL@',$INFO['userinfo']['mail'],$tpl);
        $tpl = str_replace('@DATE@',$conf['dformat'],$tpl);
        // we need the callback to work around strftime's char limit
        $tpl = preg_replace_callback('/%./',create_function('$m','return strftime($m[0]);'),$tpl);

        $event->result=$tpl;
        $event->preventDefault();
  }
}
This post was edited 2 times, last on 2019-01-03, 21:07 by roga.
Avatar
LarsDW223 #2
Member since Sep 2014 · 399 posts · Location: Paderborn
Group memberships: Members
Show profile · Link to this post
As far as I understand it, the function regex_template will only be called if a new page is created.

The line
controller->register_hook('COMMON_PAGETPL_LOAD', 'BEFORE', $this, 'regex_template', array());
tells the DokuWiki core to call the function regex_template if the event COMMON_PAGETPL_LOAD is fired/triggered. And that event only seems to be fired if a new page is created, see https://www.dokuwiki.org/devel:event:common_pagetpl_load.
Avatar
roga #3
Member since Sep 2018 · 35 posts · Location: Zürich, Switzerland
Group memberships: Members
Show profile · Link to this post
That's Right. I wrote, that I used that Plugin. I created a new page but in the debug.log after that I could not found a entry. I have no Idea why ...
This post was edited on 2019-01-03, 19:15 by roga.
Avatar
LarsDW223 #4
Member since Sep 2014 · 399 posts · Location: Paderborn
Group memberships: Members
Show profile · Link to this post
I did the following:
- placed your source code in a file lib/plugins/regextemplate/action.php
- created a file plugin.info.txt in the same folder
- set directory/file permissions for webserver user (e.g. www-data)

In file plugin.info.txt I put the following content:
base   regextemplate
 author Cédric Villemain
 email  cedric.villemain@dalibo.com
 date   2019-01-03
 name   Regex-Template Plugin
 desc   Choose template from regex on $ID
 url    https://www.dokuwiki.org/plugin:regex_template

I enabled the config option allowdebug and entered the ID of an not existing page in my browsers address bar (e.g. http://localhost/doku.php?id=start123). Then I clicked on "create page" and "cancel".

Then I opened the debug log file and found this line in it:
21:00:56 127.0.0.1: PLUGIN REGEX_TEMPLATE CALLED!

(The line 21:00:53 127.0.0.1: PLUGIN REGEX_TEMPLATE REGISTERED! appears multiple times but the line with ...CALLED! only once)

So to me it seems to be working.
Avatar
roga #5
Member since Sep 2018 · 35 posts · Location: Zürich, Switzerland
Group memberships: Members
Show profile · Link to this post
LarsDW223,

Thank you for your reply,

You're right! I repeated your steps and can confirm it works now!

I don't know, what i have made wrong, but i'm happy now and can investigate the plugin further.

thank you for your help + time

kind regards
Roland
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters:
Go to forum
Imprint
This board is powered by the Unclassified NewsBoard software, 20150713-dev, © 2003-2015 by Yves Goergen
Current time: 2019-06-17, 21:05:32 (UTC +02:00)