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