xref: /plugin/gitbacked/action/editcommit.php (revision 2377428f8b648cfefa2fa22872891c9660c04812)
1<?php
2/**
3 * DokuWiki Plugin gitbacked (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Wolfgang Gassler <wolfgang@gassler.org>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
13if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
14if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
15
16require_once DOKU_PLUGIN.'action.php';
17require_once dirname(__FILE__).'/../lib/Git.php';
18
19class action_plugin_gitbacked_editcommit extends DokuWiki_Action_Plugin {
20
21    function __construct() {
22        global $conf;
23        $this->temp_dir = $conf['tmpdir'].'/gitbacked';
24        io_mkdir_p($this->temp_dir);
25    }
26
27    public function register(Doku_Event_Handler &$controller) {
28
29        $controller->register_hook('IO_WIKIPAGE_WRITE', 'AFTER', $this, 'handle_io_wikipage_write');
30        $controller->register_hook('MEDIA_UPLOAD_FINISH', 'AFTER', $this, 'handle_media_upload');
31        $controller->register_hook('MEDIA_DELETE_FILE', 'AFTER', $this, 'handle_media_deletion');
32        $controller->register_hook('DOKUWIKI_DONE', 'AFTER', $this, 'handle_periodic_pull');
33    }
34
35    private function initRepo() {
36        //get path to the repo root (by default DokuWiki's savedir)
37        $repoPath = DOKU_INC.$this->getConf('repoPath');
38        //init the repo and create a new one if it is not present
39        io_mkdir_p($repoPath);
40        $repo = new GitRepo($repoPath, true, true);
41        //set git working directory (by default DokuWiki's savedir)
42        $repoWorkDir = DOKU_INC.$this->getConf('repoWorkDir');
43        $repo->git_path .= ' --work-tree '.escapeshellarg($repoWorkDir);
44
45        $params = str_replace(
46            array('%mail%','%user%'),
47            array($this->getAuthorMail(),$this->getAuthor()),
48            $this->getConf('addParams'));
49        if ($params) {
50            $repo->git_path .= ' '.$params;
51        }
52        return $repo;
53    }
54
55    private function commitFile($filePath,$message) {
56
57        $repo = $this->initRepo();
58
59        //add the changed file and set the commit message
60        $repo->add($filePath);
61        $repo->commit($message);
62
63        //if the push after Commit option is set we push the active branch to origin
64        if ($this->getConf('pushAfterCommit')) {
65            $repo->push('origin',$repo->active_branch());
66        }
67
68    }
69
70    private function getAuthor() {
71        return $GLOBALS['USERINFO']['name'];
72    }
73
74    private function getAuthorMail() {
75        return $GLOBALS['USERINFO']['mail'];
76    }
77
78    public function handle_periodic_pull(Doku_Event &$event, $param) {
79        if ($this->getConf('periodicPull')) {
80            $lastPullFile = $this->temp_dir.'/lastpull.txt';
81            //check if the lastPullFile exists
82            if (is_file($lastPullFile)) {
83                $lastPull = unserialize(file_get_contents($lastPullFile));
84            } else {
85                $lastPull = 0;
86            }
87            //calculate time between pulls in seconds
88            $timeToWait = $this->getConf('periodicMinutes')*60;
89            $now = time();
90
91            //if it is time to run a pull request
92            if ($lastPull+$timeToWait < $now) {
93                $repo = $this->initRepo();
94
95                //execute the pull request
96                $repo->pull('origin',$repo->active_branch());
97
98                //save the current time to the file to track the last pull execution
99                file_put_contents($lastPullFile,serialize(time()));
100            }
101        }
102    }
103
104    public function handle_media_deletion(Doku_Event &$event, $param) {
105        $mediaPath = $event->data['path'];
106        $mediaName = $event->data['name'];
107
108        $message = str_replace(
109            array('%media%','%user%'),
110            array($mediaName,$this->getAuthor()),
111            $this->getConf('commitMediaMsgDel')
112        );
113
114        $this->commitFile($mediaPath,$message);
115
116    }
117
118    public function handle_media_upload(Doku_Event &$event, $param) {
119
120        $mediaPath = $event->data[1];
121        $mediaName = $event->data[2];
122
123        $message = str_replace(
124            array('%media%','%user%'),
125            array($mediaName,$this->getAuthor()),
126            $this->getConf('commitMediaMsg')
127        );
128
129        $this->commitFile($mediaPath,$message);
130
131    }
132
133    public function handle_io_wikipage_write(Doku_Event &$event, $param) {
134
135        $rev = $event->data[3];
136
137        /* On update to an existing page this event is called twice,
138         * once for the transfer of the old version to the attic (rev will have a value)
139         * and once to write the new version of the page into the wiki (rev is false)
140         */
141        if (!$rev) {
142
143            $pagePath = $event->data[0][0];
144            $pageName = $event->data[2];
145            $pageContent = $event->data[0][1];
146
147            // get the summary directly from the form input
148            // as the metadata hasn't updated yet
149            $editSummary = $GLOBALS['INPUT']->str('summary');
150
151            // empty content indicates a page deletion
152            if ($pageContent == '') {
153                // get the commit text for deletions
154                $msgTemplate = $this->getConf('commitPageMsgDel');
155
156                // bad hack as DokuWiki deletes the file after this event
157                // thus, let's delete the file by ourselves, so git can recognize the deletion
158                // DokuWiki uses @unlink as well, so no error should be thrown if we delete it twice
159                @unlink($pagePath);
160
161            } else {
162                //get the commit text for edits
163                $msgTemplate = $this->getConf('commitPageMsg');
164            }
165
166            $message = str_replace(
167                array('%page%','%summary%','%user%'),
168                array($pageName,$editSummary,$this->getAuthor()),
169                $msgTemplate
170            );
171
172            $this->commitFile($pagePath,$message);
173
174        }
175
176    }
177
178}
179
180// vim:ts=4:sw=4:et:
181