xref: /plugin/gitbacked/action/editcommit.php (revision eeb1a5999713f6494da977712f7d917b7b433341)
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';
18require_once dirname(__FILE__).'/../lib/GitBackedUtil.php';
19
20class action_plugin_gitbacked_editcommit extends DokuWiki_Action_Plugin {
21
22    function __construct() {
23        $this->temp_dir = GitBackedUtil::getTempDir();
24    }
25
26    public function register(Doku_Event_Handler $controller) {
27
28        $controller->register_hook('IO_WIKIPAGE_WRITE', 'AFTER', $this, 'handle_io_wikipage_write');
29        $controller->register_hook('MEDIA_UPLOAD_FINISH', 'AFTER', $this, 'handle_media_upload');
30        $controller->register_hook('MEDIA_DELETE_FILE', 'AFTER', $this, 'handle_media_deletion');
31        $controller->register_hook('DOKUWIKI_DONE', 'AFTER', $this, 'handle_periodic_pull');
32    }
33
34    private function initRepo() {
35        //get path to the repo root (by default DokuWiki's savedir)
36        $repoPath = GitBackedUtil::getEffectivePath($this->getConf('repoPath'));
37        $gitPath = trim($this->getConf('gitPath'));
38        if ($gitPath !== '') {
39            Git::set_bin($gitPath);
40        }
41        //init the repo and create a new one if it is not present
42        io_mkdir_p($repoPath);
43        $repo = new GitRepo($repoPath, $this, true, true);
44        //set git working directory (by default DokuWiki's savedir)
45        $repoWorkDir = $this->getConf('repoWorkDir');
46        if (!empty($repoWorkDir)) {
47            $repoWorkDir = GitBackedUtil::getEffectivePath($repoWorkDir);
48        }
49        Git::set_bin(empty($repoWorkDir) ? Git::get_bin() : Git::get_bin().' --work-tree '.escapeshellarg($repoWorkDir));
50        $params = str_replace(
51            array('%mail%','%user%'),
52            array($this->getAuthorMail(),$this->getAuthor()),
53            $this->getConf('addParams'));
54        if ($params) {
55            Git::set_bin(Git::get_bin().' '.$params);
56        }
57        return $repo;
58    }
59
60	private function isIgnored($filePath) {
61		$ignore = false;
62		$ignorePaths = trim($this->getConf('ignorePaths'));
63		if ($ignorePaths !== '') {
64			$paths = explode(',',$ignorePaths);
65			foreach($paths as $path) {
66				if (strstr($filePath,$path)) {
67					$ignore = true;
68				}
69			}
70		}
71		return $ignore;
72	}
73
74    private function commitFile($filePath,$message) {
75		if (!$this->isIgnored($filePath)) {
76			try {
77				$repo = $this->initRepo();
78
79				//add the changed file and set the commit message
80				$repo->add($filePath);
81				$repo->commit($message);
82
83				//if the push after Commit option is set we push the active branch to origin
84				if ($this->getConf('pushAfterCommit')) {
85					$repo->push('origin',$repo->active_branch());
86				}
87			} catch (Exception $e) {
88				if (!$this->isNotifyByEmailOnGitCommandError()) {
89					throw new Exception('Git committing or pushing failed: '.$e->getMessage(), 1, $e);
90				}
91				return;
92			}
93		}
94    }
95
96    private function getAuthor() {
97        return $GLOBALS['USERINFO']['name'];
98    }
99
100    private function getAuthorMail() {
101        return $GLOBALS['USERINFO']['mail'];
102    }
103
104    public function handle_periodic_pull(Doku_Event &$event, $param) {
105        if ($this->getConf('periodicPull')) {
106            $lastPullFile = $this->temp_dir.'/lastpull.txt';
107            //check if the lastPullFile exists
108            if (is_file($lastPullFile)) {
109                $lastPull = unserialize(file_get_contents($lastPullFile));
110            } else {
111                $lastPull = 0;
112            }
113            //calculate time between pulls in seconds
114            $timeToWait = $this->getConf('periodicMinutes')*60;
115            $now = time();
116
117            //if it is time to run a pull request
118            if ($lastPull+$timeToWait < $now) {
119				try {
120                	$repo = $this->initRepo();
121
122                	//execute the pull request
123                	$repo->pull('origin',$repo->active_branch());
124				} catch (Exception $e) {
125					if (!$this->isNotifyByEmailOnGitCommandError()) {
126						throw new Exception('Git command failed to perform periodic pull: '.$e->getMessage(), 2, $e);
127					}
128					return;
129				}
130
131                //save the current time to the file to track the last pull execution
132                file_put_contents($lastPullFile,serialize(time()));
133            }
134        }
135    }
136
137    public function handle_media_deletion(Doku_Event &$event, $param) {
138        $mediaPath = $event->data['path'];
139        $mediaName = $event->data['name'];
140
141        $message = str_replace(
142            array('%media%','%user%'),
143            array($mediaName,$this->getAuthor()),
144            $this->getConf('commitMediaMsgDel')
145        );
146
147        $this->commitFile($mediaPath,$message);
148
149    }
150
151    public function handle_media_upload(Doku_Event &$event, $param) {
152
153        $mediaPath = $event->data[1];
154        $mediaName = $event->data[2];
155
156        $message = str_replace(
157            array('%media%','%user%'),
158            array($mediaName,$this->getAuthor()),
159            $this->getConf('commitMediaMsg')
160        );
161
162        $this->commitFile($mediaPath,$message);
163
164    }
165
166    public function handle_io_wikipage_write(Doku_Event &$event, $param) {
167
168        $rev = $event->data[3];
169
170        /* On update to an existing page this event is called twice,
171         * once for the transfer of the old version to the attic (rev will have a value)
172         * and once to write the new version of the page into the wiki (rev is false)
173         */
174        if (!$rev) {
175
176            $pagePath = $event->data[0][0];
177            $pageName = $event->data[2];
178            $pageContent = $event->data[0][1];
179
180            // get the summary directly from the form input
181            // as the metadata hasn't updated yet
182            $editSummary = $GLOBALS['INPUT']->str('summary');
183
184            // empty content indicates a page deletion
185            if ($pageContent == '') {
186                // get the commit text for deletions
187                $msgTemplate = $this->getConf('commitPageMsgDel');
188
189                // bad hack as DokuWiki deletes the file after this event
190                // thus, let's delete the file by ourselves, so git can recognize the deletion
191                // DokuWiki uses @unlink as well, so no error should be thrown if we delete it twice
192                @unlink($pagePath);
193
194            } else {
195                //get the commit text for edits
196                $msgTemplate = $this->getConf('commitPageMsg');
197            }
198
199            $message = str_replace(
200                array('%page%','%summary%','%user%'),
201                array($pageName,$editSummary,$this->getAuthor()),
202                $msgTemplate
203            );
204
205            $this->commitFile($pagePath,$message);
206
207        }
208    }
209
210	// ====== Error notification helpers ======
211	/**
212	 * Notifies error on create_new
213	 *
214	 * @access  public
215	 * @param   string  repository path
216	 * @param   string  reference path / remote reference
217	 * @param   string  error message
218	 * @return  bool
219	 */
220	public function notify_create_new_error($repo_path, $reference, $error_message) {
221		$template_replacements = array(
222			'GIT_REPO_PATH' => $repo_path,
223			'GIT_REFERENCE' => (empty($reference) ? 'n/a' : $reference),
224			'GIT_ERROR_MESSAGE' => $error_message
225		);
226		return $this->notifyByMail('mail_create_new_error_subject', 'mail_create_new_error', $template_replacements);
227	}
228
229	/**
230	 * Notifies error on setting repo path
231	 *
232	 * @access  public
233	 * @param   string  repository path
234	 * @param   string  error message
235	 * @return  bool
236	 */
237	public function notify_repo_path_error($repo_path, $error_message) {
238		$template_replacements = array(
239			'GIT_REPO_PATH' => $repo_path,
240			'GIT_ERROR_MESSAGE' => $error_message
241		);
242		return $this->notifyByMail('mail_repo_path_error_subject', 'mail_repo_path_error', $template_replacements);
243	}
244
245	/**
246	 * Notifies error on git command
247	 *
248	 * @access  public
249	 * @param   string  repository path
250	 * @param   string  current working dir
251	 * @param   string  command line
252	 * @param   int     exit code of command (status)
253	 * @param   string  error message
254	 * @return  bool
255	 */
256	public function notify_command_error($repo_path, $cwd, $command, $status, $error_message) {
257		$template_replacements = array(
258			'GIT_REPO_PATH' => $repo_path,
259			'GIT_CWD' => $cwd,
260			'GIT_COMMAND' => $command,
261			'GIT_COMMAND_EXITCODE' => $status,
262			'GIT_ERROR_MESSAGE' => $error_message
263		);
264		return $this->notifyByMail('mail_command_error_subject', 'mail_command_error', $template_replacements);
265	}
266
267	/**
268	 * Notifies success on git command
269	 *
270	 * @access  public
271	 * @param   string  repository path
272	 * @param   string  current working dir
273	 * @param   string  command line
274	 * @return  bool
275	 */
276	public function notify_command_success($repo_path, $cwd, $command) {
277		if (!$this->getConf('notifyByMailOnSuccess')) {
278			return false;
279		}
280		$template_replacements = array(
281			'GIT_REPO_PATH' => $repo_path,
282			'GIT_CWD' => $cwd,
283			'GIT_COMMAND' => $command
284		);
285		return $this->notifyByMail('mail_command_success_subject', 'mail_command_success', $template_replacements);
286	}
287
288	/**
289	 * Send an eMail, if eMail address is configured
290	 *
291	 * @access  public
292	 * @param   string  lang id for the subject
293	 * @param   string  lang id for the template(.txt)
294	 * @param   array   array of replacements
295	 * @return  bool
296	 */
297	public function notifyByMail($subject_id, $template_id, $template_replacements) {
298		$ret = false;
299		dbglog("GitBacked - notifyByMail: [subject_id=".$subject_id.", template_id=".$template_id.", template_replacements=".$template_replacements."]");
300		if (!$this->isNotifyByEmailOnGitCommandError()) {
301			return $ret;
302		}
303		//$template_text = rawLocale($template_id); // this works for core artifacts only - not for plugins
304		$template_filename = $this->localFN($template_id);
305        $template_text = file_get_contents($template_filename);
306		$template_html = $this->render_text($template_text);
307
308		$mailer = new \Mailer();
309		$mailer->to($this->getEmailAddressOnErrorConfigured());
310		dbglog("GitBacked - lang check['".$subject_id."']: ".$this->getLang($subject_id));
311		dbglog("GitBacked - template text['".$template_id."']: ".$template_text);
312		dbglog("GitBacked - template html['".$template_id."']: ".$template_html);
313		$mailer->subject($this->getLang($subject_id));
314		$mailer->setBody($template_text, $template_replacements, null, $template_html);
315		$ret = $mailer->send();
316
317        return $ret;
318	}
319
320	/**
321	 * Check, if eMail is to be sent on a Git command error.
322	 *
323	 * @access  public
324	 * @return  bool
325	 */
326	public function isNotifyByEmailOnGitCommandError() {
327		$emailAddressOnError = $this->getEmailAddressOnErrorConfigured();
328		return !empty($emailAddressOnError);
329	}
330
331	/**
332	 * Get the eMail address configured for notifications.
333	 *
334	 * @access  public
335	 * @return  string
336	 */
337	public function getEmailAddressOnErrorConfigured() {
338		$emailAddressOnError = trim($this->getConf('emailAddressOnError'));
339		return $emailAddressOnError;
340	}
341
342}
343
344// vim:ts=4:sw=4:et:
345