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