1fa53f2a3SWolfgang Gassler<?php 22762023dSMarkus Hoffrogge 3*c365e7dbSmhoffroguse dokuwiki\Search\Indexer; 4*c365e7dbSmhoffrog 5fa53f2a3SWolfgang Gassler/** 6fa53f2a3SWolfgang Gassler * DokuWiki Plugin gitbacked (Action Component) 7fa53f2a3SWolfgang Gassler * 8fa53f2a3SWolfgang Gassler * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 9fa53f2a3SWolfgang Gassler * @author Wolfgang Gassler <wolfgang@gassler.org> 10fa53f2a3SWolfgang Gassler */ 11fa53f2a3SWolfgang Gassler 122762023dSMarkus Hoffrogge// phpcs:disable PSR1.Files.SideEffects.FoundWithSymbols 13fa53f2a3SWolfgang Gassler// must be run within Dokuwiki 14fa53f2a3SWolfgang Gasslerif (!defined('DOKU_INC')) die(); 15fa53f2a3SWolfgang Gassler 16fa53f2a3SWolfgang Gasslerif (!defined('DOKU_LF')) define('DOKU_LF', "\n"); 17fa53f2a3SWolfgang Gasslerif (!defined('DOKU_TAB')) define('DOKU_TAB', "\t"); 18fa53f2a3SWolfgang Gasslerif (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/'); 19fa53f2a3SWolfgang Gassler 202762023dSMarkus Hoffroggerequire_once __DIR__ . '/../loader.php'; 21fa53f2a3SWolfgang Gassler 222762023dSMarkus Hoffroggeuse dokuwiki\Extension\ActionPlugin; 232762023dSMarkus Hoffroggeuse dokuwiki\Extension\EventHandler; 242762023dSMarkus Hoffroggeuse dokuwiki\Extension\Event; 25fa53f2a3SWolfgang Gassler 262762023dSMarkus Hoffroggeuse woolfg\dokuwiki\plugin\gitbacked\Git; 272762023dSMarkus Hoffroggeuse woolfg\dokuwiki\plugin\gitbacked\GitRepo; 282762023dSMarkus Hoffroggeuse woolfg\dokuwiki\plugin\gitbacked\GitBackedUtil; 292762023dSMarkus Hoffrogge 302762023dSMarkus Hoffrogge// phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace 312762023dSMarkus Hoffrogge// phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps 322762023dSMarkus Hoffroggeclass action_plugin_gitbacked_editcommit extends ActionPlugin 332762023dSMarkus Hoffrogge{ 342762023dSMarkus Hoffrogge /** 352762023dSMarkus Hoffrogge * Temporary directory for this gitbacked plugin. 362762023dSMarkus Hoffrogge * 372762023dSMarkus Hoffrogge * @var string 382762023dSMarkus Hoffrogge */ 392762023dSMarkus Hoffrogge private $temp_dir; 402762023dSMarkus Hoffrogge 412762023dSMarkus Hoffrogge public function __construct() 422762023dSMarkus Hoffrogge { 43eeb1a599SMarkus Hoffrogge $this->temp_dir = GitBackedUtil::getTempDir(); 4400ce3f12SDanny Lin } 4500ce3f12SDanny Lin 462762023dSMarkus Hoffrogge public function register(EventHandler $controller) 472762023dSMarkus Hoffrogge { 482762023dSMarkus Hoffrogge $controller->register_hook('IO_WIKIPAGE_WRITE', 'AFTER', $this, 'handleIOWikiPageWrite'); 492762023dSMarkus Hoffrogge $controller->register_hook('MEDIA_UPLOAD_FINISH', 'AFTER', $this, 'handleMediaUpload'); 502762023dSMarkus Hoffrogge $controller->register_hook('MEDIA_DELETE_FILE', 'AFTER', $this, 'handleMediaDeletion'); 512762023dSMarkus Hoffrogge $controller->register_hook('DOKUWIKI_DONE', 'AFTER', $this, 'handlePeriodicPull'); 52442c3981SWolfgang Gassler } 53442c3981SWolfgang Gassler 542762023dSMarkus Hoffrogge private function initRepo() 552762023dSMarkus Hoffrogge { 56442c3981SWolfgang Gassler //get path to the repo root (by default DokuWiki's savedir) 57dee8dca1SMarkus Hoffrogge $repoPath = GitBackedUtil::getEffectivePath($this->getConf('repoPath')); 58635161d0SCarsten Teibes $gitPath = trim($this->getConf('gitPath')); 59635161d0SCarsten Teibes if ($gitPath !== '') { 602762023dSMarkus Hoffrogge Git::setBin($gitPath); 61635161d0SCarsten Teibes } 62442c3981SWolfgang Gassler //init the repo and create a new one if it is not present 634eba9b44SDanny Lin io_mkdir_p($repoPath); 64e8224fc2SMarkus Hoffrogge $repo = new GitRepo($repoPath, $this, true, true); 654eba9b44SDanny Lin //set git working directory (by default DokuWiki's savedir) 66dee8dca1SMarkus Hoffrogge $repoWorkDir = $this->getConf('repoWorkDir'); 67dee8dca1SMarkus Hoffrogge if (!empty($repoWorkDir)) { 68dee8dca1SMarkus Hoffrogge $repoWorkDir = GitBackedUtil::getEffectivePath($repoWorkDir); 69dee8dca1SMarkus Hoffrogge } 702762023dSMarkus Hoffrogge Git::setBin(empty($repoWorkDir) ? Git::getBin() 712762023dSMarkus Hoffrogge : Git::getBin() . ' --work-tree ' . escapeshellarg($repoWorkDir)); 720d7cb616SBirkir A. Barkarson $params = str_replace( 73*c365e7dbSmhoffrog ['%mail%', '%user%'], 74*c365e7dbSmhoffrog [$this->getAuthorMail(), $this->getAuthor()], 752762023dSMarkus Hoffrogge $this->getConf('addParams') 762762023dSMarkus Hoffrogge ); 77442c3981SWolfgang Gassler if ($params) { 782762023dSMarkus Hoffrogge Git::setBin(Git::getBin() . ' ' . $params); 79442c3981SWolfgang Gassler } 80b92b117aSWolfgang Gassler return $repo; 81b92b117aSWolfgang Gassler } 82b92b117aSWolfgang Gassler 832762023dSMarkus Hoffrogge private function isIgnored($filePath) 842762023dSMarkus Hoffrogge { 8566f21a70SWolfgang Gassler $ignore = false; 8666f21a70SWolfgang Gassler $ignorePaths = trim($this->getConf('ignorePaths')); 8766f21a70SWolfgang Gassler if ($ignorePaths !== '') { 8866f21a70SWolfgang Gassler $paths = explode(',', $ignorePaths); 8966f21a70SWolfgang Gassler foreach ($paths as $path) { 9066f21a70SWolfgang Gassler if (strstr($filePath, $path)) { 9166f21a70SWolfgang Gassler $ignore = true; 9266f21a70SWolfgang Gassler } 9366f21a70SWolfgang Gassler } 9466f21a70SWolfgang Gassler } 9566f21a70SWolfgang Gassler return $ignore; 9666f21a70SWolfgang Gassler } 9766f21a70SWolfgang Gassler 982762023dSMarkus Hoffrogge private function commitFile($filePath, $message) 992762023dSMarkus Hoffrogge { 10066f21a70SWolfgang Gassler if (!$this->isIgnored($filePath)) { 101e8224fc2SMarkus Hoffrogge try { 102b92b117aSWolfgang Gassler $repo = $this->initRepo(); 103442c3981SWolfgang Gassler 104442c3981SWolfgang Gassler //add the changed file and set the commit message 105442c3981SWolfgang Gassler $repo->add($filePath); 106442c3981SWolfgang Gassler $repo->commit($message); 107442c3981SWolfgang Gassler 108442c3981SWolfgang Gassler //if the push after Commit option is set we push the active branch to origin 109442c3981SWolfgang Gassler if ($this->getConf('pushAfterCommit')) { 1102762023dSMarkus Hoffrogge $repo->push('origin', $repo->activeBranch()); 111442c3981SWolfgang Gassler } 112e8224fc2SMarkus Hoffrogge } catch (Exception $e) { 113e8224fc2SMarkus Hoffrogge if (!$this->isNotifyByEmailOnGitCommandError()) { 114e8224fc2SMarkus Hoffrogge throw new Exception('Git committing or pushing failed: ' . $e->getMessage(), 1, $e); 11566f21a70SWolfgang Gassler } 116e8224fc2SMarkus Hoffrogge return; 117e8224fc2SMarkus Hoffrogge } 118e8224fc2SMarkus Hoffrogge } 119442c3981SWolfgang Gassler } 120442c3981SWolfgang Gassler 1212762023dSMarkus Hoffrogge private function getAuthor() 1222762023dSMarkus Hoffrogge { 123442c3981SWolfgang Gassler return $GLOBALS['USERINFO']['name']; 124442c3981SWolfgang Gassler } 125442c3981SWolfgang Gassler 1262762023dSMarkus Hoffrogge private function getAuthorMail() 1272762023dSMarkus Hoffrogge { 1280d7cb616SBirkir A. Barkarson return $GLOBALS['USERINFO']['mail']; 1290d7cb616SBirkir A. Barkarson } 1300d7cb616SBirkir A. Barkarson 1312762023dSMarkus Hoffrogge private function computeLocalPath() 1322762023dSMarkus Hoffrogge { 133a2effbcbSmsx80 global $conf; 134a2effbcbSmsx80 $repoPath = str_replace('\\', '/', realpath(GitBackedUtil::getEffectivePath($this->getConf('repoPath')))); 135a2effbcbSmsx80 $datadir = $conf['datadir']; // already normalized 136*c365e7dbSmhoffrog if (substr($datadir, 0, strlen($repoPath)) !== $repoPath) { 137a2effbcbSmsx80 throw new Exception('Datadir not inside repoPath ??'); 138a2effbcbSmsx80 } 139a2effbcbSmsx80 return substr($datadir, strlen($repoPath) + 1); 140a2effbcbSmsx80 } 141a2effbcbSmsx80 1422762023dSMarkus Hoffrogge private function updatePage($page) 1432762023dSMarkus Hoffrogge { 144a2effbcbSmsx80 145*c365e7dbSmhoffrog if (is_callable(Indexer::class . '::getInstance')) { 146*c365e7dbSmhoffrog $Indexer = Indexer::getInstance(); 147a2effbcbSmsx80 $success = $Indexer->addPage($page, false, false); 148a2effbcbSmsx80 } elseif (class_exists('Doku_Indexer')) { 149a2effbcbSmsx80 $success = idx_addPage($page, false, false); 150a2effbcbSmsx80 } else { 151a2effbcbSmsx80 // Failed to index the page. Your DokuWiki is older than release 2011-05-25 "Rincewind" 152a2effbcbSmsx80 $success = false; 153a2effbcbSmsx80 } 154a2effbcbSmsx80 155a2effbcbSmsx80 echo "Update $page: $success <br/>"; 156a2effbcbSmsx80 } 157a2effbcbSmsx80 1582762023dSMarkus Hoffrogge public function handlePeriodicPull(Event &$event, $param) 1592762023dSMarkus Hoffrogge { 1602377428fSDanny Lin if ($this->getConf('periodicPull')) { 161a2effbcbSmsx80 $enableIndexUpdate = $this->getConf('updateIndexOnPull'); 1622377428fSDanny Lin $lastPullFile = $this->temp_dir . '/lastpull.txt'; 1632377428fSDanny Lin //check if the lastPullFile exists 1642377428fSDanny Lin if (is_file($lastPullFile)) { 1652377428fSDanny Lin $lastPull = unserialize(file_get_contents($lastPullFile)); 1662377428fSDanny Lin } else { 1672377428fSDanny Lin $lastPull = 0; 1682377428fSDanny Lin } 1692377428fSDanny Lin //calculate time between pulls in seconds 1702377428fSDanny Lin $timeToWait = $this->getConf('periodicMinutes') * 60; 1712377428fSDanny Lin $now = time(); 1722377428fSDanny Lin 1732377428fSDanny Lin //if it is time to run a pull request 1742377428fSDanny Lin if ($lastPull + $timeToWait < $now) { 175e8224fc2SMarkus Hoffrogge try { 1762377428fSDanny Lin $repo = $this->initRepo(); 1772762023dSMarkus Hoffrogge if ($enableIndexUpdate) { 178a2effbcbSmsx80 $localPath = $this->computeLocalPath(); 179a2effbcbSmsx80 180a2effbcbSmsx80 // store current revision id 181a2effbcbSmsx80 $revBefore = $repo->run('rev-parse HEAD'); 182a2effbcbSmsx80 } 1832377428fSDanny Lin 1842377428fSDanny Lin //execute the pull request 1852762023dSMarkus Hoffrogge $repo->pull('origin', $repo->activeBranch()); 186a2effbcbSmsx80 1872762023dSMarkus Hoffrogge if ($enableIndexUpdate) { 188a2effbcbSmsx80 // store new revision id 189a2effbcbSmsx80 $revAfter = $repo->run('rev-parse HEAD'); 190a2effbcbSmsx80 1912762023dSMarkus Hoffrogge if (strcmp($revBefore, $revAfter) != 0) { 192a2effbcbSmsx80 // if there were some changes, get the list of all changed files 193a2effbcbSmsx80 $changedFilesPage = $repo->run('diff --name-only ' . $revBefore . ' ' . $revAfter); 194a2effbcbSmsx80 $changedFiles = preg_split("/\r\n|\n|\r/", $changedFilesPage); 195a2effbcbSmsx80 1962762023dSMarkus Hoffrogge foreach ($changedFiles as $cf) { 197a2effbcbSmsx80 // check if the file is inside localPath, that is, it's a page 1982762023dSMarkus Hoffrogge if (substr($cf, 0, strlen($localPath)) === $localPath) { 199a2effbcbSmsx80 // convert from relative filename to page name 200a2effbcbSmsx80 // for example: local/path/dir/subdir/test.txt -> dir:subdir:test 2012762023dSMarkus Hoffrogge // -4 removes .txt 2022762023dSMarkus Hoffrogge $page = str_replace('/', ':', substr($cf, strlen($localPath) + 1, -4)); 203a2effbcbSmsx80 204a2effbcbSmsx80 // update the page 205a2effbcbSmsx80 $this->updatePage($page); 2062762023dSMarkus Hoffrogge } else { 207a2effbcbSmsx80 echo "Page NOT to update: $cf <br/>"; 208a2effbcbSmsx80 } 209a2effbcbSmsx80 } 210a2effbcbSmsx80 } 211a2effbcbSmsx80 } 212e8224fc2SMarkus Hoffrogge } catch (Exception $e) { 213e8224fc2SMarkus Hoffrogge if (!$this->isNotifyByEmailOnGitCommandError()) { 214e8224fc2SMarkus Hoffrogge throw new Exception('Git command failed to perform periodic pull: ' . $e->getMessage(), 2, $e); 215e8224fc2SMarkus Hoffrogge } 216e8224fc2SMarkus Hoffrogge return; 217e8224fc2SMarkus Hoffrogge } 2182377428fSDanny Lin 2192377428fSDanny Lin //save the current time to the file to track the last pull execution 2202377428fSDanny Lin file_put_contents($lastPullFile, serialize(time())); 2212377428fSDanny Lin } 2222377428fSDanny Lin } 2232377428fSDanny Lin } 2242377428fSDanny Lin 2252762023dSMarkus Hoffrogge public function handleMediaDeletion(Event &$event, $param) 2262762023dSMarkus Hoffrogge { 227442c3981SWolfgang Gassler $mediaPath = $event->data['path']; 228442c3981SWolfgang Gassler $mediaName = $event->data['name']; 229442c3981SWolfgang Gassler 230442c3981SWolfgang Gassler $message = str_replace( 231*c365e7dbSmhoffrog ['%media%', '%user%'], 232*c365e7dbSmhoffrog [$mediaName, $this->getAuthor()], 233442c3981SWolfgang Gassler $this->getConf('commitMediaMsgDel') 234442c3981SWolfgang Gassler ); 235442c3981SWolfgang Gassler 236442c3981SWolfgang Gassler $this->commitFile($mediaPath, $message); 237442c3981SWolfgang Gassler } 238442c3981SWolfgang Gassler 2392762023dSMarkus Hoffrogge public function handleMediaUpload(Event &$event, $param) 2402762023dSMarkus Hoffrogge { 241442c3981SWolfgang Gassler 242442c3981SWolfgang Gassler $mediaPath = $event->data[1]; 243442c3981SWolfgang Gassler $mediaName = $event->data[2]; 244442c3981SWolfgang Gassler 245442c3981SWolfgang Gassler $message = str_replace( 246*c365e7dbSmhoffrog ['%media%', '%user%'], 247*c365e7dbSmhoffrog [$mediaName, $this->getAuthor()], 248442c3981SWolfgang Gassler $this->getConf('commitMediaMsg') 249442c3981SWolfgang Gassler ); 250442c3981SWolfgang Gassler 251442c3981SWolfgang Gassler $this->commitFile($mediaPath, $message); 252fa53f2a3SWolfgang Gassler } 253fa53f2a3SWolfgang Gassler 2542762023dSMarkus Hoffrogge public function handleIOWikiPageWrite(Event &$event, $param) 2552762023dSMarkus Hoffrogge { 256fa53f2a3SWolfgang Gassler 257fa53f2a3SWolfgang Gassler $rev = $event->data[3]; 258fa53f2a3SWolfgang Gassler 259fa53f2a3SWolfgang Gassler /* On update to an existing page this event is called twice, 260fa53f2a3SWolfgang Gassler * once for the transfer of the old version to the attic (rev will have a value) 261fa53f2a3SWolfgang Gassler * and once to write the new version of the page into the wiki (rev is false) 262fa53f2a3SWolfgang Gassler */ 263fa53f2a3SWolfgang Gassler if (!$rev) { 264fa53f2a3SWolfgang Gassler $pagePath = $event->data[0][0]; 265fa53f2a3SWolfgang Gassler $pageName = $event->data[2]; 266442c3981SWolfgang Gassler $pageContent = $event->data[0][1]; 267fa53f2a3SWolfgang Gassler 2687af27dc9SWolfgang Gassler // get the summary directly from the form input 269e7471cfaSDanny Lin // as the metadata hasn't updated yet 2707af27dc9SWolfgang Gassler $editSummary = $GLOBALS['INPUT']->str('summary'); 271442c3981SWolfgang Gassler 272442c3981SWolfgang Gassler // empty content indicates a page deletion 273442c3981SWolfgang Gassler if ($pageContent == '') { 274442c3981SWolfgang Gassler // get the commit text for deletions 275d4e1c54bSWolfgang Gassler $msgTemplate = $this->getConf('commitPageMsgDel'); 276442c3981SWolfgang Gassler 277442c3981SWolfgang Gassler // bad hack as DokuWiki deletes the file after this event 278442c3981SWolfgang Gassler // thus, let's delete the file by ourselves, so git can recognize the deletion 279442c3981SWolfgang Gassler // DokuWiki uses @unlink as well, so no error should be thrown if we delete it twice 280442c3981SWolfgang Gassler @unlink($pagePath); 281442c3981SWolfgang Gassler } else { 282442c3981SWolfgang Gassler //get the commit text for edits 283d4e1c54bSWolfgang Gassler $msgTemplate = $this->getConf('commitPageMsg'); 284442c3981SWolfgang Gassler } 285442c3981SWolfgang Gassler 286fa53f2a3SWolfgang Gassler $message = str_replace( 287*c365e7dbSmhoffrog ['%page%', '%summary%', '%user%'], 288*c365e7dbSmhoffrog [$pageName, $editSummary, $this->getAuthor()], 289442c3981SWolfgang Gassler $msgTemplate 290fa53f2a3SWolfgang Gassler ); 291fa53f2a3SWolfgang Gassler 292442c3981SWolfgang Gassler $this->commitFile($pagePath, $message); 293fa53f2a3SWolfgang Gassler } 294e8224fc2SMarkus Hoffrogge } 295fa53f2a3SWolfgang Gassler 296e8224fc2SMarkus Hoffrogge // ====== Error notification helpers ====== 297e8224fc2SMarkus Hoffrogge /** 298e8224fc2SMarkus Hoffrogge * Notifies error on create_new 299e8224fc2SMarkus Hoffrogge * 300e8224fc2SMarkus Hoffrogge * @access public 301e8224fc2SMarkus Hoffrogge * @param string repository path 302e8224fc2SMarkus Hoffrogge * @param string reference path / remote reference 303e8224fc2SMarkus Hoffrogge * @param string error message 304e8224fc2SMarkus Hoffrogge * @return bool 305e8224fc2SMarkus Hoffrogge */ 3062762023dSMarkus Hoffrogge public function notifyCreateNewError($repo_path, $reference, $error_message) 3072762023dSMarkus Hoffrogge { 308*c365e7dbSmhoffrog $template_replacements = ['GIT_REPO_PATH' => $repo_path, 'GIT_REFERENCE' => (empty($reference) ? 'n/a' : $reference), 'GIT_ERROR_MESSAGE' => $error_message]; 309e8224fc2SMarkus Hoffrogge return $this->notifyByMail('mail_create_new_error_subject', 'mail_create_new_error', $template_replacements); 310e8224fc2SMarkus Hoffrogge } 311e8224fc2SMarkus Hoffrogge 312e8224fc2SMarkus Hoffrogge /** 313e8224fc2SMarkus Hoffrogge * Notifies error on setting repo path 314e8224fc2SMarkus Hoffrogge * 315e8224fc2SMarkus Hoffrogge * @access public 316e8224fc2SMarkus Hoffrogge * @param string repository path 317e8224fc2SMarkus Hoffrogge * @param string error message 318e8224fc2SMarkus Hoffrogge * @return bool 319e8224fc2SMarkus Hoffrogge */ 3202762023dSMarkus Hoffrogge public function notifyRepoPathError($repo_path, $error_message) 3212762023dSMarkus Hoffrogge { 322*c365e7dbSmhoffrog $template_replacements = ['GIT_REPO_PATH' => $repo_path, 'GIT_ERROR_MESSAGE' => $error_message]; 323e8224fc2SMarkus Hoffrogge return $this->notifyByMail('mail_repo_path_error_subject', 'mail_repo_path_error', $template_replacements); 324e8224fc2SMarkus Hoffrogge } 325e8224fc2SMarkus Hoffrogge 326e8224fc2SMarkus Hoffrogge /** 327e8224fc2SMarkus Hoffrogge * Notifies error on git command 328e8224fc2SMarkus Hoffrogge * 329e8224fc2SMarkus Hoffrogge * @access public 330e8224fc2SMarkus Hoffrogge * @param string repository path 331e8224fc2SMarkus Hoffrogge * @param string current working dir 332e8224fc2SMarkus Hoffrogge * @param string command line 333e8224fc2SMarkus Hoffrogge * @param int exit code of command (status) 334e8224fc2SMarkus Hoffrogge * @param string error message 335e8224fc2SMarkus Hoffrogge * @return bool 336e8224fc2SMarkus Hoffrogge */ 3372762023dSMarkus Hoffrogge public function notifyCommandError($repo_path, $cwd, $command, $status, $error_message) 3382762023dSMarkus Hoffrogge { 339*c365e7dbSmhoffrog $template_replacements = ['GIT_REPO_PATH' => $repo_path, 'GIT_CWD' => $cwd, 'GIT_COMMAND' => $command, 'GIT_COMMAND_EXITCODE' => $status, 'GIT_ERROR_MESSAGE' => $error_message]; 340e8224fc2SMarkus Hoffrogge return $this->notifyByMail('mail_command_error_subject', 'mail_command_error', $template_replacements); 341e8224fc2SMarkus Hoffrogge } 342e8224fc2SMarkus Hoffrogge 343e8224fc2SMarkus Hoffrogge /** 344e8224fc2SMarkus Hoffrogge * Notifies success on git command 345e8224fc2SMarkus Hoffrogge * 346e8224fc2SMarkus Hoffrogge * @access public 347e8224fc2SMarkus Hoffrogge * @param string repository path 348e8224fc2SMarkus Hoffrogge * @param string current working dir 349e8224fc2SMarkus Hoffrogge * @param string command line 350e8224fc2SMarkus Hoffrogge * @return bool 351e8224fc2SMarkus Hoffrogge */ 3522762023dSMarkus Hoffrogge public function notifyCommandSuccess($repo_path, $cwd, $command) 3532762023dSMarkus Hoffrogge { 354e8224fc2SMarkus Hoffrogge if (!$this->getConf('notifyByMailOnSuccess')) { 355e8224fc2SMarkus Hoffrogge return false; 356e8224fc2SMarkus Hoffrogge } 357*c365e7dbSmhoffrog $template_replacements = ['GIT_REPO_PATH' => $repo_path, 'GIT_CWD' => $cwd, 'GIT_COMMAND' => $command]; 358e8224fc2SMarkus Hoffrogge return $this->notifyByMail('mail_command_success_subject', 'mail_command_success', $template_replacements); 359e8224fc2SMarkus Hoffrogge } 360e8224fc2SMarkus Hoffrogge 361e8224fc2SMarkus Hoffrogge /** 362e8224fc2SMarkus Hoffrogge * Send an eMail, if eMail address is configured 363e8224fc2SMarkus Hoffrogge * 364e8224fc2SMarkus Hoffrogge * @access public 365e8224fc2SMarkus Hoffrogge * @param string lang id for the subject 366e8224fc2SMarkus Hoffrogge * @param string lang id for the template(.txt) 367e8224fc2SMarkus Hoffrogge * @param array array of replacements 368e8224fc2SMarkus Hoffrogge * @return bool 369e8224fc2SMarkus Hoffrogge */ 3702762023dSMarkus Hoffrogge public function notifyByMail($subject_id, $template_id, $template_replacements) 3712762023dSMarkus Hoffrogge { 372e8224fc2SMarkus Hoffrogge $ret = false; 3732762023dSMarkus Hoffrogge //dbglog("GitBacked - notifyByMail: [subject_id=" . $subject_id 3742762023dSMarkus Hoffrogge // . ", template_id=" . $template_id 3752762023dSMarkus Hoffrogge // . ", template_replacements=" . $template_replacements . "]"); 376e8224fc2SMarkus Hoffrogge if (!$this->isNotifyByEmailOnGitCommandError()) { 377e8224fc2SMarkus Hoffrogge return $ret; 378e8224fc2SMarkus Hoffrogge } 379e8224fc2SMarkus Hoffrogge //$template_text = rawLocale($template_id); // this works for core artifacts only - not for plugins 380e8224fc2SMarkus Hoffrogge $template_filename = $this->localFN($template_id); 381e8224fc2SMarkus Hoffrogge $template_text = file_get_contents($template_filename); 382e8224fc2SMarkus Hoffrogge $template_html = $this->render_text($template_text); 383e8224fc2SMarkus Hoffrogge 384e8224fc2SMarkus Hoffrogge $mailer = new \Mailer(); 385e8224fc2SMarkus Hoffrogge $mailer->to($this->getEmailAddressOnErrorConfigured()); 386dd477e30SMarkus Hoffrogge //dbglog("GitBacked - lang check['".$subject_id."']: ".$this->getLang($subject_id)); 387dd477e30SMarkus Hoffrogge //dbglog("GitBacked - template text['".$template_id."']: ".$template_text); 388dd477e30SMarkus Hoffrogge //dbglog("GitBacked - template html['".$template_id."']: ".$template_html); 389e8224fc2SMarkus Hoffrogge $mailer->subject($this->getLang($subject_id)); 390e8224fc2SMarkus Hoffrogge $mailer->setBody($template_text, $template_replacements, null, $template_html); 391*c365e7dbSmhoffrog 392e8224fc2SMarkus Hoffrogge $ret = $mailer->send(); 393e8224fc2SMarkus Hoffrogge 394e8224fc2SMarkus Hoffrogge return $ret; 395e8224fc2SMarkus Hoffrogge } 396e8224fc2SMarkus Hoffrogge 397e8224fc2SMarkus Hoffrogge /** 398e8224fc2SMarkus Hoffrogge * Check, if eMail is to be sent on a Git command error. 399e8224fc2SMarkus Hoffrogge * 400e8224fc2SMarkus Hoffrogge * @access public 401e8224fc2SMarkus Hoffrogge * @return bool 402e8224fc2SMarkus Hoffrogge */ 4032762023dSMarkus Hoffrogge public function isNotifyByEmailOnGitCommandError() 4042762023dSMarkus Hoffrogge { 405e8224fc2SMarkus Hoffrogge $emailAddressOnError = $this->getEmailAddressOnErrorConfigured(); 406e8224fc2SMarkus Hoffrogge return !empty($emailAddressOnError); 407e8224fc2SMarkus Hoffrogge } 408e8224fc2SMarkus Hoffrogge 409e8224fc2SMarkus Hoffrogge /** 410e8224fc2SMarkus Hoffrogge * Get the eMail address configured for notifications. 411e8224fc2SMarkus Hoffrogge * 412e8224fc2SMarkus Hoffrogge * @access public 413e8224fc2SMarkus Hoffrogge * @return string 414e8224fc2SMarkus Hoffrogge */ 4152762023dSMarkus Hoffrogge public function getEmailAddressOnErrorConfigured() 4162762023dSMarkus Hoffrogge { 417e8224fc2SMarkus Hoffrogge $emailAddressOnError = trim($this->getConf('emailAddressOnError')); 418e8224fc2SMarkus Hoffrogge return $emailAddressOnError; 419fa53f2a3SWolfgang Gassler } 420fa53f2a3SWolfgang Gassler} 4212762023dSMarkus Hoffrogge// phpcs:enable Squiz.Classes.ValidClassName.NotCamelCaps 4222762023dSMarkus Hoffrogge// phpcs:enable PSR1.Classes.ClassDeclaration.MissingNamespace 423fa53f2a3SWolfgang Gassler 424fa53f2a3SWolfgang Gassler// vim:ts=4:sw=4:et: 425