1<?php 2/** 3 * DokuWiki Plugin gitbacked (Action Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Wolfgang Gassler <wolfgang@gassler.org> 7 */ 8 9// must be run within Dokuwiki 10if (!defined('DOKU_INC')) die(); 11 12if (!defined('DOKU_LF')) define('DOKU_LF', "\n"); 13if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t"); 14if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 15 16require_once DOKU_PLUGIN.'action.php'; 17require_once dirname(__FILE__).'/../lib/Git.php'; 18 19class action_plugin_gitbacked_editcommit extends DokuWiki_Action_Plugin { 20 21 function __construct() { 22 global $conf; 23 $this->temp_dir = $conf['tmpdir'].'/gitbacked'; 24 io_mkdir_p($this->temp_dir); 25 } 26 27 public function register(Doku_Event_Handler $controller) { 28 29 $controller->register_hook('IO_WIKIPAGE_WRITE', 'AFTER', $this, 'handle_io_wikipage_write'); 30 $controller->register_hook('MEDIA_UPLOAD_FINISH', 'AFTER', $this, 'handle_media_upload'); 31 $controller->register_hook('MEDIA_DELETE_FILE', 'AFTER', $this, 'handle_media_deletion'); 32 $controller->register_hook('DOKUWIKI_DONE', 'AFTER', $this, 'handle_periodic_pull'); 33 } 34 35 private function initRepo() { 36 //get path to the repo root (by default DokuWiki's savedir) 37 $repoPath = $this->getConf('repoPath'); 38 if (!defined('DOKU_FARM') && $repoPath[0] !== '/') { 39 $repoPath = DOKU_INC.$this->getConf('repoPath'); 40 } 41 //set the path to the git binary 42 $gitPath = trim($this->getConf('gitPath')); 43 if ($gitPath !== '') { 44 Git::set_bin($gitPath); 45 } 46 //init the repo and create a new one if it is not present 47 io_mkdir_p($repoPath); 48 $repo = new GitRepo($repoPath, $this, true, true); 49 //set git working directory (by default DokuWiki's savedir) 50 $repoWorkDir = DOKU_INC.$this->getConf('repoWorkDir'); 51 Git::set_bin(Git::get_bin().' --work-tree '.escapeshellarg($repoWorkDir)); 52 53 $params = str_replace( 54 array('%mail%','%user%'), 55 array($this->getAuthorMail(),$this->getAuthor()), 56 $this->getConf('addParams')); 57 if ($params) { 58 Git::set_bin(Git::get_bin().' '.$params); 59 } 60 return $repo; 61 } 62 63 private function isIgnored($filePath) { 64 $ignore = false; 65 $ignorePaths = trim($this->getConf('ignorePaths')); 66 if ($ignorePaths !== '') { 67 $paths = explode(',',$ignorePaths); 68 foreach($paths as $path) { 69 if (strstr($filePath,$path)) { 70 $ignore = true; 71 } 72 } 73 } 74 return $ignore; 75 } 76 77 private function commitFile($filePath,$message) { 78 if (!$this->isIgnored($filePath)) { 79 try { 80 $repo = $this->initRepo(); 81 82 //add the changed file and set the commit message 83 $repo->add($filePath); 84 $repo->commit($message); 85 86 //if the push after Commit option is set we push the active branch to origin 87 if ($this->getConf('pushAfterCommit')) { 88 $repo->push('origin',$repo->active_branch()); 89 } 90 } catch (Exception $e) { 91 if (!$this->isNotifyByEmailOnGitCommandError()) { 92 throw new Exception('Git committing or pushing failed: '.$e->getMessage(), 1, $e); 93 } 94 return; 95 } 96 } 97 } 98 99 private function getAuthor() { 100 return $GLOBALS['USERINFO']['name']; 101 } 102 103 private function getAuthorMail() { 104 return $GLOBALS['USERINFO']['mail']; 105 } 106 107 public function handle_periodic_pull(Doku_Event &$event, $param) { 108 if ($this->getConf('periodicPull')) { 109 $lastPullFile = $this->temp_dir.'/lastpull.txt'; 110 //check if the lastPullFile exists 111 if (is_file($lastPullFile)) { 112 $lastPull = unserialize(file_get_contents($lastPullFile)); 113 } else { 114 $lastPull = 0; 115 } 116 //calculate time between pulls in seconds 117 $timeToWait = $this->getConf('periodicMinutes')*60; 118 $now = time(); 119 120 //if it is time to run a pull request 121 if ($lastPull+$timeToWait < $now) { 122 try { 123 $repo = $this->initRepo(); 124 125 //execute the pull request 126 $repo->pull('origin',$repo->active_branch()); 127 } catch (Exception $e) { 128 if (!$this->isNotifyByEmailOnGitCommandError()) { 129 throw new Exception('Git command failed to perform periodic pull: '.$e->getMessage(), 2, $e); 130 } 131 return; 132 } 133 134 //save the current time to the file to track the last pull execution 135 file_put_contents($lastPullFile,serialize(time())); 136 } 137 } 138 } 139 140 public function handle_media_deletion(Doku_Event &$event, $param) { 141 $mediaPath = $event->data['path']; 142 $mediaName = $event->data['name']; 143 144 $message = str_replace( 145 array('%media%','%user%'), 146 array($mediaName,$this->getAuthor()), 147 $this->getConf('commitMediaMsgDel') 148 ); 149 150 $this->commitFile($mediaPath,$message); 151 152 } 153 154 public function handle_media_upload(Doku_Event &$event, $param) { 155 156 $mediaPath = $event->data[1]; 157 $mediaName = $event->data[2]; 158 159 $message = str_replace( 160 array('%media%','%user%'), 161 array($mediaName,$this->getAuthor()), 162 $this->getConf('commitMediaMsg') 163 ); 164 165 $this->commitFile($mediaPath,$message); 166 167 } 168 169 public function handle_io_wikipage_write(Doku_Event &$event, $param) { 170 171 $rev = $event->data[3]; 172 173 /* On update to an existing page this event is called twice, 174 * once for the transfer of the old version to the attic (rev will have a value) 175 * and once to write the new version of the page into the wiki (rev is false) 176 */ 177 if (!$rev) { 178 179 $pagePath = $event->data[0][0]; 180 $pageName = $event->data[2]; 181 $pageContent = $event->data[0][1]; 182 183 // get the summary directly from the form input 184 // as the metadata hasn't updated yet 185 $editSummary = $GLOBALS['INPUT']->str('summary'); 186 187 // empty content indicates a page deletion 188 if ($pageContent == '') { 189 // get the commit text for deletions 190 $msgTemplate = $this->getConf('commitPageMsgDel'); 191 192 // bad hack as DokuWiki deletes the file after this event 193 // thus, let's delete the file by ourselves, so git can recognize the deletion 194 // DokuWiki uses @unlink as well, so no error should be thrown if we delete it twice 195 @unlink($pagePath); 196 197 } else { 198 //get the commit text for edits 199 $msgTemplate = $this->getConf('commitPageMsg'); 200 } 201 202 $message = str_replace( 203 array('%page%','%summary%','%user%'), 204 array($pageName,$editSummary,$this->getAuthor()), 205 $msgTemplate 206 ); 207 208 $this->commitFile($pagePath,$message); 209 210 } 211 } 212 213 // ====== Error notification helpers ====== 214 /** 215 * Notifies error on create_new 216 * 217 * @access public 218 * @param string repository path 219 * @param string reference path / remote reference 220 * @param string error message 221 * @return bool 222 */ 223 public function notify_create_new_error($repo_path, $reference, $error_message) { 224 $template_replacements = array( 225 'GIT_REPO_PATH' => $repo_path, 226 'GIT_REFERENCE' => (empty($reference) ? 'n/a' : $reference), 227 'GIT_ERROR_MESSAGE' => $error_message 228 ); 229 return $this->notifyByMail('mail_create_new_error_subject', 'mail_create_new_error', $template_replacements); 230 } 231 232 /** 233 * Notifies error on setting repo path 234 * 235 * @access public 236 * @param string repository path 237 * @param string error message 238 * @return bool 239 */ 240 public function notify_repo_path_error($repo_path, $error_message) { 241 $template_replacements = array( 242 'GIT_REPO_PATH' => $repo_path, 243 'GIT_ERROR_MESSAGE' => $error_message 244 ); 245 return $this->notifyByMail('mail_repo_path_error_subject', 'mail_repo_path_error', $template_replacements); 246 } 247 248 /** 249 * Notifies error on git command 250 * 251 * @access public 252 * @param string repository path 253 * @param string current working dir 254 * @param string command line 255 * @param int exit code of command (status) 256 * @param string error message 257 * @return bool 258 */ 259 public function notify_command_error($repo_path, $cwd, $command, $status, $error_message) { 260 $template_replacements = array( 261 'GIT_REPO_PATH' => $repo_path, 262 'GIT_CWD' => $cwd, 263 'GIT_COMMAND' => $command, 264 'GIT_COMMAND_EXITCODE' => $status, 265 'GIT_ERROR_MESSAGE' => $error_message 266 ); 267 return $this->notifyByMail('mail_command_error_subject', 'mail_command_error', $template_replacements); 268 } 269 270 /** 271 * Notifies success on git command 272 * 273 * @access public 274 * @param string repository path 275 * @param string current working dir 276 * @param string command line 277 * @return bool 278 */ 279 public function notify_command_success($repo_path, $cwd, $command) { 280 if (!$this->getConf('notifyByMailOnSuccess')) { 281 return false; 282 } 283 $template_replacements = array( 284 'GIT_REPO_PATH' => $repo_path, 285 'GIT_CWD' => $cwd, 286 'GIT_COMMAND' => $command 287 ); 288 return $this->notifyByMail('mail_command_success_subject', 'mail_command_success', $template_replacements); 289 } 290 291 /** 292 * Send an eMail, if eMail address is configured 293 * 294 * @access public 295 * @param string lang id for the subject 296 * @param string lang id for the template(.txt) 297 * @param array array of replacements 298 * @return bool 299 */ 300 public function notifyByMail($subject_id, $template_id, $template_replacements) { 301 $ret = false; 302 dbglog("GitBacked - notifyByMail: [subject_id=".$subject_id.", template_id=".$template_id.", template_replacements=".$template_replacements."]"); 303 if (!$this->isNotifyByEmailOnGitCommandError()) { 304 return $ret; 305 } 306 //$template_text = rawLocale($template_id); // this works for core artifacts only - not for plugins 307 $template_filename = $this->localFN($template_id); 308 $template_text = file_get_contents($template_filename); 309 $template_html = $this->render_text($template_text); 310 311 $mailer = new \Mailer(); 312 $mailer->to($this->getEmailAddressOnErrorConfigured()); 313 dbglog("GitBacked - lang check['".$subject_id."']: ".$this->getLang($subject_id)); 314 dbglog("GitBacked - template text['".$template_id."']: ".$template_text); 315 dbglog("GitBacked - template html['".$template_id."']: ".$template_html); 316 $mailer->subject($this->getLang($subject_id)); 317 $mailer->setBody($template_text, $template_replacements, null, $template_html); 318 $ret = $mailer->send(); 319 320 return $ret; 321 } 322 323 /** 324 * Check, if eMail is to be sent on a Git command error. 325 * 326 * @access public 327 * @return bool 328 */ 329 public function isNotifyByEmailOnGitCommandError() { 330 $emailAddressOnError = $this->getEmailAddressOnErrorConfigured(); 331 return !empty($emailAddressOnError); 332 } 333 334 /** 335 * Get the eMail address configured for notifications. 336 * 337 * @access public 338 * @return string 339 */ 340 public function getEmailAddressOnErrorConfigured() { 341 $emailAddressOnError = trim($this->getConf('emailAddressOnError')); 342 return $emailAddressOnError; 343 } 344 345} 346 347// vim:ts=4:sw=4:et: 348