xref: /plugin/gitbacked/action/editcommit.php (revision d4e1c54bd44dbefda8bcf80fa1690f81cb3d99fe)
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';
17fa53f2a3SWolfgang Gasslerrequire_once(DOKU_PLUGIN.'gitbacked/lib/Git.php');
18fa53f2a3SWolfgang Gassler
19fa53f2a3SWolfgang Gasslerclass action_plugin_gitbacked_editcommit extends DokuWiki_Action_Plugin {
20fa53f2a3SWolfgang Gassler
21fa53f2a3SWolfgang Gassler    public function register(Doku_Event_Handler &$controller) {
22fa53f2a3SWolfgang Gassler
23fa53f2a3SWolfgang Gassler		$controller->register_hook('IO_WIKIPAGE_WRITE', 'AFTER', $this, 'handle_io_wikipage_write');
24442c3981SWolfgang Gassler		$controller->register_hook('MEDIA_UPLOAD_FINISH', 'AFTER', $this, 'handle_media_upload');
25442c3981SWolfgang Gassler		$controller->register_hook('MEDIA_DELETE_FILE', 'AFTER', $this, 'handle_media_deletion');
26442c3981SWolfgang Gassler    }
27442c3981SWolfgang Gassler
28442c3981SWolfgang Gassler	private function commitFile($filePath,$message) {
29442c3981SWolfgang Gassler
30442c3981SWolfgang Gassler		//get path to the repo root (by default DokuWiki's savedir)
31442c3981SWolfgang Gassler		$repoPath = DOKU_INC.$this->getConf('repoPath');
32442c3981SWolfgang Gassler		//init the repo and create a new one if it is not present
33442c3981SWolfgang Gassler		$repo = new GitRepo($repoPath, true, true);
34442c3981SWolfgang Gassler
35442c3981SWolfgang Gassler		$params = $this->getConf('addParams');
36442c3981SWolfgang Gassler		if ($params) {
37442c3981SWolfgang Gassler			$repo->git_path .= ' '.$params;
38442c3981SWolfgang Gassler		}
39442c3981SWolfgang Gassler
40442c3981SWolfgang Gassler		//add the changed file and set the commit message
41442c3981SWolfgang Gassler		$repo->add($filePath);
42442c3981SWolfgang Gassler		$repo->commit($message);
43442c3981SWolfgang Gassler
44442c3981SWolfgang Gassler		//if the push after Commit option is set we push the active branch to origin
45442c3981SWolfgang Gassler		if ($this->getConf('pushAfterCommit')) {
46442c3981SWolfgang Gassler			$repo->push('origin',$repo->active_branch());
47442c3981SWolfgang Gassler		}
48442c3981SWolfgang Gassler
49442c3981SWolfgang Gassler	}
50442c3981SWolfgang Gassler
51442c3981SWolfgang Gassler	private function getAuthor() {
52442c3981SWolfgang Gassler		return $GLOBALS['USERINFO']['name'];
53442c3981SWolfgang Gassler	}
54442c3981SWolfgang Gassler
55442c3981SWolfgang Gassler	public function handle_media_deletion(Doku_Event &$event, $param) {
56442c3981SWolfgang Gassler		$mediaPath = $event->data['path'];
57442c3981SWolfgang Gassler		$mediaName = $event->data['name'];
58442c3981SWolfgang Gassler
59442c3981SWolfgang Gassler		$message = str_replace(
60442c3981SWolfgang Gassler			array('%media%','%user%'),
61442c3981SWolfgang Gassler			array($mediaName,$this->getAuthor()),
62442c3981SWolfgang Gassler			$this->getConf('commitMediaMsgDel')
63442c3981SWolfgang Gassler		);
64442c3981SWolfgang Gassler
65442c3981SWolfgang Gassler		$this->commitFile($mediaPath,$message);
66442c3981SWolfgang Gassler
67442c3981SWolfgang Gassler	}
68442c3981SWolfgang Gassler
69442c3981SWolfgang Gassler	public function handle_media_upload(Doku_Event &$event, $param) {
70442c3981SWolfgang Gassler
71442c3981SWolfgang Gassler		$mediaPath = $event->data[1];
72442c3981SWolfgang Gassler		$mediaName = $event->data[2];
73442c3981SWolfgang Gassler
74442c3981SWolfgang Gassler		$message = str_replace(
75442c3981SWolfgang Gassler			array('%media%','%user%'),
76442c3981SWolfgang Gassler			array($mediaName,$this->getAuthor()),
77442c3981SWolfgang Gassler			$this->getConf('commitMediaMsg')
78442c3981SWolfgang Gassler		);
79442c3981SWolfgang Gassler
80442c3981SWolfgang Gassler		$this->commitFile($mediaPath,$message);
81fa53f2a3SWolfgang Gassler
82fa53f2a3SWolfgang Gassler	}
83fa53f2a3SWolfgang Gassler
84fa53f2a3SWolfgang Gassler    public function handle_io_wikipage_write(Doku_Event &$event, $param) {
85fa53f2a3SWolfgang Gassler
86fa53f2a3SWolfgang Gassler		$rev = $event->data[3];
87fa53f2a3SWolfgang Gassler
88fa53f2a3SWolfgang Gassler		/* On update to an existing page this event is called twice,
89fa53f2a3SWolfgang Gassler		* once for the transfer of the old version to the attic (rev will have a value)
90fa53f2a3SWolfgang Gassler		* and once to write the new version of the page into the wiki (rev is false)
91fa53f2a3SWolfgang Gassler		*/
92fa53f2a3SWolfgang Gassler		if (!$rev) {
93fa53f2a3SWolfgang Gassler
94fa53f2a3SWolfgang Gassler			$pagePath = $event->data[0][0];
95fa53f2a3SWolfgang Gassler			$pageName = $event->data[2];
96442c3981SWolfgang Gassler			$pageContent = $event->data[0][1];
97fa53f2a3SWolfgang Gassler
98fa53f2a3SWolfgang Gassler			$editSummary = $GLOBALS['INFO']['meta']['last_change']['sum'];
99fa53f2a3SWolfgang Gassler
100442c3981SWolfgang Gassler
101442c3981SWolfgang Gassler			//empty content indicates a page deletion
102442c3981SWolfgang Gassler			if ($pageContent == '') {
103442c3981SWolfgang Gassler				//get the commit text for deletions
104*d4e1c54bSWolfgang Gassler				$msgTemplate = $this->getConf('commitPageMsgDel');
105442c3981SWolfgang Gassler
106442c3981SWolfgang Gassler				// bad hack as DokuWiki deletes the file after this event
107442c3981SWolfgang Gassler				// thus, let's delete the file by ourselves, so git can recognize the deletion
108442c3981SWolfgang Gassler				// DokuWiki uses @unlink as well, so no error should be thrown if we delete it twice
109442c3981SWolfgang Gassler				@unlink($pagePath);
110442c3981SWolfgang Gassler
111442c3981SWolfgang Gassler			} else {
112442c3981SWolfgang Gassler				//get the commit text for edits
113*d4e1c54bSWolfgang Gassler				$msgTemplate = $this->getConf('commitPageMsg');
114442c3981SWolfgang Gassler			}
115442c3981SWolfgang Gassler
116fa53f2a3SWolfgang Gassler			$message = str_replace(
117fa53f2a3SWolfgang Gassler				array('%page%','%summary%','%user%'),
118442c3981SWolfgang Gassler				array($pageName,$editSummary,$this->getAuthor()),
119442c3981SWolfgang Gassler				$msgTemplate
120fa53f2a3SWolfgang Gassler			);
121fa53f2a3SWolfgang Gassler
122442c3981SWolfgang Gassler			$this->commitFile($pagePath,$message);
123fa53f2a3SWolfgang Gassler
124fa53f2a3SWolfgang Gassler		}
125fa53f2a3SWolfgang Gassler
126fa53f2a3SWolfgang Gassler    }
127fa53f2a3SWolfgang Gassler
128fa53f2a3SWolfgang Gassler}
129fa53f2a3SWolfgang Gassler
130fa53f2a3SWolfgang Gassler// vim:ts=4:sw=4:et:
131