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 public function handle_periodic_pull(Doku_Event &$event, $param) { 36b92b117aSWolfgang Gassler if ($this->getConf('periodicPull')) { 3700ce3f12SDanny Lin $lastPullFile = $this->temp_dir.'/lastpull.txt'; 38b92b117aSWolfgang Gassler //check if the lastPullFile exists 39b92b117aSWolfgang Gassler if (is_file($lastPullFile)) { 40b92b117aSWolfgang Gassler $lastPull = unserialize(file_get_contents($lastPullFile)); 41b92b117aSWolfgang Gassler } else { 42b92b117aSWolfgang Gassler $lastPull = 0; 43b92b117aSWolfgang Gassler } 44b92b117aSWolfgang Gassler //calculate time between pulls in seconds 45b92b117aSWolfgang Gassler $timeToWait = $this->getConf('periodicMinutes')*60; 46b92b117aSWolfgang Gassler $now = time(); 47442c3981SWolfgang Gassler 48b92b117aSWolfgang Gassler //if it is time to run a pull request 49b92b117aSWolfgang Gassler if ($lastPull+$timeToWait < $now) { 50b92b117aSWolfgang Gassler $repo = $this->initRepo(); 51b92b117aSWolfgang Gassler 52b92b117aSWolfgang Gassler //execute the pull request 53b92b117aSWolfgang Gassler $repo->pull('origin',$repo->active_branch()); 54b92b117aSWolfgang Gassler 55b92b117aSWolfgang Gassler //save the current time to the file to track the last pull execution 56b92b117aSWolfgang Gassler file_put_contents($lastPullFile,serialize(time())); 57b92b117aSWolfgang Gassler } 58b92b117aSWolfgang Gassler } 59b92b117aSWolfgang Gassler } 60b92b117aSWolfgang Gassler 61b92b117aSWolfgang Gassler private function initRepo() { 62442c3981SWolfgang Gassler //get path to the repo root (by default DokuWiki's savedir) 63442c3981SWolfgang Gassler $repoPath = DOKU_INC.$this->getConf('repoPath'); 64442c3981SWolfgang Gassler //init the repo and create a new one if it is not present 65*4eba9b44SDanny Lin io_mkdir_p($repoPath); 66442c3981SWolfgang Gassler $repo = new GitRepo($repoPath, true, true); 67*4eba9b44SDanny Lin //set git working directory (by default DokuWiki's savedir) 68*4eba9b44SDanny Lin $repoWorkDir = DOKU_INC.$this->getConf('repoWorkDir'); 69*4eba9b44SDanny Lin $repo->git_path .= ' --work-tree '.escapeshellarg($repoWorkDir); 70442c3981SWolfgang Gassler 71442c3981SWolfgang Gassler $params = $this->getConf('addParams'); 72442c3981SWolfgang Gassler if ($params) { 73442c3981SWolfgang Gassler $repo->git_path .= ' '.$params; 74442c3981SWolfgang Gassler } 75b92b117aSWolfgang Gassler return $repo; 76b92b117aSWolfgang Gassler } 77b92b117aSWolfgang Gassler 78b92b117aSWolfgang Gassler private function commitFile($filePath,$message) { 79b92b117aSWolfgang Gassler 80b92b117aSWolfgang Gassler $repo = $this->initRepo(); 81442c3981SWolfgang Gassler 82442c3981SWolfgang Gassler //add the changed file and set the commit message 83442c3981SWolfgang Gassler $repo->add($filePath); 84442c3981SWolfgang Gassler $repo->commit($message); 85442c3981SWolfgang Gassler 86442c3981SWolfgang Gassler //if the push after Commit option is set we push the active branch to origin 87442c3981SWolfgang Gassler if ($this->getConf('pushAfterCommit')) { 88442c3981SWolfgang Gassler $repo->push('origin',$repo->active_branch()); 89442c3981SWolfgang Gassler } 90442c3981SWolfgang Gassler 91442c3981SWolfgang Gassler } 92442c3981SWolfgang Gassler 93442c3981SWolfgang Gassler private function getAuthor() { 94442c3981SWolfgang Gassler return $GLOBALS['USERINFO']['name']; 95442c3981SWolfgang Gassler } 96442c3981SWolfgang Gassler 97442c3981SWolfgang Gassler public function handle_media_deletion(Doku_Event &$event, $param) { 98442c3981SWolfgang Gassler $mediaPath = $event->data['path']; 99442c3981SWolfgang Gassler $mediaName = $event->data['name']; 100442c3981SWolfgang Gassler 101442c3981SWolfgang Gassler $message = str_replace( 102442c3981SWolfgang Gassler array('%media%','%user%'), 103442c3981SWolfgang Gassler array($mediaName,$this->getAuthor()), 104442c3981SWolfgang Gassler $this->getConf('commitMediaMsgDel') 105442c3981SWolfgang Gassler ); 106442c3981SWolfgang Gassler 107442c3981SWolfgang Gassler $this->commitFile($mediaPath,$message); 108442c3981SWolfgang Gassler 109442c3981SWolfgang Gassler } 110442c3981SWolfgang Gassler 111442c3981SWolfgang Gassler public function handle_media_upload(Doku_Event &$event, $param) { 112442c3981SWolfgang Gassler 113442c3981SWolfgang Gassler $mediaPath = $event->data[1]; 114442c3981SWolfgang Gassler $mediaName = $event->data[2]; 115442c3981SWolfgang Gassler 116442c3981SWolfgang Gassler $message = str_replace( 117442c3981SWolfgang Gassler array('%media%','%user%'), 118442c3981SWolfgang Gassler array($mediaName,$this->getAuthor()), 119442c3981SWolfgang Gassler $this->getConf('commitMediaMsg') 120442c3981SWolfgang Gassler ); 121442c3981SWolfgang Gassler 122442c3981SWolfgang Gassler $this->commitFile($mediaPath,$message); 123fa53f2a3SWolfgang Gassler 124fa53f2a3SWolfgang Gassler } 125fa53f2a3SWolfgang Gassler 126fa53f2a3SWolfgang Gassler public function handle_io_wikipage_write(Doku_Event &$event, $param) { 127fa53f2a3SWolfgang Gassler 128fa53f2a3SWolfgang Gassler $rev = $event->data[3]; 129fa53f2a3SWolfgang Gassler 130fa53f2a3SWolfgang Gassler /* On update to an existing page this event is called twice, 131fa53f2a3SWolfgang Gassler * once for the transfer of the old version to the attic (rev will have a value) 132fa53f2a3SWolfgang Gassler * and once to write the new version of the page into the wiki (rev is false) 133fa53f2a3SWolfgang Gassler */ 134fa53f2a3SWolfgang Gassler if (!$rev) { 135fa53f2a3SWolfgang Gassler 136fa53f2a3SWolfgang Gassler $pagePath = $event->data[0][0]; 137fa53f2a3SWolfgang Gassler $pageName = $event->data[2]; 138442c3981SWolfgang Gassler $pageContent = $event->data[0][1]; 139fa53f2a3SWolfgang Gassler 1407af27dc9SWolfgang Gassler // get the summary directly from the form input 141e7471cfaSDanny Lin // as the metadata hasn't updated yet 1427af27dc9SWolfgang Gassler $editSummary = $GLOBALS['INPUT']->str('summary'); 143442c3981SWolfgang Gassler 144442c3981SWolfgang Gassler // empty content indicates a page deletion 145442c3981SWolfgang Gassler if ($pageContent == '') { 146442c3981SWolfgang Gassler // get the commit text for deletions 147d4e1c54bSWolfgang Gassler $msgTemplate = $this->getConf('commitPageMsgDel'); 148442c3981SWolfgang Gassler 149442c3981SWolfgang Gassler // bad hack as DokuWiki deletes the file after this event 150442c3981SWolfgang Gassler // thus, let's delete the file by ourselves, so git can recognize the deletion 151442c3981SWolfgang Gassler // DokuWiki uses @unlink as well, so no error should be thrown if we delete it twice 152442c3981SWolfgang Gassler @unlink($pagePath); 153442c3981SWolfgang Gassler 154442c3981SWolfgang Gassler } else { 155442c3981SWolfgang Gassler //get the commit text for edits 156d4e1c54bSWolfgang Gassler $msgTemplate = $this->getConf('commitPageMsg'); 157442c3981SWolfgang Gassler } 158442c3981SWolfgang Gassler 159fa53f2a3SWolfgang Gassler $message = str_replace( 160fa53f2a3SWolfgang Gassler array('%page%','%summary%','%user%'), 161442c3981SWolfgang Gassler array($pageName,$editSummary,$this->getAuthor()), 162442c3981SWolfgang Gassler $msgTemplate 163fa53f2a3SWolfgang Gassler ); 164fa53f2a3SWolfgang Gassler 165442c3981SWolfgang Gassler $this->commitFile($pagePath,$message); 166fa53f2a3SWolfgang Gassler 167fa53f2a3SWolfgang Gassler } 168fa53f2a3SWolfgang Gassler 169fa53f2a3SWolfgang Gassler } 170fa53f2a3SWolfgang Gassler 171fa53f2a3SWolfgang Gassler} 172fa53f2a3SWolfgang Gassler 173fa53f2a3SWolfgang Gassler// vim:ts=4:sw=4:et: 174