xref: /plugin/gitbacked/action/editcommit.php (revision 2762023dfb29a64197cb442f664aa321f9f5bc87)
1fa53f2a3SWolfgang Gassler<?php
2*2762023dSMarkus Hoffrogge
3fa53f2a3SWolfgang Gassler/**
4fa53f2a3SWolfgang Gassler * DokuWiki Plugin gitbacked (Action Component)
5fa53f2a3SWolfgang Gassler *
6fa53f2a3SWolfgang Gassler * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7fa53f2a3SWolfgang Gassler * @author  Wolfgang Gassler <wolfgang@gassler.org>
8fa53f2a3SWolfgang Gassler */
9fa53f2a3SWolfgang Gassler
10*2762023dSMarkus Hoffrogge// phpcs:disable PSR1.Files.SideEffects.FoundWithSymbols
11fa53f2a3SWolfgang Gassler// must be run within Dokuwiki
12fa53f2a3SWolfgang Gasslerif (!defined('DOKU_INC')) die();
13fa53f2a3SWolfgang Gassler
14fa53f2a3SWolfgang Gasslerif (!defined('DOKU_LF')) define('DOKU_LF', "\n");
15fa53f2a3SWolfgang Gasslerif (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
16fa53f2a3SWolfgang Gasslerif (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
17fa53f2a3SWolfgang Gassler
18*2762023dSMarkus Hoffroggerequire_once __DIR__ . '/../loader.php';
19fa53f2a3SWolfgang Gassler
20*2762023dSMarkus Hoffroggeuse dokuwiki\Extension\ActionPlugin;
21*2762023dSMarkus Hoffroggeuse dokuwiki\Extension\EventHandler;
22*2762023dSMarkus Hoffroggeuse dokuwiki\Extension\Event;
23fa53f2a3SWolfgang Gassler
24*2762023dSMarkus Hoffroggeuse woolfg\dokuwiki\plugin\gitbacked\Git;
25*2762023dSMarkus Hoffroggeuse woolfg\dokuwiki\plugin\gitbacked\GitRepo;
26*2762023dSMarkus Hoffroggeuse woolfg\dokuwiki\plugin\gitbacked\GitBackedUtil;
27*2762023dSMarkus Hoffrogge
28*2762023dSMarkus Hoffrogge// phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
29*2762023dSMarkus Hoffrogge// phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps
30*2762023dSMarkus Hoffroggeclass action_plugin_gitbacked_editcommit extends ActionPlugin
31*2762023dSMarkus Hoffrogge{
32*2762023dSMarkus Hoffrogge    /**
33*2762023dSMarkus Hoffrogge     * Temporary directory for this gitbacked plugin.
34*2762023dSMarkus Hoffrogge     *
35*2762023dSMarkus Hoffrogge     * @var string
36*2762023dSMarkus Hoffrogge     */
37*2762023dSMarkus Hoffrogge    private $temp_dir;
38*2762023dSMarkus Hoffrogge
39*2762023dSMarkus Hoffrogge    public function __construct()
40*2762023dSMarkus Hoffrogge    {
41eeb1a599SMarkus Hoffrogge        $this->temp_dir = GitBackedUtil::getTempDir();
4200ce3f12SDanny Lin    }
4300ce3f12SDanny Lin
44*2762023dSMarkus Hoffrogge    public function register(EventHandler $controller)
45*2762023dSMarkus Hoffrogge    {
46*2762023dSMarkus Hoffrogge        $controller->register_hook('IO_WIKIPAGE_WRITE', 'AFTER', $this, 'handleIOWikiPageWrite');
47*2762023dSMarkus Hoffrogge        $controller->register_hook('MEDIA_UPLOAD_FINISH', 'AFTER', $this, 'handleMediaUpload');
48*2762023dSMarkus Hoffrogge        $controller->register_hook('MEDIA_DELETE_FILE', 'AFTER', $this, 'handleMediaDeletion');
49*2762023dSMarkus Hoffrogge        $controller->register_hook('DOKUWIKI_DONE', 'AFTER', $this, 'handlePeriodicPull');
50442c3981SWolfgang Gassler    }
51442c3981SWolfgang Gassler
52*2762023dSMarkus Hoffrogge    private function initRepo()
53*2762023dSMarkus Hoffrogge    {
54442c3981SWolfgang Gassler        //get path to the repo root (by default DokuWiki's savedir)
55dee8dca1SMarkus Hoffrogge        $repoPath = GitBackedUtil::getEffectivePath($this->getConf('repoPath'));
56635161d0SCarsten Teibes        $gitPath = trim($this->getConf('gitPath'));
57635161d0SCarsten Teibes        if ($gitPath !== '') {
58*2762023dSMarkus Hoffrogge            Git::setBin($gitPath);
59635161d0SCarsten Teibes        }
60442c3981SWolfgang Gassler        //init the repo and create a new one if it is not present
614eba9b44SDanny Lin        io_mkdir_p($repoPath);
62e8224fc2SMarkus Hoffrogge        $repo = new GitRepo($repoPath, $this, true, true);
634eba9b44SDanny Lin        //set git working directory (by default DokuWiki's savedir)
64dee8dca1SMarkus Hoffrogge        $repoWorkDir = $this->getConf('repoWorkDir');
65dee8dca1SMarkus Hoffrogge        if (!empty($repoWorkDir)) {
66dee8dca1SMarkus Hoffrogge            $repoWorkDir = GitBackedUtil::getEffectivePath($repoWorkDir);
67dee8dca1SMarkus Hoffrogge        }
68*2762023dSMarkus Hoffrogge        Git::setBin(empty($repoWorkDir) ? Git::getBin()
69*2762023dSMarkus Hoffrogge            : Git::getBin() . ' --work-tree ' . escapeshellarg($repoWorkDir));
700d7cb616SBirkir A. Barkarson        $params = str_replace(
710d7cb616SBirkir A. Barkarson            array('%mail%', '%user%'),
720d7cb616SBirkir A. Barkarson            array($this->getAuthorMail(), $this->getAuthor()),
73*2762023dSMarkus Hoffrogge            $this->getConf('addParams')
74*2762023dSMarkus Hoffrogge        );
75442c3981SWolfgang Gassler        if ($params) {
76*2762023dSMarkus Hoffrogge            Git::setBin(Git::getBin() . ' ' . $params);
77442c3981SWolfgang Gassler        }
78b92b117aSWolfgang Gassler        return $repo;
79b92b117aSWolfgang Gassler    }
80b92b117aSWolfgang Gassler
81*2762023dSMarkus Hoffrogge    private function isIgnored($filePath)
82*2762023dSMarkus Hoffrogge    {
8366f21a70SWolfgang Gassler        $ignore = false;
8466f21a70SWolfgang Gassler        $ignorePaths = trim($this->getConf('ignorePaths'));
8566f21a70SWolfgang Gassler        if ($ignorePaths !== '') {
8666f21a70SWolfgang Gassler            $paths = explode(',', $ignorePaths);
8766f21a70SWolfgang Gassler            foreach ($paths as $path) {
8866f21a70SWolfgang Gassler                if (strstr($filePath, $path)) {
8966f21a70SWolfgang Gassler                    $ignore = true;
9066f21a70SWolfgang Gassler                }
9166f21a70SWolfgang Gassler            }
9266f21a70SWolfgang Gassler        }
9366f21a70SWolfgang Gassler        return $ignore;
9466f21a70SWolfgang Gassler    }
9566f21a70SWolfgang Gassler
96*2762023dSMarkus Hoffrogge    private function commitFile($filePath, $message)
97*2762023dSMarkus Hoffrogge    {
9866f21a70SWolfgang Gassler        if (!$this->isIgnored($filePath)) {
99e8224fc2SMarkus Hoffrogge            try {
100b92b117aSWolfgang Gassler                $repo = $this->initRepo();
101442c3981SWolfgang Gassler
102442c3981SWolfgang Gassler                //add the changed file and set the commit message
103442c3981SWolfgang Gassler                $repo->add($filePath);
104442c3981SWolfgang Gassler                $repo->commit($message);
105442c3981SWolfgang Gassler
106442c3981SWolfgang Gassler                //if the push after Commit option is set we push the active branch to origin
107442c3981SWolfgang Gassler                if ($this->getConf('pushAfterCommit')) {
108*2762023dSMarkus Hoffrogge                    $repo->push('origin', $repo->activeBranch());
109442c3981SWolfgang Gassler                }
110e8224fc2SMarkus Hoffrogge            } catch (Exception $e) {
111e8224fc2SMarkus Hoffrogge                if (!$this->isNotifyByEmailOnGitCommandError()) {
112e8224fc2SMarkus Hoffrogge                    throw new Exception('Git committing or pushing failed: ' . $e->getMessage(), 1, $e);
11366f21a70SWolfgang Gassler                }
114e8224fc2SMarkus Hoffrogge                return;
115e8224fc2SMarkus Hoffrogge            }
116e8224fc2SMarkus Hoffrogge        }
117442c3981SWolfgang Gassler    }
118442c3981SWolfgang Gassler
119*2762023dSMarkus Hoffrogge    private function getAuthor()
120*2762023dSMarkus Hoffrogge    {
121442c3981SWolfgang Gassler        return $GLOBALS['USERINFO']['name'];
122442c3981SWolfgang Gassler    }
123442c3981SWolfgang Gassler
124*2762023dSMarkus Hoffrogge    private function getAuthorMail()
125*2762023dSMarkus Hoffrogge    {
1260d7cb616SBirkir A. Barkarson        return $GLOBALS['USERINFO']['mail'];
1270d7cb616SBirkir A. Barkarson    }
1280d7cb616SBirkir A. Barkarson
129*2762023dSMarkus Hoffrogge    private function computeLocalPath()
130*2762023dSMarkus Hoffrogge    {
131a2effbcbSmsx80        global $conf;
132a2effbcbSmsx80        $repoPath = str_replace('\\', '/', realpath(GitBackedUtil::getEffectivePath($this->getConf('repoPath'))));
133a2effbcbSmsx80        $datadir = $conf['datadir']; // already normalized
134*2762023dSMarkus Hoffrogge        if (!(substr($datadir, 0, strlen($repoPath)) === $repoPath)) {
135a2effbcbSmsx80            throw new Exception('Datadir not inside repoPath ??');
136a2effbcbSmsx80        }
137a2effbcbSmsx80        return substr($datadir, strlen($repoPath) + 1);
138a2effbcbSmsx80    }
139a2effbcbSmsx80
140*2762023dSMarkus Hoffrogge    private function updatePage($page)
141*2762023dSMarkus Hoffrogge    {
142a2effbcbSmsx80
143*2762023dSMarkus Hoffrogge        if (is_callable('\\dokuwiki\\Search\\Indexer::getInstance')) {
144*2762023dSMarkus Hoffrogge            $Indexer = \dokuwiki\Search\Indexer::getInstance();
145a2effbcbSmsx80            $success = $Indexer->addPage($page, false, false);
146a2effbcbSmsx80        } elseif (class_exists('Doku_Indexer')) {
147a2effbcbSmsx80            $success = idx_addPage($page, false, false);
148a2effbcbSmsx80        } else {
149a2effbcbSmsx80            // Failed to index the page. Your DokuWiki is older than release 2011-05-25 "Rincewind"
150a2effbcbSmsx80            $success = false;
151a2effbcbSmsx80        }
152a2effbcbSmsx80
153a2effbcbSmsx80        echo "Update $page: $success <br/>";
154a2effbcbSmsx80    }
155a2effbcbSmsx80
156*2762023dSMarkus Hoffrogge    public function handlePeriodicPull(Event &$event, $param)
157*2762023dSMarkus Hoffrogge    {
1582377428fSDanny Lin        if ($this->getConf('periodicPull')) {
159a2effbcbSmsx80            $enableIndexUpdate = $this->getConf('updateIndexOnPull');
1602377428fSDanny Lin            $lastPullFile = $this->temp_dir . '/lastpull.txt';
1612377428fSDanny Lin            //check if the lastPullFile exists
1622377428fSDanny Lin            if (is_file($lastPullFile)) {
1632377428fSDanny Lin                $lastPull = unserialize(file_get_contents($lastPullFile));
1642377428fSDanny Lin            } else {
1652377428fSDanny Lin                $lastPull = 0;
1662377428fSDanny Lin            }
1672377428fSDanny Lin            //calculate time between pulls in seconds
1682377428fSDanny Lin            $timeToWait = $this->getConf('periodicMinutes') * 60;
1692377428fSDanny Lin            $now = time();
1702377428fSDanny Lin
1712377428fSDanny Lin            //if it is time to run a pull request
1722377428fSDanny Lin            if ($lastPull + $timeToWait < $now) {
173e8224fc2SMarkus Hoffrogge                try {
1742377428fSDanny Lin                    $repo = $this->initRepo();
175*2762023dSMarkus Hoffrogge                    if ($enableIndexUpdate) {
176a2effbcbSmsx80                        $localPath = $this->computeLocalPath();
177a2effbcbSmsx80
178a2effbcbSmsx80                        // store current revision id
179a2effbcbSmsx80                        $revBefore = $repo->run('rev-parse HEAD');
180a2effbcbSmsx80                    }
1812377428fSDanny Lin
1822377428fSDanny Lin                    //execute the pull request
183*2762023dSMarkus Hoffrogge                    $repo->pull('origin', $repo->activeBranch());
184a2effbcbSmsx80
185*2762023dSMarkus Hoffrogge                    if ($enableIndexUpdate) {
186a2effbcbSmsx80                        // store new revision id
187a2effbcbSmsx80                        $revAfter = $repo->run('rev-parse HEAD');
188a2effbcbSmsx80
189*2762023dSMarkus Hoffrogge                        if (strcmp($revBefore, $revAfter) != 0) {
190a2effbcbSmsx80                            // if there were some changes, get the list of all changed files
191a2effbcbSmsx80                            $changedFilesPage = $repo->run('diff --name-only ' . $revBefore . ' ' . $revAfter);
192a2effbcbSmsx80                            $changedFiles = preg_split("/\r\n|\n|\r/", $changedFilesPage);
193a2effbcbSmsx80
194*2762023dSMarkus Hoffrogge                            foreach ($changedFiles as $cf) {
195a2effbcbSmsx80                                // check if the file is inside localPath, that is, it's a page
196*2762023dSMarkus Hoffrogge                                if (substr($cf, 0, strlen($localPath)) === $localPath) {
197a2effbcbSmsx80                                    // convert from relative filename to page name
198a2effbcbSmsx80                                    // for example: local/path/dir/subdir/test.txt -> dir:subdir:test
199*2762023dSMarkus Hoffrogge                                    // -4 removes .txt
200*2762023dSMarkus Hoffrogge                                    $page = str_replace('/', ':', substr($cf, strlen($localPath) + 1, -4));
201a2effbcbSmsx80
202a2effbcbSmsx80                                    // update the page
203a2effbcbSmsx80                                    $this->updatePage($page);
204*2762023dSMarkus Hoffrogge                                } else {
205a2effbcbSmsx80                                    echo "Page NOT to update: $cf <br/>";
206a2effbcbSmsx80                                }
207a2effbcbSmsx80                            }
208a2effbcbSmsx80                        }
209a2effbcbSmsx80                    }
210e8224fc2SMarkus Hoffrogge                } catch (Exception $e) {
211e8224fc2SMarkus Hoffrogge                    if (!$this->isNotifyByEmailOnGitCommandError()) {
212e8224fc2SMarkus Hoffrogge                        throw new Exception('Git command failed to perform periodic pull: ' . $e->getMessage(), 2, $e);
213e8224fc2SMarkus Hoffrogge                    }
214e8224fc2SMarkus Hoffrogge                    return;
215e8224fc2SMarkus Hoffrogge                }
2162377428fSDanny Lin
2172377428fSDanny Lin                //save the current time to the file to track the last pull execution
2182377428fSDanny Lin                file_put_contents($lastPullFile, serialize(time()));
2192377428fSDanny Lin            }
2202377428fSDanny Lin        }
2212377428fSDanny Lin    }
2222377428fSDanny Lin
223*2762023dSMarkus Hoffrogge    public function handleMediaDeletion(Event &$event, $param)
224*2762023dSMarkus Hoffrogge    {
225442c3981SWolfgang Gassler        $mediaPath = $event->data['path'];
226442c3981SWolfgang Gassler        $mediaName = $event->data['name'];
227442c3981SWolfgang Gassler
228442c3981SWolfgang Gassler        $message = str_replace(
229442c3981SWolfgang Gassler            array('%media%', '%user%'),
230442c3981SWolfgang Gassler            array($mediaName, $this->getAuthor()),
231442c3981SWolfgang Gassler            $this->getConf('commitMediaMsgDel')
232442c3981SWolfgang Gassler        );
233442c3981SWolfgang Gassler
234442c3981SWolfgang Gassler        $this->commitFile($mediaPath, $message);
235442c3981SWolfgang Gassler    }
236442c3981SWolfgang Gassler
237*2762023dSMarkus Hoffrogge    public function handleMediaUpload(Event &$event, $param)
238*2762023dSMarkus Hoffrogge    {
239442c3981SWolfgang Gassler
240442c3981SWolfgang Gassler        $mediaPath = $event->data[1];
241442c3981SWolfgang Gassler        $mediaName = $event->data[2];
242442c3981SWolfgang Gassler
243442c3981SWolfgang Gassler        $message = str_replace(
244442c3981SWolfgang Gassler            array('%media%', '%user%'),
245442c3981SWolfgang Gassler            array($mediaName, $this->getAuthor()),
246442c3981SWolfgang Gassler            $this->getConf('commitMediaMsg')
247442c3981SWolfgang Gassler        );
248442c3981SWolfgang Gassler
249442c3981SWolfgang Gassler        $this->commitFile($mediaPath, $message);
250fa53f2a3SWolfgang Gassler    }
251fa53f2a3SWolfgang Gassler
252*2762023dSMarkus Hoffrogge    public function handleIOWikiPageWrite(Event &$event, $param)
253*2762023dSMarkus Hoffrogge    {
254fa53f2a3SWolfgang Gassler
255fa53f2a3SWolfgang Gassler        $rev = $event->data[3];
256fa53f2a3SWolfgang Gassler
257fa53f2a3SWolfgang Gassler        /* On update to an existing page this event is called twice,
258fa53f2a3SWolfgang Gassler         * once for the transfer of the old version to the attic (rev will have a value)
259fa53f2a3SWolfgang Gassler         * and once to write the new version of the page into the wiki (rev is false)
260fa53f2a3SWolfgang Gassler         */
261fa53f2a3SWolfgang Gassler        if (!$rev) {
262fa53f2a3SWolfgang Gassler            $pagePath = $event->data[0][0];
263fa53f2a3SWolfgang Gassler            $pageName = $event->data[2];
264442c3981SWolfgang Gassler            $pageContent = $event->data[0][1];
265fa53f2a3SWolfgang Gassler
2667af27dc9SWolfgang Gassler            // get the summary directly from the form input
267e7471cfaSDanny Lin            // as the metadata hasn't updated yet
2687af27dc9SWolfgang Gassler            $editSummary = $GLOBALS['INPUT']->str('summary');
269442c3981SWolfgang Gassler
270442c3981SWolfgang Gassler            // empty content indicates a page deletion
271442c3981SWolfgang Gassler            if ($pageContent == '') {
272442c3981SWolfgang Gassler                // get the commit text for deletions
273d4e1c54bSWolfgang Gassler                $msgTemplate = $this->getConf('commitPageMsgDel');
274442c3981SWolfgang Gassler
275442c3981SWolfgang Gassler                // bad hack as DokuWiki deletes the file after this event
276442c3981SWolfgang Gassler                // thus, let's delete the file by ourselves, so git can recognize the deletion
277442c3981SWolfgang Gassler                // DokuWiki uses @unlink as well, so no error should be thrown if we delete it twice
278442c3981SWolfgang Gassler                @unlink($pagePath);
279442c3981SWolfgang Gassler            } else {
280442c3981SWolfgang Gassler                //get the commit text for edits
281d4e1c54bSWolfgang Gassler                $msgTemplate = $this->getConf('commitPageMsg');
282442c3981SWolfgang Gassler            }
283442c3981SWolfgang Gassler
284fa53f2a3SWolfgang Gassler            $message = str_replace(
285fa53f2a3SWolfgang Gassler                array('%page%', '%summary%', '%user%'),
286442c3981SWolfgang Gassler                array($pageName, $editSummary, $this->getAuthor()),
287442c3981SWolfgang Gassler                $msgTemplate
288fa53f2a3SWolfgang Gassler            );
289fa53f2a3SWolfgang Gassler
290442c3981SWolfgang Gassler            $this->commitFile($pagePath, $message);
291fa53f2a3SWolfgang Gassler        }
292e8224fc2SMarkus Hoffrogge    }
293fa53f2a3SWolfgang Gassler
294e8224fc2SMarkus Hoffrogge    // ====== Error notification helpers ======
295e8224fc2SMarkus Hoffrogge    /**
296e8224fc2SMarkus Hoffrogge     * Notifies error on create_new
297e8224fc2SMarkus Hoffrogge     *
298e8224fc2SMarkus Hoffrogge     * @access  public
299e8224fc2SMarkus Hoffrogge     * @param   string  repository path
300e8224fc2SMarkus Hoffrogge     * @param   string  reference path / remote reference
301e8224fc2SMarkus Hoffrogge     * @param   string  error message
302e8224fc2SMarkus Hoffrogge     * @return  bool
303e8224fc2SMarkus Hoffrogge     */
304*2762023dSMarkus Hoffrogge    public function notifyCreateNewError($repo_path, $reference, $error_message)
305*2762023dSMarkus Hoffrogge    {
306e8224fc2SMarkus Hoffrogge        $template_replacements = array(
307e8224fc2SMarkus Hoffrogge            'GIT_REPO_PATH' => $repo_path,
308e8224fc2SMarkus Hoffrogge            'GIT_REFERENCE' => (empty($reference) ? 'n/a' : $reference),
309e8224fc2SMarkus Hoffrogge            'GIT_ERROR_MESSAGE' => $error_message
310e8224fc2SMarkus Hoffrogge        );
311e8224fc2SMarkus Hoffrogge        return $this->notifyByMail('mail_create_new_error_subject', 'mail_create_new_error', $template_replacements);
312e8224fc2SMarkus Hoffrogge    }
313e8224fc2SMarkus Hoffrogge
314e8224fc2SMarkus Hoffrogge    /**
315e8224fc2SMarkus Hoffrogge     * Notifies error on setting repo path
316e8224fc2SMarkus Hoffrogge     *
317e8224fc2SMarkus Hoffrogge     * @access  public
318e8224fc2SMarkus Hoffrogge     * @param   string  repository path
319e8224fc2SMarkus Hoffrogge     * @param   string  error message
320e8224fc2SMarkus Hoffrogge     * @return  bool
321e8224fc2SMarkus Hoffrogge     */
322*2762023dSMarkus Hoffrogge    public function notifyRepoPathError($repo_path, $error_message)
323*2762023dSMarkus Hoffrogge    {
324e8224fc2SMarkus Hoffrogge        $template_replacements = array(
325e8224fc2SMarkus Hoffrogge            'GIT_REPO_PATH' => $repo_path,
326e8224fc2SMarkus Hoffrogge            'GIT_ERROR_MESSAGE' => $error_message
327e8224fc2SMarkus Hoffrogge        );
328e8224fc2SMarkus Hoffrogge        return $this->notifyByMail('mail_repo_path_error_subject', 'mail_repo_path_error', $template_replacements);
329e8224fc2SMarkus Hoffrogge    }
330e8224fc2SMarkus Hoffrogge
331e8224fc2SMarkus Hoffrogge    /**
332e8224fc2SMarkus Hoffrogge     * Notifies error on git command
333e8224fc2SMarkus Hoffrogge     *
334e8224fc2SMarkus Hoffrogge     * @access  public
335e8224fc2SMarkus Hoffrogge     * @param   string  repository path
336e8224fc2SMarkus Hoffrogge     * @param   string  current working dir
337e8224fc2SMarkus Hoffrogge     * @param   string  command line
338e8224fc2SMarkus Hoffrogge     * @param   int     exit code of command (status)
339e8224fc2SMarkus Hoffrogge     * @param   string  error message
340e8224fc2SMarkus Hoffrogge     * @return  bool
341e8224fc2SMarkus Hoffrogge     */
342*2762023dSMarkus Hoffrogge    public function notifyCommandError($repo_path, $cwd, $command, $status, $error_message)
343*2762023dSMarkus Hoffrogge    {
344e8224fc2SMarkus Hoffrogge        $template_replacements = array(
345e8224fc2SMarkus Hoffrogge            'GIT_REPO_PATH' => $repo_path,
346e8224fc2SMarkus Hoffrogge            'GIT_CWD' => $cwd,
347e8224fc2SMarkus Hoffrogge            'GIT_COMMAND' => $command,
348e8224fc2SMarkus Hoffrogge            'GIT_COMMAND_EXITCODE' => $status,
349e8224fc2SMarkus Hoffrogge            'GIT_ERROR_MESSAGE' => $error_message
350e8224fc2SMarkus Hoffrogge        );
351e8224fc2SMarkus Hoffrogge        return $this->notifyByMail('mail_command_error_subject', 'mail_command_error', $template_replacements);
352e8224fc2SMarkus Hoffrogge    }
353e8224fc2SMarkus Hoffrogge
354e8224fc2SMarkus Hoffrogge    /**
355e8224fc2SMarkus Hoffrogge     * Notifies success on git command
356e8224fc2SMarkus Hoffrogge     *
357e8224fc2SMarkus Hoffrogge     * @access  public
358e8224fc2SMarkus Hoffrogge     * @param   string  repository path
359e8224fc2SMarkus Hoffrogge     * @param   string  current working dir
360e8224fc2SMarkus Hoffrogge     * @param   string  command line
361e8224fc2SMarkus Hoffrogge     * @return  bool
362e8224fc2SMarkus Hoffrogge     */
363*2762023dSMarkus Hoffrogge    public function notifyCommandSuccess($repo_path, $cwd, $command)
364*2762023dSMarkus Hoffrogge    {
365e8224fc2SMarkus Hoffrogge        if (!$this->getConf('notifyByMailOnSuccess')) {
366e8224fc2SMarkus Hoffrogge            return false;
367e8224fc2SMarkus Hoffrogge        }
368e8224fc2SMarkus Hoffrogge        $template_replacements = array(
369e8224fc2SMarkus Hoffrogge            'GIT_REPO_PATH' => $repo_path,
370e8224fc2SMarkus Hoffrogge            'GIT_CWD' => $cwd,
371e8224fc2SMarkus Hoffrogge            'GIT_COMMAND' => $command
372e8224fc2SMarkus Hoffrogge        );
373e8224fc2SMarkus Hoffrogge        return $this->notifyByMail('mail_command_success_subject', 'mail_command_success', $template_replacements);
374e8224fc2SMarkus Hoffrogge    }
375e8224fc2SMarkus Hoffrogge
376e8224fc2SMarkus Hoffrogge    /**
377e8224fc2SMarkus Hoffrogge     * Send an eMail, if eMail address is configured
378e8224fc2SMarkus Hoffrogge     *
379e8224fc2SMarkus Hoffrogge     * @access  public
380e8224fc2SMarkus Hoffrogge     * @param   string  lang id for the subject
381e8224fc2SMarkus Hoffrogge     * @param   string  lang id for the template(.txt)
382e8224fc2SMarkus Hoffrogge     * @param   array   array of replacements
383e8224fc2SMarkus Hoffrogge     * @return  bool
384e8224fc2SMarkus Hoffrogge     */
385*2762023dSMarkus Hoffrogge    public function notifyByMail($subject_id, $template_id, $template_replacements)
386*2762023dSMarkus Hoffrogge    {
387e8224fc2SMarkus Hoffrogge        $ret = false;
388*2762023dSMarkus Hoffrogge        //dbglog("GitBacked - notifyByMail: [subject_id=" . $subject_id
389*2762023dSMarkus Hoffrogge        //    . ", template_id=" . $template_id
390*2762023dSMarkus Hoffrogge        //    . ", template_replacements=" . $template_replacements . "]");
391e8224fc2SMarkus Hoffrogge        if (!$this->isNotifyByEmailOnGitCommandError()) {
392e8224fc2SMarkus Hoffrogge            return $ret;
393e8224fc2SMarkus Hoffrogge        }
394e8224fc2SMarkus Hoffrogge        //$template_text = rawLocale($template_id); // this works for core artifacts only - not for plugins
395e8224fc2SMarkus Hoffrogge        $template_filename = $this->localFN($template_id);
396e8224fc2SMarkus Hoffrogge        $template_text = file_get_contents($template_filename);
397e8224fc2SMarkus Hoffrogge        $template_html = $this->render_text($template_text);
398e8224fc2SMarkus Hoffrogge
399e8224fc2SMarkus Hoffrogge        $mailer = new \Mailer();
400e8224fc2SMarkus Hoffrogge        $mailer->to($this->getEmailAddressOnErrorConfigured());
401dd477e30SMarkus Hoffrogge        //dbglog("GitBacked - lang check['".$subject_id."']: ".$this->getLang($subject_id));
402dd477e30SMarkus Hoffrogge        //dbglog("GitBacked - template text['".$template_id."']: ".$template_text);
403dd477e30SMarkus Hoffrogge        //dbglog("GitBacked - template html['".$template_id."']: ".$template_html);
404e8224fc2SMarkus Hoffrogge        $mailer->subject($this->getLang($subject_id));
405e8224fc2SMarkus Hoffrogge        $mailer->setBody($template_text, $template_replacements, null, $template_html);
406e8224fc2SMarkus Hoffrogge        $ret = $mailer->send();
407e8224fc2SMarkus Hoffrogge
408e8224fc2SMarkus Hoffrogge        return $ret;
409e8224fc2SMarkus Hoffrogge    }
410e8224fc2SMarkus Hoffrogge
411e8224fc2SMarkus Hoffrogge    /**
412e8224fc2SMarkus Hoffrogge     * Check, if eMail is to be sent on a Git command error.
413e8224fc2SMarkus Hoffrogge     *
414e8224fc2SMarkus Hoffrogge     * @access  public
415e8224fc2SMarkus Hoffrogge     * @return  bool
416e8224fc2SMarkus Hoffrogge     */
417*2762023dSMarkus Hoffrogge    public function isNotifyByEmailOnGitCommandError()
418*2762023dSMarkus Hoffrogge    {
419e8224fc2SMarkus Hoffrogge        $emailAddressOnError = $this->getEmailAddressOnErrorConfigured();
420e8224fc2SMarkus Hoffrogge        return !empty($emailAddressOnError);
421e8224fc2SMarkus Hoffrogge    }
422e8224fc2SMarkus Hoffrogge
423e8224fc2SMarkus Hoffrogge    /**
424e8224fc2SMarkus Hoffrogge     * Get the eMail address configured for notifications.
425e8224fc2SMarkus Hoffrogge     *
426e8224fc2SMarkus Hoffrogge     * @access  public
427e8224fc2SMarkus Hoffrogge     * @return  string
428e8224fc2SMarkus Hoffrogge     */
429*2762023dSMarkus Hoffrogge    public function getEmailAddressOnErrorConfigured()
430*2762023dSMarkus Hoffrogge    {
431e8224fc2SMarkus Hoffrogge        $emailAddressOnError = trim($this->getConf('emailAddressOnError'));
432e8224fc2SMarkus Hoffrogge        return $emailAddressOnError;
433fa53f2a3SWolfgang Gassler    }
434fa53f2a3SWolfgang Gassler}
435*2762023dSMarkus Hoffrogge// phpcs:enable Squiz.Classes.ValidClassName.NotCamelCaps
436*2762023dSMarkus Hoffrogge// phpcs:enable PSR1.Classes.ClassDeclaration.MissingNamespace
437fa53f2a3SWolfgang Gassler
438fa53f2a3SWolfgang Gassler// vim:ts=4:sw=4:et:
439