1<?php 2/** 3 * DokuWiki Plugin gitbacked (Action Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Wolfgang Gassler <wolfgang@gassler.org> 7 */ 8 9// must be run within Dokuwiki 10if (!defined('DOKU_INC')) die(); 11 12if (!defined('DOKU_LF')) define('DOKU_LF', "\n"); 13if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t"); 14if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 15 16require_once DOKU_PLUGIN.'action.php'; 17require_once(DOKU_PLUGIN.'gitbacked/lib/Git.php'); 18 19class action_plugin_gitbacked_editcommit extends DokuWiki_Action_Plugin { 20 21 public function register(Doku_Event_Handler &$controller) { 22 23 $controller->register_hook('IO_WIKIPAGE_WRITE', 'AFTER', $this, 'handle_io_wikipage_write'); 24 $controller->register_hook('MEDIA_UPLOAD_FINISH', 'AFTER', $this, 'handle_media_upload'); 25 $controller->register_hook('MEDIA_DELETE_FILE', 'AFTER', $this, 'handle_media_deletion'); 26 } 27 28 private function commitFile($filePath,$message) { 29 30 //get path to the repo root (by default DokuWiki's savedir) 31 $repoPath = DOKU_INC.$this->getConf('repoPath'); 32 //init the repo and create a new one if it is not present 33 $repo = new GitRepo($repoPath, true, true); 34 35 $params = $this->getConf('addParams'); 36 if ($params) { 37 $repo->git_path .= ' '.$params; 38 } 39 40 //add the changed file and set the commit message 41 $repo->add($filePath); 42 $repo->commit($message); 43 44 //if the push after Commit option is set we push the active branch to origin 45 if ($this->getConf('pushAfterCommit')) { 46 $repo->push('origin',$repo->active_branch()); 47 } 48 49 } 50 51 private function getAuthor() { 52 return $GLOBALS['USERINFO']['name']; 53 } 54 55 public function handle_media_deletion(Doku_Event &$event, $param) { 56 $mediaPath = $event->data['path']; 57 $mediaName = $event->data['name']; 58 59 $message = str_replace( 60 array('%media%','%user%'), 61 array($mediaName,$this->getAuthor()), 62 $this->getConf('commitMediaMsgDel') 63 ); 64 65 $this->commitFile($mediaPath,$message); 66 67 } 68 69 public function handle_media_upload(Doku_Event &$event, $param) { 70 71 $mediaPath = $event->data[1]; 72 $mediaName = $event->data[2]; 73 74 $message = str_replace( 75 array('%media%','%user%'), 76 array($mediaName,$this->getAuthor()), 77 $this->getConf('commitMediaMsg') 78 ); 79 80 $this->commitFile($mediaPath,$message); 81 82 } 83 84 public function handle_io_wikipage_write(Doku_Event &$event, $param) { 85 86 $rev = $event->data[3]; 87 88 /* On update to an existing page this event is called twice, 89 * once for the transfer of the old version to the attic (rev will have a value) 90 * and once to write the new version of the page into the wiki (rev is false) 91 */ 92 if (!$rev) { 93 94 $pagePath = $event->data[0][0]; 95 $pageName = $event->data[2]; 96 $pageContent = $event->data[0][1]; 97 98 $editSummary = $GLOBALS['INFO']['meta']['last_change']['sum']; 99 100 101 //empty content indicates a page deletion 102 if ($pageContent == '') { 103 //get the commit text for deletions 104 $msgTemplate = $this->getConf('commitPageMsgDel'); 105 106 // bad hack as DokuWiki deletes the file after this event 107 // thus, let's delete the file by ourselves, so git can recognize the deletion 108 // DokuWiki uses @unlink as well, so no error should be thrown if we delete it twice 109 @unlink($pagePath); 110 111 } else { 112 //get the commit text for edits 113 $msgTemplate = $this->getConf('commitPageMsg'); 114 } 115 116 $message = str_replace( 117 array('%page%','%summary%','%user%'), 118 array($pageName,$editSummary,$this->getAuthor()), 119 $msgTemplate 120 ); 121 122 $this->commitFile($pagePath,$message); 123 124 } 125 126 } 127 128} 129 130// vim:ts=4:sw=4:et: 131