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; 23*aeb41f7dSMarkus 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 1202762023dSMarkus Hoffrogge private function getAuthor() 1212762023dSMarkus Hoffrogge { 122442c3981SWolfgang Gassler return $GLOBALS['USERINFO']['name']; 123442c3981SWolfgang Gassler } 124442c3981SWolfgang Gassler 1252762023dSMarkus Hoffrogge private function getAuthorMail() 1262762023dSMarkus Hoffrogge { 1270d7cb616SBirkir A. Barkarson return $GLOBALS['USERINFO']['mail']; 1280d7cb616SBirkir A. Barkarson } 1290d7cb616SBirkir A. Barkarson 1302762023dSMarkus Hoffrogge private function computeLocalPath() 1312762023dSMarkus Hoffrogge { 132a2effbcbSmsx80 global $conf; 133a2effbcbSmsx80 $repoPath = str_replace('\\', '/', realpath(GitBackedUtil::getEffectivePath($this->getConf('repoPath')))); 134a2effbcbSmsx80 $datadir = $conf['datadir']; // already normalized 135c365e7dbSmhoffrog if (substr($datadir, 0, strlen($repoPath)) !== $repoPath) { 136a2effbcbSmsx80 throw new Exception('Datadir not inside repoPath ??'); 137a2effbcbSmsx80 } 138a2effbcbSmsx80 return substr($datadir, strlen($repoPath) + 1); 139a2effbcbSmsx80 } 140a2effbcbSmsx80 1412762023dSMarkus Hoffrogge private function updatePage($page) 1422762023dSMarkus Hoffrogge { 143a2effbcbSmsx80 144c365e7dbSmhoffrog if (is_callable(Indexer::class . '::getInstance')) { 145c365e7dbSmhoffrog $Indexer = Indexer::getInstance(); 146a2effbcbSmsx80 $success = $Indexer->addPage($page, false, false); 147a2effbcbSmsx80 } elseif (class_exists('Doku_Indexer')) { 148a2effbcbSmsx80 $success = idx_addPage($page, false, false); 149a2effbcbSmsx80 } else { 150a2effbcbSmsx80 // Failed to index the page. Your DokuWiki is older than release 2011-05-25 "Rincewind" 151a2effbcbSmsx80 $success = false; 152a2effbcbSmsx80 } 153a2effbcbSmsx80 154a2effbcbSmsx80 echo "Update $page: $success <br/>"; 155a2effbcbSmsx80 } 156a2effbcbSmsx80 1572762023dSMarkus Hoffrogge public function handlePeriodicPull(Event &$event, $param) 1582762023dSMarkus Hoffrogge { 1592377428fSDanny Lin if ($this->getConf('periodicPull')) { 160a2effbcbSmsx80 $enableIndexUpdate = $this->getConf('updateIndexOnPull'); 1612377428fSDanny Lin $lastPullFile = $this->temp_dir . '/lastpull.txt'; 1622377428fSDanny Lin //check if the lastPullFile exists 1632377428fSDanny Lin if (is_file($lastPullFile)) { 1642377428fSDanny Lin $lastPull = unserialize(file_get_contents($lastPullFile)); 1652377428fSDanny Lin } else { 1662377428fSDanny Lin $lastPull = 0; 1672377428fSDanny Lin } 1682377428fSDanny Lin //calculate time between pulls in seconds 1692377428fSDanny Lin $timeToWait = $this->getConf('periodicMinutes') * 60; 1702377428fSDanny Lin $now = time(); 1712377428fSDanny Lin 1722377428fSDanny Lin //if it is time to run a pull request 1732377428fSDanny Lin if ($lastPull + $timeToWait < $now) { 174e8224fc2SMarkus Hoffrogge try { 1752377428fSDanny Lin $repo = $this->initRepo(); 1762762023dSMarkus Hoffrogge if ($enableIndexUpdate) { 177a2effbcbSmsx80 $localPath = $this->computeLocalPath(); 178a2effbcbSmsx80 179a2effbcbSmsx80 // store current revision id 180a2effbcbSmsx80 $revBefore = $repo->run('rev-parse HEAD'); 181a2effbcbSmsx80 } 1822377428fSDanny Lin 1832377428fSDanny Lin //execute the pull request 1842762023dSMarkus Hoffrogge $repo->pull('origin', $repo->activeBranch()); 185a2effbcbSmsx80 1862762023dSMarkus Hoffrogge if ($enableIndexUpdate) { 187a2effbcbSmsx80 // store new revision id 188a2effbcbSmsx80 $revAfter = $repo->run('rev-parse HEAD'); 189a2effbcbSmsx80 1902762023dSMarkus Hoffrogge if (strcmp($revBefore, $revAfter) != 0) { 191a2effbcbSmsx80 // if there were some changes, get the list of all changed files 192a2effbcbSmsx80 $changedFilesPage = $repo->run('diff --name-only ' . $revBefore . ' ' . $revAfter); 193a2effbcbSmsx80 $changedFiles = preg_split("/\r\n|\n|\r/", $changedFilesPage); 194a2effbcbSmsx80 1952762023dSMarkus Hoffrogge foreach ($changedFiles as $cf) { 196a2effbcbSmsx80 // check if the file is inside localPath, that is, it's a page 1972762023dSMarkus Hoffrogge if (substr($cf, 0, strlen($localPath)) === $localPath) { 198a2effbcbSmsx80 // convert from relative filename to page name 199a2effbcbSmsx80 // for example: local/path/dir/subdir/test.txt -> dir:subdir:test 2002762023dSMarkus Hoffrogge // -4 removes .txt 2012762023dSMarkus Hoffrogge $page = str_replace('/', ':', substr($cf, strlen($localPath) + 1, -4)); 202a2effbcbSmsx80 203a2effbcbSmsx80 // update the page 204a2effbcbSmsx80 $this->updatePage($page); 2052762023dSMarkus Hoffrogge } else { 206a2effbcbSmsx80 echo "Page NOT to update: $cf <br/>"; 207a2effbcbSmsx80 } 208a2effbcbSmsx80 } 209a2effbcbSmsx80 } 210a2effbcbSmsx80 } 211e8224fc2SMarkus Hoffrogge } catch (Exception $e) { 212e8224fc2SMarkus Hoffrogge if (!$this->isNotifyByEmailOnGitCommandError()) { 213e8224fc2SMarkus Hoffrogge throw new Exception('Git command failed to perform periodic pull: ' . $e->getMessage(), 2, $e); 214e8224fc2SMarkus Hoffrogge } 215e8224fc2SMarkus Hoffrogge return; 216e8224fc2SMarkus Hoffrogge } 2172377428fSDanny Lin 2182377428fSDanny Lin //save the current time to the file to track the last pull execution 2192377428fSDanny Lin file_put_contents($lastPullFile, serialize(time())); 2202377428fSDanny Lin } 2212377428fSDanny Lin } 2222377428fSDanny Lin } 2232377428fSDanny Lin 2242762023dSMarkus Hoffrogge public function handleMediaDeletion(Event &$event, $param) 2252762023dSMarkus Hoffrogge { 226442c3981SWolfgang Gassler $mediaPath = $event->data['path']; 227442c3981SWolfgang Gassler $mediaName = $event->data['name']; 228442c3981SWolfgang Gassler 229442c3981SWolfgang Gassler $message = str_replace( 230c365e7dbSmhoffrog ['%media%', '%user%'], 231c365e7dbSmhoffrog [$mediaName, $this->getAuthor()], 232442c3981SWolfgang Gassler $this->getConf('commitMediaMsgDel') 233442c3981SWolfgang Gassler ); 234442c3981SWolfgang Gassler 235442c3981SWolfgang Gassler $this->commitFile($mediaPath, $message); 236442c3981SWolfgang Gassler } 237442c3981SWolfgang Gassler 2382762023dSMarkus Hoffrogge public function handleMediaUpload(Event &$event, $param) 2392762023dSMarkus Hoffrogge { 240442c3981SWolfgang Gassler 241442c3981SWolfgang Gassler $mediaPath = $event->data[1]; 242442c3981SWolfgang Gassler $mediaName = $event->data[2]; 243442c3981SWolfgang Gassler 244442c3981SWolfgang Gassler $message = str_replace( 245c365e7dbSmhoffrog ['%media%', '%user%'], 246c365e7dbSmhoffrog [$mediaName, $this->getAuthor()], 247442c3981SWolfgang Gassler $this->getConf('commitMediaMsg') 248442c3981SWolfgang Gassler ); 249442c3981SWolfgang Gassler 250442c3981SWolfgang Gassler $this->commitFile($mediaPath, $message); 251fa53f2a3SWolfgang Gassler } 252fa53f2a3SWolfgang Gassler 2532762023dSMarkus Hoffrogge public function handleIOWikiPageWrite(Event &$event, $param) 2542762023dSMarkus Hoffrogge { 255fa53f2a3SWolfgang Gassler 256fa53f2a3SWolfgang Gassler $rev = $event->data[3]; 257fa53f2a3SWolfgang Gassler 258fa53f2a3SWolfgang Gassler /* On update to an existing page this event is called twice, 259fa53f2a3SWolfgang Gassler * once for the transfer of the old version to the attic (rev will have a value) 260fa53f2a3SWolfgang Gassler * and once to write the new version of the page into the wiki (rev is false) 261fa53f2a3SWolfgang Gassler */ 262fa53f2a3SWolfgang Gassler if (!$rev) { 263fa53f2a3SWolfgang Gassler $pagePath = $event->data[0][0]; 264fa53f2a3SWolfgang Gassler $pageName = $event->data[2]; 265442c3981SWolfgang Gassler $pageContent = $event->data[0][1]; 266fa53f2a3SWolfgang Gassler 2677af27dc9SWolfgang Gassler // get the summary directly from the form input 268e7471cfaSDanny Lin // as the metadata hasn't updated yet 2697af27dc9SWolfgang Gassler $editSummary = $GLOBALS['INPUT']->str('summary'); 270442c3981SWolfgang Gassler 271442c3981SWolfgang Gassler // empty content indicates a page deletion 272442c3981SWolfgang Gassler if ($pageContent == '') { 273442c3981SWolfgang Gassler // get the commit text for deletions 274d4e1c54bSWolfgang Gassler $msgTemplate = $this->getConf('commitPageMsgDel'); 275442c3981SWolfgang Gassler 276442c3981SWolfgang Gassler // bad hack as DokuWiki deletes the file after this event 277442c3981SWolfgang Gassler // thus, let's delete the file by ourselves, so git can recognize the deletion 278442c3981SWolfgang Gassler // DokuWiki uses @unlink as well, so no error should be thrown if we delete it twice 279442c3981SWolfgang Gassler @unlink($pagePath); 280442c3981SWolfgang Gassler } else { 281442c3981SWolfgang Gassler //get the commit text for edits 282d4e1c54bSWolfgang Gassler $msgTemplate = $this->getConf('commitPageMsg'); 283442c3981SWolfgang Gassler } 284442c3981SWolfgang Gassler 285fa53f2a3SWolfgang Gassler $message = str_replace( 286c365e7dbSmhoffrog ['%page%', '%summary%', '%user%'], 287c365e7dbSmhoffrog [$pageName, $editSummary, $this->getAuthor()], 288442c3981SWolfgang Gassler $msgTemplate 289fa53f2a3SWolfgang Gassler ); 290fa53f2a3SWolfgang Gassler 291442c3981SWolfgang Gassler $this->commitFile($pagePath, $message); 292fa53f2a3SWolfgang Gassler } 293e8224fc2SMarkus Hoffrogge } 294fa53f2a3SWolfgang Gassler 295e8224fc2SMarkus Hoffrogge // ====== Error notification helpers ====== 296e8224fc2SMarkus Hoffrogge /** 297e8224fc2SMarkus Hoffrogge * Notifies error on create_new 298e8224fc2SMarkus Hoffrogge * 299e8224fc2SMarkus Hoffrogge * @access public 300e8224fc2SMarkus Hoffrogge * @param string repository path 301e8224fc2SMarkus Hoffrogge * @param string reference path / remote reference 302e8224fc2SMarkus Hoffrogge * @param string error message 303e8224fc2SMarkus Hoffrogge * @return bool 304e8224fc2SMarkus Hoffrogge */ 3052762023dSMarkus Hoffrogge public function notifyCreateNewError($repo_path, $reference, $error_message) 3062762023dSMarkus Hoffrogge { 307*aeb41f7dSMarkus Hoffrogge $template_replacements = [ 308*aeb41f7dSMarkus Hoffrogge 'GIT_REPO_PATH' => $repo_path, 309*aeb41f7dSMarkus Hoffrogge 'GIT_REFERENCE' => (empty($reference) ? 'n/a' : $reference), 310*aeb41f7dSMarkus Hoffrogge 'GIT_ERROR_MESSAGE' => $error_message 311*aeb41f7dSMarkus Hoffrogge ]; 312e8224fc2SMarkus Hoffrogge return $this->notifyByMail('mail_create_new_error_subject', 'mail_create_new_error', $template_replacements); 313e8224fc2SMarkus Hoffrogge } 314e8224fc2SMarkus Hoffrogge 315e8224fc2SMarkus Hoffrogge /** 316e8224fc2SMarkus Hoffrogge * Notifies error on setting repo path 317e8224fc2SMarkus Hoffrogge * 318e8224fc2SMarkus Hoffrogge * @access public 319e8224fc2SMarkus Hoffrogge * @param string repository path 320e8224fc2SMarkus Hoffrogge * @param string error message 321e8224fc2SMarkus Hoffrogge * @return bool 322e8224fc2SMarkus Hoffrogge */ 3232762023dSMarkus Hoffrogge public function notifyRepoPathError($repo_path, $error_message) 3242762023dSMarkus Hoffrogge { 325*aeb41f7dSMarkus Hoffrogge $template_replacements = [ 326*aeb41f7dSMarkus Hoffrogge 'GIT_REPO_PATH' => $repo_path, 327*aeb41f7dSMarkus Hoffrogge 'GIT_ERROR_MESSAGE' => $error_message 328*aeb41f7dSMarkus Hoffrogge ]; 329e8224fc2SMarkus Hoffrogge return $this->notifyByMail('mail_repo_path_error_subject', 'mail_repo_path_error', $template_replacements); 330e8224fc2SMarkus Hoffrogge } 331e8224fc2SMarkus Hoffrogge 332e8224fc2SMarkus Hoffrogge /** 333e8224fc2SMarkus Hoffrogge * Notifies error on git command 334e8224fc2SMarkus Hoffrogge * 335e8224fc2SMarkus Hoffrogge * @access public 336e8224fc2SMarkus Hoffrogge * @param string repository path 337e8224fc2SMarkus Hoffrogge * @param string current working dir 338e8224fc2SMarkus Hoffrogge * @param string command line 339e8224fc2SMarkus Hoffrogge * @param int exit code of command (status) 340e8224fc2SMarkus Hoffrogge * @param string error message 341e8224fc2SMarkus Hoffrogge * @return bool 342e8224fc2SMarkus Hoffrogge */ 3432762023dSMarkus Hoffrogge public function notifyCommandError($repo_path, $cwd, $command, $status, $error_message) 3442762023dSMarkus Hoffrogge { 345*aeb41f7dSMarkus Hoffrogge $template_replacements = [ 346*aeb41f7dSMarkus Hoffrogge 'GIT_REPO_PATH' => $repo_path, 347*aeb41f7dSMarkus Hoffrogge 'GIT_CWD' => $cwd, 348*aeb41f7dSMarkus Hoffrogge 'GIT_COMMAND' => $command, 349*aeb41f7dSMarkus Hoffrogge 'GIT_COMMAND_EXITCODE' => $status, 350*aeb41f7dSMarkus Hoffrogge 'GIT_ERROR_MESSAGE' => $error_message 351*aeb41f7dSMarkus Hoffrogge ]; 352e8224fc2SMarkus Hoffrogge return $this->notifyByMail('mail_command_error_subject', 'mail_command_error', $template_replacements); 353e8224fc2SMarkus Hoffrogge } 354e8224fc2SMarkus Hoffrogge 355e8224fc2SMarkus Hoffrogge /** 356e8224fc2SMarkus Hoffrogge * Notifies success on git command 357e8224fc2SMarkus Hoffrogge * 358e8224fc2SMarkus Hoffrogge * @access public 359e8224fc2SMarkus Hoffrogge * @param string repository path 360e8224fc2SMarkus Hoffrogge * @param string current working dir 361e8224fc2SMarkus Hoffrogge * @param string command line 362e8224fc2SMarkus Hoffrogge * @return bool 363e8224fc2SMarkus Hoffrogge */ 3642762023dSMarkus Hoffrogge public function notifyCommandSuccess($repo_path, $cwd, $command) 3652762023dSMarkus Hoffrogge { 366e8224fc2SMarkus Hoffrogge if (!$this->getConf('notifyByMailOnSuccess')) { 367e8224fc2SMarkus Hoffrogge return false; 368e8224fc2SMarkus Hoffrogge } 369*aeb41f7dSMarkus Hoffrogge $template_replacements = [ 370*aeb41f7dSMarkus Hoffrogge 'GIT_REPO_PATH' => $repo_path, 371*aeb41f7dSMarkus Hoffrogge 'GIT_CWD' => $cwd, 372*aeb41f7dSMarkus Hoffrogge 'GIT_COMMAND' => $command 373*aeb41f7dSMarkus Hoffrogge ]; 374e8224fc2SMarkus Hoffrogge return $this->notifyByMail('mail_command_success_subject', 'mail_command_success', $template_replacements); 375e8224fc2SMarkus Hoffrogge } 376e8224fc2SMarkus Hoffrogge 377e8224fc2SMarkus Hoffrogge /** 378e8224fc2SMarkus Hoffrogge * Send an eMail, if eMail address is configured 379e8224fc2SMarkus Hoffrogge * 380e8224fc2SMarkus Hoffrogge * @access public 381e8224fc2SMarkus Hoffrogge * @param string lang id for the subject 382e8224fc2SMarkus Hoffrogge * @param string lang id for the template(.txt) 383e8224fc2SMarkus Hoffrogge * @param array array of replacements 384e8224fc2SMarkus Hoffrogge * @return bool 385e8224fc2SMarkus Hoffrogge */ 3862762023dSMarkus Hoffrogge public function notifyByMail($subject_id, $template_id, $template_replacements) 3872762023dSMarkus Hoffrogge { 388e8224fc2SMarkus Hoffrogge $ret = false; 3892762023dSMarkus Hoffrogge //dbglog("GitBacked - notifyByMail: [subject_id=" . $subject_id 3902762023dSMarkus Hoffrogge // . ", template_id=" . $template_id 3912762023dSMarkus Hoffrogge // . ", template_replacements=" . $template_replacements . "]"); 392e8224fc2SMarkus Hoffrogge if (!$this->isNotifyByEmailOnGitCommandError()) { 393e8224fc2SMarkus Hoffrogge return $ret; 394e8224fc2SMarkus Hoffrogge } 395e8224fc2SMarkus Hoffrogge //$template_text = rawLocale($template_id); // this works for core artifacts only - not for plugins 396e8224fc2SMarkus Hoffrogge $template_filename = $this->localFN($template_id); 397e8224fc2SMarkus Hoffrogge $template_text = file_get_contents($template_filename); 398e8224fc2SMarkus Hoffrogge $template_html = $this->render_text($template_text); 399e8224fc2SMarkus Hoffrogge 400e8224fc2SMarkus Hoffrogge $mailer = new \Mailer(); 401e8224fc2SMarkus Hoffrogge $mailer->to($this->getEmailAddressOnErrorConfigured()); 402dd477e30SMarkus Hoffrogge //dbglog("GitBacked - lang check['".$subject_id."']: ".$this->getLang($subject_id)); 403dd477e30SMarkus Hoffrogge //dbglog("GitBacked - template text['".$template_id."']: ".$template_text); 404dd477e30SMarkus Hoffrogge //dbglog("GitBacked - template html['".$template_id."']: ".$template_html); 405e8224fc2SMarkus Hoffrogge $mailer->subject($this->getLang($subject_id)); 406e8224fc2SMarkus Hoffrogge $mailer->setBody($template_text, $template_replacements, null, $template_html); 407c365e7dbSmhoffrog 408e8224fc2SMarkus Hoffrogge $ret = $mailer->send(); 409e8224fc2SMarkus Hoffrogge 410e8224fc2SMarkus Hoffrogge return $ret; 411e8224fc2SMarkus Hoffrogge } 412e8224fc2SMarkus Hoffrogge 413e8224fc2SMarkus Hoffrogge /** 414e8224fc2SMarkus Hoffrogge * Check, if eMail is to be sent on a Git command error. 415e8224fc2SMarkus Hoffrogge * 416e8224fc2SMarkus Hoffrogge * @access public 417e8224fc2SMarkus Hoffrogge * @return bool 418e8224fc2SMarkus Hoffrogge */ 4192762023dSMarkus Hoffrogge public function isNotifyByEmailOnGitCommandError() 4202762023dSMarkus Hoffrogge { 421e8224fc2SMarkus Hoffrogge $emailAddressOnError = $this->getEmailAddressOnErrorConfigured(); 422e8224fc2SMarkus Hoffrogge return !empty($emailAddressOnError); 423e8224fc2SMarkus Hoffrogge } 424e8224fc2SMarkus Hoffrogge 425e8224fc2SMarkus Hoffrogge /** 426e8224fc2SMarkus Hoffrogge * Get the eMail address configured for notifications. 427e8224fc2SMarkus Hoffrogge * 428e8224fc2SMarkus Hoffrogge * @access public 429e8224fc2SMarkus Hoffrogge * @return string 430e8224fc2SMarkus Hoffrogge */ 4312762023dSMarkus Hoffrogge public function getEmailAddressOnErrorConfigured() 4322762023dSMarkus Hoffrogge { 433e8224fc2SMarkus Hoffrogge $emailAddressOnError = trim($this->getConf('emailAddressOnError')); 434e8224fc2SMarkus Hoffrogge return $emailAddressOnError; 435fa53f2a3SWolfgang Gassler } 436fa53f2a3SWolfgang Gassler} 4372762023dSMarkus Hoffrogge// phpcs:enable Squiz.Classes.ValidClassName.NotCamelCaps 4382762023dSMarkus Hoffrogge// phpcs:enable PSR1.Classes.ClassDeclaration.MissingNamespace 439fa53f2a3SWolfgang Gassler 440fa53f2a3SWolfgang Gassler// vim:ts=4:sw=4:et: 441