11aeb2b4dSghi<?php 21aeb2b4dSghi 31aeb2b4dSghiif(!defined('DOKU_INC')) die(); 41aeb2b4dSghidefine(APPROVED, 'Approved'); 51aeb2b4dSghi 6af3e3cd8SSzymon Olewniczakdefine(METADATA_VERSION_KEY, 'plugin_approve_version'); 7af3e3cd8SSzymon Olewniczak 81aeb2b4dSghiclass action_plugin_approve_approve extends DokuWiki_Action_Plugin { 91aeb2b4dSghi 1050481663SSzymon Olewniczak private $hlp; 1150481663SSzymon Olewniczak function __construct(){ 1250481663SSzymon Olewniczak $this->hlp = plugin_load('helper', 'approve'); 1350481663SSzymon Olewniczak } 1450481663SSzymon Olewniczak 1550481663SSzymon Olewniczak function register(Doku_Event_Handler $controller) { 1650481663SSzymon Olewniczak 171aeb2b4dSghi $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, handle_approve, array()); 1838d03fbdSghi $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, handle_viewer, array()); 191aeb2b4dSghi $controller->register_hook('TPL_ACT_RENDER', 'AFTER', $this, handle_diff_accept, array()); 201aeb2b4dSghi $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, handle_display_banner, array()); 211aeb2b4dSghi $controller->register_hook('HTML_SHOWREV_OUTPUT', 'BEFORE', $this, handle_showrev, array()); 22af3e3cd8SSzymon Olewniczak // ensure a page revision is created when summary changes: 23af3e3cd8SSzymon Olewniczak $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'BEFORE', $this, 'handle_pagesave_before'); 241aeb2b4dSghi } 251aeb2b4dSghi 26d0c5854eSSzymon Olewniczak function handle_diff_accept(Doku_Event $event, $param) { 2750481663SSzymon Olewniczak global $ID; 2850481663SSzymon Olewniczak 2950481663SSzymon Olewniczak if ($this->hlp->in_namespace($this->getConf('no_apr_namespaces'), $ID)) return; 3050481663SSzymon Olewniczak 311aeb2b4dSghi if ($event->data == 'diff' && isset($_GET['approve'])) { 321aeb2b4dSghi ptln('<a href="'.DOKU_URL.'doku.php?id='.$_GET['id'].'&approve=approve">'.$this->getLang('approve').'</a>'); 331aeb2b4dSghi } 341aeb2b4dSghi } 351aeb2b4dSghi 36d0c5854eSSzymon Olewniczak function handle_showrev(Doku_Event $event, $param) { 371aeb2b4dSghi global $ID, $REV; 381aeb2b4dSghi 391aeb2b4dSghi $last = $this->find_lastest_approved(); 401aeb2b4dSghi if ($last == $REV) 411aeb2b4dSghi $event->preventDefault(); 421aeb2b4dSghi } 431aeb2b4dSghi 441aeb2b4dSghi function can_approve() { 451aeb2b4dSghi global $ID; 462cf0ddf9Sghi return auth_quickaclcheck($ID) >= AUTH_DELETE; 471aeb2b4dSghi } 481aeb2b4dSghi 49d0c5854eSSzymon Olewniczak function handle_approve(Doku_Event $event, $param) { 50c7f8f6c0Sghi global $ID, $REV, $INFO; 5150481663SSzymon Olewniczak 5250481663SSzymon Olewniczak if ($this->hlp->in_namespace($this->getConf('no_apr_namespaces'), $ID)) return; 5350481663SSzymon Olewniczak 541aeb2b4dSghi if ($event->data == 'show' && isset($_GET['approve'])) { 5538d03fbdSghi if ( ! $this->can_approve()) return; 5638d03fbdSghi 57af3e3cd8SSzymon Olewniczak //create new page revison 58af3e3cd8SSzymon Olewniczak saveWikiText($ID, rawWiki($ID), APPROVED); 591aeb2b4dSghi 601aeb2b4dSghi header('Location: ?id='.$ID); 611aeb2b4dSghi } 6238d03fbdSghi } 63d0c5854eSSzymon Olewniczak function handle_viewer(Doku_Event $event, $param) { 6438d03fbdSghi global $REV, $ID; 6538d03fbdSghi if ($event->data != 'show') return; 66a99f41c6SRuud Habing if (auth_quickaclcheck($ID) > AUTH_READ || ($this->hlp->in_namespace($this->getConf('no_apr_namespaces'), $ID))) return; 671aeb2b4dSghi 681aeb2b4dSghi $last = $this->find_lastest_approved(); 6938d03fbdSghi //no page is approved 7038d03fbdSghi if ($last == -1) return; 7138d03fbdSghi //approved page is the newest page 7238d03fbdSghi if ($last == 0) return; 7338d03fbdSghi 7438d03fbdSghi //if we are viewing lastest revision, show last approved 7538d03fbdSghi if ($REV == 0) header("Location: ?id=$ID&rev=$last"); 761aeb2b4dSghi } 771aeb2b4dSghi function find_lastest_approved() { 781aeb2b4dSghi global $ID; 791aeb2b4dSghi $m = p_get_metadata($ID); 801aeb2b4dSghi $sum = $m['last_change']['sum']; 811aeb2b4dSghi if ($sum == APPROVED) 821aeb2b4dSghi return 0; 831aeb2b4dSghi 841aeb2b4dSghi $changelog = new PageChangeLog($ID); 851aeb2b4dSghi //wyszukaj najnowszej zatwierdzonej 861aeb2b4dSghi //poszukaj w dół 871aeb2b4dSghi $chs = $changelog->getRevisions(0, 10000); 881aeb2b4dSghi foreach ($chs as $rev) { 891aeb2b4dSghi $ch = $changelog->getRevisionInfo($rev); 901aeb2b4dSghi if ($ch['sum'] == APPROVED) 911aeb2b4dSghi return $rev; 921aeb2b4dSghi } 931aeb2b4dSghi return -1; 941aeb2b4dSghi } 951aeb2b4dSghi 96d0c5854eSSzymon Olewniczak function handle_display_banner(Doku_Event $event, $param) { 971aeb2b4dSghi global $ID, $REV, $INFO; 981aeb2b4dSghi 9950481663SSzymon Olewniczak if ($this->hlp->in_namespace($this->getConf('no_apr_namespaces'), $ID)) return; 1001aeb2b4dSghi if ($event->data != 'show') return; 1011aeb2b4dSghi if (!$INFO['exists']) return; 1021aeb2b4dSghi 103cf995419SSzymon Olewniczak $sum = $this->hlp->page_sum($ID, $REV); 1041aeb2b4dSghi 1051aeb2b4dSghi ptln('<div class="approval '.($sum == APPROVED ? 'approved_yes' : 'approved_no').'">'); 1061aeb2b4dSghi 1071aeb2b4dSghi tpl_pageinfo(); 1081aeb2b4dSghi ptln(' | '); 109274d699aSghi $last_approved_rev = $this->find_lastest_approved(); 1101aeb2b4dSghi if ($sum == APPROVED) { 111af3e3cd8SSzymon Olewniczak $version = p_get_metadata($ID, METADATA_VERSION_KEY); 112*47b144feSSzymon Olewniczak if (!$version) { 113*47b144feSSzymon Olewniczak $version = $this->calculateVersion($ID); 114*47b144feSSzymon Olewniczak p_set_metadata($ID, array(METADATA_VERSION_KEY => $version)); 115*47b144feSSzymon Olewniczak } 116af3e3cd8SSzymon Olewniczak 117*47b144feSSzymon Olewniczak ptln('<span>'.$this->getLang('approved').'</span> (' . $this->getLang('version') . ': ' . $version 118*47b144feSSzymon Olewniczak . ')'); 1191aeb2b4dSghi if ($REV != 0 && auth_quickaclcheck($ID) > AUTH_READ) { 1201aeb2b4dSghi ptln('<a href="'.wl($ID).'">'); 121af3e3cd8SSzymon Olewniczak ptln($this->getLang(p_get_metadata($ID, 'last_change sum') == APPROVED ? 'newest_approved' : 'newest_draft')); 1221aeb2b4dSghi ptln('</a>'); 1231aeb2b4dSghi } else if ($REV != 0 && $REV != $last_approved_rev) { 1241aeb2b4dSghi ptln('<a href="'.wl($ID).'">'); 1251aeb2b4dSghi ptln($this->getLang('newest_approved')); 1261aeb2b4dSghi ptln('</a>'); 1271aeb2b4dSghi } 1281aeb2b4dSghi } else { 1291aeb2b4dSghi ptln('<span>'.$this->getLang('draft').'</span>'); 1301aeb2b4dSghi 131274d699aSghi if ($last_approved_rev == -1) { 132274d699aSghi if ($REV != 0) { 133274d699aSghi ptln('<a href="'.wl($ID).'">'); 134274d699aSghi ptln($this->getLang('newest_draft')); 135274d699aSghi ptln('</a>'); 136274d699aSghi } 137274d699aSghi } else { 1381aeb2b4dSghi if ($last_approved_rev != 0) 1391aeb2b4dSghi ptln('<a href="'.wl($ID, array('rev' => $last_approved_rev)).'">'); 1401aeb2b4dSghi else 1411aeb2b4dSghi ptln('<a href="'.wl($ID).'">'); 1421aeb2b4dSghi 1431aeb2b4dSghi ptln($this->getLang('newest_approved')); 1441aeb2b4dSghi ptln('</a>'); 1451aeb2b4dSghi } 1461aeb2b4dSghi 1471aeb2b4dSghi //można zatwierdzać tylko najnowsze strony 1481aeb2b4dSghi if ($REV == 0 && $this->can_approve()) { 1491aeb2b4dSghi ptln('<a href="'.wl($ID, array('rev' => $last_approved_rev, 'do' => 'diff', 1501aeb2b4dSghi 'approve' => 'approve')).'">'); 1511aeb2b4dSghi ptln($this->getLang('approve')); 1521aeb2b4dSghi ptln('</a>'); 1531aeb2b4dSghi } 1541aeb2b4dSghi } 1551aeb2b4dSghi ptln('</div>'); 1561aeb2b4dSghi } 1571aeb2b4dSghi 158af3e3cd8SSzymon Olewniczak /** 159af3e3cd8SSzymon Olewniczak * Check if the page has to be changed 160af3e3cd8SSzymon Olewniczak * 161af3e3cd8SSzymon Olewniczak * @param Doku_Event $event event object by reference 162af3e3cd8SSzymon Olewniczak * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 163af3e3cd8SSzymon Olewniczak * handler was registered] 164af3e3cd8SSzymon Olewniczak * @return void 165af3e3cd8SSzymon Olewniczak */ 166af3e3cd8SSzymon Olewniczak public function handle_pagesave_before(Doku_Event $event, $param) { 167af3e3cd8SSzymon Olewniczak $id = $event->data['id']; 168af3e3cd8SSzymon Olewniczak if ($this->hlp->in_namespace($this->getConf('no_apr_namespaces'), $id)) return; 169af3e3cd8SSzymon Olewniczak 170af3e3cd8SSzymon Olewniczak //save page if summary is provided 171af3e3cd8SSzymon Olewniczak if($event->data['summary'] == APPROVED) { 172af3e3cd8SSzymon Olewniczak $event->data['contentChanged'] = true; 173af3e3cd8SSzymon Olewniczak 174af3e3cd8SSzymon Olewniczak $version = p_get_metadata($id, METADATA_VERSION_KEY); 175af3e3cd8SSzymon Olewniczak 176af3e3cd8SSzymon Olewniczak //calculate current version 177af3e3cd8SSzymon Olewniczak if (!$version) { 178af3e3cd8SSzymon Olewniczak $version = $this->calculateVersion($id); 179af3e3cd8SSzymon Olewniczak } else { 180af3e3cd8SSzymon Olewniczak $version += 1; 181af3e3cd8SSzymon Olewniczak } 182af3e3cd8SSzymon Olewniczak 183af3e3cd8SSzymon Olewniczak p_set_metadata($id, array(METADATA_VERSION_KEY => $version)); 184af3e3cd8SSzymon Olewniczak } 185af3e3cd8SSzymon Olewniczak } 186af3e3cd8SSzymon Olewniczak 187af3e3cd8SSzymon Olewniczak /** 188af3e3cd8SSzymon Olewniczak * Calculate current version 189af3e3cd8SSzymon Olewniczak * 190af3e3cd8SSzymon Olewniczak * @param $id 191af3e3cd8SSzymon Olewniczak * @return int 192af3e3cd8SSzymon Olewniczak */ 193af3e3cd8SSzymon Olewniczak protected function calculateVersion($id) { 194af3e3cd8SSzymon Olewniczak $version = 1; 195af3e3cd8SSzymon Olewniczak 196af3e3cd8SSzymon Olewniczak $changelog = new PageChangeLog($id); 197af3e3cd8SSzymon Olewniczak $first = 0; 198af3e3cd8SSzymon Olewniczak $num = 100; 199af3e3cd8SSzymon Olewniczak while (count($revs = $changelog->getRevisions($first, $num)) > 0) { 200af3e3cd8SSzymon Olewniczak foreach ($revs as $rev) { 201af3e3cd8SSzymon Olewniczak $revInfo = $changelog->getRevisionInfo($rev); 202af3e3cd8SSzymon Olewniczak if ($revInfo['sum'] == APPROVED) { 203af3e3cd8SSzymon Olewniczak $version += 1; 204af3e3cd8SSzymon Olewniczak } 205af3e3cd8SSzymon Olewniczak } 206af3e3cd8SSzymon Olewniczak $first += $num; 207af3e3cd8SSzymon Olewniczak } 208af3e3cd8SSzymon Olewniczak 209af3e3cd8SSzymon Olewniczak return $version; 210af3e3cd8SSzymon Olewniczak } 211af3e3cd8SSzymon Olewniczak 2121aeb2b4dSghi} 213