1<?php
2/**
3 * DokuWiki Plugin autostartpage (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Charles Knight <charles@rabidaudio.com>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class action_plugin_autostartpage extends DokuWiki_Action_Plugin {
13
14    /**
15     * Registers a callback function for a given event
16     *
17     * @param Doku_Event_Handler $controller DokuWiki's event controller object
18     * @return void
19     */
20    public function register(Doku_Event_Handler &$controller) {
21       $controller->register_hook('IO_NAMESPACE_CREATED', 'AFTER', $this, 'autostartpage_handle');
22    }
23
24    /**
25     * [Custom event handler which performs action]
26     *
27     * @author Charles Knight, charles@rabidaudio.com
28     * @param Doku_Event $event  event object by reference
29     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
30     *                           handler was registered]
31     * @return void
32     */
33
34    public function autostartpage_handle(Doku_Event &$event, $param) {
35        global $conf;
36        global $INFO;
37
38        $templatefile = wikiFN($this->getConf('templatefile'), '', false);
39        if(@file_exists($templatefile)){
40            $wikitext=io_readFile($templatefile);
41        }
42
43        $ns=$event->data[0];
44        $ns_type=$event->data[1];
45        if($ns_type === "pages" && $wikitext){
46            $id=$ns.":".$conf['start'];
47            $file=wikiFN($id);
48            $silent=$this->getConf('silent');
49            $ns_sepchar = ":";
50
51            $parent=implode($ns_sepchar, array_splice(preg_split("/".preg_quote($ns_sepchar, "/")."/", $ns), 0, -1));
52            $goodns=preg_replace("/".$conf['sepchar']."/"," ",noNS($ns));
53            $page=preg_replace("/".$conf['sepchar']."/"," ",noNS($id));
54            $f=$conf['start'];
55
56            /**THESE ARE THE CODES FOR TEMPLATES**/
57            // @ID@         full ID of the page
58            // @NS@         namespace of the page
59            // @PAGE@       page name (ID without namespace and underscores replaced by spaces)
60            // @!PAGE@      same as above but with the first character uppercased
61            // @!!PAGE@     same as above but with the first character of all words uppercased
62            // @!PAGE!@     same as above but with all characters uppercased
63            // @FILE@       page name (ID without namespace, underscores kept as is)
64            // @!FILE@      same as above but with the first character uppercased
65            // @!FILE!@     same as above but with all characters uppercased
66            // @USER@       ID of user who is creating the page
67            // @NAME@       name of user who is creating the page
68            // @MAIL@       mail address of user who is creating the page
69            // @DATE@       date and time when edit session started
70            /**PLUS WE ADDED THESE**/
71            // @!NS@        namespace of the page (with spaces) but with the first character uppercased
72            // @!!NS@       namespace of the page (with spaces) but with the first character of all words uppercased
73            // @!!NS!@      namespace of the page (with spaces) but with all characters uppercased
74            // @PARENT@     the name of the parent namespace. Blank if parent is top
75            // @DATE=STRFTIME@   Where `STRFTIME` is a strftime configure string of page creation time,
76            //       e.g. %a %d-%m-%y => Thu 06-12-12
77
78            $wikitext=preg_replace("/@NS@/", $ns, $wikitext);
79            $wikitext=preg_replace("/@!NS@/", ucfirst($goodns), $wikitext);
80            $wikitext=preg_replace("/@!!NS@/", ucwords($goodns), $wikitext);
81            $wikitext=preg_replace("/@!!NS!@/", strtoupper($goodns), $wikitext);
82            $wikitext=preg_replace("/@ID@/", $id, $wikitext);
83            $wikitext=preg_replace("/@PAGE@/",$page, $wikitext);
84            $wikitext=preg_replace("/@!PAGE@/",ucfirst($page), $wikitext);
85            $wikitext=preg_replace("/@!!PAGE@/",$uupage=ucwords($page), $wikitext);
86            $wikitext=preg_replace("/@!PAGE!@/",strtoupper($page), $wikitext);
87            $wikitext=preg_replace("/@FILE@/",$f, $wikitext);
88            $wikitext=preg_replace("/@!FILE@/",ucfirst($f), $wikitext);
89            $wikitext=preg_replace("/@!FILE!@/",strtoupper($f), $wikitext);
90            $wikitext=preg_replace("/@USER@/",$_SERVER['REMOTE_USER'], $wikitext);
91            $wikitext=preg_replace("/@NAME@/",$INFO['userinfo']['name'], $wikitext);
92            $wikitext=preg_replace("/@MAIL@/",$INFO['userinfo']['mail'], $wikitext);
93            $wikitext=preg_replace("/@DATE@/",strftime("%D"), $wikitext);
94            $wikitext=preg_replace("/@PARENT@/",$parent, $wikitext);
95            if(preg_match("/@DATE=(.*)@/", $wikitext, $matches)){
96                $wikitext=str_replace($matches[0], strftime($matches[1]), $wikitext);
97            }
98
99            if(auth_quickaclcheck($id) >= AUTH_CREATE || $this->getConf('forcecreate')){
100
101                if(!@file_exists($file)){
102
103                    saveWikiText($id, $wikitext, "autostartpage", $minor = false);
104                    $ok = @file_exists($file);
105
106                    if ($ok and !$silent){
107                        msg($this->getLang('createmsg').' <a href="'.wl($id).'">'.noNS($id).'</a>', 1);
108                    }elseif (!$silent){
109                        msg($this->getLang('failmsg'), -1);
110                    }
111                }
112            }else{
113                msg($this->getLang('failmsg'), -1);
114            }
115        }elseif (!$wikitext and !$silent){
116            msg($this->getLang('templatemissing'));
117        }
118    }
119}
120
121// vim:ts=4:sw=4:et:
122