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