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'; 18fa53f2a3SWolfgang Gassler 19fa53f2a3SWolfgang Gasslerclass action_plugin_gitbacked_editcommit extends DokuWiki_Action_Plugin { 20fa53f2a3SWolfgang Gassler 2100ce3f12SDanny Lin function __construct() { 2200ce3f12SDanny Lin global $conf; 2300ce3f12SDanny Lin $this->temp_dir = $conf['tmpdir'].'/gitbacked'; 2400ce3f12SDanny Lin io_mkdir_p($this->temp_dir); 2500ce3f12SDanny Lin } 2600ce3f12SDanny Lin 27fa53f2a3SWolfgang Gassler public function register(Doku_Event_Handler &$controller) { 28fa53f2a3SWolfgang Gassler 29fa53f2a3SWolfgang Gassler $controller->register_hook('IO_WIKIPAGE_WRITE', 'AFTER', $this, 'handle_io_wikipage_write'); 30442c3981SWolfgang Gassler $controller->register_hook('MEDIA_UPLOAD_FINISH', 'AFTER', $this, 'handle_media_upload'); 31442c3981SWolfgang Gassler $controller->register_hook('MEDIA_DELETE_FILE', 'AFTER', $this, 'handle_media_deletion'); 32b92b117aSWolfgang Gassler $controller->register_hook('DOKUWIKI_DONE', 'AFTER', $this, 'handle_periodic_pull'); 33442c3981SWolfgang Gassler } 34442c3981SWolfgang Gassler 35b92b117aSWolfgang Gassler private function initRepo() { 36442c3981SWolfgang Gassler //get path to the repo root (by default DokuWiki's savedir) 3738f8ac72SWolfgang Gassler if(defined('DOKU_FARM')) { 3838f8ac72SWolfgang Gassler $repoPath = $this->getConf('repoPath'); 3938f8ac72SWolfgang Gassler } else { 40442c3981SWolfgang Gassler $repoPath = DOKU_INC.$this->getConf('repoPath'); 4138f8ac72SWolfgang Gassler } 42442c3981SWolfgang Gassler //init the repo and create a new one if it is not present 434eba9b44SDanny Lin io_mkdir_p($repoPath); 44442c3981SWolfgang Gassler $repo = new GitRepo($repoPath, true, true); 454eba9b44SDanny Lin //set git working directory (by default DokuWiki's savedir) 464eba9b44SDanny Lin $repoWorkDir = DOKU_INC.$this->getConf('repoWorkDir'); 47*985a1bc7SCarsten Teibes Git::set_bin(Git::get_bin().' --work-tree '.escapeshellarg($repoWorkDir)); 48442c3981SWolfgang Gassler 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) { 54*985a1bc7SCarsten 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) { 74b92b117aSWolfgang Gassler 7566f21a70SWolfgang Gassler if (!$this->isIgnored($filePath)) { 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 } 8666f21a70SWolfgang Gassler } 87442c3981SWolfgang Gassler 88442c3981SWolfgang Gassler } 89442c3981SWolfgang Gassler 90442c3981SWolfgang Gassler private function getAuthor() { 91442c3981SWolfgang Gassler return $GLOBALS['USERINFO']['name']; 92442c3981SWolfgang Gassler } 93442c3981SWolfgang Gassler 940d7cb616SBirkir A. Barkarson private function getAuthorMail() { 950d7cb616SBirkir A. Barkarson return $GLOBALS['USERINFO']['mail']; 960d7cb616SBirkir A. Barkarson } 970d7cb616SBirkir A. Barkarson 982377428fSDanny Lin public function handle_periodic_pull(Doku_Event &$event, $param) { 992377428fSDanny Lin if ($this->getConf('periodicPull')) { 1002377428fSDanny Lin $lastPullFile = $this->temp_dir.'/lastpull.txt'; 1012377428fSDanny Lin //check if the lastPullFile exists 1022377428fSDanny Lin if (is_file($lastPullFile)) { 1032377428fSDanny Lin $lastPull = unserialize(file_get_contents($lastPullFile)); 1042377428fSDanny Lin } else { 1052377428fSDanny Lin $lastPull = 0; 1062377428fSDanny Lin } 1072377428fSDanny Lin //calculate time between pulls in seconds 1082377428fSDanny Lin $timeToWait = $this->getConf('periodicMinutes')*60; 1092377428fSDanny Lin $now = time(); 1102377428fSDanny Lin 1112377428fSDanny Lin //if it is time to run a pull request 1122377428fSDanny Lin if ($lastPull+$timeToWait < $now) { 1132377428fSDanny Lin $repo = $this->initRepo(); 1142377428fSDanny Lin 1152377428fSDanny Lin //execute the pull request 1162377428fSDanny Lin $repo->pull('origin',$repo->active_branch()); 1172377428fSDanny Lin 1182377428fSDanny Lin //save the current time to the file to track the last pull execution 1192377428fSDanny Lin file_put_contents($lastPullFile,serialize(time())); 1202377428fSDanny Lin } 1212377428fSDanny Lin } 1222377428fSDanny Lin } 1232377428fSDanny Lin 124442c3981SWolfgang Gassler public function handle_media_deletion(Doku_Event &$event, $param) { 125442c3981SWolfgang Gassler $mediaPath = $event->data['path']; 126442c3981SWolfgang Gassler $mediaName = $event->data['name']; 127442c3981SWolfgang Gassler 128442c3981SWolfgang Gassler $message = str_replace( 129442c3981SWolfgang Gassler array('%media%','%user%'), 130442c3981SWolfgang Gassler array($mediaName,$this->getAuthor()), 131442c3981SWolfgang Gassler $this->getConf('commitMediaMsgDel') 132442c3981SWolfgang Gassler ); 133442c3981SWolfgang Gassler 134442c3981SWolfgang Gassler $this->commitFile($mediaPath,$message); 135442c3981SWolfgang Gassler 136442c3981SWolfgang Gassler } 137442c3981SWolfgang Gassler 138442c3981SWolfgang Gassler public function handle_media_upload(Doku_Event &$event, $param) { 139442c3981SWolfgang Gassler 140442c3981SWolfgang Gassler $mediaPath = $event->data[1]; 141442c3981SWolfgang Gassler $mediaName = $event->data[2]; 142442c3981SWolfgang Gassler 143442c3981SWolfgang Gassler $message = str_replace( 144442c3981SWolfgang Gassler array('%media%','%user%'), 145442c3981SWolfgang Gassler array($mediaName,$this->getAuthor()), 146442c3981SWolfgang Gassler $this->getConf('commitMediaMsg') 147442c3981SWolfgang Gassler ); 148442c3981SWolfgang Gassler 149442c3981SWolfgang Gassler $this->commitFile($mediaPath,$message); 150fa53f2a3SWolfgang Gassler 151fa53f2a3SWolfgang Gassler } 152fa53f2a3SWolfgang Gassler 153fa53f2a3SWolfgang Gassler public function handle_io_wikipage_write(Doku_Event &$event, $param) { 154fa53f2a3SWolfgang Gassler 155fa53f2a3SWolfgang Gassler $rev = $event->data[3]; 156fa53f2a3SWolfgang Gassler 157fa53f2a3SWolfgang Gassler /* On update to an existing page this event is called twice, 158fa53f2a3SWolfgang Gassler * once for the transfer of the old version to the attic (rev will have a value) 159fa53f2a3SWolfgang Gassler * and once to write the new version of the page into the wiki (rev is false) 160fa53f2a3SWolfgang Gassler */ 161fa53f2a3SWolfgang Gassler if (!$rev) { 162fa53f2a3SWolfgang Gassler 163fa53f2a3SWolfgang Gassler $pagePath = $event->data[0][0]; 164fa53f2a3SWolfgang Gassler $pageName = $event->data[2]; 165442c3981SWolfgang Gassler $pageContent = $event->data[0][1]; 166fa53f2a3SWolfgang Gassler 1677af27dc9SWolfgang Gassler // get the summary directly from the form input 168e7471cfaSDanny Lin // as the metadata hasn't updated yet 1697af27dc9SWolfgang Gassler $editSummary = $GLOBALS['INPUT']->str('summary'); 170442c3981SWolfgang Gassler 171442c3981SWolfgang Gassler // empty content indicates a page deletion 172442c3981SWolfgang Gassler if ($pageContent == '') { 173442c3981SWolfgang Gassler // get the commit text for deletions 174d4e1c54bSWolfgang Gassler $msgTemplate = $this->getConf('commitPageMsgDel'); 175442c3981SWolfgang Gassler 176442c3981SWolfgang Gassler // bad hack as DokuWiki deletes the file after this event 177442c3981SWolfgang Gassler // thus, let's delete the file by ourselves, so git can recognize the deletion 178442c3981SWolfgang Gassler // DokuWiki uses @unlink as well, so no error should be thrown if we delete it twice 179442c3981SWolfgang Gassler @unlink($pagePath); 180442c3981SWolfgang Gassler 181442c3981SWolfgang Gassler } else { 182442c3981SWolfgang Gassler //get the commit text for edits 183d4e1c54bSWolfgang Gassler $msgTemplate = $this->getConf('commitPageMsg'); 184442c3981SWolfgang Gassler } 185442c3981SWolfgang Gassler 186fa53f2a3SWolfgang Gassler $message = str_replace( 187fa53f2a3SWolfgang Gassler array('%page%','%summary%','%user%'), 188442c3981SWolfgang Gassler array($pageName,$editSummary,$this->getAuthor()), 189442c3981SWolfgang Gassler $msgTemplate 190fa53f2a3SWolfgang Gassler ); 191fa53f2a3SWolfgang Gassler 192442c3981SWolfgang Gassler $this->commitFile($pagePath,$message); 193fa53f2a3SWolfgang Gassler 194fa53f2a3SWolfgang Gassler } 195fa53f2a3SWolfgang Gassler 196fa53f2a3SWolfgang Gassler } 197fa53f2a3SWolfgang Gassler 198fa53f2a3SWolfgang Gassler} 199fa53f2a3SWolfgang Gassler 200fa53f2a3SWolfgang Gassler// vim:ts=4:sw=4:et: 201