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