xref: /plugin/gitbacked/action/editcommit.php (revision 635161d0b509deaca3533f7581e597f8f252f583)
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)
3738f8ac72SWolfgang Gassler        if(defined('DOKU_FARM')) {
3838f8ac72SWolfgang Gassler            $repoPath = $this->getConf('repoPath');
3938f8ac72SWolfgang Gassler        } else {
40442c3981SWolfgang Gassler            $repoPath = DOKU_INC.$this->getConf('repoPath');
4138f8ac72SWolfgang Gassler        }
42*635161d0SCarsten Teibes        //set the path to the git binary
43*635161d0SCarsten Teibes        $gitPath = trim($this->getConf('gitPath'));
44*635161d0SCarsten Teibes        if ($gitPath !== '') {
45*635161d0SCarsten Teibes            Git::set_bin($gitPath);
46*635161d0SCarsten Teibes        }
47442c3981SWolfgang Gassler        //init the repo and create a new one if it is not present
484eba9b44SDanny Lin        io_mkdir_p($repoPath);
49442c3981SWolfgang Gassler        $repo = new GitRepo($repoPath, true, true);
504eba9b44SDanny Lin        //set git working directory (by default DokuWiki's savedir)
514eba9b44SDanny Lin        $repoWorkDir = DOKU_INC.$this->getConf('repoWorkDir');
52985a1bc7SCarsten Teibes        Git::set_bin(Git::get_bin().' --work-tree '.escapeshellarg($repoWorkDir));
53442c3981SWolfgang Gassler
540d7cb616SBirkir A. Barkarson        $params = str_replace(
550d7cb616SBirkir A. Barkarson            array('%mail%','%user%'),
560d7cb616SBirkir A. Barkarson            array($this->getAuthorMail(),$this->getAuthor()),
570d7cb616SBirkir A. Barkarson            $this->getConf('addParams'));
58442c3981SWolfgang Gassler        if ($params) {
59985a1bc7SCarsten Teibes            Git::set_bin(Git::get_bin().' '.$params);
60442c3981SWolfgang Gassler        }
61b92b117aSWolfgang Gassler        return $repo;
62b92b117aSWolfgang Gassler    }
63b92b117aSWolfgang Gassler
6466f21a70SWolfgang Gassler	private function isIgnored($filePath) {
6566f21a70SWolfgang Gassler		$ignore = false;
6666f21a70SWolfgang Gassler		$ignorePaths = trim($this->getConf('ignorePaths'));
6766f21a70SWolfgang Gassler		if ($ignorePaths !== '') {
6866f21a70SWolfgang Gassler			$paths = explode(',',$ignorePaths);
6966f21a70SWolfgang Gassler			foreach($paths as $path) {
7066f21a70SWolfgang Gassler				if (strstr($filePath,$path)) {
7166f21a70SWolfgang Gassler					$ignore = true;
7266f21a70SWolfgang Gassler				}
7366f21a70SWolfgang Gassler			}
7466f21a70SWolfgang Gassler		}
7566f21a70SWolfgang Gassler		return $ignore;
7666f21a70SWolfgang Gassler	}
7766f21a70SWolfgang Gassler
78b92b117aSWolfgang Gassler    private function commitFile($filePath,$message) {
79b92b117aSWolfgang Gassler
8066f21a70SWolfgang Gassler		if (!$this->isIgnored($filePath)) {
81b92b117aSWolfgang Gassler			$repo = $this->initRepo();
82442c3981SWolfgang Gassler
83442c3981SWolfgang Gassler			//add the changed file and set the commit message
84442c3981SWolfgang Gassler			$repo->add($filePath);
85442c3981SWolfgang Gassler			$repo->commit($message);
86442c3981SWolfgang Gassler
87442c3981SWolfgang Gassler			//if the push after Commit option is set we push the active branch to origin
88442c3981SWolfgang Gassler			if ($this->getConf('pushAfterCommit')) {
89442c3981SWolfgang Gassler				$repo->push('origin',$repo->active_branch());
90442c3981SWolfgang Gassler			}
9166f21a70SWolfgang Gassler		}
92442c3981SWolfgang Gassler
93442c3981SWolfgang Gassler    }
94442c3981SWolfgang Gassler
95442c3981SWolfgang Gassler    private function getAuthor() {
96442c3981SWolfgang Gassler        return $GLOBALS['USERINFO']['name'];
97442c3981SWolfgang Gassler    }
98442c3981SWolfgang Gassler
990d7cb616SBirkir A. Barkarson    private function getAuthorMail() {
1000d7cb616SBirkir A. Barkarson        return $GLOBALS['USERINFO']['mail'];
1010d7cb616SBirkir A. Barkarson    }
1020d7cb616SBirkir A. Barkarson
1032377428fSDanny Lin    public function handle_periodic_pull(Doku_Event &$event, $param) {
1042377428fSDanny Lin        if ($this->getConf('periodicPull')) {
1052377428fSDanny Lin            $lastPullFile = $this->temp_dir.'/lastpull.txt';
1062377428fSDanny Lin            //check if the lastPullFile exists
1072377428fSDanny Lin            if (is_file($lastPullFile)) {
1082377428fSDanny Lin                $lastPull = unserialize(file_get_contents($lastPullFile));
1092377428fSDanny Lin            } else {
1102377428fSDanny Lin                $lastPull = 0;
1112377428fSDanny Lin            }
1122377428fSDanny Lin            //calculate time between pulls in seconds
1132377428fSDanny Lin            $timeToWait = $this->getConf('periodicMinutes')*60;
1142377428fSDanny Lin            $now = time();
1152377428fSDanny Lin
1162377428fSDanny Lin            //if it is time to run a pull request
1172377428fSDanny Lin            if ($lastPull+$timeToWait < $now) {
1182377428fSDanny Lin                $repo = $this->initRepo();
1192377428fSDanny Lin
1202377428fSDanny Lin                //execute the pull request
1212377428fSDanny Lin                $repo->pull('origin',$repo->active_branch());
1222377428fSDanny Lin
1232377428fSDanny Lin                //save the current time to the file to track the last pull execution
1242377428fSDanny Lin                file_put_contents($lastPullFile,serialize(time()));
1252377428fSDanny Lin            }
1262377428fSDanny Lin        }
1272377428fSDanny Lin    }
1282377428fSDanny Lin
129442c3981SWolfgang Gassler    public function handle_media_deletion(Doku_Event &$event, $param) {
130442c3981SWolfgang Gassler        $mediaPath = $event->data['path'];
131442c3981SWolfgang Gassler        $mediaName = $event->data['name'];
132442c3981SWolfgang Gassler
133442c3981SWolfgang Gassler        $message = str_replace(
134442c3981SWolfgang Gassler            array('%media%','%user%'),
135442c3981SWolfgang Gassler            array($mediaName,$this->getAuthor()),
136442c3981SWolfgang Gassler            $this->getConf('commitMediaMsgDel')
137442c3981SWolfgang Gassler        );
138442c3981SWolfgang Gassler
139442c3981SWolfgang Gassler        $this->commitFile($mediaPath,$message);
140442c3981SWolfgang Gassler
141442c3981SWolfgang Gassler    }
142442c3981SWolfgang Gassler
143442c3981SWolfgang Gassler    public function handle_media_upload(Doku_Event &$event, $param) {
144442c3981SWolfgang Gassler
145442c3981SWolfgang Gassler        $mediaPath = $event->data[1];
146442c3981SWolfgang Gassler        $mediaName = $event->data[2];
147442c3981SWolfgang Gassler
148442c3981SWolfgang Gassler        $message = str_replace(
149442c3981SWolfgang Gassler            array('%media%','%user%'),
150442c3981SWolfgang Gassler            array($mediaName,$this->getAuthor()),
151442c3981SWolfgang Gassler            $this->getConf('commitMediaMsg')
152442c3981SWolfgang Gassler        );
153442c3981SWolfgang Gassler
154442c3981SWolfgang Gassler        $this->commitFile($mediaPath,$message);
155fa53f2a3SWolfgang Gassler
156fa53f2a3SWolfgang Gassler    }
157fa53f2a3SWolfgang Gassler
158fa53f2a3SWolfgang Gassler    public function handle_io_wikipage_write(Doku_Event &$event, $param) {
159fa53f2a3SWolfgang Gassler
160fa53f2a3SWolfgang Gassler        $rev = $event->data[3];
161fa53f2a3SWolfgang Gassler
162fa53f2a3SWolfgang Gassler        /* On update to an existing page this event is called twice,
163fa53f2a3SWolfgang Gassler         * once for the transfer of the old version to the attic (rev will have a value)
164fa53f2a3SWolfgang Gassler         * and once to write the new version of the page into the wiki (rev is false)
165fa53f2a3SWolfgang Gassler         */
166fa53f2a3SWolfgang Gassler        if (!$rev) {
167fa53f2a3SWolfgang Gassler
168fa53f2a3SWolfgang Gassler            $pagePath = $event->data[0][0];
169fa53f2a3SWolfgang Gassler            $pageName = $event->data[2];
170442c3981SWolfgang Gassler            $pageContent = $event->data[0][1];
171fa53f2a3SWolfgang Gassler
1727af27dc9SWolfgang Gassler            // get the summary directly from the form input
173e7471cfaSDanny Lin            // as the metadata hasn't updated yet
1747af27dc9SWolfgang Gassler            $editSummary = $GLOBALS['INPUT']->str('summary');
175442c3981SWolfgang Gassler
176442c3981SWolfgang Gassler            // empty content indicates a page deletion
177442c3981SWolfgang Gassler            if ($pageContent == '') {
178442c3981SWolfgang Gassler                // get the commit text for deletions
179d4e1c54bSWolfgang Gassler                $msgTemplate = $this->getConf('commitPageMsgDel');
180442c3981SWolfgang Gassler
181442c3981SWolfgang Gassler                // bad hack as DokuWiki deletes the file after this event
182442c3981SWolfgang Gassler                // thus, let's delete the file by ourselves, so git can recognize the deletion
183442c3981SWolfgang Gassler                // DokuWiki uses @unlink as well, so no error should be thrown if we delete it twice
184442c3981SWolfgang Gassler                @unlink($pagePath);
185442c3981SWolfgang Gassler
186442c3981SWolfgang Gassler            } else {
187442c3981SWolfgang Gassler                //get the commit text for edits
188d4e1c54bSWolfgang Gassler                $msgTemplate = $this->getConf('commitPageMsg');
189442c3981SWolfgang Gassler            }
190442c3981SWolfgang Gassler
191fa53f2a3SWolfgang Gassler            $message = str_replace(
192fa53f2a3SWolfgang Gassler                array('%page%','%summary%','%user%'),
193442c3981SWolfgang Gassler                array($pageName,$editSummary,$this->getAuthor()),
194442c3981SWolfgang Gassler                $msgTemplate
195fa53f2a3SWolfgang Gassler            );
196fa53f2a3SWolfgang Gassler
197442c3981SWolfgang Gassler            $this->commitFile($pagePath,$message);
198fa53f2a3SWolfgang Gassler
199fa53f2a3SWolfgang Gassler        }
200fa53f2a3SWolfgang Gassler
201fa53f2a3SWolfgang Gassler    }
202fa53f2a3SWolfgang Gassler
203fa53f2a3SWolfgang Gassler}
204fa53f2a3SWolfgang Gassler
205fa53f2a3SWolfgang Gassler// vim:ts=4:sw=4:et:
206