[dokuwiki] Re: inconsistent usage of DOKU_TPL / DOKU_TPLINC

  • From: Roland Hager <roland.hager@xxxxxxxxxxxx>
  • To: "dokuwiki@xxxxxxxxxxxxx" <dokuwiki@xxxxxxxxxxxxx>
  • Date: Thu, 23 Sep 2010 10:06:38 +0200

Since nobody answered yet I'm going to discuss my little changes one by one and kindly ask YOU to give a short statement if it could have some side-effects or such. Any comments to one or all changes are wellcome - Thanks in advance! (I posted the patches with my last mail, if anybody is interested)


By the way ... is there something like a testsuit or a testmatrix? How can I assure my changes won't break something else?


##################
2 changes in: inc/init.php
##################

@@ -110,6 +110,10 @@
+// Everything personal which needs the $conf-Array should go here
+$post_confload = fullpath(dirname(__FILE__)).'/post_confload.php';
+if (@file_exists($post_confload)) include($post_confload);
+

Here I introduced an inclusion to have custom code in a separated file which needs to have the $conf array already set up. No big deal. That's the file to set your custom DOKU_TPLINC, DOKU_TPL, TPLBASEDIR and DOKU_TPLSEARCHDIR as needed.

@@ -169,6 +173,16 @@
+// define real Template basedirectory
+if(!defined('DOKU_TPLBASEDIR')) define('DOKU_TPLBASEDIR',
+        DOKU_INC.'lib/tpl/');
+
+// define the default directory where templates are stored
+// multiple directories separated by ':' are possible
+if(!defined('DOKU_TPLSEARCHDIR')) define('DOKU_TPLSEARCHDIR',
+        DOKU_TPLBASEDIR);
+
+

Here they are: The two new tpl-Constants! I 'd like DOKU_TPLSEARCHDIR to contain more than one path to allow users to choose between their custom templates and the one provided by us. Therefore I need to change ... well see later on. If not set prior to this,the default tpl-dir is set.


######################
2 changes in:  inc/template.php
######################
@@ -17,10 +17,12 @@
 function template($tpl){
   global $conf;

-  if(@is_readable(DOKU_INC.'lib/tpl/'.$conf['template'].'/'.$tpl))
-    return DOKU_INC.'lib/tpl/'.$conf['template'].'/'.$tpl;
-
-  return DOKU_INC.'lib/tpl/default/'.$tpl;
+  // check if the configure template exists
+  if( @is_readable(DOKU_TPLINC) ){
+    return DOKU_TPLINC.'/'.$tpl;
+  }
+ // if it does not exist use the default template in the default templatedir
+  return DOKU_INC.'lib/tpl/default/'.$tpl;
 }


As you can see I removed the hard coded default tpl-path and used the constant DOKU_TPLINC. Since DOKU_TPLINC is always set in inc/init.php for me this seems to be no problem at all.


@@ -335,11 +337,11 @@
   // load stylesheets
$head['link'][] = array('rel'=>'stylesheet', 'media'=>'all', 'type'=>'text/css', - 'href'=>DOKU_BASE.'lib/exe/css.php?s=all&t='.$conf['template'].'&tseed='.$tseed); + 'href'=>DOKU_BASE.'lib/exe/css.php?s=all&tseed='.$tseed); $head['link'][] = array('rel'=>'stylesheet', 'media'=>'screen', 'type'=>'text/css', - 'href'=>DOKU_BASE.'lib/exe/css.php?t='.$conf['template'].'&tseed='.$tseed); + 'href'=>DOKU_BASE.'lib/exe/css.php?tseed='.$tseed); $head['link'][] = array('rel'=>'stylesheet', 'media'=>'print', 'type'=>'text/css', - 'href'=>DOKU_BASE.'lib/exe/css.php?s=print&t='.$conf['template'].'&tseed='.$tseed); + 'href'=>DOKU_BASE.'lib/exe/css.php?s=print&tseed='.$tseed);


Here I omitted the GET Parameter "t" containing the name of the configured template. This parameter is used in lib/exe/css.php. If set, css.php will use the default tpl-path with this tpl-name. If "t" is not set css.php will use the constants, which is what I want. I cannot see a proper reason why "t" should be set here. Is there a situation where $conf['template'] is change after being set in init.php? If not, it's not necessary to set the tpl-name, since css.php also knows the same value from init.php.




############################################
1 little change in: lib/plugins/config/settings/config.metadata.php
############################################
@@ -87,7 +87,7 @@
-$meta['template'] = array('dirchoice','_dir' => DOKU_INC.'lib/tpl/','_pattern' => '/^[\w-]+$/'); +$meta['template'] = array('dirchoice','_dir' => DOKU_TPLSEARCHDIR,'_pattern' => '/^[\w-]+$/');

Make use of the new DOKU_TPLSEARCHDIR constant. Remember this one can contain multiple paths separated by a colon. Thats why I changed the next function.



#####################################
1 change in: lib/plugins/config/settings/config.class.php
#####################################
@@ -753,16 +753,21 @@
       // populate $this->_choices with a list of directories
       $list = array();

-      if ($dh = @opendir($this->_dir)) {
-        while (false !== ($entry = readdir($dh))) {
-          if ($entry == '.' || $entry == '..') continue;
- if ($this->_pattern && !preg_match($this->_pattern,$entry)) continue;
+      $_dirs = explode(':', $this->_dir);
+
+      foreach($_dirs as $dir){
+        if ($dh = @opendir($dir)) {
+          while (false !== ($entry = readdir($dh))) {
+            if ($entry == '.' || $entry == '..') continue;
+ if ($this->_pattern && !preg_match($this->_pattern,$entry)) continue;

- $file = (is_link($this->_dir.$entry)) ? readlink($this->_dir.$entry) : $entry;
-          if (is_dir($this->_dir.$file)) $list[] = $entry;
+ $file = (is_link($dir.$entry)) ? readlink($dir.$entry) : $entry;
+            if (is_dir($dir.$file)) $list[] = $entry;
+          }
+          closedir($dh);
         }
-        closedir($dh);
       }
+      $list = array_unique($list);
       sort($list);
       $this->_choices = $list;


OK, to let the user choose the templates from their custom and the default tpl-dir, both must be read-in and listed. I just explode the given string by a colon and do exactly the same as before but for all elements of the created array. If there is no colon - the function acts as before. This change is not needed for a custom tpl-dir but it is very convenient to be able to select from multiple dirs.



#########################
1 change in: lib/exe/opensearch.php
#########################

@@ -17,9 +17,11 @@
 if(file_exists(DOKU_INC.'favicon.ico')){
     $ico = DOKU_URL.'favicon.ico';
 }elseif(file_exists(DOKU_TPLINC.'images/favicon.ico')){
-    $ico = DOKU_URL.'lib/tpl/'.$conf['template'].'/images/favicon.ico';
+    $ico = DOKU_TPL.'images/favicon.ico';
 }elseif(file_exists(DOKU_TPLINC.'favicon.ico')){
-    $ico = DOKU_URL.'lib/tpl/'.$conf['template'].'/favicon.ico';
+    $ico = DOKU_TPL.'favicon.ico';
+}elseif(file_exists(DOKU_TPLBASEDIR.'default/images/favicon.ico')){
+       $ico = DOKU_TPL.'default/images/favicon.ico';
 }else{
     $ico = DOKU_URL.'lib/tpl/default/images/favicon.ico';
 }


Again I just switch from the hard coded tpl-path to the constants. I don'nt see a reason not to do it.


Wow - at least YOU read my mail to it's end - thanks! :-)
Roland

Other related posts: