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