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'); 24*442c3981SWolfgang Gassler $controller->register_hook('MEDIA_UPLOAD_FINISH', 'AFTER', $this, 'handle_media_upload'); 25*442c3981SWolfgang Gassler $controller->register_hook('MEDIA_DELETE_FILE', 'AFTER', $this, 'handle_media_deletion'); 26*442c3981SWolfgang Gassler } 27*442c3981SWolfgang Gassler 28*442c3981SWolfgang Gassler private function commitFile($filePath,$message) { 29*442c3981SWolfgang Gassler 30*442c3981SWolfgang Gassler //get path to the repo root (by default DokuWiki's savedir) 31*442c3981SWolfgang Gassler $repoPath = DOKU_INC.$this->getConf('repoPath'); 32*442c3981SWolfgang Gassler //init the repo and create a new one if it is not present 33*442c3981SWolfgang Gassler $repo = new GitRepo($repoPath, true, true); 34*442c3981SWolfgang Gassler 35*442c3981SWolfgang Gassler $params = $this->getConf('addParams'); 36*442c3981SWolfgang Gassler if ($params) { 37*442c3981SWolfgang Gassler $repo->git_path .= ' '.$params; 38*442c3981SWolfgang Gassler } 39*442c3981SWolfgang Gassler 40*442c3981SWolfgang Gassler //add the changed file and set the commit message 41*442c3981SWolfgang Gassler $repo->add($filePath); 42*442c3981SWolfgang Gassler $repo->commit($message); 43*442c3981SWolfgang Gassler 44*442c3981SWolfgang Gassler //if the push after Commit option is set we push the active branch to origin 45*442c3981SWolfgang Gassler if ($this->getConf('pushAfterCommit')) { 46*442c3981SWolfgang Gassler $repo->push('origin',$repo->active_branch()); 47*442c3981SWolfgang Gassler } 48*442c3981SWolfgang Gassler 49*442c3981SWolfgang Gassler } 50*442c3981SWolfgang Gassler 51*442c3981SWolfgang Gassler private function getAuthor() { 52*442c3981SWolfgang Gassler return $GLOBALS['USERINFO']['name']; 53*442c3981SWolfgang Gassler } 54*442c3981SWolfgang Gassler 55*442c3981SWolfgang Gassler public function handle_media_deletion(Doku_Event &$event, $param) { 56*442c3981SWolfgang Gassler $mediaPath = $event->data['path']; 57*442c3981SWolfgang Gassler $mediaName = $event->data['name']; 58*442c3981SWolfgang Gassler 59*442c3981SWolfgang Gassler $message = str_replace( 60*442c3981SWolfgang Gassler array('%media%','%user%'), 61*442c3981SWolfgang Gassler array($mediaName,$this->getAuthor()), 62*442c3981SWolfgang Gassler $this->getConf('commitMediaMsgDel') 63*442c3981SWolfgang Gassler ); 64*442c3981SWolfgang Gassler 65*442c3981SWolfgang Gassler $this->commitFile($mediaPath,$message); 66*442c3981SWolfgang Gassler 67*442c3981SWolfgang Gassler } 68*442c3981SWolfgang Gassler 69*442c3981SWolfgang Gassler public function handle_media_upload(Doku_Event &$event, $param) { 70*442c3981SWolfgang Gassler 71*442c3981SWolfgang Gassler $mediaPath = $event->data[1]; 72*442c3981SWolfgang Gassler $mediaName = $event->data[2]; 73*442c3981SWolfgang Gassler 74*442c3981SWolfgang Gassler $message = str_replace( 75*442c3981SWolfgang Gassler array('%media%','%user%'), 76*442c3981SWolfgang Gassler array($mediaName,$this->getAuthor()), 77*442c3981SWolfgang Gassler $this->getConf('commitMediaMsg') 78*442c3981SWolfgang Gassler ); 79*442c3981SWolfgang Gassler 80*442c3981SWolfgang Gassler $this->commitFile($mediaPath,$message); 81fa53f2a3SWolfgang Gassler 82fa53f2a3SWolfgang Gassler } 83fa53f2a3SWolfgang Gassler 84fa53f2a3SWolfgang Gassler public function handle_io_wikipage_write(Doku_Event &$event, $param) { 85fa53f2a3SWolfgang Gassler 86fa53f2a3SWolfgang Gassler $rev = $event->data[3]; 87fa53f2a3SWolfgang Gassler 88fa53f2a3SWolfgang Gassler /* On update to an existing page this event is called twice, 89fa53f2a3SWolfgang Gassler * once for the transfer of the old version to the attic (rev will have a value) 90fa53f2a3SWolfgang Gassler * and once to write the new version of the page into the wiki (rev is false) 91fa53f2a3SWolfgang Gassler */ 92fa53f2a3SWolfgang Gassler if (!$rev) { 93fa53f2a3SWolfgang Gassler 94fa53f2a3SWolfgang Gassler $pagePath = $event->data[0][0]; 95fa53f2a3SWolfgang Gassler $pageName = $event->data[2]; 96*442c3981SWolfgang Gassler $pageContent = $event->data[0][1]; 97fa53f2a3SWolfgang Gassler 98fa53f2a3SWolfgang Gassler $editSummary = $GLOBALS['INFO']['meta']['last_change']['sum']; 99fa53f2a3SWolfgang Gassler 100*442c3981SWolfgang Gassler 101*442c3981SWolfgang Gassler //empty content indicates a page deletion 102*442c3981SWolfgang Gassler if ($pageContent == '') { 103*442c3981SWolfgang Gassler //get the commit text for deletions 104*442c3981SWolfgang Gassler $msgTemplate = $this->getConf('commitPageMsgDel') 105*442c3981SWolfgang Gassler 106*442c3981SWolfgang Gassler // bad hack as DokuWiki deletes the file after this event 107*442c3981SWolfgang Gassler // thus, let's delete the file by ourselves, so git can recognize the deletion 108*442c3981SWolfgang Gassler // DokuWiki uses @unlink as well, so no error should be thrown if we delete it twice 109*442c3981SWolfgang Gassler @unlink($pagePath); 110*442c3981SWolfgang Gassler 111*442c3981SWolfgang Gassler } else { 112*442c3981SWolfgang Gassler //get the commit text for edits 113*442c3981SWolfgang Gassler $msgTemplate = $this->getConf('commitPageMsg') 114*442c3981SWolfgang Gassler } 115*442c3981SWolfgang Gassler 116fa53f2a3SWolfgang Gassler $message = str_replace( 117fa53f2a3SWolfgang Gassler array('%page%','%summary%','%user%'), 118*442c3981SWolfgang Gassler array($pageName,$editSummary,$this->getAuthor()), 119*442c3981SWolfgang Gassler $msgTemplate 120fa53f2a3SWolfgang Gassler ); 121fa53f2a3SWolfgang Gassler 122*442c3981SWolfgang Gassler $this->commitFile($pagePath,$message); 123fa53f2a3SWolfgang Gassler 124fa53f2a3SWolfgang Gassler } 125fa53f2a3SWolfgang Gassler 126fa53f2a3SWolfgang Gassler } 127fa53f2a3SWolfgang Gassler 128fa53f2a3SWolfgang Gassler} 129fa53f2a3SWolfgang Gassler 130fa53f2a3SWolfgang Gassler// vim:ts=4:sw=4:et: 131