xref: /plugin/gitbacked/action/editcommit.php (revision 19993fa11ebf2600fe53b7d553d064aac942e1a2)
1fa53f2a3SWolfgang Gassler<?php
22762023dSMarkus 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
102762023dSMarkus 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
182762023dSMarkus Hoffroggerequire_once __DIR__ . '/../loader.php';
19fa53f2a3SWolfgang Gassler
202762023dSMarkus Hoffroggeuse dokuwiki\Extension\ActionPlugin;
212762023dSMarkus Hoffroggeuse dokuwiki\Extension\EventHandler;
222762023dSMarkus Hoffroggeuse dokuwiki\Extension\Event;
23aeb41f7dSMarkus Hoffroggeuse dokuwiki\Search\Indexer;
24fa53f2a3SWolfgang Gassler
252762023dSMarkus Hoffroggeuse woolfg\dokuwiki\plugin\gitbacked\Git;
262762023dSMarkus Hoffroggeuse woolfg\dokuwiki\plugin\gitbacked\GitRepo;
272762023dSMarkus Hoffroggeuse woolfg\dokuwiki\plugin\gitbacked\GitBackedUtil;
282762023dSMarkus Hoffrogge
292762023dSMarkus Hoffrogge// phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
302762023dSMarkus Hoffrogge// phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps
312762023dSMarkus Hoffroggeclass action_plugin_gitbacked_editcommit extends ActionPlugin
322762023dSMarkus Hoffrogge{
332762023dSMarkus Hoffrogge    /**
342762023dSMarkus Hoffrogge     * Temporary directory for this gitbacked plugin.
352762023dSMarkus Hoffrogge     *
362762023dSMarkus Hoffrogge     * @var string
372762023dSMarkus Hoffrogge     */
382762023dSMarkus Hoffrogge    private $temp_dir;
392762023dSMarkus Hoffrogge
402762023dSMarkus Hoffrogge    public function __construct()
412762023dSMarkus Hoffrogge    {
42eeb1a599SMarkus Hoffrogge        $this->temp_dir = GitBackedUtil::getTempDir();
4300ce3f12SDanny Lin    }
4400ce3f12SDanny Lin
452762023dSMarkus Hoffrogge    public function register(EventHandler $controller)
462762023dSMarkus Hoffrogge    {
472762023dSMarkus Hoffrogge        $controller->register_hook('IO_WIKIPAGE_WRITE', 'AFTER', $this, 'handleIOWikiPageWrite');
482762023dSMarkus Hoffrogge        $controller->register_hook('MEDIA_UPLOAD_FINISH', 'AFTER', $this, 'handleMediaUpload');
492762023dSMarkus Hoffrogge        $controller->register_hook('MEDIA_DELETE_FILE', 'AFTER', $this, 'handleMediaDeletion');
502762023dSMarkus Hoffrogge        $controller->register_hook('DOKUWIKI_DONE', 'AFTER', $this, 'handlePeriodicPull');
51442c3981SWolfgang Gassler    }
52442c3981SWolfgang Gassler
532762023dSMarkus Hoffrogge    private function initRepo()
542762023dSMarkus Hoffrogge    {
55442c3981SWolfgang Gassler        //get path to the repo root (by default DokuWiki's savedir)
56dee8dca1SMarkus Hoffrogge        $repoPath = GitBackedUtil::getEffectivePath($this->getConf('repoPath'));
57635161d0SCarsten Teibes        $gitPath = trim($this->getConf('gitPath'));
58635161d0SCarsten Teibes        if ($gitPath !== '') {
592762023dSMarkus Hoffrogge            Git::setBin($gitPath);
60635161d0SCarsten Teibes        }
61442c3981SWolfgang Gassler        //init the repo and create a new one if it is not present
624eba9b44SDanny Lin        io_mkdir_p($repoPath);
63e8224fc2SMarkus Hoffrogge        $repo = new GitRepo($repoPath, $this, true, true);
644eba9b44SDanny Lin        //set git working directory (by default DokuWiki's savedir)
65dee8dca1SMarkus Hoffrogge        $repoWorkDir = $this->getConf('repoWorkDir');
66dee8dca1SMarkus Hoffrogge        if (!empty($repoWorkDir)) {
67dee8dca1SMarkus Hoffrogge            $repoWorkDir = GitBackedUtil::getEffectivePath($repoWorkDir);
68dee8dca1SMarkus Hoffrogge        }
692762023dSMarkus Hoffrogge        Git::setBin(empty($repoWorkDir) ? Git::getBin()
702762023dSMarkus Hoffrogge            : Git::getBin() . ' --work-tree ' . escapeshellarg($repoWorkDir));
710d7cb616SBirkir A. Barkarson        $params = str_replace(
72c365e7dbSmhoffrog            ['%mail%', '%user%'],
73c365e7dbSmhoffrog            [$this->getAuthorMail(), $this->getAuthor()],
742762023dSMarkus Hoffrogge            $this->getConf('addParams')
752762023dSMarkus Hoffrogge        );
76442c3981SWolfgang Gassler        if ($params) {
772762023dSMarkus Hoffrogge            Git::setBin(Git::getBin() . ' ' . $params);
78442c3981SWolfgang Gassler        }
79b92b117aSWolfgang Gassler        return $repo;
80b92b117aSWolfgang Gassler    }
81b92b117aSWolfgang Gassler
822762023dSMarkus Hoffrogge    private function isIgnored($filePath)
832762023dSMarkus Hoffrogge    {
8466f21a70SWolfgang Gassler        $ignore = false;
8566f21a70SWolfgang Gassler        $ignorePaths = trim($this->getConf('ignorePaths'));
8666f21a70SWolfgang Gassler        if ($ignorePaths !== '') {
8766f21a70SWolfgang Gassler            $paths = explode(',', $ignorePaths);
8866f21a70SWolfgang Gassler            foreach ($paths as $path) {
8966f21a70SWolfgang Gassler                if (strstr($filePath, $path)) {
9066f21a70SWolfgang Gassler                    $ignore = true;
9166f21a70SWolfgang Gassler                }
9266f21a70SWolfgang Gassler            }
9366f21a70SWolfgang Gassler        }
9466f21a70SWolfgang Gassler        return $ignore;
9566f21a70SWolfgang Gassler    }
9666f21a70SWolfgang Gassler
972762023dSMarkus Hoffrogge    private function commitFile($filePath, $message)
982762023dSMarkus Hoffrogge    {
9966f21a70SWolfgang Gassler        if (!$this->isIgnored($filePath)) {
100e8224fc2SMarkus Hoffrogge            try {
101b92b117aSWolfgang Gassler                $repo = $this->initRepo();
102442c3981SWolfgang Gassler
103442c3981SWolfgang Gassler                //add the changed file and set the commit message
104442c3981SWolfgang Gassler                $repo->add($filePath);
105442c3981SWolfgang Gassler                $repo->commit($message);
106442c3981SWolfgang Gassler
107442c3981SWolfgang Gassler                //if the push after Commit option is set we push the active branch to origin
108442c3981SWolfgang Gassler                if ($this->getConf('pushAfterCommit')) {
1092762023dSMarkus Hoffrogge                    $repo->push('origin', $repo->activeBranch());
110442c3981SWolfgang Gassler                }
111e8224fc2SMarkus Hoffrogge            } catch (Exception $e) {
112e8224fc2SMarkus Hoffrogge                if (!$this->isNotifyByEmailOnGitCommandError()) {
113e8224fc2SMarkus Hoffrogge                    throw new Exception('Git committing or pushing failed: ' . $e->getMessage(), 1, $e);
11466f21a70SWolfgang Gassler                }
115e8224fc2SMarkus Hoffrogge                return;
116e8224fc2SMarkus Hoffrogge            }
117e8224fc2SMarkus Hoffrogge        }
118442c3981SWolfgang Gassler    }
119442c3981SWolfgang Gassler
120*19993fa1Sribsey    private function getUserInfo($key) {
121*19993fa1Sribsey        if (isset($GLOBALS['USERINFO'][$key])) {
122*19993fa1Sribsey            return $GLOBALS['USERINFO'][$key];
123*19993fa1Sribsey        }
124*19993fa1Sribsey        return '';
125442c3981SWolfgang Gassler    }
126442c3981SWolfgang Gassler
127*19993fa1Sribsey    private function getAuthor() {
128*19993fa1Sribsey        return $this->getUserInfo('name');
129*19993fa1Sribsey    }
130*19993fa1Sribsey
131*19993fa1Sribsey    private function getAuthorMail() {
132*19993fa1Sribsey        return $this->getUserInfo('mail');
1330d7cb616SBirkir A. Barkarson    }
1340d7cb616SBirkir A. Barkarson
1352762023dSMarkus Hoffrogge    private function computeLocalPath()
1362762023dSMarkus Hoffrogge    {
137a2effbcbSmsx80        global $conf;
138a2effbcbSmsx80        $repoPath = str_replace('\\', '/', realpath(GitBackedUtil::getEffectivePath($this->getConf('repoPath'))));
139a2effbcbSmsx80        $datadir = $conf['datadir']; // already normalized
140c365e7dbSmhoffrog        if (substr($datadir, 0, strlen($repoPath)) !== $repoPath) {
141a2effbcbSmsx80            throw new Exception('Datadir not inside repoPath ??');
142a2effbcbSmsx80        }
143a2effbcbSmsx80        return substr($datadir, strlen($repoPath) + 1);
144a2effbcbSmsx80    }
145a2effbcbSmsx80
1462762023dSMarkus Hoffrogge    private function updatePage($page)
1472762023dSMarkus Hoffrogge    {
148a2effbcbSmsx80
149c365e7dbSmhoffrog        if (is_callable(Indexer::class . '::getInstance')) {
150c365e7dbSmhoffrog            $Indexer = Indexer::getInstance();
151a2effbcbSmsx80            $success = $Indexer->addPage($page, false, false);
152a2effbcbSmsx80        } elseif (class_exists('Doku_Indexer')) {
153a2effbcbSmsx80            $success = idx_addPage($page, false, false);
154a2effbcbSmsx80        } else {
155a2effbcbSmsx80            // Failed to index the page. Your DokuWiki is older than release 2011-05-25 "Rincewind"
156a2effbcbSmsx80            $success = false;
157a2effbcbSmsx80        }
158a2effbcbSmsx80
159a2effbcbSmsx80        echo "Update $page: $success <br/>";
160a2effbcbSmsx80    }
161a2effbcbSmsx80
1622762023dSMarkus Hoffrogge    public function handlePeriodicPull(Event &$event, $param)
1632762023dSMarkus Hoffrogge    {
1642377428fSDanny Lin        if ($this->getConf('periodicPull')) {
165a2effbcbSmsx80            $enableIndexUpdate = $this->getConf('updateIndexOnPull');
1662377428fSDanny Lin            $lastPullFile = $this->temp_dir . '/lastpull.txt';
1672377428fSDanny Lin            //check if the lastPullFile exists
1682377428fSDanny Lin            if (is_file($lastPullFile)) {
1692377428fSDanny Lin                $lastPull = unserialize(file_get_contents($lastPullFile));
1702377428fSDanny Lin            } else {
1712377428fSDanny Lin                $lastPull = 0;
1722377428fSDanny Lin            }
1732377428fSDanny Lin            //calculate time between pulls in seconds
1742377428fSDanny Lin            $timeToWait = $this->getConf('periodicMinutes') * 60;
1752377428fSDanny Lin            $now = time();
1762377428fSDanny Lin
1772377428fSDanny Lin            //if it is time to run a pull request
1782377428fSDanny Lin            if ($lastPull + $timeToWait < $now) {
179e8224fc2SMarkus Hoffrogge                try {
1802377428fSDanny Lin                    $repo = $this->initRepo();
1812762023dSMarkus Hoffrogge                    if ($enableIndexUpdate) {
182a2effbcbSmsx80                        $localPath = $this->computeLocalPath();
183a2effbcbSmsx80
184a2effbcbSmsx80                        // store current revision id
185a2effbcbSmsx80                        $revBefore = $repo->run('rev-parse HEAD');
186a2effbcbSmsx80                    }
1872377428fSDanny Lin
1882377428fSDanny Lin                    //execute the pull request
1892762023dSMarkus Hoffrogge                    $repo->pull('origin', $repo->activeBranch());
190a2effbcbSmsx80
1912762023dSMarkus Hoffrogge                    if ($enableIndexUpdate) {
192a2effbcbSmsx80                        // store new revision id
193a2effbcbSmsx80                        $revAfter = $repo->run('rev-parse HEAD');
194a2effbcbSmsx80
1952762023dSMarkus Hoffrogge                        if (strcmp($revBefore, $revAfter) != 0) {
196a2effbcbSmsx80                            // if there were some changes, get the list of all changed files
197a2effbcbSmsx80                            $changedFilesPage = $repo->run('diff --name-only ' . $revBefore . ' ' . $revAfter);
198a2effbcbSmsx80                            $changedFiles = preg_split("/\r\n|\n|\r/", $changedFilesPage);
199a2effbcbSmsx80
2002762023dSMarkus Hoffrogge                            foreach ($changedFiles as $cf) {
201a2effbcbSmsx80                                // check if the file is inside localPath, that is, it's a page
2022762023dSMarkus Hoffrogge                                if (substr($cf, 0, strlen($localPath)) === $localPath) {
203a2effbcbSmsx80                                    // convert from relative filename to page name
204a2effbcbSmsx80                                    // for example: local/path/dir/subdir/test.txt -> dir:subdir:test
2052762023dSMarkus Hoffrogge                                    // -4 removes .txt
2062762023dSMarkus Hoffrogge                                    $page = str_replace('/', ':', substr($cf, strlen($localPath) + 1, -4));
207a2effbcbSmsx80
208a2effbcbSmsx80                                    // update the page
209a2effbcbSmsx80                                    $this->updatePage($page);
2102762023dSMarkus Hoffrogge                                } else {
211a2effbcbSmsx80                                    echo "Page NOT to update: $cf <br/>";
212a2effbcbSmsx80                                }
213a2effbcbSmsx80                            }
214a2effbcbSmsx80                        }
215a2effbcbSmsx80                    }
216e8224fc2SMarkus Hoffrogge                } catch (Exception $e) {
217e8224fc2SMarkus Hoffrogge                    if (!$this->isNotifyByEmailOnGitCommandError()) {
218e8224fc2SMarkus Hoffrogge                        throw new Exception('Git command failed to perform periodic pull: ' . $e->getMessage(), 2, $e);
219e8224fc2SMarkus Hoffrogge                    }
220e8224fc2SMarkus Hoffrogge                    return;
221e8224fc2SMarkus Hoffrogge                }
2222377428fSDanny Lin
2232377428fSDanny Lin                //save the current time to the file to track the last pull execution
2242377428fSDanny Lin                file_put_contents($lastPullFile, serialize(time()));
2252377428fSDanny Lin            }
2262377428fSDanny Lin        }
2272377428fSDanny Lin    }
2282377428fSDanny Lin
2292762023dSMarkus Hoffrogge    public function handleMediaDeletion(Event &$event, $param)
2302762023dSMarkus Hoffrogge    {
231442c3981SWolfgang Gassler        $mediaPath = $event->data['path'];
232442c3981SWolfgang Gassler        $mediaName = $event->data['name'];
233442c3981SWolfgang Gassler
234442c3981SWolfgang Gassler        $message = str_replace(
235c365e7dbSmhoffrog            ['%media%', '%user%'],
236c365e7dbSmhoffrog            [$mediaName, $this->getAuthor()],
237442c3981SWolfgang Gassler            $this->getConf('commitMediaMsgDel')
238442c3981SWolfgang Gassler        );
239442c3981SWolfgang Gassler
240442c3981SWolfgang Gassler        $this->commitFile($mediaPath, $message);
241442c3981SWolfgang Gassler    }
242442c3981SWolfgang Gassler
2432762023dSMarkus Hoffrogge    public function handleMediaUpload(Event &$event, $param)
2442762023dSMarkus Hoffrogge    {
245442c3981SWolfgang Gassler
246442c3981SWolfgang Gassler        $mediaPath = $event->data[1];
247442c3981SWolfgang Gassler        $mediaName = $event->data[2];
248442c3981SWolfgang Gassler
249442c3981SWolfgang Gassler        $message = str_replace(
250c365e7dbSmhoffrog            ['%media%', '%user%'],
251c365e7dbSmhoffrog            [$mediaName, $this->getAuthor()],
252442c3981SWolfgang Gassler            $this->getConf('commitMediaMsg')
253442c3981SWolfgang Gassler        );
254442c3981SWolfgang Gassler
255442c3981SWolfgang Gassler        $this->commitFile($mediaPath, $message);
256fa53f2a3SWolfgang Gassler    }
257fa53f2a3SWolfgang Gassler
2582762023dSMarkus Hoffrogge    public function handleIOWikiPageWrite(Event &$event, $param)
2592762023dSMarkus Hoffrogge    {
260fa53f2a3SWolfgang Gassler
261fa53f2a3SWolfgang Gassler        $rev = $event->data[3];
262fa53f2a3SWolfgang Gassler
263fa53f2a3SWolfgang Gassler        /* On update to an existing page this event is called twice,
264fa53f2a3SWolfgang Gassler         * once for the transfer of the old version to the attic (rev will have a value)
265fa53f2a3SWolfgang Gassler         * and once to write the new version of the page into the wiki (rev is false)
266fa53f2a3SWolfgang Gassler         */
267fa53f2a3SWolfgang Gassler        if (!$rev) {
268fa53f2a3SWolfgang Gassler            $pagePath = $event->data[0][0];
269fa53f2a3SWolfgang Gassler            $pageName = $event->data[2];
270442c3981SWolfgang Gassler            $pageContent = $event->data[0][1];
271fa53f2a3SWolfgang Gassler
2727af27dc9SWolfgang Gassler            // get the summary directly from the form input
273e7471cfaSDanny Lin            // as the metadata hasn't updated yet
2747af27dc9SWolfgang Gassler            $editSummary = $GLOBALS['INPUT']->str('summary');
275442c3981SWolfgang Gassler
276442c3981SWolfgang Gassler            // empty content indicates a page deletion
277442c3981SWolfgang Gassler            if ($pageContent == '') {
278442c3981SWolfgang Gassler                // get the commit text for deletions
279d4e1c54bSWolfgang Gassler                $msgTemplate = $this->getConf('commitPageMsgDel');
280442c3981SWolfgang Gassler
281442c3981SWolfgang Gassler                // bad hack as DokuWiki deletes the file after this event
282442c3981SWolfgang Gassler                // thus, let's delete the file by ourselves, so git can recognize the deletion
283442c3981SWolfgang Gassler                // DokuWiki uses @unlink as well, so no error should be thrown if we delete it twice
284442c3981SWolfgang Gassler                @unlink($pagePath);
285442c3981SWolfgang Gassler            } else {
286442c3981SWolfgang Gassler                //get the commit text for edits
287d4e1c54bSWolfgang Gassler                $msgTemplate = $this->getConf('commitPageMsg');
288442c3981SWolfgang Gassler            }
289442c3981SWolfgang Gassler
290fa53f2a3SWolfgang Gassler            $message = str_replace(
291c365e7dbSmhoffrog                ['%page%', '%summary%', '%user%'],
292c365e7dbSmhoffrog                [$pageName, $editSummary, $this->getAuthor()],
293442c3981SWolfgang Gassler                $msgTemplate
294fa53f2a3SWolfgang Gassler            );
295fa53f2a3SWolfgang Gassler
296442c3981SWolfgang Gassler            $this->commitFile($pagePath, $message);
297fa53f2a3SWolfgang Gassler        }
298e8224fc2SMarkus Hoffrogge    }
299fa53f2a3SWolfgang Gassler
300e8224fc2SMarkus Hoffrogge    // ====== Error notification helpers ======
301e8224fc2SMarkus Hoffrogge    /**
302e8224fc2SMarkus Hoffrogge     * Notifies error on create_new
303e8224fc2SMarkus Hoffrogge     *
304e8224fc2SMarkus Hoffrogge     * @access  public
305e8224fc2SMarkus Hoffrogge     * @param   string  repository path
306e8224fc2SMarkus Hoffrogge     * @param   string  reference path / remote reference
307e8224fc2SMarkus Hoffrogge     * @param   string  error message
308e8224fc2SMarkus Hoffrogge     * @return  bool
309e8224fc2SMarkus Hoffrogge     */
3102762023dSMarkus Hoffrogge    public function notifyCreateNewError($repo_path, $reference, $error_message)
3112762023dSMarkus Hoffrogge    {
312aeb41f7dSMarkus Hoffrogge        $template_replacements = [
313aeb41f7dSMarkus Hoffrogge            'GIT_REPO_PATH' => $repo_path,
314aeb41f7dSMarkus Hoffrogge            'GIT_REFERENCE' => (empty($reference) ? 'n/a' : $reference),
315aeb41f7dSMarkus Hoffrogge            'GIT_ERROR_MESSAGE' => $error_message
316aeb41f7dSMarkus Hoffrogge        ];
317e8224fc2SMarkus Hoffrogge        return $this->notifyByMail('mail_create_new_error_subject', 'mail_create_new_error', $template_replacements);
318e8224fc2SMarkus Hoffrogge    }
319e8224fc2SMarkus Hoffrogge
320e8224fc2SMarkus Hoffrogge    /**
321e8224fc2SMarkus Hoffrogge     * Notifies error on setting repo path
322e8224fc2SMarkus Hoffrogge     *
323e8224fc2SMarkus Hoffrogge     * @access  public
324e8224fc2SMarkus Hoffrogge     * @param   string  repository path
325e8224fc2SMarkus Hoffrogge     * @param   string  error message
326e8224fc2SMarkus Hoffrogge     * @return  bool
327e8224fc2SMarkus Hoffrogge     */
3282762023dSMarkus Hoffrogge    public function notifyRepoPathError($repo_path, $error_message)
3292762023dSMarkus Hoffrogge    {
330aeb41f7dSMarkus Hoffrogge        $template_replacements = [
331aeb41f7dSMarkus Hoffrogge            'GIT_REPO_PATH' => $repo_path,
332aeb41f7dSMarkus Hoffrogge            'GIT_ERROR_MESSAGE' => $error_message
333aeb41f7dSMarkus Hoffrogge        ];
334e8224fc2SMarkus Hoffrogge        return $this->notifyByMail('mail_repo_path_error_subject', 'mail_repo_path_error', $template_replacements);
335e8224fc2SMarkus Hoffrogge    }
336e8224fc2SMarkus Hoffrogge
337e8224fc2SMarkus Hoffrogge    /**
338e8224fc2SMarkus Hoffrogge     * Notifies error on git command
339e8224fc2SMarkus Hoffrogge     *
340e8224fc2SMarkus Hoffrogge     * @access  public
341e8224fc2SMarkus Hoffrogge     * @param   string  repository path
342e8224fc2SMarkus Hoffrogge     * @param   string  current working dir
343e8224fc2SMarkus Hoffrogge     * @param   string  command line
344e8224fc2SMarkus Hoffrogge     * @param   int     exit code of command (status)
345e8224fc2SMarkus Hoffrogge     * @param   string  error message
346e8224fc2SMarkus Hoffrogge     * @return  bool
347e8224fc2SMarkus Hoffrogge     */
3482762023dSMarkus Hoffrogge    public function notifyCommandError($repo_path, $cwd, $command, $status, $error_message)
3492762023dSMarkus Hoffrogge    {
350aeb41f7dSMarkus Hoffrogge        $template_replacements = [
351aeb41f7dSMarkus Hoffrogge            'GIT_REPO_PATH' => $repo_path,
352aeb41f7dSMarkus Hoffrogge            'GIT_CWD' => $cwd,
353aeb41f7dSMarkus Hoffrogge            'GIT_COMMAND' => $command,
354aeb41f7dSMarkus Hoffrogge            'GIT_COMMAND_EXITCODE' => $status,
355aeb41f7dSMarkus Hoffrogge            'GIT_ERROR_MESSAGE' => $error_message
356aeb41f7dSMarkus Hoffrogge        ];
357e8224fc2SMarkus Hoffrogge        return $this->notifyByMail('mail_command_error_subject', 'mail_command_error', $template_replacements);
358e8224fc2SMarkus Hoffrogge    }
359e8224fc2SMarkus Hoffrogge
360e8224fc2SMarkus Hoffrogge    /**
361e8224fc2SMarkus Hoffrogge     * Notifies success on git command
362e8224fc2SMarkus Hoffrogge     *
363e8224fc2SMarkus Hoffrogge     * @access  public
364e8224fc2SMarkus Hoffrogge     * @param   string  repository path
365e8224fc2SMarkus Hoffrogge     * @param   string  current working dir
366e8224fc2SMarkus Hoffrogge     * @param   string  command line
367e8224fc2SMarkus Hoffrogge     * @return  bool
368e8224fc2SMarkus Hoffrogge     */
3692762023dSMarkus Hoffrogge    public function notifyCommandSuccess($repo_path, $cwd, $command)
3702762023dSMarkus Hoffrogge    {
371e8224fc2SMarkus Hoffrogge        if (!$this->getConf('notifyByMailOnSuccess')) {
372e8224fc2SMarkus Hoffrogge            return false;
373e8224fc2SMarkus Hoffrogge        }
374aeb41f7dSMarkus Hoffrogge        $template_replacements = [
375aeb41f7dSMarkus Hoffrogge            'GIT_REPO_PATH' => $repo_path,
376aeb41f7dSMarkus Hoffrogge            'GIT_CWD' => $cwd,
377aeb41f7dSMarkus Hoffrogge            'GIT_COMMAND' => $command
378aeb41f7dSMarkus Hoffrogge        ];
379e8224fc2SMarkus Hoffrogge        return $this->notifyByMail('mail_command_success_subject', 'mail_command_success', $template_replacements);
380e8224fc2SMarkus Hoffrogge    }
381e8224fc2SMarkus Hoffrogge
382e8224fc2SMarkus Hoffrogge    /**
383e8224fc2SMarkus Hoffrogge     * Send an eMail, if eMail address is configured
384e8224fc2SMarkus Hoffrogge     *
385e8224fc2SMarkus Hoffrogge     * @access  public
386e8224fc2SMarkus Hoffrogge     * @param   string  lang id for the subject
387e8224fc2SMarkus Hoffrogge     * @param   string  lang id for the template(.txt)
388e8224fc2SMarkus Hoffrogge     * @param   array   array of replacements
389e8224fc2SMarkus Hoffrogge     * @return  bool
390e8224fc2SMarkus Hoffrogge     */
3912762023dSMarkus Hoffrogge    public function notifyByMail($subject_id, $template_id, $template_replacements)
3922762023dSMarkus Hoffrogge    {
393e8224fc2SMarkus Hoffrogge        $ret = false;
3942762023dSMarkus Hoffrogge        //dbglog("GitBacked - notifyByMail: [subject_id=" . $subject_id
3952762023dSMarkus Hoffrogge        //    . ", template_id=" . $template_id
3962762023dSMarkus Hoffrogge        //    . ", template_replacements=" . $template_replacements . "]");
397e8224fc2SMarkus Hoffrogge        if (!$this->isNotifyByEmailOnGitCommandError()) {
398e8224fc2SMarkus Hoffrogge            return $ret;
399e8224fc2SMarkus Hoffrogge        }
400e8224fc2SMarkus Hoffrogge        //$template_text = rawLocale($template_id); // this works for core artifacts only - not for plugins
401e8224fc2SMarkus Hoffrogge        $template_filename = $this->localFN($template_id);
402e8224fc2SMarkus Hoffrogge        $template_text = file_get_contents($template_filename);
403e8224fc2SMarkus Hoffrogge        $template_html = $this->render_text($template_text);
404e8224fc2SMarkus Hoffrogge
405e8224fc2SMarkus Hoffrogge        $mailer = new \Mailer();
406e8224fc2SMarkus Hoffrogge        $mailer->to($this->getEmailAddressOnErrorConfigured());
407dd477e30SMarkus Hoffrogge        //dbglog("GitBacked - lang check['".$subject_id."']: ".$this->getLang($subject_id));
408dd477e30SMarkus Hoffrogge        //dbglog("GitBacked - template text['".$template_id."']: ".$template_text);
409dd477e30SMarkus Hoffrogge        //dbglog("GitBacked - template html['".$template_id."']: ".$template_html);
410e8224fc2SMarkus Hoffrogge        $mailer->subject($this->getLang($subject_id));
411e8224fc2SMarkus Hoffrogge        $mailer->setBody($template_text, $template_replacements, null, $template_html);
412c365e7dbSmhoffrog
413e8224fc2SMarkus Hoffrogge        $ret = $mailer->send();
414e8224fc2SMarkus Hoffrogge
415e8224fc2SMarkus Hoffrogge        return $ret;
416e8224fc2SMarkus Hoffrogge    }
417e8224fc2SMarkus Hoffrogge
418e8224fc2SMarkus Hoffrogge    /**
419e8224fc2SMarkus Hoffrogge     * Check, if eMail is to be sent on a Git command error.
420e8224fc2SMarkus Hoffrogge     *
421e8224fc2SMarkus Hoffrogge     * @access  public
422e8224fc2SMarkus Hoffrogge     * @return  bool
423e8224fc2SMarkus Hoffrogge     */
4242762023dSMarkus Hoffrogge    public function isNotifyByEmailOnGitCommandError()
4252762023dSMarkus Hoffrogge    {
426e8224fc2SMarkus Hoffrogge        $emailAddressOnError = $this->getEmailAddressOnErrorConfigured();
427e8224fc2SMarkus Hoffrogge        return !empty($emailAddressOnError);
428e8224fc2SMarkus Hoffrogge    }
429e8224fc2SMarkus Hoffrogge
430e8224fc2SMarkus Hoffrogge    /**
431e8224fc2SMarkus Hoffrogge     * Get the eMail address configured for notifications.
432e8224fc2SMarkus Hoffrogge     *
433e8224fc2SMarkus Hoffrogge     * @access  public
434e8224fc2SMarkus Hoffrogge     * @return  string
435e8224fc2SMarkus Hoffrogge     */
4362762023dSMarkus Hoffrogge    public function getEmailAddressOnErrorConfigured()
4372762023dSMarkus Hoffrogge    {
438e8224fc2SMarkus Hoffrogge        $emailAddressOnError = trim($this->getConf('emailAddressOnError'));
439e8224fc2SMarkus Hoffrogge        return $emailAddressOnError;
440fa53f2a3SWolfgang Gassler    }
441fa53f2a3SWolfgang Gassler}
4422762023dSMarkus Hoffrogge// phpcs:enable Squiz.Classes.ValidClassName.NotCamelCaps
4432762023dSMarkus Hoffrogge// phpcs:enable PSR1.Classes.ClassDeclaration.MissingNamespace
444fa53f2a3SWolfgang Gassler
445fa53f2a3SWolfgang Gassler// vim:ts=4:sw=4:et:
446