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'; 17fa53f2a3SWolfgang Gasslerrequire_once(DOKU_PLUGIN.'gitbacked/lib/Git.php'); 18fa53f2a3SWolfgang Gassler 19fa53f2a3SWolfgang Gasslerclass action_plugin_gitbacked_editcommit extends DokuWiki_Action_Plugin { 20fa53f2a3SWolfgang Gassler 21fa53f2a3SWolfgang Gassler public function register(Doku_Event_Handler &$controller) { 22fa53f2a3SWolfgang Gassler 23fa53f2a3SWolfgang Gassler $controller->register_hook('IO_WIKIPAGE_WRITE', 'AFTER', $this, 'handle_io_wikipage_write'); 24442c3981SWolfgang Gassler $controller->register_hook('MEDIA_UPLOAD_FINISH', 'AFTER', $this, 'handle_media_upload'); 25442c3981SWolfgang Gassler $controller->register_hook('MEDIA_DELETE_FILE', 'AFTER', $this, 'handle_media_deletion'); 26b92b117aSWolfgang Gassler $controller->register_hook('DOKUWIKI_DONE', 'AFTER', $this, 'handle_periodic_pull'); 27442c3981SWolfgang Gassler } 28442c3981SWolfgang Gassler 29b92b117aSWolfgang Gassler public function handle_periodic_pull(Doku_Event &$event, $param) { 30b92b117aSWolfgang Gassler if ($this->getConf('periodicPull')) { 31b92b117aSWolfgang Gassler $lastPullFile = DOKU_PLUGIN.'gitbacked/action/lastpull.txt'; 32b92b117aSWolfgang Gassler //check if the lastPullFile exists 33b92b117aSWolfgang Gassler if (is_file($lastPullFile)) { 34b92b117aSWolfgang Gassler $lastPull = unserialize(file_get_contents($lastPullFile)); 35b92b117aSWolfgang Gassler } else { 36b92b117aSWolfgang Gassler $lastPull = 0; 37b92b117aSWolfgang Gassler } 38b92b117aSWolfgang Gassler //calculate time between pulls in seconds 39b92b117aSWolfgang Gassler $timeToWait = $this->getConf('periodicMinutes')*60; 40b92b117aSWolfgang Gassler $now = time(); 41442c3981SWolfgang Gassler 42b92b117aSWolfgang Gassler //if it is time to run a pull request 43b92b117aSWolfgang Gassler if ($lastPull+$timeToWait < $now) { 44b92b117aSWolfgang Gassler $repo = $this->initRepo(); 45b92b117aSWolfgang Gassler 46b92b117aSWolfgang Gassler //execute the pull request 47b92b117aSWolfgang Gassler $repo->pull('origin',$repo->active_branch()); 48b92b117aSWolfgang Gassler 49b92b117aSWolfgang Gassler //save the current time to the file to track the last pull execution 50b92b117aSWolfgang Gassler file_put_contents($lastPullFile,serialize(time())); 51b92b117aSWolfgang Gassler } 52b92b117aSWolfgang Gassler } 53b92b117aSWolfgang Gassler } 54b92b117aSWolfgang Gassler 55b92b117aSWolfgang Gassler private function initRepo() { 56442c3981SWolfgang Gassler //get path to the repo root (by default DokuWiki's savedir) 57442c3981SWolfgang Gassler $repoPath = DOKU_INC.$this->getConf('repoPath'); 58442c3981SWolfgang Gassler //init the repo and create a new one if it is not present 59442c3981SWolfgang Gassler $repo = new GitRepo($repoPath, true, true); 60442c3981SWolfgang Gassler 61442c3981SWolfgang Gassler $params = $this->getConf('addParams'); 62442c3981SWolfgang Gassler if ($params) { 63442c3981SWolfgang Gassler $repo->git_path .= ' '.$params; 64442c3981SWolfgang Gassler } 65b92b117aSWolfgang Gassler return $repo; 66b92b117aSWolfgang Gassler } 67b92b117aSWolfgang Gassler 68b92b117aSWolfgang Gassler private function commitFile($filePath,$message) { 69b92b117aSWolfgang Gassler 70b92b117aSWolfgang Gassler $repo = $this->initRepo(); 71442c3981SWolfgang Gassler 72442c3981SWolfgang Gassler //add the changed file and set the commit message 73442c3981SWolfgang Gassler $repo->add($filePath); 74442c3981SWolfgang Gassler $repo->commit($message); 75442c3981SWolfgang Gassler 76442c3981SWolfgang Gassler //if the push after Commit option is set we push the active branch to origin 77442c3981SWolfgang Gassler if ($this->getConf('pushAfterCommit')) { 78442c3981SWolfgang Gassler $repo->push('origin',$repo->active_branch()); 79442c3981SWolfgang Gassler } 80442c3981SWolfgang Gassler 81442c3981SWolfgang Gassler } 82442c3981SWolfgang Gassler 83442c3981SWolfgang Gassler private function getAuthor() { 84442c3981SWolfgang Gassler return $GLOBALS['USERINFO']['name']; 85442c3981SWolfgang Gassler } 86442c3981SWolfgang Gassler 87442c3981SWolfgang Gassler public function handle_media_deletion(Doku_Event &$event, $param) { 88442c3981SWolfgang Gassler $mediaPath = $event->data['path']; 89442c3981SWolfgang Gassler $mediaName = $event->data['name']; 90442c3981SWolfgang Gassler 91442c3981SWolfgang Gassler $message = str_replace( 92442c3981SWolfgang Gassler array('%media%','%user%'), 93442c3981SWolfgang Gassler array($mediaName,$this->getAuthor()), 94442c3981SWolfgang Gassler $this->getConf('commitMediaMsgDel') 95442c3981SWolfgang Gassler ); 96442c3981SWolfgang Gassler 97442c3981SWolfgang Gassler $this->commitFile($mediaPath,$message); 98442c3981SWolfgang Gassler 99442c3981SWolfgang Gassler } 100442c3981SWolfgang Gassler 101442c3981SWolfgang Gassler public function handle_media_upload(Doku_Event &$event, $param) { 102442c3981SWolfgang Gassler 103442c3981SWolfgang Gassler $mediaPath = $event->data[1]; 104442c3981SWolfgang Gassler $mediaName = $event->data[2]; 105442c3981SWolfgang Gassler 106442c3981SWolfgang Gassler $message = str_replace( 107442c3981SWolfgang Gassler array('%media%','%user%'), 108442c3981SWolfgang Gassler array($mediaName,$this->getAuthor()), 109442c3981SWolfgang Gassler $this->getConf('commitMediaMsg') 110442c3981SWolfgang Gassler ); 111442c3981SWolfgang Gassler 112442c3981SWolfgang Gassler $this->commitFile($mediaPath,$message); 113fa53f2a3SWolfgang Gassler 114fa53f2a3SWolfgang Gassler } 115fa53f2a3SWolfgang Gassler 116fa53f2a3SWolfgang Gassler public function handle_io_wikipage_write(Doku_Event &$event, $param) { 117fa53f2a3SWolfgang Gassler 118fa53f2a3SWolfgang Gassler $rev = $event->data[3]; 119fa53f2a3SWolfgang Gassler 120fa53f2a3SWolfgang Gassler /* On update to an existing page this event is called twice, 121fa53f2a3SWolfgang Gassler * once for the transfer of the old version to the attic (rev will have a value) 122fa53f2a3SWolfgang Gassler * and once to write the new version of the page into the wiki (rev is false) 123fa53f2a3SWolfgang Gassler */ 124fa53f2a3SWolfgang Gassler if (!$rev) { 125fa53f2a3SWolfgang Gassler 126fa53f2a3SWolfgang Gassler $pagePath = $event->data[0][0]; 127fa53f2a3SWolfgang Gassler $pageName = $event->data[2]; 128442c3981SWolfgang Gassler $pageContent = $event->data[0][1]; 129fa53f2a3SWolfgang Gassler 1307af27dc9SWolfgang Gassler // get the summary directly from the form input 131*e7471cfaSDanny Lin // as the metadata hasn't updated yet 1327af27dc9SWolfgang Gassler $editSummary = $GLOBALS['INPUT']->str('summary'); 133442c3981SWolfgang Gassler 134442c3981SWolfgang Gassler // empty content indicates a page deletion 135442c3981SWolfgang Gassler if ($pageContent == '') { 136442c3981SWolfgang Gassler // get the commit text for deletions 137d4e1c54bSWolfgang Gassler $msgTemplate = $this->getConf('commitPageMsgDel'); 138442c3981SWolfgang Gassler 139442c3981SWolfgang Gassler // bad hack as DokuWiki deletes the file after this event 140442c3981SWolfgang Gassler // thus, let's delete the file by ourselves, so git can recognize the deletion 141442c3981SWolfgang Gassler // DokuWiki uses @unlink as well, so no error should be thrown if we delete it twice 142442c3981SWolfgang Gassler @unlink($pagePath); 143442c3981SWolfgang Gassler 144442c3981SWolfgang Gassler } else { 145442c3981SWolfgang Gassler //get the commit text for edits 146d4e1c54bSWolfgang Gassler $msgTemplate = $this->getConf('commitPageMsg'); 147442c3981SWolfgang Gassler } 148442c3981SWolfgang Gassler 149fa53f2a3SWolfgang Gassler $message = str_replace( 150fa53f2a3SWolfgang Gassler array('%page%','%summary%','%user%'), 151442c3981SWolfgang Gassler array($pageName,$editSummary,$this->getAuthor()), 152442c3981SWolfgang Gassler $msgTemplate 153fa53f2a3SWolfgang Gassler ); 154fa53f2a3SWolfgang Gassler 155442c3981SWolfgang Gassler $this->commitFile($pagePath,$message); 156fa53f2a3SWolfgang Gassler 157fa53f2a3SWolfgang Gassler } 158fa53f2a3SWolfgang Gassler 159fa53f2a3SWolfgang Gassler } 160fa53f2a3SWolfgang Gassler 161fa53f2a3SWolfgang Gassler} 162fa53f2a3SWolfgang Gassler 163fa53f2a3SWolfgang Gassler// vim:ts=4:sw=4:et: 164