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