xref: /plugin/gitbacked/action/editcommit.php (revision a2effbcbe4aa7783cef7914640d7168cba1e9add)
1fa53f2a3SWolfgang Gassler<?php
2fa53f2a3SWolfgang Gassler/**
3fa53f2a3SWolfgang Gassler * DokuWiki Plugin gitbacked (Action Component)
4fa53f2a3SWolfgang Gassler *
5fa53f2a3SWolfgang Gassler * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6fa53f2a3SWolfgang Gassler * @author  Wolfgang Gassler <wolfgang@gassler.org>
7fa53f2a3SWolfgang Gassler */
8fa53f2a3SWolfgang Gassler
9fa53f2a3SWolfgang Gassler// must be run within Dokuwiki
10fa53f2a3SWolfgang Gasslerif (!defined('DOKU_INC')) die();
11fa53f2a3SWolfgang Gassler
12fa53f2a3SWolfgang Gasslerif (!defined('DOKU_LF')) define('DOKU_LF', "\n");
13fa53f2a3SWolfgang Gasslerif (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
14fa53f2a3SWolfgang Gasslerif (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
15fa53f2a3SWolfgang Gassler
1600ce3f12SDanny Linrequire_once dirname(__FILE__).'/../lib/Git.php';
17dee8dca1SMarkus Hoffroggerequire_once dirname(__FILE__).'/../lib/GitBackedUtil.php';
18fa53f2a3SWolfgang Gassler
19fa53f2a3SWolfgang Gasslerclass action_plugin_gitbacked_editcommit extends DokuWiki_Action_Plugin {
20fa53f2a3SWolfgang Gassler
2100ce3f12SDanny Lin    function __construct() {
22eeb1a599SMarkus Hoffrogge        $this->temp_dir = GitBackedUtil::getTempDir();
2300ce3f12SDanny Lin    }
2400ce3f12SDanny Lin
25a6cdc68cSMichael Sorg    public function register(Doku_Event_Handler $controller) {
26fa53f2a3SWolfgang Gassler
27fa53f2a3SWolfgang Gassler        $controller->register_hook('IO_WIKIPAGE_WRITE', 'AFTER', $this, 'handle_io_wikipage_write');
28442c3981SWolfgang Gassler        $controller->register_hook('MEDIA_UPLOAD_FINISH', 'AFTER', $this, 'handle_media_upload');
29442c3981SWolfgang Gassler        $controller->register_hook('MEDIA_DELETE_FILE', 'AFTER', $this, 'handle_media_deletion');
30b92b117aSWolfgang Gassler        $controller->register_hook('DOKUWIKI_DONE', 'AFTER', $this, 'handle_periodic_pull');
31442c3981SWolfgang Gassler    }
32442c3981SWolfgang Gassler
33b92b117aSWolfgang Gassler    private function initRepo() {
34442c3981SWolfgang Gassler        //get path to the repo root (by default DokuWiki's savedir)
35dee8dca1SMarkus Hoffrogge        $repoPath = GitBackedUtil::getEffectivePath($this->getConf('repoPath'));
36635161d0SCarsten Teibes        $gitPath = trim($this->getConf('gitPath'));
37635161d0SCarsten Teibes        if ($gitPath !== '') {
38635161d0SCarsten Teibes            Git::set_bin($gitPath);
39635161d0SCarsten Teibes        }
40442c3981SWolfgang Gassler        //init the repo and create a new one if it is not present
414eba9b44SDanny Lin        io_mkdir_p($repoPath);
42e8224fc2SMarkus Hoffrogge        $repo = new GitRepo($repoPath, $this, true, true);
434eba9b44SDanny Lin        //set git working directory (by default DokuWiki's savedir)
44dee8dca1SMarkus Hoffrogge        $repoWorkDir = $this->getConf('repoWorkDir');
45dee8dca1SMarkus Hoffrogge        if (!empty($repoWorkDir)) {
46dee8dca1SMarkus Hoffrogge            $repoWorkDir = GitBackedUtil::getEffectivePath($repoWorkDir);
47dee8dca1SMarkus Hoffrogge        }
48dee8dca1SMarkus Hoffrogge        Git::set_bin(empty($repoWorkDir) ? Git::get_bin() : Git::get_bin().' --work-tree '.escapeshellarg($repoWorkDir));
490d7cb616SBirkir A. Barkarson        $params = str_replace(
500d7cb616SBirkir A. Barkarson            array('%mail%','%user%'),
510d7cb616SBirkir A. Barkarson            array($this->getAuthorMail(),$this->getAuthor()),
520d7cb616SBirkir A. Barkarson            $this->getConf('addParams'));
53442c3981SWolfgang Gassler        if ($params) {
54985a1bc7SCarsten Teibes            Git::set_bin(Git::get_bin().' '.$params);
55442c3981SWolfgang Gassler        }
56b92b117aSWolfgang Gassler        return $repo;
57b92b117aSWolfgang Gassler    }
58b92b117aSWolfgang Gassler
5966f21a70SWolfgang Gassler	private function isIgnored($filePath) {
6066f21a70SWolfgang Gassler		$ignore = false;
6166f21a70SWolfgang Gassler		$ignorePaths = trim($this->getConf('ignorePaths'));
6266f21a70SWolfgang Gassler		if ($ignorePaths !== '') {
6366f21a70SWolfgang Gassler			$paths = explode(',',$ignorePaths);
6466f21a70SWolfgang Gassler			foreach($paths as $path) {
6566f21a70SWolfgang Gassler				if (strstr($filePath,$path)) {
6666f21a70SWolfgang Gassler					$ignore = true;
6766f21a70SWolfgang Gassler				}
6866f21a70SWolfgang Gassler			}
6966f21a70SWolfgang Gassler		}
7066f21a70SWolfgang Gassler		return $ignore;
7166f21a70SWolfgang Gassler	}
7266f21a70SWolfgang Gassler
73b92b117aSWolfgang Gassler    private function commitFile($filePath,$message) {
7466f21a70SWolfgang Gassler		if (!$this->isIgnored($filePath)) {
75e8224fc2SMarkus Hoffrogge			try {
76b92b117aSWolfgang Gassler				$repo = $this->initRepo();
77442c3981SWolfgang Gassler
78442c3981SWolfgang Gassler				//add the changed file and set the commit message
79442c3981SWolfgang Gassler				$repo->add($filePath);
80442c3981SWolfgang Gassler				$repo->commit($message);
81442c3981SWolfgang Gassler
82442c3981SWolfgang Gassler				//if the push after Commit option is set we push the active branch to origin
83442c3981SWolfgang Gassler				if ($this->getConf('pushAfterCommit')) {
84442c3981SWolfgang Gassler					$repo->push('origin',$repo->active_branch());
85442c3981SWolfgang Gassler				}
86e8224fc2SMarkus Hoffrogge			} catch (Exception $e) {
87e8224fc2SMarkus Hoffrogge				if (!$this->isNotifyByEmailOnGitCommandError()) {
88e8224fc2SMarkus Hoffrogge					throw new Exception('Git committing or pushing failed: '.$e->getMessage(), 1, $e);
8966f21a70SWolfgang Gassler				}
90e8224fc2SMarkus Hoffrogge				return;
91e8224fc2SMarkus Hoffrogge			}
92e8224fc2SMarkus Hoffrogge		}
93442c3981SWolfgang Gassler    }
94442c3981SWolfgang Gassler
95442c3981SWolfgang Gassler    private function getAuthor() {
96442c3981SWolfgang Gassler        return $GLOBALS['USERINFO']['name'];
97442c3981SWolfgang Gassler    }
98442c3981SWolfgang Gassler
990d7cb616SBirkir A. Barkarson    private function getAuthorMail() {
1000d7cb616SBirkir A. Barkarson        return $GLOBALS['USERINFO']['mail'];
1010d7cb616SBirkir A. Barkarson    }
1020d7cb616SBirkir A. Barkarson
103*a2effbcbSmsx80	private function computeLocalPath() {
104*a2effbcbSmsx80		global $conf;
105*a2effbcbSmsx80		$repoPath = str_replace('\\', '/', realpath(GitBackedUtil::getEffectivePath($this->getConf('repoPath'))));
106*a2effbcbSmsx80		$datadir = $conf['datadir']; // already normalized
107*a2effbcbSmsx80		if(!(substr($datadir, 0, strlen($repoPath)) === $repoPath))
108*a2effbcbSmsx80		{
109*a2effbcbSmsx80			throw new Exception('Datadir not inside repoPath ??');
110*a2effbcbSmsx80		}
111*a2effbcbSmsx80		return substr($datadir, strlen($repoPath)+1);
112*a2effbcbSmsx80	}
113*a2effbcbSmsx80
114*a2effbcbSmsx80	private function updatePage($page){
115*a2effbcbSmsx80
116*a2effbcbSmsx80		if (is_callable('dokuwiki\Search\Indexer::getInstance')) {
117*a2effbcbSmsx80			$Indexer = Indexer::getInstance();
118*a2effbcbSmsx80			$success = $Indexer->addPage($page, false, false);
119*a2effbcbSmsx80		} elseif (class_exists('Doku_Indexer')) {
120*a2effbcbSmsx80			$success = idx_addPage($page, false, false);
121*a2effbcbSmsx80		} else {
122*a2effbcbSmsx80		   // Failed to index the page. Your DokuWiki is older than release 2011-05-25 "Rincewind"
123*a2effbcbSmsx80		   $success = false;
124*a2effbcbSmsx80		}
125*a2effbcbSmsx80
126*a2effbcbSmsx80		echo "Update $page: $success <br/>";
127*a2effbcbSmsx80
128*a2effbcbSmsx80	}
129*a2effbcbSmsx80
1302377428fSDanny Lin    public function handle_periodic_pull(Doku_Event &$event, $param) {
1312377428fSDanny Lin        if ($this->getConf('periodicPull')) {
132*a2effbcbSmsx80			$enableIndexUpdate = $this->getConf('updateIndexOnPull');
1332377428fSDanny Lin            $lastPullFile = $this->temp_dir.'/lastpull.txt';
1342377428fSDanny Lin            //check if the lastPullFile exists
1352377428fSDanny Lin            if (is_file($lastPullFile)) {
1362377428fSDanny Lin                $lastPull = unserialize(file_get_contents($lastPullFile));
1372377428fSDanny Lin            } else {
1382377428fSDanny Lin                $lastPull = 0;
1392377428fSDanny Lin            }
1402377428fSDanny Lin            //calculate time between pulls in seconds
1412377428fSDanny Lin            $timeToWait = $this->getConf('periodicMinutes')*60;
1422377428fSDanny Lin            $now = time();
1432377428fSDanny Lin
144*a2effbcbSmsx80
1452377428fSDanny Lin            //if it is time to run a pull request
1462377428fSDanny Lin            if ($lastPull+$timeToWait < $now) {
147e8224fc2SMarkus Hoffrogge				try {
148*a2effbcbSmsx80
1492377428fSDanny Lin                	$repo = $this->initRepo();
150*a2effbcbSmsx80					if($enableIndexUpdate)
151*a2effbcbSmsx80					{
152*a2effbcbSmsx80						$localPath = $this -> computeLocalPath();
153*a2effbcbSmsx80
154*a2effbcbSmsx80						// store current revision id
155*a2effbcbSmsx80						$revBefore = $repo->run('rev-parse HEAD');
156*a2effbcbSmsx80					}
1572377428fSDanny Lin
1582377428fSDanny Lin                	//execute the pull request
1592377428fSDanny Lin                	$repo->pull('origin',$repo->active_branch());
160*a2effbcbSmsx80
161*a2effbcbSmsx80					if($enableIndexUpdate)
162*a2effbcbSmsx80					{
163*a2effbcbSmsx80						// store new revision id
164*a2effbcbSmsx80						$revAfter = $repo->run('rev-parse HEAD');
165*a2effbcbSmsx80
166*a2effbcbSmsx80						if(strcmp($revBefore, $revAfter) != 0)
167*a2effbcbSmsx80						{
168*a2effbcbSmsx80							// if there were some changes, get the list of all changed files
169*a2effbcbSmsx80							$changedFilesPage = $repo->run('diff --name-only '.$revBefore.' '.$revAfter);
170*a2effbcbSmsx80							$changedFiles = preg_split("/\r\n|\n|\r/", $changedFilesPage);
171*a2effbcbSmsx80
172*a2effbcbSmsx80							foreach ($changedFiles as $cf)
173*a2effbcbSmsx80							{
174*a2effbcbSmsx80								// check if the file is inside localPath, that is, it's a page
175*a2effbcbSmsx80								if(substr($cf, 0, strlen($localPath)) === $localPath)
176*a2effbcbSmsx80								{
177*a2effbcbSmsx80									// convert from relative filename to page name
178*a2effbcbSmsx80									// for example:	local/path/dir/subdir/test.txt -> dir:subdir:test
179*a2effbcbSmsx80									$page =  str_replace('/', ':',substr($cf, strlen($localPath)+1, -4)); // -4 removes .txt
180*a2effbcbSmsx80
181*a2effbcbSmsx80									// update the page
182*a2effbcbSmsx80									$this -> updatePage($page);
183*a2effbcbSmsx80								}
184*a2effbcbSmsx80								else
185*a2effbcbSmsx80								{
186*a2effbcbSmsx80									echo "Page NOT to update: $cf <br/>";
187*a2effbcbSmsx80								}
188*a2effbcbSmsx80							}
189*a2effbcbSmsx80
190*a2effbcbSmsx80						}
191*a2effbcbSmsx80					}
192*a2effbcbSmsx80
193e8224fc2SMarkus Hoffrogge				} catch (Exception $e) {
194e8224fc2SMarkus Hoffrogge					if (!$this->isNotifyByEmailOnGitCommandError()) {
195e8224fc2SMarkus Hoffrogge						throw new Exception('Git command failed to perform periodic pull: '.$e->getMessage(), 2, $e);
196e8224fc2SMarkus Hoffrogge					}
197e8224fc2SMarkus Hoffrogge					return;
198e8224fc2SMarkus Hoffrogge				}
1992377428fSDanny Lin
2002377428fSDanny Lin                //save the current time to the file to track the last pull execution
2012377428fSDanny Lin                file_put_contents($lastPullFile,serialize(time()));
2022377428fSDanny Lin            }
2032377428fSDanny Lin        }
2042377428fSDanny Lin    }
2052377428fSDanny Lin
206442c3981SWolfgang Gassler    public function handle_media_deletion(Doku_Event &$event, $param) {
207442c3981SWolfgang Gassler        $mediaPath = $event->data['path'];
208442c3981SWolfgang Gassler        $mediaName = $event->data['name'];
209442c3981SWolfgang Gassler
210442c3981SWolfgang Gassler        $message = str_replace(
211442c3981SWolfgang Gassler            array('%media%','%user%'),
212442c3981SWolfgang Gassler            array($mediaName,$this->getAuthor()),
213442c3981SWolfgang Gassler            $this->getConf('commitMediaMsgDel')
214442c3981SWolfgang Gassler        );
215442c3981SWolfgang Gassler
216442c3981SWolfgang Gassler        $this->commitFile($mediaPath,$message);
217442c3981SWolfgang Gassler
218442c3981SWolfgang Gassler    }
219442c3981SWolfgang Gassler
220442c3981SWolfgang Gassler    public function handle_media_upload(Doku_Event &$event, $param) {
221442c3981SWolfgang Gassler
222442c3981SWolfgang Gassler        $mediaPath = $event->data[1];
223442c3981SWolfgang Gassler        $mediaName = $event->data[2];
224442c3981SWolfgang Gassler
225442c3981SWolfgang Gassler        $message = str_replace(
226442c3981SWolfgang Gassler            array('%media%','%user%'),
227442c3981SWolfgang Gassler            array($mediaName,$this->getAuthor()),
228442c3981SWolfgang Gassler            $this->getConf('commitMediaMsg')
229442c3981SWolfgang Gassler        );
230442c3981SWolfgang Gassler
231442c3981SWolfgang Gassler        $this->commitFile($mediaPath,$message);
232fa53f2a3SWolfgang Gassler
233fa53f2a3SWolfgang Gassler    }
234fa53f2a3SWolfgang Gassler
235fa53f2a3SWolfgang Gassler    public function handle_io_wikipage_write(Doku_Event &$event, $param) {
236fa53f2a3SWolfgang Gassler
237fa53f2a3SWolfgang Gassler        $rev = $event->data[3];
238fa53f2a3SWolfgang Gassler
239fa53f2a3SWolfgang Gassler        /* On update to an existing page this event is called twice,
240fa53f2a3SWolfgang Gassler         * once for the transfer of the old version to the attic (rev will have a value)
241fa53f2a3SWolfgang Gassler         * and once to write the new version of the page into the wiki (rev is false)
242fa53f2a3SWolfgang Gassler         */
243fa53f2a3SWolfgang Gassler        if (!$rev) {
244fa53f2a3SWolfgang Gassler
245fa53f2a3SWolfgang Gassler            $pagePath = $event->data[0][0];
246fa53f2a3SWolfgang Gassler            $pageName = $event->data[2];
247442c3981SWolfgang Gassler            $pageContent = $event->data[0][1];
248fa53f2a3SWolfgang Gassler
2497af27dc9SWolfgang Gassler            // get the summary directly from the form input
250e7471cfaSDanny Lin            // as the metadata hasn't updated yet
2517af27dc9SWolfgang Gassler            $editSummary = $GLOBALS['INPUT']->str('summary');
252442c3981SWolfgang Gassler
253442c3981SWolfgang Gassler            // empty content indicates a page deletion
254442c3981SWolfgang Gassler            if ($pageContent == '') {
255442c3981SWolfgang Gassler                // get the commit text for deletions
256d4e1c54bSWolfgang Gassler                $msgTemplate = $this->getConf('commitPageMsgDel');
257442c3981SWolfgang Gassler
258442c3981SWolfgang Gassler                // bad hack as DokuWiki deletes the file after this event
259442c3981SWolfgang Gassler                // thus, let's delete the file by ourselves, so git can recognize the deletion
260442c3981SWolfgang Gassler                // DokuWiki uses @unlink as well, so no error should be thrown if we delete it twice
261442c3981SWolfgang Gassler                @unlink($pagePath);
262442c3981SWolfgang Gassler
263442c3981SWolfgang Gassler            } else {
264442c3981SWolfgang Gassler                //get the commit text for edits
265d4e1c54bSWolfgang Gassler                $msgTemplate = $this->getConf('commitPageMsg');
266442c3981SWolfgang Gassler            }
267442c3981SWolfgang Gassler
268fa53f2a3SWolfgang Gassler            $message = str_replace(
269fa53f2a3SWolfgang Gassler                array('%page%','%summary%','%user%'),
270442c3981SWolfgang Gassler                array($pageName,$editSummary,$this->getAuthor()),
271442c3981SWolfgang Gassler                $msgTemplate
272fa53f2a3SWolfgang Gassler            );
273fa53f2a3SWolfgang Gassler
274442c3981SWolfgang Gassler            $this->commitFile($pagePath,$message);
275fa53f2a3SWolfgang Gassler
276fa53f2a3SWolfgang Gassler        }
277e8224fc2SMarkus Hoffrogge    }
278fa53f2a3SWolfgang Gassler
279e8224fc2SMarkus Hoffrogge	// ====== Error notification helpers ======
280e8224fc2SMarkus Hoffrogge	/**
281e8224fc2SMarkus Hoffrogge	 * Notifies error on create_new
282e8224fc2SMarkus Hoffrogge	 *
283e8224fc2SMarkus Hoffrogge	 * @access  public
284e8224fc2SMarkus Hoffrogge	 * @param   string  repository path
285e8224fc2SMarkus Hoffrogge	 * @param   string  reference path / remote reference
286e8224fc2SMarkus Hoffrogge	 * @param   string  error message
287e8224fc2SMarkus Hoffrogge	 * @return  bool
288e8224fc2SMarkus Hoffrogge	 */
289e8224fc2SMarkus Hoffrogge	public function notify_create_new_error($repo_path, $reference, $error_message) {
290e8224fc2SMarkus Hoffrogge		$template_replacements = array(
291e8224fc2SMarkus Hoffrogge			'GIT_REPO_PATH' => $repo_path,
292e8224fc2SMarkus Hoffrogge			'GIT_REFERENCE' => (empty($reference) ? 'n/a' : $reference),
293e8224fc2SMarkus Hoffrogge			'GIT_ERROR_MESSAGE' => $error_message
294e8224fc2SMarkus Hoffrogge		);
295e8224fc2SMarkus Hoffrogge		return $this->notifyByMail('mail_create_new_error_subject', 'mail_create_new_error', $template_replacements);
296e8224fc2SMarkus Hoffrogge	}
297e8224fc2SMarkus Hoffrogge
298e8224fc2SMarkus Hoffrogge	/**
299e8224fc2SMarkus Hoffrogge	 * Notifies error on setting repo path
300e8224fc2SMarkus Hoffrogge	 *
301e8224fc2SMarkus Hoffrogge	 * @access  public
302e8224fc2SMarkus Hoffrogge	 * @param   string  repository path
303e8224fc2SMarkus Hoffrogge	 * @param   string  error message
304e8224fc2SMarkus Hoffrogge	 * @return  bool
305e8224fc2SMarkus Hoffrogge	 */
306e8224fc2SMarkus Hoffrogge	public function notify_repo_path_error($repo_path, $error_message) {
307e8224fc2SMarkus Hoffrogge		$template_replacements = array(
308e8224fc2SMarkus Hoffrogge			'GIT_REPO_PATH' => $repo_path,
309e8224fc2SMarkus Hoffrogge			'GIT_ERROR_MESSAGE' => $error_message
310e8224fc2SMarkus Hoffrogge		);
311e8224fc2SMarkus Hoffrogge		return $this->notifyByMail('mail_repo_path_error_subject', 'mail_repo_path_error', $template_replacements);
312e8224fc2SMarkus Hoffrogge	}
313e8224fc2SMarkus Hoffrogge
314e8224fc2SMarkus Hoffrogge	/**
315e8224fc2SMarkus Hoffrogge	 * Notifies error on git command
316e8224fc2SMarkus Hoffrogge	 *
317e8224fc2SMarkus Hoffrogge	 * @access  public
318e8224fc2SMarkus Hoffrogge	 * @param   string  repository path
319e8224fc2SMarkus Hoffrogge	 * @param   string  current working dir
320e8224fc2SMarkus Hoffrogge	 * @param   string  command line
321e8224fc2SMarkus Hoffrogge	 * @param   int     exit code of command (status)
322e8224fc2SMarkus Hoffrogge	 * @param   string  error message
323e8224fc2SMarkus Hoffrogge	 * @return  bool
324e8224fc2SMarkus Hoffrogge	 */
325e8224fc2SMarkus Hoffrogge	public function notify_command_error($repo_path, $cwd, $command, $status, $error_message) {
326e8224fc2SMarkus Hoffrogge		$template_replacements = array(
327e8224fc2SMarkus Hoffrogge			'GIT_REPO_PATH' => $repo_path,
328e8224fc2SMarkus Hoffrogge			'GIT_CWD' => $cwd,
329e8224fc2SMarkus Hoffrogge			'GIT_COMMAND' => $command,
330e8224fc2SMarkus Hoffrogge			'GIT_COMMAND_EXITCODE' => $status,
331e8224fc2SMarkus Hoffrogge			'GIT_ERROR_MESSAGE' => $error_message
332e8224fc2SMarkus Hoffrogge		);
333e8224fc2SMarkus Hoffrogge		return $this->notifyByMail('mail_command_error_subject', 'mail_command_error', $template_replacements);
334e8224fc2SMarkus Hoffrogge	}
335e8224fc2SMarkus Hoffrogge
336e8224fc2SMarkus Hoffrogge	/**
337e8224fc2SMarkus Hoffrogge	 * Notifies success on git command
338e8224fc2SMarkus Hoffrogge	 *
339e8224fc2SMarkus Hoffrogge	 * @access  public
340e8224fc2SMarkus Hoffrogge	 * @param   string  repository path
341e8224fc2SMarkus Hoffrogge	 * @param   string  current working dir
342e8224fc2SMarkus Hoffrogge	 * @param   string  command line
343e8224fc2SMarkus Hoffrogge	 * @return  bool
344e8224fc2SMarkus Hoffrogge	 */
345e8224fc2SMarkus Hoffrogge	public function notify_command_success($repo_path, $cwd, $command) {
346e8224fc2SMarkus Hoffrogge		if (!$this->getConf('notifyByMailOnSuccess')) {
347e8224fc2SMarkus Hoffrogge			return false;
348e8224fc2SMarkus Hoffrogge		}
349e8224fc2SMarkus Hoffrogge		$template_replacements = array(
350e8224fc2SMarkus Hoffrogge			'GIT_REPO_PATH' => $repo_path,
351e8224fc2SMarkus Hoffrogge			'GIT_CWD' => $cwd,
352e8224fc2SMarkus Hoffrogge			'GIT_COMMAND' => $command
353e8224fc2SMarkus Hoffrogge		);
354e8224fc2SMarkus Hoffrogge		return $this->notifyByMail('mail_command_success_subject', 'mail_command_success', $template_replacements);
355e8224fc2SMarkus Hoffrogge	}
356e8224fc2SMarkus Hoffrogge
357e8224fc2SMarkus Hoffrogge	/**
358e8224fc2SMarkus Hoffrogge	 * Send an eMail, if eMail address is configured
359e8224fc2SMarkus Hoffrogge	 *
360e8224fc2SMarkus Hoffrogge	 * @access  public
361e8224fc2SMarkus Hoffrogge	 * @param   string  lang id for the subject
362e8224fc2SMarkus Hoffrogge	 * @param   string  lang id for the template(.txt)
363e8224fc2SMarkus Hoffrogge	 * @param   array   array of replacements
364e8224fc2SMarkus Hoffrogge	 * @return  bool
365e8224fc2SMarkus Hoffrogge	 */
366e8224fc2SMarkus Hoffrogge	public function notifyByMail($subject_id, $template_id, $template_replacements) {
367e8224fc2SMarkus Hoffrogge		$ret = false;
368dd477e30SMarkus Hoffrogge		//dbglog("GitBacked - notifyByMail: [subject_id=".$subject_id.", template_id=".$template_id.", template_replacements=".$template_replacements."]");
369e8224fc2SMarkus Hoffrogge		if (!$this->isNotifyByEmailOnGitCommandError()) {
370e8224fc2SMarkus Hoffrogge			return $ret;
371e8224fc2SMarkus Hoffrogge		}
372e8224fc2SMarkus Hoffrogge		//$template_text = rawLocale($template_id); // this works for core artifacts only - not for plugins
373e8224fc2SMarkus Hoffrogge		$template_filename = $this->localFN($template_id);
374e8224fc2SMarkus Hoffrogge        $template_text = file_get_contents($template_filename);
375e8224fc2SMarkus Hoffrogge		$template_html = $this->render_text($template_text);
376e8224fc2SMarkus Hoffrogge
377e8224fc2SMarkus Hoffrogge		$mailer = new \Mailer();
378e8224fc2SMarkus Hoffrogge		$mailer->to($this->getEmailAddressOnErrorConfigured());
379dd477e30SMarkus Hoffrogge		//dbglog("GitBacked - lang check['".$subject_id."']: ".$this->getLang($subject_id));
380dd477e30SMarkus Hoffrogge		//dbglog("GitBacked - template text['".$template_id."']: ".$template_text);
381dd477e30SMarkus Hoffrogge		//dbglog("GitBacked - template html['".$template_id."']: ".$template_html);
382e8224fc2SMarkus Hoffrogge		$mailer->subject($this->getLang($subject_id));
383e8224fc2SMarkus Hoffrogge		$mailer->setBody($template_text, $template_replacements, null, $template_html);
384e8224fc2SMarkus Hoffrogge		$ret = $mailer->send();
385e8224fc2SMarkus Hoffrogge
386e8224fc2SMarkus Hoffrogge        return $ret;
387e8224fc2SMarkus Hoffrogge	}
388e8224fc2SMarkus Hoffrogge
389e8224fc2SMarkus Hoffrogge	/**
390e8224fc2SMarkus Hoffrogge	 * Check, if eMail is to be sent on a Git command error.
391e8224fc2SMarkus Hoffrogge	 *
392e8224fc2SMarkus Hoffrogge	 * @access  public
393e8224fc2SMarkus Hoffrogge	 * @return  bool
394e8224fc2SMarkus Hoffrogge	 */
395e8224fc2SMarkus Hoffrogge	public function isNotifyByEmailOnGitCommandError() {
396e8224fc2SMarkus Hoffrogge		$emailAddressOnError = $this->getEmailAddressOnErrorConfigured();
397e8224fc2SMarkus Hoffrogge		return !empty($emailAddressOnError);
398e8224fc2SMarkus Hoffrogge	}
399e8224fc2SMarkus Hoffrogge
400e8224fc2SMarkus Hoffrogge	/**
401e8224fc2SMarkus Hoffrogge	 * Get the eMail address configured for notifications.
402e8224fc2SMarkus Hoffrogge	 *
403e8224fc2SMarkus Hoffrogge	 * @access  public
404e8224fc2SMarkus Hoffrogge	 * @return  string
405e8224fc2SMarkus Hoffrogge	 */
406e8224fc2SMarkus Hoffrogge	public function getEmailAddressOnErrorConfigured() {
407e8224fc2SMarkus Hoffrogge		$emailAddressOnError = trim($this->getConf('emailAddressOnError'));
408e8224fc2SMarkus Hoffrogge		return $emailAddressOnError;
409fa53f2a3SWolfgang Gassler	}
410fa53f2a3SWolfgang Gassler
411fa53f2a3SWolfgang Gassler}
412fa53f2a3SWolfgang Gassler
413fa53f2a3SWolfgang Gassler// vim:ts=4:sw=4:et:
414