xref: /plugin/gitbacked/action/editcommit.php (revision 00ce3f12df4188eb841865c6353b9fad9a97db26)
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';
17*00ce3f12SDanny Linrequire_once dirname(__FILE__).'/../lib/Git.php';
18fa53f2a3SWolfgang Gassler
19fa53f2a3SWolfgang Gasslerclass action_plugin_gitbacked_editcommit extends DokuWiki_Action_Plugin {
20fa53f2a3SWolfgang Gassler
21*00ce3f12SDanny Lin    function __construct() {
22*00ce3f12SDanny Lin        global $conf;
23*00ce3f12SDanny Lin        $this->temp_dir = $conf['tmpdir'].'/gitbacked';
24*00ce3f12SDanny Lin        io_mkdir_p($this->temp_dir);
25*00ce3f12SDanny Lin    }
26*00ce3f12SDanny 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    public function handle_periodic_pull(Doku_Event &$event, $param) {
36b92b117aSWolfgang Gassler        if ($this->getConf('periodicPull')) {
37*00ce3f12SDanny Lin            $lastPullFile = $this->temp_dir.'/lastpull.txt';
38b92b117aSWolfgang Gassler            //check if the lastPullFile exists
39b92b117aSWolfgang Gassler            if (is_file($lastPullFile)) {
40b92b117aSWolfgang Gassler                $lastPull = unserialize(file_get_contents($lastPullFile));
41b92b117aSWolfgang Gassler            } else {
42b92b117aSWolfgang Gassler                $lastPull = 0;
43b92b117aSWolfgang Gassler            }
44b92b117aSWolfgang Gassler            //calculate time between pulls in seconds
45b92b117aSWolfgang Gassler            $timeToWait = $this->getConf('periodicMinutes')*60;
46b92b117aSWolfgang Gassler            $now = time();
47442c3981SWolfgang Gassler
48b92b117aSWolfgang Gassler            //if it is time to run a pull request
49b92b117aSWolfgang Gassler            if ($lastPull+$timeToWait < $now) {
50b92b117aSWolfgang Gassler                $repo = $this->initRepo();
51b92b117aSWolfgang Gassler
52b92b117aSWolfgang Gassler                //execute the pull request
53b92b117aSWolfgang Gassler                $repo->pull('origin',$repo->active_branch());
54b92b117aSWolfgang Gassler
55b92b117aSWolfgang Gassler                //save the current time to the file to track the last pull execution
56b92b117aSWolfgang Gassler                file_put_contents($lastPullFile,serialize(time()));
57b92b117aSWolfgang Gassler            }
58b92b117aSWolfgang Gassler        }
59b92b117aSWolfgang Gassler    }
60b92b117aSWolfgang Gassler
61b92b117aSWolfgang Gassler    private function initRepo() {
62442c3981SWolfgang Gassler        //get path to the repo root (by default DokuWiki's savedir)
63442c3981SWolfgang Gassler        $repoPath = DOKU_INC.$this->getConf('repoPath');
64442c3981SWolfgang Gassler        //init the repo and create a new one if it is not present
65442c3981SWolfgang Gassler        $repo = new GitRepo($repoPath, true, true);
66442c3981SWolfgang Gassler
67442c3981SWolfgang Gassler        $params = $this->getConf('addParams');
68442c3981SWolfgang Gassler        if ($params) {
69442c3981SWolfgang Gassler            $repo->git_path .= ' '.$params;
70442c3981SWolfgang Gassler        }
71b92b117aSWolfgang Gassler        return $repo;
72b92b117aSWolfgang Gassler    }
73b92b117aSWolfgang Gassler
74b92b117aSWolfgang Gassler    private function commitFile($filePath,$message) {
75b92b117aSWolfgang Gassler
76b92b117aSWolfgang Gassler        $repo = $this->initRepo();
77442c3981SWolfgang Gassler
78442c3981SWolfgang Gassler        //add the changed file and set the commit message
79442c3981SWolfgang Gassler        $repo->add($filePath);
80442c3981SWolfgang Gassler        $repo->commit($message);
81442c3981SWolfgang Gassler
82442c3981SWolfgang Gassler        //if the push after Commit option is set we push the active branch to origin
83442c3981SWolfgang Gassler        if ($this->getConf('pushAfterCommit')) {
84442c3981SWolfgang Gassler            $repo->push('origin',$repo->active_branch());
85442c3981SWolfgang Gassler        }
86442c3981SWolfgang Gassler
87442c3981SWolfgang Gassler    }
88442c3981SWolfgang Gassler
89442c3981SWolfgang Gassler    private function getAuthor() {
90442c3981SWolfgang Gassler        return $GLOBALS['USERINFO']['name'];
91442c3981SWolfgang Gassler    }
92442c3981SWolfgang Gassler
93442c3981SWolfgang Gassler    public function handle_media_deletion(Doku_Event &$event, $param) {
94442c3981SWolfgang Gassler        $mediaPath = $event->data['path'];
95442c3981SWolfgang Gassler        $mediaName = $event->data['name'];
96442c3981SWolfgang Gassler
97442c3981SWolfgang Gassler        $message = str_replace(
98442c3981SWolfgang Gassler            array('%media%','%user%'),
99442c3981SWolfgang Gassler            array($mediaName,$this->getAuthor()),
100442c3981SWolfgang Gassler            $this->getConf('commitMediaMsgDel')
101442c3981SWolfgang Gassler        );
102442c3981SWolfgang Gassler
103442c3981SWolfgang Gassler        $this->commitFile($mediaPath,$message);
104442c3981SWolfgang Gassler
105442c3981SWolfgang Gassler    }
106442c3981SWolfgang Gassler
107442c3981SWolfgang Gassler    public function handle_media_upload(Doku_Event &$event, $param) {
108442c3981SWolfgang Gassler
109442c3981SWolfgang Gassler        $mediaPath = $event->data[1];
110442c3981SWolfgang Gassler        $mediaName = $event->data[2];
111442c3981SWolfgang Gassler
112442c3981SWolfgang Gassler        $message = str_replace(
113442c3981SWolfgang Gassler            array('%media%','%user%'),
114442c3981SWolfgang Gassler            array($mediaName,$this->getAuthor()),
115442c3981SWolfgang Gassler            $this->getConf('commitMediaMsg')
116442c3981SWolfgang Gassler        );
117442c3981SWolfgang Gassler
118442c3981SWolfgang Gassler        $this->commitFile($mediaPath,$message);
119fa53f2a3SWolfgang Gassler
120fa53f2a3SWolfgang Gassler    }
121fa53f2a3SWolfgang Gassler
122fa53f2a3SWolfgang Gassler    public function handle_io_wikipage_write(Doku_Event &$event, $param) {
123fa53f2a3SWolfgang Gassler
124fa53f2a3SWolfgang Gassler        $rev = $event->data[3];
125fa53f2a3SWolfgang Gassler
126fa53f2a3SWolfgang Gassler        /* On update to an existing page this event is called twice,
127fa53f2a3SWolfgang Gassler         * once for the transfer of the old version to the attic (rev will have a value)
128fa53f2a3SWolfgang Gassler         * and once to write the new version of the page into the wiki (rev is false)
129fa53f2a3SWolfgang Gassler         */
130fa53f2a3SWolfgang Gassler        if (!$rev) {
131fa53f2a3SWolfgang Gassler
132fa53f2a3SWolfgang Gassler            $pagePath = $event->data[0][0];
133fa53f2a3SWolfgang Gassler            $pageName = $event->data[2];
134442c3981SWolfgang Gassler            $pageContent = $event->data[0][1];
135fa53f2a3SWolfgang Gassler
1367af27dc9SWolfgang Gassler            // get the summary directly from the form input
137e7471cfaSDanny Lin            // as the metadata hasn't updated yet
1387af27dc9SWolfgang Gassler            $editSummary = $GLOBALS['INPUT']->str('summary');
139442c3981SWolfgang Gassler
140442c3981SWolfgang Gassler            // empty content indicates a page deletion
141442c3981SWolfgang Gassler            if ($pageContent == '') {
142442c3981SWolfgang Gassler                // get the commit text for deletions
143d4e1c54bSWolfgang Gassler                $msgTemplate = $this->getConf('commitPageMsgDel');
144442c3981SWolfgang Gassler
145442c3981SWolfgang Gassler                // bad hack as DokuWiki deletes the file after this event
146442c3981SWolfgang Gassler                // thus, let's delete the file by ourselves, so git can recognize the deletion
147442c3981SWolfgang Gassler                // DokuWiki uses @unlink as well, so no error should be thrown if we delete it twice
148442c3981SWolfgang Gassler                @unlink($pagePath);
149442c3981SWolfgang Gassler
150442c3981SWolfgang Gassler            } else {
151442c3981SWolfgang Gassler                //get the commit text for edits
152d4e1c54bSWolfgang Gassler                $msgTemplate = $this->getConf('commitPageMsg');
153442c3981SWolfgang Gassler            }
154442c3981SWolfgang Gassler
155fa53f2a3SWolfgang Gassler            $message = str_replace(
156fa53f2a3SWolfgang Gassler                array('%page%','%summary%','%user%'),
157442c3981SWolfgang Gassler                array($pageName,$editSummary,$this->getAuthor()),
158442c3981SWolfgang Gassler                $msgTemplate
159fa53f2a3SWolfgang Gassler            );
160fa53f2a3SWolfgang Gassler
161442c3981SWolfgang Gassler            $this->commitFile($pagePath,$message);
162fa53f2a3SWolfgang Gassler
163fa53f2a3SWolfgang Gassler        }
164fa53f2a3SWolfgang Gassler
165fa53f2a3SWolfgang Gassler    }
166fa53f2a3SWolfgang Gassler
167fa53f2a3SWolfgang Gassler}
168fa53f2a3SWolfgang Gassler
169fa53f2a3SWolfgang Gassler// vim:ts=4:sw=4:et:
170