xref: /plugin/gitbacked/action/editcommit.php (revision 7af27dc9a251b9ea7140d079ca8dce4aa3f0c23a)
1<?php
2/**
3 * DokuWiki Plugin gitbacked (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Wolfgang Gassler <wolfgang@gassler.org>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
13if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
14if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
15
16require_once DOKU_PLUGIN.'action.php';
17require_once(DOKU_PLUGIN.'gitbacked/lib/Git.php');
18
19class action_plugin_gitbacked_editcommit extends DokuWiki_Action_Plugin {
20
21    public function register(Doku_Event_Handler &$controller) {
22
23		$controller->register_hook('IO_WIKIPAGE_WRITE', 'AFTER', $this, 'handle_io_wikipage_write');
24		$controller->register_hook('MEDIA_UPLOAD_FINISH', 'AFTER', $this, 'handle_media_upload');
25		$controller->register_hook('MEDIA_DELETE_FILE', 'AFTER', $this, 'handle_media_deletion');
26    }
27
28	private function commitFile($filePath,$message) {
29
30		//get path to the repo root (by default DokuWiki's savedir)
31		$repoPath = DOKU_INC.$this->getConf('repoPath');
32		//init the repo and create a new one if it is not present
33		$repo = new GitRepo($repoPath, true, true);
34
35		$params = $this->getConf('addParams');
36		if ($params) {
37			$repo->git_path .= ' '.$params;
38		}
39
40		//add the changed file and set the commit message
41		$repo->add($filePath);
42		$repo->commit($message);
43
44		//if the push after Commit option is set we push the active branch to origin
45		if ($this->getConf('pushAfterCommit')) {
46			$repo->push('origin',$repo->active_branch());
47		}
48
49	}
50
51	private function getAuthor() {
52		return $GLOBALS['USERINFO']['name'];
53	}
54
55	public function handle_media_deletion(Doku_Event &$event, $param) {
56		$mediaPath = $event->data['path'];
57		$mediaName = $event->data['name'];
58
59		$message = str_replace(
60			array('%media%','%user%'),
61			array($mediaName,$this->getAuthor()),
62			$this->getConf('commitMediaMsgDel')
63		);
64
65		$this->commitFile($mediaPath,$message);
66
67	}
68
69	public function handle_media_upload(Doku_Event &$event, $param) {
70
71		$mediaPath = $event->data[1];
72		$mediaName = $event->data[2];
73
74		$message = str_replace(
75			array('%media%','%user%'),
76			array($mediaName,$this->getAuthor()),
77			$this->getConf('commitMediaMsg')
78		);
79
80		$this->commitFile($mediaPath,$message);
81
82	}
83
84    public function handle_io_wikipage_write(Doku_Event &$event, $param) {
85
86		$rev = $event->data[3];
87
88		/* On update to an existing page this event is called twice,
89		* once for the transfer of the old version to the attic (rev will have a value)
90		* and once to write the new version of the page into the wiki (rev is false)
91		*/
92		if (!$rev) {
93
94			$pagePath = $event->data[0][0];
95			$pageName = $event->data[2];
96			$pageContent = $event->data[0][1];
97
98			// get the summary directly from the form input
99			// as the metadata hasn't updated yer
100			$editSummary = $GLOBALS['INPUT']->str('summary');
101
102			// empty content indicates a page deletion
103			if ($pageContent == '') {
104				// get the commit text for deletions
105				$msgTemplate = $this->getConf('commitPageMsgDel');
106
107				// bad hack as DokuWiki deletes the file after this event
108				// thus, let's delete the file by ourselves, so git can recognize the deletion
109				// DokuWiki uses @unlink as well, so no error should be thrown if we delete it twice
110				@unlink($pagePath);
111
112			} else {
113				//get the commit text for edits
114				$msgTemplate = $this->getConf('commitPageMsg');
115			}
116
117			$message = str_replace(
118				array('%page%','%summary%','%user%'),
119				array($pageName,$editSummary,$this->getAuthor()),
120				$msgTemplate
121			);
122
123			$this->commitFile($pagePath,$message);
124
125		}
126
127    }
128
129}
130
131// vim:ts=4:sw=4:et:
132