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 if(defined('DOKU_FARM')) { 38 $repoPath = $this->getConf('repoPath'); 39 } else { 40 $repoPath = DOKU_INC.$this->getConf('repoPath'); 41 } 42 //init the repo and create a new one if it is not present 43 io_mkdir_p($repoPath); 44 $repo = new GitRepo($repoPath, true, true); 45 //set git working directory (by default DokuWiki's savedir) 46 $repoWorkDir = DOKU_INC.$this->getConf('repoWorkDir'); 47 $repo->git_path .= ' --work-tree '.escapeshellarg($repoWorkDir); 48 49 $params = str_replace( 50 array('%mail%','%user%'), 51 array($this->getAuthorMail(),$this->getAuthor()), 52 $this->getConf('addParams')); 53 if ($params) { 54 $repo->git_path .= ' '.$params; 55 } 56 return $repo; 57 } 58 59 private function commitFile($filePath,$message) { 60 61 $repo = $this->initRepo(); 62 63 //add the changed file and set the commit message 64 $repo->add($filePath); 65 $repo->commit($message); 66 67 //if the push after Commit option is set we push the active branch to origin 68 if ($this->getConf('pushAfterCommit')) { 69 $repo->push('origin',$repo->active_branch()); 70 } 71 72 } 73 74 private function getAuthor() { 75 return $GLOBALS['USERINFO']['name']; 76 } 77 78 private function getAuthorMail() { 79 return $GLOBALS['USERINFO']['mail']; 80 } 81 82 public function handle_periodic_pull(Doku_Event &$event, $param) { 83 if ($this->getConf('periodicPull')) { 84 $lastPullFile = $this->temp_dir.'/lastpull.txt'; 85 //check if the lastPullFile exists 86 if (is_file($lastPullFile)) { 87 $lastPull = unserialize(file_get_contents($lastPullFile)); 88 } else { 89 $lastPull = 0; 90 } 91 //calculate time between pulls in seconds 92 $timeToWait = $this->getConf('periodicMinutes')*60; 93 $now = time(); 94 95 //if it is time to run a pull request 96 if ($lastPull+$timeToWait < $now) { 97 $repo = $this->initRepo(); 98 99 //execute the pull request 100 $repo->pull('origin',$repo->active_branch()); 101 102 //save the current time to the file to track the last pull execution 103 file_put_contents($lastPullFile,serialize(time())); 104 } 105 } 106 } 107 108 public function handle_media_deletion(Doku_Event &$event, $param) { 109 $mediaPath = $event->data['path']; 110 $mediaName = $event->data['name']; 111 112 $message = str_replace( 113 array('%media%','%user%'), 114 array($mediaName,$this->getAuthor()), 115 $this->getConf('commitMediaMsgDel') 116 ); 117 118 $this->commitFile($mediaPath,$message); 119 120 } 121 122 public function handle_media_upload(Doku_Event &$event, $param) { 123 124 $mediaPath = $event->data[1]; 125 $mediaName = $event->data[2]; 126 127 $message = str_replace( 128 array('%media%','%user%'), 129 array($mediaName,$this->getAuthor()), 130 $this->getConf('commitMediaMsg') 131 ); 132 133 $this->commitFile($mediaPath,$message); 134 135 } 136 137 public function handle_io_wikipage_write(Doku_Event &$event, $param) { 138 139 $rev = $event->data[3]; 140 141 /* On update to an existing page this event is called twice, 142 * once for the transfer of the old version to the attic (rev will have a value) 143 * and once to write the new version of the page into the wiki (rev is false) 144 */ 145 if (!$rev) { 146 147 $pagePath = $event->data[0][0]; 148 $pageName = $event->data[2]; 149 $pageContent = $event->data[0][1]; 150 151 // get the summary directly from the form input 152 // as the metadata hasn't updated yet 153 $editSummary = $GLOBALS['INPUT']->str('summary'); 154 155 // empty content indicates a page deletion 156 if ($pageContent == '') { 157 // get the commit text for deletions 158 $msgTemplate = $this->getConf('commitPageMsgDel'); 159 160 // bad hack as DokuWiki deletes the file after this event 161 // thus, let's delete the file by ourselves, so git can recognize the deletion 162 // DokuWiki uses @unlink as well, so no error should be thrown if we delete it twice 163 @unlink($pagePath); 164 165 } else { 166 //get the commit text for edits 167 $msgTemplate = $this->getConf('commitPageMsg'); 168 } 169 170 $message = str_replace( 171 array('%page%','%summary%','%user%'), 172 array($pageName,$editSummary,$this->getAuthor()), 173 $msgTemplate 174 ); 175 176 $this->commitFile($pagePath,$message); 177 178 } 179 180 } 181 182} 183 184// vim:ts=4:sw=4:et: 185