xref: /plugin/gitbacked/action/editcommit.php (revision 2377428f8b648cfefa2fa22872891c9660c04812)
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';
1700ce3f12SDanny Linrequire_once dirname(__FILE__).'/../lib/Git.php';
18fa53f2a3SWolfgang Gassler
19fa53f2a3SWolfgang Gasslerclass action_plugin_gitbacked_editcommit extends DokuWiki_Action_Plugin {
20fa53f2a3SWolfgang Gassler
2100ce3f12SDanny Lin    function __construct() {
2200ce3f12SDanny Lin        global $conf;
2300ce3f12SDanny Lin        $this->temp_dir = $conf['tmpdir'].'/gitbacked';
2400ce3f12SDanny Lin        io_mkdir_p($this->temp_dir);
2500ce3f12SDanny Lin    }
2600ce3f12SDanny Lin
27fa53f2a3SWolfgang Gassler    public function register(Doku_Event_Handler &$controller) {
28fa53f2a3SWolfgang Gassler
29fa53f2a3SWolfgang Gassler        $controller->register_hook('IO_WIKIPAGE_WRITE', 'AFTER', $this, 'handle_io_wikipage_write');
30442c3981SWolfgang Gassler        $controller->register_hook('MEDIA_UPLOAD_FINISH', 'AFTER', $this, 'handle_media_upload');
31442c3981SWolfgang Gassler        $controller->register_hook('MEDIA_DELETE_FILE', 'AFTER', $this, 'handle_media_deletion');
32b92b117aSWolfgang Gassler        $controller->register_hook('DOKUWIKI_DONE', 'AFTER', $this, 'handle_periodic_pull');
33442c3981SWolfgang Gassler    }
34442c3981SWolfgang Gassler
35b92b117aSWolfgang Gassler    private function initRepo() {
36442c3981SWolfgang Gassler        //get path to the repo root (by default DokuWiki's savedir)
37442c3981SWolfgang Gassler        $repoPath = DOKU_INC.$this->getConf('repoPath');
38442c3981SWolfgang Gassler        //init the repo and create a new one if it is not present
394eba9b44SDanny Lin        io_mkdir_p($repoPath);
40442c3981SWolfgang Gassler        $repo = new GitRepo($repoPath, true, true);
414eba9b44SDanny Lin        //set git working directory (by default DokuWiki's savedir)
424eba9b44SDanny Lin        $repoWorkDir = DOKU_INC.$this->getConf('repoWorkDir');
434eba9b44SDanny Lin        $repo->git_path .= ' --work-tree '.escapeshellarg($repoWorkDir);
44442c3981SWolfgang Gassler
450d7cb616SBirkir A. Barkarson        $params = str_replace(
460d7cb616SBirkir A. Barkarson            array('%mail%','%user%'),
470d7cb616SBirkir A. Barkarson            array($this->getAuthorMail(),$this->getAuthor()),
480d7cb616SBirkir A. Barkarson            $this->getConf('addParams'));
49442c3981SWolfgang Gassler        if ($params) {
50442c3981SWolfgang Gassler            $repo->git_path .= ' '.$params;
51442c3981SWolfgang Gassler        }
52b92b117aSWolfgang Gassler        return $repo;
53b92b117aSWolfgang Gassler    }
54b92b117aSWolfgang Gassler
55b92b117aSWolfgang Gassler    private function commitFile($filePath,$message) {
56b92b117aSWolfgang Gassler
57b92b117aSWolfgang Gassler        $repo = $this->initRepo();
58442c3981SWolfgang Gassler
59442c3981SWolfgang Gassler        //add the changed file and set the commit message
60442c3981SWolfgang Gassler        $repo->add($filePath);
61442c3981SWolfgang Gassler        $repo->commit($message);
62442c3981SWolfgang Gassler
63442c3981SWolfgang Gassler        //if the push after Commit option is set we push the active branch to origin
64442c3981SWolfgang Gassler        if ($this->getConf('pushAfterCommit')) {
65442c3981SWolfgang Gassler            $repo->push('origin',$repo->active_branch());
66442c3981SWolfgang Gassler        }
67442c3981SWolfgang Gassler
68442c3981SWolfgang Gassler    }
69442c3981SWolfgang Gassler
70442c3981SWolfgang Gassler    private function getAuthor() {
71442c3981SWolfgang Gassler        return $GLOBALS['USERINFO']['name'];
72442c3981SWolfgang Gassler    }
73442c3981SWolfgang Gassler
740d7cb616SBirkir A. Barkarson    private function getAuthorMail() {
750d7cb616SBirkir A. Barkarson        return $GLOBALS['USERINFO']['mail'];
760d7cb616SBirkir A. Barkarson    }
770d7cb616SBirkir A. Barkarson
78*2377428fSDanny Lin    public function handle_periodic_pull(Doku_Event &$event, $param) {
79*2377428fSDanny Lin        if ($this->getConf('periodicPull')) {
80*2377428fSDanny Lin            $lastPullFile = $this->temp_dir.'/lastpull.txt';
81*2377428fSDanny Lin            //check if the lastPullFile exists
82*2377428fSDanny Lin            if (is_file($lastPullFile)) {
83*2377428fSDanny Lin                $lastPull = unserialize(file_get_contents($lastPullFile));
84*2377428fSDanny Lin            } else {
85*2377428fSDanny Lin                $lastPull = 0;
86*2377428fSDanny Lin            }
87*2377428fSDanny Lin            //calculate time between pulls in seconds
88*2377428fSDanny Lin            $timeToWait = $this->getConf('periodicMinutes')*60;
89*2377428fSDanny Lin            $now = time();
90*2377428fSDanny Lin
91*2377428fSDanny Lin            //if it is time to run a pull request
92*2377428fSDanny Lin            if ($lastPull+$timeToWait < $now) {
93*2377428fSDanny Lin                $repo = $this->initRepo();
94*2377428fSDanny Lin
95*2377428fSDanny Lin                //execute the pull request
96*2377428fSDanny Lin                $repo->pull('origin',$repo->active_branch());
97*2377428fSDanny Lin
98*2377428fSDanny Lin                //save the current time to the file to track the last pull execution
99*2377428fSDanny Lin                file_put_contents($lastPullFile,serialize(time()));
100*2377428fSDanny Lin            }
101*2377428fSDanny Lin        }
102*2377428fSDanny Lin    }
103*2377428fSDanny Lin
104442c3981SWolfgang Gassler    public function handle_media_deletion(Doku_Event &$event, $param) {
105442c3981SWolfgang Gassler        $mediaPath = $event->data['path'];
106442c3981SWolfgang Gassler        $mediaName = $event->data['name'];
107442c3981SWolfgang Gassler
108442c3981SWolfgang Gassler        $message = str_replace(
109442c3981SWolfgang Gassler            array('%media%','%user%'),
110442c3981SWolfgang Gassler            array($mediaName,$this->getAuthor()),
111442c3981SWolfgang Gassler            $this->getConf('commitMediaMsgDel')
112442c3981SWolfgang Gassler        );
113442c3981SWolfgang Gassler
114442c3981SWolfgang Gassler        $this->commitFile($mediaPath,$message);
115442c3981SWolfgang Gassler
116442c3981SWolfgang Gassler    }
117442c3981SWolfgang Gassler
118442c3981SWolfgang Gassler    public function handle_media_upload(Doku_Event &$event, $param) {
119442c3981SWolfgang Gassler
120442c3981SWolfgang Gassler        $mediaPath = $event->data[1];
121442c3981SWolfgang Gassler        $mediaName = $event->data[2];
122442c3981SWolfgang Gassler
123442c3981SWolfgang Gassler        $message = str_replace(
124442c3981SWolfgang Gassler            array('%media%','%user%'),
125442c3981SWolfgang Gassler            array($mediaName,$this->getAuthor()),
126442c3981SWolfgang Gassler            $this->getConf('commitMediaMsg')
127442c3981SWolfgang Gassler        );
128442c3981SWolfgang Gassler
129442c3981SWolfgang Gassler        $this->commitFile($mediaPath,$message);
130fa53f2a3SWolfgang Gassler
131fa53f2a3SWolfgang Gassler    }
132fa53f2a3SWolfgang Gassler
133fa53f2a3SWolfgang Gassler    public function handle_io_wikipage_write(Doku_Event &$event, $param) {
134fa53f2a3SWolfgang Gassler
135fa53f2a3SWolfgang Gassler        $rev = $event->data[3];
136fa53f2a3SWolfgang Gassler
137fa53f2a3SWolfgang Gassler        /* On update to an existing page this event is called twice,
138fa53f2a3SWolfgang Gassler         * once for the transfer of the old version to the attic (rev will have a value)
139fa53f2a3SWolfgang Gassler         * and once to write the new version of the page into the wiki (rev is false)
140fa53f2a3SWolfgang Gassler         */
141fa53f2a3SWolfgang Gassler        if (!$rev) {
142fa53f2a3SWolfgang Gassler
143fa53f2a3SWolfgang Gassler            $pagePath = $event->data[0][0];
144fa53f2a3SWolfgang Gassler            $pageName = $event->data[2];
145442c3981SWolfgang Gassler            $pageContent = $event->data[0][1];
146fa53f2a3SWolfgang Gassler
1477af27dc9SWolfgang Gassler            // get the summary directly from the form input
148e7471cfaSDanny Lin            // as the metadata hasn't updated yet
1497af27dc9SWolfgang Gassler            $editSummary = $GLOBALS['INPUT']->str('summary');
150442c3981SWolfgang Gassler
151442c3981SWolfgang Gassler            // empty content indicates a page deletion
152442c3981SWolfgang Gassler            if ($pageContent == '') {
153442c3981SWolfgang Gassler                // get the commit text for deletions
154d4e1c54bSWolfgang Gassler                $msgTemplate = $this->getConf('commitPageMsgDel');
155442c3981SWolfgang Gassler
156442c3981SWolfgang Gassler                // bad hack as DokuWiki deletes the file after this event
157442c3981SWolfgang Gassler                // thus, let's delete the file by ourselves, so git can recognize the deletion
158442c3981SWolfgang Gassler                // DokuWiki uses @unlink as well, so no error should be thrown if we delete it twice
159442c3981SWolfgang Gassler                @unlink($pagePath);
160442c3981SWolfgang Gassler
161442c3981SWolfgang Gassler            } else {
162442c3981SWolfgang Gassler                //get the commit text for edits
163d4e1c54bSWolfgang Gassler                $msgTemplate = $this->getConf('commitPageMsg');
164442c3981SWolfgang Gassler            }
165442c3981SWolfgang Gassler
166fa53f2a3SWolfgang Gassler            $message = str_replace(
167fa53f2a3SWolfgang Gassler                array('%page%','%summary%','%user%'),
168442c3981SWolfgang Gassler                array($pageName,$editSummary,$this->getAuthor()),
169442c3981SWolfgang Gassler                $msgTemplate
170fa53f2a3SWolfgang Gassler            );
171fa53f2a3SWolfgang Gassler
172442c3981SWolfgang Gassler            $this->commitFile($pagePath,$message);
173fa53f2a3SWolfgang Gassler
174fa53f2a3SWolfgang Gassler        }
175fa53f2a3SWolfgang Gassler
176fa53f2a3SWolfgang Gassler    }
177fa53f2a3SWolfgang Gassler
178fa53f2a3SWolfgang Gassler}
179fa53f2a3SWolfgang Gassler
180fa53f2a3SWolfgang Gassler// vim:ts=4:sw=4:et:
181