periodic pull implemented
authorWolfgang Gassler <wolfgang@gassler.org>
Mon, 12 Nov 2012 14:29:59 +0000 (15:29 +0100)
committerWolfgang Gassler <wolfgang@gassler.org>
Mon, 12 Nov 2012 14:29:59 +0000 (15:29 +0100)
action/editcommit.php
conf/default.php
conf/metadata.php
lang/en/settings.php
lib/Git.php

index 053b2db..e1480c7 100644 (file)
@@ -23,10 +23,36 @@ class action_plugin_gitbacked_editcommit extends DokuWiki_Action_Plugin {
                $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');
+               $controller->register_hook('DOKUWIKI_DONE', 'AFTER', $this, 'handle_periodic_pull');
     }
 
-       private function commitFile($filePath,$message) {
+       public function handle_periodic_pull(Doku_Event &$event, $param) {
+               if ($this->getConf('periodicPull')) {
+                       $lastPullFile = DOKU_PLUGIN.'gitbacked/action/lastpull.txt';
+                       //check if the lastPullFile exists
+                       if (is_file($lastPullFile)) {
+                               $lastPull = unserialize(file_get_contents($lastPullFile));
+                       } else {
+                               $lastPull = 0;
+                       }
+                       //calculate time between pulls in seconds
+                       $timeToWait = $this->getConf('periodicMinutes')*60;
+                       $now = time();
+
+                       //if it is time to run a pull request
+                       if ($lastPull+$timeToWait < $now) {
+                               $repo = $this->initRepo();
 
+                               //execute the pull request
+                               $repo->pull('origin',$repo->active_branch());
+
+                               //save the current time to the file to track the last pull execution
+                               file_put_contents($lastPullFile,serialize(time()));
+                       }
+               }
+       }
+
+       private function initRepo() {
                //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
@@ -36,6 +62,12 @@ class action_plugin_gitbacked_editcommit extends DokuWiki_Action_Plugin {
                if ($params) {
                        $repo->git_path .= ' '.$params;
                }
+               return $repo;
+       }
+
+       private function commitFile($filePath,$message) {
+
+               $repo = $this->initRepo();
                
                //add the changed file and set the commit message
                $repo->add($filePath);
index 7847d98..adca93d 100644 (file)
@@ -6,6 +6,8 @@
  */
 
 $conf['pushAfterCommit'] = 0;
+$conf['periodicPull'] = 0;
+$conf['periodicMinutes'] = 60;
 $conf['commitPageMsg'] = '%page% changed with %summary% by %user%';
 $conf['commitPageMsgDel']      = '%page% deleted %summary% by %user%';
 $conf['commitMediaMsg']        = '%media% uploaded by %user%';
index 1e0d0eb..867ee61 100644 (file)
@@ -6,6 +6,8 @@
  */
 
 $meta['pushAfterCommit'] = array('onoff');
+$meta['periodicPull'] = array('onoff');
+$meta['periodicMinutes'] = array('numeric');
 $meta['commitPageMsg'] = array('string');
 $meta['commitPageMsgDel'] = array('string');
 $meta['commitMediaMsg'] = array('string');
index 3abb57a..adf067c 100644 (file)
@@ -7,6 +7,8 @@
  */
 
 $lang['pushAfterCommit'] = 'Push active branch to remote origin after every commit';
+$lang['periodicPull'] = 'Pull the remote git repository every "periodicMinutes" triggered by a http page request';
+$lang['periodicMinutes'] = 'Timespan (in minutes) between periodic pull requests';
 $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)';
index 0d0dd96..3e0b7fd 100644 (file)
@@ -448,6 +448,20 @@ class GitRepo {
     {
         return $this->run("push --tags $remote $branch");
     }
+
+    /**
+     * Pull specific branch from remote
+     *
+     * Accepts the name of the remote and local branch
+     *
+     * @param string $remote
+     * @param string $branch
+     * @return string
+     */
+    public function pull($remote, $branch)
+    {
+        return $this->run("pull $remote $branch");
+    }
     
     /**
      * Sets the project description.