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 654eba9b44SDanny Lin io_mkdir_p($repoPath); 66442c3981SWolfgang Gassler $repo = new GitRepo($repoPath, true, true); 674eba9b44SDanny Lin //set git working directory (by default DokuWiki's savedir) 684eba9b44SDanny Lin $repoWorkDir = DOKU_INC.$this->getConf('repoWorkDir'); 694eba9b44SDanny Lin $repo->git_path .= ' --work-tree '.escapeshellarg($repoWorkDir); 70442c3981SWolfgang Gassler 71*0d7cb616SBirkir A. Barkarson $params = str_replace( 72*0d7cb616SBirkir A. Barkarson array('%mail%','%user%'), 73*0d7cb616SBirkir A. Barkarson array($this->getAuthorMail(),$this->getAuthor()), 74*0d7cb616SBirkir A. Barkarson $this->getConf('addParams')); 75442c3981SWolfgang Gassler if ($params) { 76442c3981SWolfgang Gassler $repo->git_path .= ' '.$params; 77442c3981SWolfgang Gassler } 78b92b117aSWolfgang Gassler return $repo; 79b92b117aSWolfgang Gassler } 80b92b117aSWolfgang Gassler 81b92b117aSWolfgang Gassler private function commitFile($filePath,$message) { 82b92b117aSWolfgang Gassler 83b92b117aSWolfgang Gassler $repo = $this->initRepo(); 84442c3981SWolfgang Gassler 85442c3981SWolfgang Gassler //add the changed file and set the commit message 86442c3981SWolfgang Gassler $repo->add($filePath); 87442c3981SWolfgang Gassler $repo->commit($message); 88442c3981SWolfgang Gassler 89442c3981SWolfgang Gassler //if the push after Commit option is set we push the active branch to origin 90442c3981SWolfgang Gassler if ($this->getConf('pushAfterCommit')) { 91442c3981SWolfgang Gassler $repo->push('origin',$repo->active_branch()); 92442c3981SWolfgang Gassler } 93442c3981SWolfgang Gassler 94442c3981SWolfgang Gassler } 95442c3981SWolfgang Gassler 96442c3981SWolfgang Gassler private function getAuthor() { 97442c3981SWolfgang Gassler return $GLOBALS['USERINFO']['name']; 98442c3981SWolfgang Gassler } 99442c3981SWolfgang Gassler 100*0d7cb616SBirkir A. Barkarson private function getAuthorMail() { 101*0d7cb616SBirkir A. Barkarson return $GLOBALS['USERINFO']['mail']; 102*0d7cb616SBirkir A. Barkarson } 103*0d7cb616SBirkir A. Barkarson 104442c3981SWolfgang Gassler public function handle_media_deletion(Doku_Event &$event, $param) { 105442c3981SWolfgang Gassler $mediaPath = $event->data['path']; 106442c3981SWolfgang Gassler $mediaName = $event->data['name']; 107442c3981SWolfgang Gassler 108442c3981SWolfgang Gassler $message = str_replace( 109442c3981SWolfgang Gassler array('%media%','%user%'), 110442c3981SWolfgang Gassler array($mediaName,$this->getAuthor()), 111442c3981SWolfgang Gassler $this->getConf('commitMediaMsgDel') 112442c3981SWolfgang Gassler ); 113442c3981SWolfgang Gassler 114442c3981SWolfgang Gassler $this->commitFile($mediaPath,$message); 115442c3981SWolfgang Gassler 116442c3981SWolfgang Gassler } 117442c3981SWolfgang Gassler 118442c3981SWolfgang Gassler public function handle_media_upload(Doku_Event &$event, $param) { 119442c3981SWolfgang Gassler 120442c3981SWolfgang Gassler $mediaPath = $event->data[1]; 121442c3981SWolfgang Gassler $mediaName = $event->data[2]; 122442c3981SWolfgang Gassler 123442c3981SWolfgang Gassler $message = str_replace( 124442c3981SWolfgang Gassler array('%media%','%user%'), 125442c3981SWolfgang Gassler array($mediaName,$this->getAuthor()), 126442c3981SWolfgang Gassler $this->getConf('commitMediaMsg') 127442c3981SWolfgang Gassler ); 128442c3981SWolfgang Gassler 129442c3981SWolfgang Gassler $this->commitFile($mediaPath,$message); 130fa53f2a3SWolfgang Gassler 131fa53f2a3SWolfgang Gassler } 132fa53f2a3SWolfgang Gassler 133fa53f2a3SWolfgang Gassler public function handle_io_wikipage_write(Doku_Event &$event, $param) { 134fa53f2a3SWolfgang Gassler 135fa53f2a3SWolfgang Gassler $rev = $event->data[3]; 136fa53f2a3SWolfgang Gassler 137fa53f2a3SWolfgang Gassler /* On update to an existing page this event is called twice, 138fa53f2a3SWolfgang Gassler * once for the transfer of the old version to the attic (rev will have a value) 139fa53f2a3SWolfgang Gassler * and once to write the new version of the page into the wiki (rev is false) 140fa53f2a3SWolfgang Gassler */ 141fa53f2a3SWolfgang Gassler if (!$rev) { 142fa53f2a3SWolfgang Gassler 143fa53f2a3SWolfgang Gassler $pagePath = $event->data[0][0]; 144fa53f2a3SWolfgang Gassler $pageName = $event->data[2]; 145442c3981SWolfgang Gassler $pageContent = $event->data[0][1]; 146fa53f2a3SWolfgang Gassler 1477af27dc9SWolfgang Gassler // get the summary directly from the form input 148e7471cfaSDanny Lin // as the metadata hasn't updated yet 1497af27dc9SWolfgang Gassler $editSummary = $GLOBALS['INPUT']->str('summary'); 150442c3981SWolfgang Gassler 151442c3981SWolfgang Gassler // empty content indicates a page deletion 152442c3981SWolfgang Gassler if ($pageContent == '') { 153442c3981SWolfgang Gassler // get the commit text for deletions 154d4e1c54bSWolfgang Gassler $msgTemplate = $this->getConf('commitPageMsgDel'); 155442c3981SWolfgang Gassler 156442c3981SWolfgang Gassler // bad hack as DokuWiki deletes the file after this event 157442c3981SWolfgang Gassler // thus, let's delete the file by ourselves, so git can recognize the deletion 158442c3981SWolfgang Gassler // DokuWiki uses @unlink as well, so no error should be thrown if we delete it twice 159442c3981SWolfgang Gassler @unlink($pagePath); 160442c3981SWolfgang Gassler 161442c3981SWolfgang Gassler } else { 162442c3981SWolfgang Gassler //get the commit text for edits 163d4e1c54bSWolfgang Gassler $msgTemplate = $this->getConf('commitPageMsg'); 164442c3981SWolfgang Gassler } 165442c3981SWolfgang Gassler 166fa53f2a3SWolfgang Gassler $message = str_replace( 167fa53f2a3SWolfgang Gassler array('%page%','%summary%','%user%'), 168442c3981SWolfgang Gassler array($pageName,$editSummary,$this->getAuthor()), 169442c3981SWolfgang Gassler $msgTemplate 170fa53f2a3SWolfgang Gassler ); 171fa53f2a3SWolfgang Gassler 172442c3981SWolfgang Gassler $this->commitFile($pagePath,$message); 173fa53f2a3SWolfgang Gassler 174fa53f2a3SWolfgang Gassler } 175fa53f2a3SWolfgang Gassler 176fa53f2a3SWolfgang Gassler } 177fa53f2a3SWolfgang Gassler 178fa53f2a3SWolfgang Gassler} 179fa53f2a3SWolfgang Gassler 180fa53f2a3SWolfgang Gassler// vim:ts=4:sw=4:et: 181