xref: /plugin/gitbacked/action/editcommit.php (revision 635161d0b509deaca3533f7581e597f8f252f583)
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        if(defined('DOKU_FARM')) {
38            $repoPath = $this->getConf('repoPath');
39        } else {
40            $repoPath = DOKU_INC.$this->getConf('repoPath');
41        }
42        //set the path to the git binary
43        $gitPath = trim($this->getConf('gitPath'));
44        if ($gitPath !== '') {
45            Git::set_bin($gitPath);
46        }
47        //init the repo and create a new one if it is not present
48        io_mkdir_p($repoPath);
49        $repo = new GitRepo($repoPath, true, true);
50        //set git working directory (by default DokuWiki's savedir)
51        $repoWorkDir = DOKU_INC.$this->getConf('repoWorkDir');
52        Git::set_bin(Git::get_bin().' --work-tree '.escapeshellarg($repoWorkDir));
53
54        $params = str_replace(
55            array('%mail%','%user%'),
56            array($this->getAuthorMail(),$this->getAuthor()),
57            $this->getConf('addParams'));
58        if ($params) {
59            Git::set_bin(Git::get_bin().' '.$params);
60        }
61        return $repo;
62    }
63
64	private function isIgnored($filePath) {
65		$ignore = false;
66		$ignorePaths = trim($this->getConf('ignorePaths'));
67		if ($ignorePaths !== '') {
68			$paths = explode(',',$ignorePaths);
69			foreach($paths as $path) {
70				if (strstr($filePath,$path)) {
71					$ignore = true;
72				}
73			}
74		}
75		return $ignore;
76	}
77
78    private function commitFile($filePath,$message) {
79
80		if (!$this->isIgnored($filePath)) {
81			$repo = $this->initRepo();
82
83			//add the changed file and set the commit message
84			$repo->add($filePath);
85			$repo->commit($message);
86
87			//if the push after Commit option is set we push the active branch to origin
88			if ($this->getConf('pushAfterCommit')) {
89				$repo->push('origin',$repo->active_branch());
90			}
91		}
92
93    }
94
95    private function getAuthor() {
96        return $GLOBALS['USERINFO']['name'];
97    }
98
99    private function getAuthorMail() {
100        return $GLOBALS['USERINFO']['mail'];
101    }
102
103    public function handle_periodic_pull(Doku_Event &$event, $param) {
104        if ($this->getConf('periodicPull')) {
105            $lastPullFile = $this->temp_dir.'/lastpull.txt';
106            //check if the lastPullFile exists
107            if (is_file($lastPullFile)) {
108                $lastPull = unserialize(file_get_contents($lastPullFile));
109            } else {
110                $lastPull = 0;
111            }
112            //calculate time between pulls in seconds
113            $timeToWait = $this->getConf('periodicMinutes')*60;
114            $now = time();
115
116            //if it is time to run a pull request
117            if ($lastPull+$timeToWait < $now) {
118                $repo = $this->initRepo();
119
120                //execute the pull request
121                $repo->pull('origin',$repo->active_branch());
122
123                //save the current time to the file to track the last pull execution
124                file_put_contents($lastPullFile,serialize(time()));
125            }
126        }
127    }
128
129    public function handle_media_deletion(Doku_Event &$event, $param) {
130        $mediaPath = $event->data['path'];
131        $mediaName = $event->data['name'];
132
133        $message = str_replace(
134            array('%media%','%user%'),
135            array($mediaName,$this->getAuthor()),
136            $this->getConf('commitMediaMsgDel')
137        );
138
139        $this->commitFile($mediaPath,$message);
140
141    }
142
143    public function handle_media_upload(Doku_Event &$event, $param) {
144
145        $mediaPath = $event->data[1];
146        $mediaName = $event->data[2];
147
148        $message = str_replace(
149            array('%media%','%user%'),
150            array($mediaName,$this->getAuthor()),
151            $this->getConf('commitMediaMsg')
152        );
153
154        $this->commitFile($mediaPath,$message);
155
156    }
157
158    public function handle_io_wikipage_write(Doku_Event &$event, $param) {
159
160        $rev = $event->data[3];
161
162        /* On update to an existing page this event is called twice,
163         * once for the transfer of the old version to the attic (rev will have a value)
164         * and once to write the new version of the page into the wiki (rev is false)
165         */
166        if (!$rev) {
167
168            $pagePath = $event->data[0][0];
169            $pageName = $event->data[2];
170            $pageContent = $event->data[0][1];
171
172            // get the summary directly from the form input
173            // as the metadata hasn't updated yet
174            $editSummary = $GLOBALS['INPUT']->str('summary');
175
176            // empty content indicates a page deletion
177            if ($pageContent == '') {
178                // get the commit text for deletions
179                $msgTemplate = $this->getConf('commitPageMsgDel');
180
181                // bad hack as DokuWiki deletes the file after this event
182                // thus, let's delete the file by ourselves, so git can recognize the deletion
183                // DokuWiki uses @unlink as well, so no error should be thrown if we delete it twice
184                @unlink($pagePath);
185
186            } else {
187                //get the commit text for edits
188                $msgTemplate = $this->getConf('commitPageMsg');
189            }
190
191            $message = str_replace(
192                array('%page%','%summary%','%user%'),
193                array($pageName,$editSummary,$this->getAuthor()),
194                $msgTemplate
195            );
196
197            $this->commitFile($pagePath,$message);
198
199        }
200
201    }
202
203}
204
205// vim:ts=4:sw=4:et:
206