xref: /plugin/approve/action/approve.php (revision a99f41c6f8d693e000642604bd19275b43dcd649)
11aeb2b4dSghi<?php
21aeb2b4dSghi
31aeb2b4dSghiif(!defined('DOKU_INC')) die();
41aeb2b4dSghidefine(APPROVED, 'Approved');
51aeb2b4dSghi
61aeb2b4dSghiclass action_plugin_approve_approve extends DokuWiki_Action_Plugin {
71aeb2b4dSghi
850481663SSzymon Olewniczak    private $hlp;
950481663SSzymon Olewniczak    function __construct(){
1050481663SSzymon Olewniczak        $this->hlp = plugin_load('helper', 'approve');
1150481663SSzymon Olewniczak    }
1250481663SSzymon Olewniczak
1350481663SSzymon Olewniczak    function register(Doku_Event_Handler $controller) {
1450481663SSzymon Olewniczak
151aeb2b4dSghi        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, handle_approve, array());
1638d03fbdSghi        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, handle_viewer, array());
171aeb2b4dSghi        $controller->register_hook('TPL_ACT_RENDER', 'AFTER', $this, handle_diff_accept, array());
181aeb2b4dSghi        $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, handle_display_banner, array());
191aeb2b4dSghi        $controller->register_hook('HTML_SHOWREV_OUTPUT', 'BEFORE', $this, handle_showrev, array());
201aeb2b4dSghi    }
211aeb2b4dSghi
22d0c5854eSSzymon Olewniczak	function handle_diff_accept(Doku_Event $event, $param) {
2350481663SSzymon Olewniczak		global $ID;
2450481663SSzymon Olewniczak
2550481663SSzymon Olewniczak		if ($this->hlp->in_namespace($this->getConf('no_apr_namespaces'), $ID)) return;
2650481663SSzymon Olewniczak
271aeb2b4dSghi		if ($event->data == 'diff' && isset($_GET['approve'])) {
281aeb2b4dSghi			ptln('<a href="'.DOKU_URL.'doku.php?id='.$_GET['id'].'&approve=approve">'.$this->getLang('approve').'</a>');
291aeb2b4dSghi		}
301aeb2b4dSghi	}
311aeb2b4dSghi
32d0c5854eSSzymon Olewniczak	function handle_showrev(Doku_Event $event, $param) {
331aeb2b4dSghi		global $ID, $REV;
341aeb2b4dSghi
351aeb2b4dSghi		$last = $this->find_lastest_approved();
361aeb2b4dSghi		if ($last == $REV)
371aeb2b4dSghi			$event->preventDefault();
381aeb2b4dSghi	}
391aeb2b4dSghi
401aeb2b4dSghi	function can_approve() {
411aeb2b4dSghi		global $ID;
422cf0ddf9Sghi		return auth_quickaclcheck($ID) >= AUTH_DELETE;
431aeb2b4dSghi	}
441aeb2b4dSghi
45d0c5854eSSzymon Olewniczak	function handle_approve(Doku_Event $event, $param) {
46c7f8f6c0Sghi		global $ID, $REV, $INFO;
4750481663SSzymon Olewniczak
4850481663SSzymon Olewniczak		if ($this->hlp->in_namespace($this->getConf('no_apr_namespaces'), $ID)) return;
4950481663SSzymon Olewniczak
501aeb2b4dSghi		if ($event->data == 'show' && isset($_GET['approve'])) {
5138d03fbdSghi		    if ( ! $this->can_approve()) return;
5238d03fbdSghi
53c7f8f6c0Sghi			//change last commit comment to Approved
54c7f8f6c0Sghi			$meta = p_read_metadata($ID);
55c7f8f6c0Sghi			$meta[current][last_change][sum] = $meta[persistent][last_change][sum] = APPROVED;
56c7f8f6c0Sghi			$meta[current][last_change][user] = $meta[persistent][last_change][user] = $INFO[client];
579f9f0967Sghi			if (!array_key_exists($INFO[client], $meta[current][contributor])) {
58c7f8f6c0Sghi			    $meta[current][contributor][$INFO[client]] = $INFO[userinfo][name];
59c7f8f6c0Sghi			    $meta[persistent][contributor][$INFO[client]] = $INFO[userinfo][name];
601aeb2b4dSghi			}
61c7f8f6c0Sghi			p_save_metadata($ID, $meta);
62c7f8f6c0Sghi			//update changelog
63c7f8f6c0Sghi			//remove last line from file
64c7f8f6c0Sghi			$changelog_file = metaFN($ID, '.changes');
65c7f8f6c0Sghi			$changes = file($changelog_file, FILE_SKIP_EMPTY_LINES);
66c7f8f6c0Sghi			$lastLogLine = array_pop($changes);
67c7f8f6c0Sghi			$info = parseChangelogLine($lastLogLine);
68c7f8f6c0Sghi
69c7f8f6c0Sghi			$info[user] = $INFO[client];
70c7f8f6c0Sghi			$info[sum] = APPROVED;
71c7f8f6c0Sghi
72c7f8f6c0Sghi			$logline = implode("\t", $info)."\n";
73c7f8f6c0Sghi			array_push($changes, $logline);
74c7f8f6c0Sghi
75c7f8f6c0Sghi			io_saveFile($changelog_file, implode('', $changes));
761aeb2b4dSghi
771aeb2b4dSghi			header('Location: ?id='.$ID);
781aeb2b4dSghi		}
7938d03fbdSghi	}
80d0c5854eSSzymon Olewniczak    function handle_viewer(Doku_Event $event, $param) {
8138d03fbdSghi        global $REV, $ID;
8238d03fbdSghi        if ($event->data != 'show') return;
83*a99f41c6SRuud Habing        if (auth_quickaclcheck($ID) > AUTH_READ || ($this->hlp->in_namespace($this->getConf('no_apr_namespaces'), $ID))) return;
841aeb2b4dSghi
851aeb2b4dSghi	    $last = $this->find_lastest_approved();
8638d03fbdSghi	    //no page is approved
8738d03fbdSghi		if ($last == -1) return;
8838d03fbdSghi		//approved page is the newest page
8938d03fbdSghi		if ($last == 0) return;
9038d03fbdSghi
9138d03fbdSghi		//if we are viewing lastest revision, show last approved
9238d03fbdSghi		if ($REV == 0) header("Location: ?id=$ID&rev=$last");
931aeb2b4dSghi	}
941aeb2b4dSghi	function find_lastest_approved() {
951aeb2b4dSghi		global $ID;
961aeb2b4dSghi		$m = p_get_metadata($ID);
971aeb2b4dSghi		$sum = $m['last_change']['sum'];
981aeb2b4dSghi		if ($sum == APPROVED)
991aeb2b4dSghi			return 0;
1001aeb2b4dSghi
1011aeb2b4dSghi		$changelog = new PageChangeLog($ID);
1021aeb2b4dSghi		//wyszukaj najnowszej zatwierdzonej
1031aeb2b4dSghi		//poszukaj w dół
1041aeb2b4dSghi		$chs = $changelog->getRevisions(0, 10000);
1051aeb2b4dSghi		foreach ($chs as $rev) {
1061aeb2b4dSghi			$ch = $changelog->getRevisionInfo($rev);
1071aeb2b4dSghi			if ($ch['sum'] == APPROVED)
1081aeb2b4dSghi				return $rev;
1091aeb2b4dSghi		}
1101aeb2b4dSghi		return -1;
1111aeb2b4dSghi	}
1121aeb2b4dSghi
113d0c5854eSSzymon Olewniczak    function handle_display_banner(Doku_Event $event, $param) {
1141aeb2b4dSghi		global $ID, $REV, $INFO;
1151aeb2b4dSghi
11650481663SSzymon Olewniczak		if ($this->hlp->in_namespace($this->getConf('no_apr_namespaces'), $ID)) return;
1171aeb2b4dSghi        if ($event->data != 'show') return;
1181aeb2b4dSghi		if (!$INFO['exists']) return;
1191aeb2b4dSghi
120cf995419SSzymon Olewniczak		$sum = $this->hlp->page_sum($ID, $REV);
1211aeb2b4dSghi
1221aeb2b4dSghi		ptln('<div class="approval '.($sum == APPROVED ? 'approved_yes' : 'approved_no').'">');
1231aeb2b4dSghi
1241aeb2b4dSghi		tpl_pageinfo();
1251aeb2b4dSghi		ptln(' | ');
126274d699aSghi		$last_approved_rev = $this->find_lastest_approved();
1271aeb2b4dSghi		if ($sum == APPROVED) {
1281aeb2b4dSghi			ptln('<span>'.$this->getLang('approved').'</span>');
1291aeb2b4dSghi			if ($REV != 0 && auth_quickaclcheck($ID) > AUTH_READ) {
1301aeb2b4dSghi				ptln('<a href="'.wl($ID).'">');
1311aeb2b4dSghi				ptln($this->getLang($m['last_change']['sum'] == APPROVED ? 'newest_approved' : 'newest_draft'));
1321aeb2b4dSghi				ptln('</a>');
1331aeb2b4dSghi			} else if ($REV != 0 && $REV != $last_approved_rev) {
1341aeb2b4dSghi				ptln('<a href="'.wl($ID).'">');
1351aeb2b4dSghi				ptln($this->getLang('newest_approved'));
1361aeb2b4dSghi				ptln('</a>');
1371aeb2b4dSghi			}
1381aeb2b4dSghi		} else {
1391aeb2b4dSghi			ptln('<span>'.$this->getLang('draft').'</span>');
1401aeb2b4dSghi
141274d699aSghi			if ($last_approved_rev == -1) {
142274d699aSghi			    if ($REV != 0) {
143274d699aSghi				    ptln('<a href="'.wl($ID).'">');
144274d699aSghi				    	ptln($this->getLang('newest_draft'));
145274d699aSghi				    ptln('</a>');
146274d699aSghi				}
147274d699aSghi			} else {
1481aeb2b4dSghi				if ($last_approved_rev != 0)
1491aeb2b4dSghi					ptln('<a href="'.wl($ID, array('rev' => $last_approved_rev)).'">');
1501aeb2b4dSghi				else
1511aeb2b4dSghi					ptln('<a href="'.wl($ID).'">');
1521aeb2b4dSghi
1531aeb2b4dSghi					ptln($this->getLang('newest_approved'));
1541aeb2b4dSghi				ptln('</a>');
1551aeb2b4dSghi			}
1561aeb2b4dSghi
1571aeb2b4dSghi			//można zatwierdzać tylko najnowsze strony
1581aeb2b4dSghi			if ($REV == 0 && $this->can_approve()) {
1591aeb2b4dSghi				ptln('<a href="'.wl($ID, array('rev' => $last_approved_rev, 'do' => 'diff',
1601aeb2b4dSghi				'approve' => 'approve')).'">');
1611aeb2b4dSghi					ptln($this->getLang('approve'));
1621aeb2b4dSghi				ptln('</a>');
1631aeb2b4dSghi			}
1641aeb2b4dSghi		}
1651aeb2b4dSghi		ptln('</div>');
1661aeb2b4dSghi	}
1671aeb2b4dSghi
1681aeb2b4dSghi}
169