xref: /plugin/gitbacked/action/editcommit.php (revision 38f8ac722d9634d8e141363312de16bdb0c3b7a3)
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)
37*38f8ac72SWolfgang Gassler        if(defined('DOKU_FARM')) {
38*38f8ac72SWolfgang Gassler            $repoPath = $this->getConf('repoPath');
39*38f8ac72SWolfgang Gassler        } else {
40442c3981SWolfgang Gassler            $repoPath = DOKU_INC.$this->getConf('repoPath');
41*38f8ac72SWolfgang Gassler        }
42442c3981SWolfgang Gassler        //init the repo and create a new one if it is not present
434eba9b44SDanny Lin        io_mkdir_p($repoPath);
44442c3981SWolfgang Gassler        $repo = new GitRepo($repoPath, true, true);
454eba9b44SDanny Lin        //set git working directory (by default DokuWiki's savedir)
464eba9b44SDanny Lin        $repoWorkDir = DOKU_INC.$this->getConf('repoWorkDir');
474eba9b44SDanny Lin        $repo->git_path .= ' --work-tree '.escapeshellarg($repoWorkDir);
48442c3981SWolfgang Gassler
490d7cb616SBirkir A. Barkarson        $params = str_replace(
500d7cb616SBirkir A. Barkarson            array('%mail%','%user%'),
510d7cb616SBirkir A. Barkarson            array($this->getAuthorMail(),$this->getAuthor()),
520d7cb616SBirkir A. Barkarson            $this->getConf('addParams'));
53442c3981SWolfgang Gassler        if ($params) {
54442c3981SWolfgang Gassler            $repo->git_path .= ' '.$params;
55442c3981SWolfgang Gassler        }
56b92b117aSWolfgang Gassler        return $repo;
57b92b117aSWolfgang Gassler    }
58b92b117aSWolfgang Gassler
59b92b117aSWolfgang Gassler    private function commitFile($filePath,$message) {
60b92b117aSWolfgang Gassler
61b92b117aSWolfgang Gassler        $repo = $this->initRepo();
62442c3981SWolfgang Gassler
63442c3981SWolfgang Gassler        //add the changed file and set the commit message
64442c3981SWolfgang Gassler        $repo->add($filePath);
65442c3981SWolfgang Gassler        $repo->commit($message);
66442c3981SWolfgang Gassler
67442c3981SWolfgang Gassler        //if the push after Commit option is set we push the active branch to origin
68442c3981SWolfgang Gassler        if ($this->getConf('pushAfterCommit')) {
69442c3981SWolfgang Gassler            $repo->push('origin',$repo->active_branch());
70442c3981SWolfgang Gassler        }
71442c3981SWolfgang Gassler
72442c3981SWolfgang Gassler    }
73442c3981SWolfgang Gassler
74442c3981SWolfgang Gassler    private function getAuthor() {
75442c3981SWolfgang Gassler        return $GLOBALS['USERINFO']['name'];
76442c3981SWolfgang Gassler    }
77442c3981SWolfgang Gassler
780d7cb616SBirkir A. Barkarson    private function getAuthorMail() {
790d7cb616SBirkir A. Barkarson        return $GLOBALS['USERINFO']['mail'];
800d7cb616SBirkir A. Barkarson    }
810d7cb616SBirkir A. Barkarson
822377428fSDanny Lin    public function handle_periodic_pull(Doku_Event &$event, $param) {
832377428fSDanny Lin        if ($this->getConf('periodicPull')) {
842377428fSDanny Lin            $lastPullFile = $this->temp_dir.'/lastpull.txt';
852377428fSDanny Lin            //check if the lastPullFile exists
862377428fSDanny Lin            if (is_file($lastPullFile)) {
872377428fSDanny Lin                $lastPull = unserialize(file_get_contents($lastPullFile));
882377428fSDanny Lin            } else {
892377428fSDanny Lin                $lastPull = 0;
902377428fSDanny Lin            }
912377428fSDanny Lin            //calculate time between pulls in seconds
922377428fSDanny Lin            $timeToWait = $this->getConf('periodicMinutes')*60;
932377428fSDanny Lin            $now = time();
942377428fSDanny Lin
952377428fSDanny Lin            //if it is time to run a pull request
962377428fSDanny Lin            if ($lastPull+$timeToWait < $now) {
972377428fSDanny Lin                $repo = $this->initRepo();
982377428fSDanny Lin
992377428fSDanny Lin                //execute the pull request
1002377428fSDanny Lin                $repo->pull('origin',$repo->active_branch());
1012377428fSDanny Lin
1022377428fSDanny Lin                //save the current time to the file to track the last pull execution
1032377428fSDanny Lin                file_put_contents($lastPullFile,serialize(time()));
1042377428fSDanny Lin            }
1052377428fSDanny Lin        }
1062377428fSDanny Lin    }
1072377428fSDanny Lin
108442c3981SWolfgang Gassler    public function handle_media_deletion(Doku_Event &$event, $param) {
109442c3981SWolfgang Gassler        $mediaPath = $event->data['path'];
110442c3981SWolfgang Gassler        $mediaName = $event->data['name'];
111442c3981SWolfgang Gassler
112442c3981SWolfgang Gassler        $message = str_replace(
113442c3981SWolfgang Gassler            array('%media%','%user%'),
114442c3981SWolfgang Gassler            array($mediaName,$this->getAuthor()),
115442c3981SWolfgang Gassler            $this->getConf('commitMediaMsgDel')
116442c3981SWolfgang Gassler        );
117442c3981SWolfgang Gassler
118442c3981SWolfgang Gassler        $this->commitFile($mediaPath,$message);
119442c3981SWolfgang Gassler
120442c3981SWolfgang Gassler    }
121442c3981SWolfgang Gassler
122442c3981SWolfgang Gassler    public function handle_media_upload(Doku_Event &$event, $param) {
123442c3981SWolfgang Gassler
124442c3981SWolfgang Gassler        $mediaPath = $event->data[1];
125442c3981SWolfgang Gassler        $mediaName = $event->data[2];
126442c3981SWolfgang Gassler
127442c3981SWolfgang Gassler        $message = str_replace(
128442c3981SWolfgang Gassler            array('%media%','%user%'),
129442c3981SWolfgang Gassler            array($mediaName,$this->getAuthor()),
130442c3981SWolfgang Gassler            $this->getConf('commitMediaMsg')
131442c3981SWolfgang Gassler        );
132442c3981SWolfgang Gassler
133442c3981SWolfgang Gassler        $this->commitFile($mediaPath,$message);
134fa53f2a3SWolfgang Gassler
135fa53f2a3SWolfgang Gassler    }
136fa53f2a3SWolfgang Gassler
137fa53f2a3SWolfgang Gassler    public function handle_io_wikipage_write(Doku_Event &$event, $param) {
138fa53f2a3SWolfgang Gassler
139fa53f2a3SWolfgang Gassler        $rev = $event->data[3];
140fa53f2a3SWolfgang Gassler
141fa53f2a3SWolfgang Gassler        /* On update to an existing page this event is called twice,
142fa53f2a3SWolfgang Gassler         * once for the transfer of the old version to the attic (rev will have a value)
143fa53f2a3SWolfgang Gassler         * and once to write the new version of the page into the wiki (rev is false)
144fa53f2a3SWolfgang Gassler         */
145fa53f2a3SWolfgang Gassler        if (!$rev) {
146fa53f2a3SWolfgang Gassler
147fa53f2a3SWolfgang Gassler            $pagePath = $event->data[0][0];
148fa53f2a3SWolfgang Gassler            $pageName = $event->data[2];
149442c3981SWolfgang Gassler            $pageContent = $event->data[0][1];
150fa53f2a3SWolfgang Gassler
1517af27dc9SWolfgang Gassler            // get the summary directly from the form input
152e7471cfaSDanny Lin            // as the metadata hasn't updated yet
1537af27dc9SWolfgang Gassler            $editSummary = $GLOBALS['INPUT']->str('summary');
154442c3981SWolfgang Gassler
155442c3981SWolfgang Gassler            // empty content indicates a page deletion
156442c3981SWolfgang Gassler            if ($pageContent == '') {
157442c3981SWolfgang Gassler                // get the commit text for deletions
158d4e1c54bSWolfgang Gassler                $msgTemplate = $this->getConf('commitPageMsgDel');
159442c3981SWolfgang Gassler
160442c3981SWolfgang Gassler                // bad hack as DokuWiki deletes the file after this event
161442c3981SWolfgang Gassler                // thus, let's delete the file by ourselves, so git can recognize the deletion
162442c3981SWolfgang Gassler                // DokuWiki uses @unlink as well, so no error should be thrown if we delete it twice
163442c3981SWolfgang Gassler                @unlink($pagePath);
164442c3981SWolfgang Gassler
165442c3981SWolfgang Gassler            } else {
166442c3981SWolfgang Gassler                //get the commit text for edits
167d4e1c54bSWolfgang Gassler                $msgTemplate = $this->getConf('commitPageMsg');
168442c3981SWolfgang Gassler            }
169442c3981SWolfgang Gassler
170fa53f2a3SWolfgang Gassler            $message = str_replace(
171fa53f2a3SWolfgang Gassler                array('%page%','%summary%','%user%'),
172442c3981SWolfgang Gassler                array($pageName,$editSummary,$this->getAuthor()),
173442c3981SWolfgang Gassler                $msgTemplate
174fa53f2a3SWolfgang Gassler            );
175fa53f2a3SWolfgang Gassler
176442c3981SWolfgang Gassler            $this->commitFile($pagePath,$message);
177fa53f2a3SWolfgang Gassler
178fa53f2a3SWolfgang Gassler        }
179fa53f2a3SWolfgang Gassler
180fa53f2a3SWolfgang Gassler    }
181fa53f2a3SWolfgang Gassler
182fa53f2a3SWolfgang Gassler}
183fa53f2a3SWolfgang Gassler
184fa53f2a3SWolfgang Gassler// vim:ts=4:sw=4:et:
185