1<?php 2/** 3 * DokuWiki Plugin watchcycle (Admin Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Szymon Olewniczak <dokuwiki@cosmocode.de> 7 */ 8 9// must be run within Dokuwiki 10if (!defined('DOKU_INC')) { 11 die(); 12} 13 14class admin_plugin_approve extends DokuWiki_Admin_Plugin 15{ 16 17 /** @var helper_plugin_sqlite */ 18 protected $sqlite; 19 20 /** @var helper_plugin_approve */ 21 protected $helper; 22 23 /** 24 * @return helper_plugin_sqlite 25 */ 26 protected function sqlite() { 27 if (!$this->sqlite) { 28 /** @var helper_plugin_approve_db $db_helper */ 29 $db_helper = plugin_load('helper', 'approve_db'); 30 $this->sqlite = $db_helper->getDB(); 31 } 32 return $this->sqlite; 33 } 34 35 /** 36 * @return helper_plugin_approve 37 */ 38 protected function helper() { 39 if (!$this->helper) { 40 $helper = plugin_load('helper', 'approve'); 41 $this->helper = $helper; 42 } 43 return $this->helper; 44 } 45 46 /** 47 * @return int sort number in admin menu 48 */ 49 public function getMenuSort() 50 { 51 return 1; 52 } 53 54 protected function getPages() { 55 global $conf; 56 $datadir = $conf['datadir']; 57 if (substr($datadir, -1) != '/') { 58 $datadir .= '/'; 59 } 60 61 $directory = new RecursiveDirectoryIterator($datadir, FilesystemIterator::SKIP_DOTS); 62 $iterator = new RecursiveIteratorIterator($directory); 63 64 $pages = []; 65 /** @var SplFileInfo $fileinfo */ 66 foreach ($iterator as $fileinfo) { 67 if (!$fileinfo->isFile()) continue; 68 69 $path = $fileinfo->getPathname(); 70 //remove .txt 71 $id = str_replace('/', ':', substr($path, strlen($datadir), -4)); 72 $pages[] = $id; 73 } 74 75 return $pages; 76 } 77 78 protected function updatePage() 79 { 80 //clean current settings 81 $this->sqlite()->query('DELETE FROM page'); 82 83 $wikiPages = $this->getPages(); 84 $no_apr_namespace = $this->helper()->no_apr_namespace(); 85 $weighted_assignments = $this->helper()->weighted_assignments(); 86 foreach ($wikiPages as $id) { 87 if ($this->helper()->isPageAssigned($id, $maintainer, $weighted_assignments)) { 88 $data = [ 89 'page' => $id, 90 'hidden' => $this->helper()->in_hidden_namespace($id, $no_apr_namespace) ? '1' : '0' 91 ]; 92 if (!blank($maintainer)) { 93 $data['maintainer'] = $maintainer; 94 } 95 $this->sqlite()->storeEntry('page', $data); 96 } 97 } 98 99// $weighted_assignments = $this->helper()->weighted_assignments(); 100// 101// $approvePages = []; 102// $wikiPages = $this->getPages(); 103// foreach ($weighted_assignments as $assignment) { 104// $ns = ltrim($assignment['namespace'], ':'); 105// $maintainer = $assignment['maintainer']; 106// if (substr($ns, -2) == '**') { 107// //remove '**' 108// $ns = substr($ns, 0, -2); 109// foreach ($wikiPages as $id) { 110// if (substr($id, 0, strlen($ns)) == $ns) { 111// $approvePages[$id] = $maintainer; 112// } 113// } 114// } elseif (substr($ns, -1) == '*') { 115// //remove '*' 116// $ns = substr($ns, 0, -1); 117// foreach ($wikiPages as $id) { 118// $noNS = substr($id, strlen($id)); 119// if (strpos($noNS, ':') === FALSE && 120// substr($id, 0, strlen($ns)) == $ns) { 121// $approvePages[$id] = $maintainer; 122// } 123// } 124// } else { 125// $approvePages[$ns] = $maintainer; 126// } 127// } 128// 129// //clean current settings 130// $this->sqlite()->query('DELETE FROM page'); 131// $no_apr_namespace = $this->helper()->no_apr_namespace(); 132// foreach ($approvePages as $id => $maintainer) { 133// $data = [ 134// 'page' => $id, 135// 'hidden' => $this->helper()->in_hidden_namespace($id, $no_apr_namespace) 136// ]; 137// if (!blank($maintainer)) { 138// $data['maintainer'] = $maintainer; 139// } 140// $this->sqlite()->storeEntry('page', $data); 141// } 142 143 } 144 145 /** 146 * Should carry out any processing required by the plugin. 147 */ 148 public function handle() 149 { 150 global $ID; 151 152 /* @var Input */ 153 global $INPUT; 154 155 if($INPUT->str('action') && $INPUT->arr('assignment') && checkSecurityToken()) { 156 $assignment = $INPUT->arr('assignment'); 157 //insert empty string as NULL 158 if ($INPUT->str('action') === 'delete') { 159 $this->sqlite()->query('DELETE FROM maintainer WHERE id=?', $assignment['id']); 160 $this->updatePage(); 161 } else if ($INPUT->str('action') === 'add' && !blank($assignment['assign'])) { 162 $data = [ 163 'namespace' => $assignment['assign'] 164 ]; 165 if (!blank($assignment['maintainer'])) { 166 $data['maintainer'] = $assignment['maintainer']; 167 } 168 $this->sqlite()->storeEntry('maintainer', $data); 169 170 $this->updatePage(); 171 } 172 173 send_redirect(wl($ID, array('do' => 'admin', 'page' => 'approve'), true, '&')); 174 } 175 } 176 177 /** 178 * Render HTML output, e.g. helpful text and a form 179 */ 180 public function html() 181 { 182 global $ID; 183 /* @var DokuWiki_Auth_Plugin $auth */ 184 global $auth; 185 186 $res = $this->sqlite()->query('SELECT * FROM maintainer'); 187 $assignments = $this->sqlite()->res2arr($res); 188 189 echo $this->locale_xhtml('assignments_intro'); 190 191 echo '<form action="' . wl($ID) . '" action="post">'; 192 echo '<input type="hidden" name="do" value="admin" />'; 193 echo '<input type="hidden" name="page" value="approve" />'; 194 echo '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />'; 195 echo '<table class="inline">'; 196 197 // header 198 echo '<tr>'; 199 echo '<th>'.$this->getLang('admin h_assignment_namespace').'</th>'; 200 echo '<th>'.$this->getLang('admin h_assignment_maintainer').'</th>'; 201 echo '<th></th>'; 202 echo '</tr>'; 203 204 // existing assignments 205 foreach($assignments as $assignment) { 206 $id = $assignment['id']; 207 $namespace = $assignment['namespace']; 208 $maintainer = $assignment['maintainer'] ? $assignment['maintainer'] : '---'; 209 210 $link = wl( 211 $ID, array( 212 'do' => 'admin', 213 'page' => 'approve', 214 'action' => 'delete', 215 'sectok' => getSecurityToken(), 216 'assignment[id]' => $id 217 ) 218 ); 219 220 echo '<tr>'; 221 echo '<td>' . hsc($namespace) . '</td>'; 222 $user = $auth->getUserData($maintainer); 223 if ($user) { 224 echo '<td>' . hsc($user['name']) . '</td>'; 225 } else { 226 echo '<td>' . hsc($maintainer) . '</td>'; 227 } 228 echo '<td><a href="' . $link . '">'.$this->getLang('admin btn_delete').'</a></td>'; 229 echo '</tr>'; 230 } 231 232 // new assignment form 233 echo '<tr>'; 234 echo '<td><input type="text" name="assignment[assign]" /></td>'; 235 echo '<td>'; 236 echo '<select name="assignment[maintainer]">'; 237 echo '<option value="">---</option>'; 238 foreach($auth->retrieveUsers() as $login => $data) { 239 echo '<option value="' . hsc($login) . '">' . hsc($data['name']) . '</option>'; 240 } 241 echo '</select>'; 242 echo '</td>'; 243 echo '<td><button type="submit" name="action" value="add">'.$this->getLang('admin btn_add').'</button></td>'; 244 echo '</tr>'; 245 246 echo '</table>'; 247 } 248} 249 250// vim:ts=4:sw=4:et: 251