media files recognized and added, delete msgs for pages and media files
authorWolfgang Gassler <wolfgang@gassler.org>
Fri, 2 Nov 2012 14:18:56 +0000 (15:18 +0100)
committerWolfgang Gassler <wolfgang@gassler.org>
Fri, 2 Nov 2012 14:18:56 +0000 (15:18 +0100)
action/editcommit.php
conf/default.php
conf/metadata.php
lang/en/settings.php
lib/Git.php

index 8e3e50b..2f15907 100644 (file)
@@ -20,10 +20,67 @@ class action_plugin_gitbacked_editcommit extends DokuWiki_Action_Plugin {
 
     public function register(Doku_Event_Handler &$controller) {
 
-       $controller->register_hook('IO_WIKIPAGE_WRITE', 'AFTER', $this, 'handle_io_wikipage_write');
-  
+               $controller->register_hook('IO_WIKIPAGE_WRITE', 'AFTER', $this, 'handle_io_wikipage_write');
+               $controller->register_hook('MEDIA_UPLOAD_FINISH', 'AFTER', $this, 'handle_media_upload');
+               $controller->register_hook('MEDIA_DELETE_FILE', 'AFTER', $this, 'handle_media_deletion');
     }
 
+       private function commitFile($filePath,$message) {
+
+               //get path to the repo root (by default DokuWiki's savedir)
+               $repoPath = DOKU_INC.$this->getConf('repoPath');
+               //init the repo and create a new one if it is not present
+               $repo = new GitRepo($repoPath, true, true);
+
+               $params = $this->getConf('addParams');
+               if ($params) {
+                       $repo->git_path .= ' '.$params;
+               }
+               
+               //add the changed file and set the commit message
+               $repo->add($filePath);
+               $repo->commit($message);
+
+               //if the push after Commit option is set we push the active branch to origin
+               if ($this->getConf('pushAfterCommit')) {
+                       $repo->push('origin',$repo->active_branch());
+               }
+
+       }
+
+       private function getAuthor() {
+               return $GLOBALS['USERINFO']['name'];
+       }
+
+       public function handle_media_deletion(Doku_Event &$event, $param) {
+               $mediaPath = $event->data['path'];
+               $mediaName = $event->data['name'];
+
+               $message = str_replace(
+                       array('%media%','%user%'),
+                       array($mediaName,$this->getAuthor()),
+                       $this->getConf('commitMediaMsgDel')
+               );
+
+               $this->commitFile($mediaPath,$message);
+
+       }
+
+       public function handle_media_upload(Doku_Event &$event, $param) {
+
+               $mediaPath = $event->data[1];
+               $mediaName = $event->data[2];
+
+               $message = str_replace(
+                       array('%media%','%user%'),
+                       array($mediaName,$this->getAuthor()),
+                       $this->getConf('commitMediaMsg')
+               );
+
+               $this->commitFile($mediaPath,$message);
+
+       }
+
     public function handle_io_wikipage_write(Doku_Event &$event, $param) {
 
                $rev = $event->data[3];
@@ -36,35 +93,33 @@ class action_plugin_gitbacked_editcommit extends DokuWiki_Action_Plugin {
 
                        $pagePath = $event->data[0][0];
                        $pageName = $event->data[2];
+                       $pageContent = $event->data[0][1];
 
-                       $authorName = $GLOBALS['USERINFO']['name'];
                        $editSummary = $GLOBALS['INFO']['meta']['last_change']['sum'];
+                       
 
-                       $message = str_replace(
-                               array('%page%','%summary%','%user%'),
-                               array($pageName,$editSummary,$authorName),
-                               $this->getConf('commitMsg')
-                       );
+                       //empty content indicates a page deletion
+                       if ($pageContent == '') {
+                               //get the commit text for deletions
+                               $msgTemplate = $this->getConf('commitPageMsgDel')
 
-                       //get path to the repo root (by default DokuWiki's savedir)
-                       $repoPath = $this->getConf('repoPath');
-                       
-                       //init the repo and create a new one if it is not present
-                       $repo = new GitRepo($repoPath, true, true);
+                               // bad hack as DokuWiki deletes the file after this event
+                               // thus, let's delete the file by ourselves, so git can recognize the deletion
+                               // DokuWiki uses @unlink as well, so no error should be thrown if we delete it twice
+                               @unlink($pagePath);
 
-                       $params = $this->getConf('addParams');
-                       if ($params) {
-                               $repo->git_path .= ' '.$params;
+                       } else {
+                               //get the commit text for edits
+                               $msgTemplate = $this->getConf('commitPageMsg')
                        }
-                       
-                       //add the changed file and set the commit message
-                       $repo->add($pagePath);
-                       $repo->commit($message);
 
-                       //if the push after Commit option is set we push the active branch to origin
-                       if ($this->getConf('pushAfterCommit')) {
-                               $repo->push('origin',$repo->active_branch());
-                       }
+                       $message = str_replace(
+                               array('%page%','%summary%','%user%'),
+                               array($pageName,$editSummary,$this->getAuthor()),
+                               $msgTemplate
+                       );
+
+                       $this->commitFile($pagePath,$message);          
 
                }
 
index b0d82bf..7847d98 100644 (file)
@@ -6,6 +6,9 @@
  */
 
 $conf['pushAfterCommit'] = 0;
-$conf['commitMsg']     = '%page% changed with %summary% by %user%';
+$conf['commitPageMsg'] = '%page% changed with %summary% by %user%';
+$conf['commitPageMsgDel']      = '%page% deleted %summary% by %user%';
+$conf['commitMediaMsg']        = '%media% uploaded by %user%';
+$conf['commitMediaMsgDel']     = '%media% deleted by %user%';
 $conf['repoPath']      = $GLOBALS['conf']['savedir'];
 $conf['addParams'] = '';
index 2da252d..1e0d0eb 100644 (file)
@@ -6,7 +6,10 @@
  */
 
 $meta['pushAfterCommit'] = array('onoff');
-$meta['commitMsg'] = array('string');
+$meta['commitPageMsg'] = array('string');
+$meta['commitPageMsgDel'] = array('string');
+$meta['commitMediaMsg'] = array('string');
+$meta['commitMediaMsgDel'] = array('string');
 $meta['repoPath'] = array('string');
 $meta['addParams'] = array('string');
 
index 7875b8b..3abb57a 100644 (file)
@@ -7,6 +7,9 @@
  */
 
 $lang['pushAfterCommit'] = 'Push active branch to remote origin after every commit';
-$lang['commitMsg']     = 'Commit Message (%user%,%summary%,%page% are replaced by the corresponding values)';
+$lang['commitPageMsg'] = 'Commit message for page edits (%user%,%summary%,%page% are replaced by the corresponding values)';
+$lang['commitPageMsgDel']      = 'Commit message for deleted pages (%user%,%summary%,%page% are replaced by the corresponding values)';
+$lang['commitMediaMsg']        = 'Commit message for media files (%user%,%media% are replaced by the corresponding values)';
+$lang['commitMediaMsgDel']     = 'Commit message for deleted media files (%user%,%media% are replaced by the corresponding values)';
 $lang['repoPath']      = 'Path of the git repo (e.g. the savedir '.$GLOBALS['conf']['savedir'].')';
 $lang['addParams'] = 'Additional git parameters (added to the git execution command)';
\ No newline at end of file
index 36e24ad..d704f82 100644 (file)
@@ -200,6 +200,7 @@ class GitRepo {
         * @return  string
         */
        protected function run_command($command) {
+
                $descriptorspec = array(
                        1 => array('pipe', 'w'),
                        2 => array('pipe', 'w'),