1fa53f2a3SWolfgang Gassler<?php 2fa53f2a3SWolfgang Gassler/** 3fa53f2a3SWolfgang Gassler * DokuWiki Plugin gitbacked (Action Component) 4fa53f2a3SWolfgang Gassler * 5fa53f2a3SWolfgang Gassler * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6fa53f2a3SWolfgang Gassler * @author Wolfgang Gassler <wolfgang@gassler.org> 7fa53f2a3SWolfgang Gassler */ 8fa53f2a3SWolfgang Gassler 9fa53f2a3SWolfgang Gassler// must be run within Dokuwiki 10fa53f2a3SWolfgang Gasslerif (!defined('DOKU_INC')) die(); 11fa53f2a3SWolfgang Gassler 12fa53f2a3SWolfgang Gasslerif (!defined('DOKU_LF')) define('DOKU_LF', "\n"); 13fa53f2a3SWolfgang Gasslerif (!defined('DOKU_TAB')) define('DOKU_TAB', "\t"); 14fa53f2a3SWolfgang Gasslerif (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 15fa53f2a3SWolfgang Gassler 1600ce3f12SDanny Linrequire_once dirname(__FILE__).'/../lib/Git.php'; 17dee8dca1SMarkus Hoffroggerequire_once dirname(__FILE__).'/../lib/GitBackedUtil.php'; 18fa53f2a3SWolfgang Gassler 19fa53f2a3SWolfgang Gasslerclass action_plugin_gitbacked_editcommit extends DokuWiki_Action_Plugin { 20fa53f2a3SWolfgang Gassler 2100ce3f12SDanny Lin function __construct() { 22eeb1a599SMarkus Hoffrogge $this->temp_dir = GitBackedUtil::getTempDir(); 2300ce3f12SDanny Lin } 2400ce3f12SDanny Lin 25a6cdc68cSMichael Sorg public function register(Doku_Event_Handler $controller) { 26fa53f2a3SWolfgang Gassler 27fa53f2a3SWolfgang Gassler $controller->register_hook('IO_WIKIPAGE_WRITE', 'AFTER', $this, 'handle_io_wikipage_write'); 28442c3981SWolfgang Gassler $controller->register_hook('MEDIA_UPLOAD_FINISH', 'AFTER', $this, 'handle_media_upload'); 29442c3981SWolfgang Gassler $controller->register_hook('MEDIA_DELETE_FILE', 'AFTER', $this, 'handle_media_deletion'); 30b92b117aSWolfgang Gassler $controller->register_hook('DOKUWIKI_DONE', 'AFTER', $this, 'handle_periodic_pull'); 31442c3981SWolfgang Gassler } 32442c3981SWolfgang Gassler 33b92b117aSWolfgang Gassler private function initRepo() { 34442c3981SWolfgang Gassler //get path to the repo root (by default DokuWiki's savedir) 35dee8dca1SMarkus Hoffrogge $repoPath = GitBackedUtil::getEffectivePath($this->getConf('repoPath')); 36635161d0SCarsten Teibes $gitPath = trim($this->getConf('gitPath')); 37635161d0SCarsten Teibes if ($gitPath !== '') { 38635161d0SCarsten Teibes Git::set_bin($gitPath); 39635161d0SCarsten Teibes } 40442c3981SWolfgang Gassler //init the repo and create a new one if it is not present 414eba9b44SDanny Lin io_mkdir_p($repoPath); 42e8224fc2SMarkus Hoffrogge $repo = new GitRepo($repoPath, $this, true, true); 434eba9b44SDanny Lin //set git working directory (by default DokuWiki's savedir) 44dee8dca1SMarkus Hoffrogge $repoWorkDir = $this->getConf('repoWorkDir'); 45dee8dca1SMarkus Hoffrogge if (!empty($repoWorkDir)) { 46dee8dca1SMarkus Hoffrogge $repoWorkDir = GitBackedUtil::getEffectivePath($repoWorkDir); 47dee8dca1SMarkus Hoffrogge } 48dee8dca1SMarkus Hoffrogge Git::set_bin(empty($repoWorkDir) ? Git::get_bin() : Git::get_bin().' --work-tree '.escapeshellarg($repoWorkDir)); 490d7cb616SBirkir A. Barkarson $params = str_replace( 500d7cb616SBirkir A. Barkarson array('%mail%','%user%'), 510d7cb616SBirkir A. Barkarson array($this->getAuthorMail(),$this->getAuthor()), 520d7cb616SBirkir A. Barkarson $this->getConf('addParams')); 53442c3981SWolfgang Gassler if ($params) { 54985a1bc7SCarsten Teibes Git::set_bin(Git::get_bin().' '.$params); 55442c3981SWolfgang Gassler } 56b92b117aSWolfgang Gassler return $repo; 57b92b117aSWolfgang Gassler } 58b92b117aSWolfgang Gassler 5966f21a70SWolfgang Gassler private function isIgnored($filePath) { 6066f21a70SWolfgang Gassler $ignore = false; 6166f21a70SWolfgang Gassler $ignorePaths = trim($this->getConf('ignorePaths')); 6266f21a70SWolfgang Gassler if ($ignorePaths !== '') { 6366f21a70SWolfgang Gassler $paths = explode(',',$ignorePaths); 6466f21a70SWolfgang Gassler foreach($paths as $path) { 6566f21a70SWolfgang Gassler if (strstr($filePath,$path)) { 6666f21a70SWolfgang Gassler $ignore = true; 6766f21a70SWolfgang Gassler } 6866f21a70SWolfgang Gassler } 6966f21a70SWolfgang Gassler } 7066f21a70SWolfgang Gassler return $ignore; 7166f21a70SWolfgang Gassler } 7266f21a70SWolfgang Gassler 73b92b117aSWolfgang Gassler private function commitFile($filePath,$message) { 7466f21a70SWolfgang Gassler if (!$this->isIgnored($filePath)) { 75e8224fc2SMarkus Hoffrogge try { 76b92b117aSWolfgang Gassler $repo = $this->initRepo(); 77442c3981SWolfgang Gassler 78442c3981SWolfgang Gassler //add the changed file and set the commit message 79442c3981SWolfgang Gassler $repo->add($filePath); 80442c3981SWolfgang Gassler $repo->commit($message); 81442c3981SWolfgang Gassler 82442c3981SWolfgang Gassler //if the push after Commit option is set we push the active branch to origin 83442c3981SWolfgang Gassler if ($this->getConf('pushAfterCommit')) { 84442c3981SWolfgang Gassler $repo->push('origin',$repo->active_branch()); 85442c3981SWolfgang Gassler } 86e8224fc2SMarkus Hoffrogge } catch (Exception $e) { 87e8224fc2SMarkus Hoffrogge if (!$this->isNotifyByEmailOnGitCommandError()) { 88e8224fc2SMarkus Hoffrogge throw new Exception('Git committing or pushing failed: '.$e->getMessage(), 1, $e); 8966f21a70SWolfgang Gassler } 90e8224fc2SMarkus Hoffrogge return; 91e8224fc2SMarkus Hoffrogge } 92e8224fc2SMarkus Hoffrogge } 93442c3981SWolfgang Gassler } 94442c3981SWolfgang Gassler 95442c3981SWolfgang Gassler private function getAuthor() { 96442c3981SWolfgang Gassler return $GLOBALS['USERINFO']['name']; 97442c3981SWolfgang Gassler } 98442c3981SWolfgang Gassler 990d7cb616SBirkir A. Barkarson private function getAuthorMail() { 1000d7cb616SBirkir A. Barkarson return $GLOBALS['USERINFO']['mail']; 1010d7cb616SBirkir A. Barkarson } 1020d7cb616SBirkir A. Barkarson 1032377428fSDanny Lin public function handle_periodic_pull(Doku_Event &$event, $param) { 1042377428fSDanny Lin if ($this->getConf('periodicPull')) { 1052377428fSDanny Lin $lastPullFile = $this->temp_dir.'/lastpull.txt'; 1062377428fSDanny Lin //check if the lastPullFile exists 1072377428fSDanny Lin if (is_file($lastPullFile)) { 1082377428fSDanny Lin $lastPull = unserialize(file_get_contents($lastPullFile)); 1092377428fSDanny Lin } else { 1102377428fSDanny Lin $lastPull = 0; 1112377428fSDanny Lin } 1122377428fSDanny Lin //calculate time between pulls in seconds 1132377428fSDanny Lin $timeToWait = $this->getConf('periodicMinutes')*60; 1142377428fSDanny Lin $now = time(); 1152377428fSDanny Lin 1162377428fSDanny Lin //if it is time to run a pull request 1172377428fSDanny Lin if ($lastPull+$timeToWait < $now) { 118e8224fc2SMarkus Hoffrogge try { 1192377428fSDanny Lin $repo = $this->initRepo(); 1202377428fSDanny Lin 1212377428fSDanny Lin //execute the pull request 1222377428fSDanny Lin $repo->pull('origin',$repo->active_branch()); 123e8224fc2SMarkus Hoffrogge } catch (Exception $e) { 124e8224fc2SMarkus Hoffrogge if (!$this->isNotifyByEmailOnGitCommandError()) { 125e8224fc2SMarkus Hoffrogge throw new Exception('Git command failed to perform periodic pull: '.$e->getMessage(), 2, $e); 126e8224fc2SMarkus Hoffrogge } 127e8224fc2SMarkus Hoffrogge return; 128e8224fc2SMarkus Hoffrogge } 1292377428fSDanny Lin 1302377428fSDanny Lin //save the current time to the file to track the last pull execution 1312377428fSDanny Lin file_put_contents($lastPullFile,serialize(time())); 1322377428fSDanny Lin } 1332377428fSDanny Lin } 1342377428fSDanny Lin } 1352377428fSDanny Lin 136442c3981SWolfgang Gassler public function handle_media_deletion(Doku_Event &$event, $param) { 137442c3981SWolfgang Gassler $mediaPath = $event->data['path']; 138442c3981SWolfgang Gassler $mediaName = $event->data['name']; 139442c3981SWolfgang Gassler 140442c3981SWolfgang Gassler $message = str_replace( 141442c3981SWolfgang Gassler array('%media%','%user%'), 142442c3981SWolfgang Gassler array($mediaName,$this->getAuthor()), 143442c3981SWolfgang Gassler $this->getConf('commitMediaMsgDel') 144442c3981SWolfgang Gassler ); 145442c3981SWolfgang Gassler 146442c3981SWolfgang Gassler $this->commitFile($mediaPath,$message); 147442c3981SWolfgang Gassler 148442c3981SWolfgang Gassler } 149442c3981SWolfgang Gassler 150442c3981SWolfgang Gassler public function handle_media_upload(Doku_Event &$event, $param) { 151442c3981SWolfgang Gassler 152442c3981SWolfgang Gassler $mediaPath = $event->data[1]; 153442c3981SWolfgang Gassler $mediaName = $event->data[2]; 154442c3981SWolfgang Gassler 155442c3981SWolfgang Gassler $message = str_replace( 156442c3981SWolfgang Gassler array('%media%','%user%'), 157442c3981SWolfgang Gassler array($mediaName,$this->getAuthor()), 158442c3981SWolfgang Gassler $this->getConf('commitMediaMsg') 159442c3981SWolfgang Gassler ); 160442c3981SWolfgang Gassler 161442c3981SWolfgang Gassler $this->commitFile($mediaPath,$message); 162fa53f2a3SWolfgang Gassler 163fa53f2a3SWolfgang Gassler } 164fa53f2a3SWolfgang Gassler 165fa53f2a3SWolfgang Gassler public function handle_io_wikipage_write(Doku_Event &$event, $param) { 166fa53f2a3SWolfgang Gassler 167fa53f2a3SWolfgang Gassler $rev = $event->data[3]; 168fa53f2a3SWolfgang Gassler 169fa53f2a3SWolfgang Gassler /* On update to an existing page this event is called twice, 170fa53f2a3SWolfgang Gassler * once for the transfer of the old version to the attic (rev will have a value) 171fa53f2a3SWolfgang Gassler * and once to write the new version of the page into the wiki (rev is false) 172fa53f2a3SWolfgang Gassler */ 173fa53f2a3SWolfgang Gassler if (!$rev) { 174fa53f2a3SWolfgang Gassler 175fa53f2a3SWolfgang Gassler $pagePath = $event->data[0][0]; 176fa53f2a3SWolfgang Gassler $pageName = $event->data[2]; 177442c3981SWolfgang Gassler $pageContent = $event->data[0][1]; 178fa53f2a3SWolfgang Gassler 1797af27dc9SWolfgang Gassler // get the summary directly from the form input 180e7471cfaSDanny Lin // as the metadata hasn't updated yet 1817af27dc9SWolfgang Gassler $editSummary = $GLOBALS['INPUT']->str('summary'); 182442c3981SWolfgang Gassler 183442c3981SWolfgang Gassler // empty content indicates a page deletion 184442c3981SWolfgang Gassler if ($pageContent == '') { 185442c3981SWolfgang Gassler // get the commit text for deletions 186d4e1c54bSWolfgang Gassler $msgTemplate = $this->getConf('commitPageMsgDel'); 187442c3981SWolfgang Gassler 188442c3981SWolfgang Gassler // bad hack as DokuWiki deletes the file after this event 189442c3981SWolfgang Gassler // thus, let's delete the file by ourselves, so git can recognize the deletion 190442c3981SWolfgang Gassler // DokuWiki uses @unlink as well, so no error should be thrown if we delete it twice 191442c3981SWolfgang Gassler @unlink($pagePath); 192442c3981SWolfgang Gassler 193442c3981SWolfgang Gassler } else { 194442c3981SWolfgang Gassler //get the commit text for edits 195d4e1c54bSWolfgang Gassler $msgTemplate = $this->getConf('commitPageMsg'); 196442c3981SWolfgang Gassler } 197442c3981SWolfgang Gassler 198fa53f2a3SWolfgang Gassler $message = str_replace( 199fa53f2a3SWolfgang Gassler array('%page%','%summary%','%user%'), 200442c3981SWolfgang Gassler array($pageName,$editSummary,$this->getAuthor()), 201442c3981SWolfgang Gassler $msgTemplate 202fa53f2a3SWolfgang Gassler ); 203fa53f2a3SWolfgang Gassler 204442c3981SWolfgang Gassler $this->commitFile($pagePath,$message); 205fa53f2a3SWolfgang Gassler 206fa53f2a3SWolfgang Gassler } 207e8224fc2SMarkus Hoffrogge } 208fa53f2a3SWolfgang Gassler 209e8224fc2SMarkus Hoffrogge // ====== Error notification helpers ====== 210e8224fc2SMarkus Hoffrogge /** 211e8224fc2SMarkus Hoffrogge * Notifies error on create_new 212e8224fc2SMarkus Hoffrogge * 213e8224fc2SMarkus Hoffrogge * @access public 214e8224fc2SMarkus Hoffrogge * @param string repository path 215e8224fc2SMarkus Hoffrogge * @param string reference path / remote reference 216e8224fc2SMarkus Hoffrogge * @param string error message 217e8224fc2SMarkus Hoffrogge * @return bool 218e8224fc2SMarkus Hoffrogge */ 219e8224fc2SMarkus Hoffrogge public function notify_create_new_error($repo_path, $reference, $error_message) { 220e8224fc2SMarkus Hoffrogge $template_replacements = array( 221e8224fc2SMarkus Hoffrogge 'GIT_REPO_PATH' => $repo_path, 222e8224fc2SMarkus Hoffrogge 'GIT_REFERENCE' => (empty($reference) ? 'n/a' : $reference), 223e8224fc2SMarkus Hoffrogge 'GIT_ERROR_MESSAGE' => $error_message 224e8224fc2SMarkus Hoffrogge ); 225e8224fc2SMarkus Hoffrogge return $this->notifyByMail('mail_create_new_error_subject', 'mail_create_new_error', $template_replacements); 226e8224fc2SMarkus Hoffrogge } 227e8224fc2SMarkus Hoffrogge 228e8224fc2SMarkus Hoffrogge /** 229e8224fc2SMarkus Hoffrogge * Notifies error on setting repo path 230e8224fc2SMarkus Hoffrogge * 231e8224fc2SMarkus Hoffrogge * @access public 232e8224fc2SMarkus Hoffrogge * @param string repository path 233e8224fc2SMarkus Hoffrogge * @param string error message 234e8224fc2SMarkus Hoffrogge * @return bool 235e8224fc2SMarkus Hoffrogge */ 236e8224fc2SMarkus Hoffrogge public function notify_repo_path_error($repo_path, $error_message) { 237e8224fc2SMarkus Hoffrogge $template_replacements = array( 238e8224fc2SMarkus Hoffrogge 'GIT_REPO_PATH' => $repo_path, 239e8224fc2SMarkus Hoffrogge 'GIT_ERROR_MESSAGE' => $error_message 240e8224fc2SMarkus Hoffrogge ); 241e8224fc2SMarkus Hoffrogge return $this->notifyByMail('mail_repo_path_error_subject', 'mail_repo_path_error', $template_replacements); 242e8224fc2SMarkus Hoffrogge } 243e8224fc2SMarkus Hoffrogge 244e8224fc2SMarkus Hoffrogge /** 245e8224fc2SMarkus Hoffrogge * Notifies error on git command 246e8224fc2SMarkus Hoffrogge * 247e8224fc2SMarkus Hoffrogge * @access public 248e8224fc2SMarkus Hoffrogge * @param string repository path 249e8224fc2SMarkus Hoffrogge * @param string current working dir 250e8224fc2SMarkus Hoffrogge * @param string command line 251e8224fc2SMarkus Hoffrogge * @param int exit code of command (status) 252e8224fc2SMarkus Hoffrogge * @param string error message 253e8224fc2SMarkus Hoffrogge * @return bool 254e8224fc2SMarkus Hoffrogge */ 255e8224fc2SMarkus Hoffrogge public function notify_command_error($repo_path, $cwd, $command, $status, $error_message) { 256e8224fc2SMarkus Hoffrogge $template_replacements = array( 257e8224fc2SMarkus Hoffrogge 'GIT_REPO_PATH' => $repo_path, 258e8224fc2SMarkus Hoffrogge 'GIT_CWD' => $cwd, 259e8224fc2SMarkus Hoffrogge 'GIT_COMMAND' => $command, 260e8224fc2SMarkus Hoffrogge 'GIT_COMMAND_EXITCODE' => $status, 261e8224fc2SMarkus Hoffrogge 'GIT_ERROR_MESSAGE' => $error_message 262e8224fc2SMarkus Hoffrogge ); 263e8224fc2SMarkus Hoffrogge return $this->notifyByMail('mail_command_error_subject', 'mail_command_error', $template_replacements); 264e8224fc2SMarkus Hoffrogge } 265e8224fc2SMarkus Hoffrogge 266e8224fc2SMarkus Hoffrogge /** 267e8224fc2SMarkus Hoffrogge * Notifies success on git command 268e8224fc2SMarkus Hoffrogge * 269e8224fc2SMarkus Hoffrogge * @access public 270e8224fc2SMarkus Hoffrogge * @param string repository path 271e8224fc2SMarkus Hoffrogge * @param string current working dir 272e8224fc2SMarkus Hoffrogge * @param string command line 273e8224fc2SMarkus Hoffrogge * @return bool 274e8224fc2SMarkus Hoffrogge */ 275e8224fc2SMarkus Hoffrogge public function notify_command_success($repo_path, $cwd, $command) { 276e8224fc2SMarkus Hoffrogge if (!$this->getConf('notifyByMailOnSuccess')) { 277e8224fc2SMarkus Hoffrogge return false; 278e8224fc2SMarkus Hoffrogge } 279e8224fc2SMarkus Hoffrogge $template_replacements = array( 280e8224fc2SMarkus Hoffrogge 'GIT_REPO_PATH' => $repo_path, 281e8224fc2SMarkus Hoffrogge 'GIT_CWD' => $cwd, 282e8224fc2SMarkus Hoffrogge 'GIT_COMMAND' => $command 283e8224fc2SMarkus Hoffrogge ); 284e8224fc2SMarkus Hoffrogge return $this->notifyByMail('mail_command_success_subject', 'mail_command_success', $template_replacements); 285e8224fc2SMarkus Hoffrogge } 286e8224fc2SMarkus Hoffrogge 287e8224fc2SMarkus Hoffrogge /** 288e8224fc2SMarkus Hoffrogge * Send an eMail, if eMail address is configured 289e8224fc2SMarkus Hoffrogge * 290e8224fc2SMarkus Hoffrogge * @access public 291e8224fc2SMarkus Hoffrogge * @param string lang id for the subject 292e8224fc2SMarkus Hoffrogge * @param string lang id for the template(.txt) 293e8224fc2SMarkus Hoffrogge * @param array array of replacements 294e8224fc2SMarkus Hoffrogge * @return bool 295e8224fc2SMarkus Hoffrogge */ 296e8224fc2SMarkus Hoffrogge public function notifyByMail($subject_id, $template_id, $template_replacements) { 297e8224fc2SMarkus Hoffrogge $ret = false; 298*dd477e30SMarkus Hoffrogge //dbglog("GitBacked - notifyByMail: [subject_id=".$subject_id.", template_id=".$template_id.", template_replacements=".$template_replacements."]"); 299e8224fc2SMarkus Hoffrogge if (!$this->isNotifyByEmailOnGitCommandError()) { 300e8224fc2SMarkus Hoffrogge return $ret; 301e8224fc2SMarkus Hoffrogge } 302e8224fc2SMarkus Hoffrogge //$template_text = rawLocale($template_id); // this works for core artifacts only - not for plugins 303e8224fc2SMarkus Hoffrogge $template_filename = $this->localFN($template_id); 304e8224fc2SMarkus Hoffrogge $template_text = file_get_contents($template_filename); 305e8224fc2SMarkus Hoffrogge $template_html = $this->render_text($template_text); 306e8224fc2SMarkus Hoffrogge 307e8224fc2SMarkus Hoffrogge $mailer = new \Mailer(); 308e8224fc2SMarkus Hoffrogge $mailer->to($this->getEmailAddressOnErrorConfigured()); 309*dd477e30SMarkus Hoffrogge //dbglog("GitBacked - lang check['".$subject_id."']: ".$this->getLang($subject_id)); 310*dd477e30SMarkus Hoffrogge //dbglog("GitBacked - template text['".$template_id."']: ".$template_text); 311*dd477e30SMarkus Hoffrogge //dbglog("GitBacked - template html['".$template_id."']: ".$template_html); 312e8224fc2SMarkus Hoffrogge $mailer->subject($this->getLang($subject_id)); 313e8224fc2SMarkus Hoffrogge $mailer->setBody($template_text, $template_replacements, null, $template_html); 314e8224fc2SMarkus Hoffrogge $ret = $mailer->send(); 315e8224fc2SMarkus Hoffrogge 316e8224fc2SMarkus Hoffrogge return $ret; 317e8224fc2SMarkus Hoffrogge } 318e8224fc2SMarkus Hoffrogge 319e8224fc2SMarkus Hoffrogge /** 320e8224fc2SMarkus Hoffrogge * Check, if eMail is to be sent on a Git command error. 321e8224fc2SMarkus Hoffrogge * 322e8224fc2SMarkus Hoffrogge * @access public 323e8224fc2SMarkus Hoffrogge * @return bool 324e8224fc2SMarkus Hoffrogge */ 325e8224fc2SMarkus Hoffrogge public function isNotifyByEmailOnGitCommandError() { 326e8224fc2SMarkus Hoffrogge $emailAddressOnError = $this->getEmailAddressOnErrorConfigured(); 327e8224fc2SMarkus Hoffrogge return !empty($emailAddressOnError); 328e8224fc2SMarkus Hoffrogge } 329e8224fc2SMarkus Hoffrogge 330e8224fc2SMarkus Hoffrogge /** 331e8224fc2SMarkus Hoffrogge * Get the eMail address configured for notifications. 332e8224fc2SMarkus Hoffrogge * 333e8224fc2SMarkus Hoffrogge * @access public 334e8224fc2SMarkus Hoffrogge * @return string 335e8224fc2SMarkus Hoffrogge */ 336e8224fc2SMarkus Hoffrogge public function getEmailAddressOnErrorConfigured() { 337e8224fc2SMarkus Hoffrogge $emailAddressOnError = trim($this->getConf('emailAddressOnError')); 338e8224fc2SMarkus Hoffrogge return $emailAddressOnError; 339fa53f2a3SWolfgang Gassler } 340fa53f2a3SWolfgang Gassler 341fa53f2a3SWolfgang Gassler} 342fa53f2a3SWolfgang Gassler 343fa53f2a3SWolfgang Gassler// vim:ts=4:sw=4:et: 344