1<?php 2/** 3 * DokuWiki Plugin todo_list (Syntax Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 */ 7 8// must be run within Dokuwiki 9if(!defined('DOKU_INC')) die(); 10 11/** 12 * Class syntax_plugin_todo_list 13 */ 14class syntax_plugin_todo_list extends syntax_plugin_todo_todo { 15 16 /** 17 * @return string Syntax mode type 18 */ 19 public function getType() { 20 return 'substition'; 21 } 22 23 /** 24 * @return string Paragraph type 25 */ 26 public function getPType() { 27 return 'block'; 28 } 29 30 /** 31 * @return int Sort order - Low numbers go before high numbers 32 */ 33 public function getSort() { 34 return 250; 35 } 36 37 /** 38 * Connect lookup pattern to lexer. 39 * 40 * @param string $mode Parser mode 41 */ 42 public function connectTo($mode) { 43 $this->Lexer->addSpecialPattern('~~TODOLIST[^~]*~~', $mode, 'plugin_todo_list'); 44 } 45 46 /** 47 * Handle matches of the todolist syntax 48 * 49 * @param string $match The match of the syntax 50 * @param int $state The state of the handler 51 * @param int $pos The position in the document 52 * @param Doku_Handler $handler The handler 53 * @return array Data for the renderer 54 */ 55 public function handle($match, $state, $pos, Doku_Handler $handler) { 56 57 $options = substr($match, 10, -2); // strip markup 58 $options = explode(' ', $options); 59 $data = array( 60 'header' => $this->getConf("Header"), 61 'completed' => 'all', 62 'assigned' => 'all', 63 'completeduserlist' => 'all', 64 'ns' => 'all', 65 'showdate' => $this->getConf("ShowdateList"), 66 'checkbox' => $this->getConf("Checkbox"), 67 'username' => $this->getConf("Username"), 68 'short' => false, 69 ); 70 $allowedvalues = array('yes', 'no'); 71 foreach($options as $option) { 72 @list($key, $value) = explode(':', $option, 2); 73 switch($key) { 74 case 'header': // how should the header be rendered? 75 if(in_array($value, array('id', 'firstheader', 'none'))) { 76 $data['header'] = $value; 77 } 78 break; 79 case 'short': 80 if(in_array($value, $allowedvalues)) { 81 $data['short'] = ($value == 'yes'); 82 } 83 break; 84 case 'showdate': 85 if(in_array($value, $allowedvalues)) { 86 $data['showdate'] = ($value == 'yes'); 87 } 88 break; 89 case 'checkbox': // should checkbox be rendered? 90 if(in_array($value, $allowedvalues)) { 91 $data['checkbox'] = ($value == 'yes'); 92 } 93 break; 94 case 'completed': 95 if(in_array($value, $allowedvalues)) { 96 $data['completed'] = ($value == 'yes'); 97 } 98 break; 99 case 'username': // how should the username be rendered? 100 if(in_array($value, array('user', 'real', 'none'))) { 101 $data['username'] = $value; 102 } 103 break; 104 case 'assigned': 105 if(in_array($value, $allowedvalues)) { 106 $data['assigned'] = ($value == 'yes'); 107 break; 108 } 109 //assigned? 110 $data['assigned'] = explode(',', $value); 111 // @date 20140317 le: if check for logged in user, also check for logged in user email address 112 if( in_array( '@@USER@@', $data['assigned'] ) ) { 113 $data['assigned'][] = '@@MAIL@@'; 114 } 115 $data['assigned'] = array_map( array($this,"__todolistTrimUser"), $data['assigned'] ); 116 break; 117 case 'completeduser': 118 $data['completeduserlist'] = explode(',', $value); 119 // @date 20140317 le: if check for logged in user, also check for logged in user email address 120 if(in_array('@@USER@@', $data['completeduserlist'])) { 121 $data['completeduserlist'][] = '@@MAIL@@'; 122 } 123 $data['completeduserlist'] = array_map( array($this,"__todolistTrimUser"), $data['completeduserlist'] ); 124 break; 125 case 'ns': 126 $data['ns'] = $value; 127 break; 128 case 'startbefore': 129 list($data['startbefore'], $data['startignore']) = $this->analyseDate($value); 130 break; 131 case 'startafter': 132 list($data['startafter'], $data['startignore']) = $this->analyseDate($value); 133 break; 134 case 'startat': 135 list($data['startat'], $data['startignore']) = $this->analyseDate($value); 136 break; 137 case 'duebefore': 138 list($data['duebefore'], $data['dueignore']) = $this->analyseDate($value); 139 break; 140 case 'dueafter': 141 list($data['dueafter'], $data['dueignore']) = $this->analyseDate($value); 142 break; 143 case 'dueat': 144 list($data['dueat'], $data['dueignore']) = $this->analyseDate($value); 145 break; 146 case 'completedbefore': 147 list($data['completedbefore']) = $this->analyseDate($value); 148 break; 149 case 'completedafter': 150 list($data['completedafter']) = $this->analyseDate($value); 151 break; 152 case 'completedat': 153 list($data['completedat']) = $this->analyseDate($value); 154 break; 155 } 156 } 157 return $data; 158 } 159 160 /** 161 * Render xhtml output or metadata 162 * 163 * @param string $mode Renderer mode (supported modes: xhtml) 164 * @param Doku_Renderer $renderer The renderer 165 * @param array $data The data from the handler() function 166 * @return bool If rendering was successful. 167 */ 168 public function render($mode, Doku_Renderer $renderer, $data) { 169 global $conf; 170 171 if($mode != 'xhtml') return false; 172 /** @var Doku_Renderer_xhtml $renderer */ 173 174 $opts['pattern'] = '/<todo([^>]*)>(.*?)<\/todo[\W]*?>/'; //all todos in a wiki page 175 $opts['ns'] = $data['ns']; 176 //TODO check if storing subpatterns doesn't cost too much resources 177 178 // search(&$data, $base, $func, $opts,$dir='',$lvl=1,$sort='natural') 179 search($todopages, $conf['datadir'], array($this, 'search_todos'), $opts); //browse wiki pages with callback to search_pattern 180 181 $todopages = $this->filterpages($todopages, $data); 182 183 foreach($todopages as &$page) { 184 uasort($page['todos'], function($a, $b) { 185 if(isset($a['due']) && isset($b['due'])) { 186 return $a['due'] <=> $b['due']; 187 } else if (isset($a['due']) xor isset($b['due'])) { 188 return isset($a['due']) ? -1 : 1; 189 } else { 190 return 0; 191 } 192 }); 193 } 194 195 if($data['short']) { 196 $this->htmlShort($renderer, $todopages, $data); 197 } else { 198 $this->htmlTodoTable($renderer, $todopages, $data); 199 } 200 201 return true; 202 } 203 204 /** 205 * Custom search callback 206 * 207 * This function is called for every found file or 208 * directory. When a directory is given to the function it has to 209 * decide if this directory should be traversed (true) or not (false). 210 * Return values for files are ignored 211 * 212 * All functions should check the ACL for document READ rights 213 * namespaces (directories) are NOT checked (when sneaky_index is 0) as this 214 * would break the recursion (You can have an nonreadable dir over a readable 215 * one deeper nested) also make sure to check the file type (for example 216 * in case of lockfiles). 217 * 218 * @param array &$data - Reference to the result data structure 219 * @param string $base - Base usually $conf['datadir'] 220 * @param string $file - current file or directory relative to $base 221 * @param string $type - Type either 'd' for directory or 'f' for file 222 * @param int $lvl - Current recursion depht 223 * @param array $opts - option array as given to search() 224 * @return bool if this directory should be traversed (true) or not (false). Return values for files are ignored. 225 */ 226 public function search_todos(&$data, $base, $file, $type, $lvl, $opts) { 227 $item['id'] = pathID($file); //get current file ID 228 229 //we do nothing with directories 230 if($type == 'd') return true; 231 232 //only search txt files 233 if(substr($file, -4) != '.txt') return true; 234 235 //check ACL 236 if(auth_quickaclcheck($item['id']) < AUTH_READ) return false; 237 238 // filter namespaces 239 if(!$this->filter_ns($item['id'], $opts['ns'])) return false; 240 241 $wikitext = rawWiki($item['id']); //get wiki text 242 243 // check if ~~NOTODO~~ is set on the page to skip this page 244 if(1 == preg_match('/~~NOTODO~~/', $wikitext)) return false; 245 246 $item['count'] = preg_match_all($opts['pattern'], $wikitext, $matches); //count how many times appears the pattern 247 if(!empty($item['count'])) { //if it appears at least once 248 $item['matches'] = $matches; 249 $data[] = $item; 250 } 251 return true; 252 } 253 254 /** 255 * filter namespaces 256 * 257 * @param $todopages array pages with all todoitems 258 * @param $item string listing parameters 259 * @return boolean if item id is in namespace 260 */ 261 private function filter_ns($item, $ns) { 262 global $ID; 263 // check if we should accept currant namespace+subnamespaces or only subnamespaces 264 $wildsubns = substr($ns, -2) == '.:'; 265 $onlysubns = !$wildsubns && (substr($ns, -1) == ':' || substr($ns, -2) == ':.'); 266// $onlyns = $onlysubns && substr($ns, -1) == '.'; 267 268 // if first char of ns is '.'replace it with current ns 269 if ($ns[0] == '.') { 270 $ns = substr($ID, 0, strrpos($ID, ':')+1).ltrim($ns, '.:'); 271 } 272 $ns = trim($ns, '.:'); 273 $len = strlen($ns); 274 $parsepage = false; 275 276 if ($parsepage = $ns == 'all') { 277 // Always return the todo pages 278 } elseif ($ns == '/') { 279 // Only return the todo page if it's in the root namespace 280 $parsepage = strpos($item, ':') === FALSE; 281 } elseif ($wildsubns) { 282 $p = strpos($item.':', ':', $len+1); 283 $x = substr($item, $len+1, $p-$len); 284 $parsepage = 0 === strpos($item, rtrim($ns.':'.$x, ':').':'); 285 } elseif ($onlysubns) { 286 $parsepage = 0 === strpos($item, $ns.':'); 287 } elseif ($parsepage = substr($item, 0, $len) == $ns) { 288 } 289 return $parsepage; 290 } 291 292 /** 293 * Expand assignee-placeholders 294 * 295 * @param $user String to be worked on 296 * @return expanded string 297 */ 298 private function __todolistExpandAssignees($user) { 299 global $USERINFO; 300 if($user == '@@USER@@' && !empty($_SERVER['REMOTE_USER'])) { //$INPUT->server->str('REMOTE_USER') 301 return $_SERVER['REMOTE_USER']; 302 } 303 // @date 20140317 le: check for logged in user email address 304 if( $user == '@@MAIL@@' && isset( $USERINFO['mail'] ) ) { 305 return $USERINFO['mail']; 306 } 307 return $user; 308 } 309 310 /** 311 * Trim input if it's a user 312 * 313 * @param $user String to be worked on 314 * @return trimmed string 315 */ 316 private function __todolistTrimUser($user) { 317 //placeholder (inspired by replacement-patterns - see https://www.dokuwiki.org/namespace_templates#replacement_patterns) 318 if( $user == '@@USER@@' || $user == '@@MAIL@@' ) { 319 return $user; 320 } 321 //user 322 return trim(ltrim($user, '@')); 323 } 324 325 /** 326 * filter the pages 327 * 328 * @param $todopages array pages with all todoitems 329 * @param $data array listing parameters 330 * @return array of filtered pages 331 */ 332 private function filterpages($todopages, $data) { 333 // skip function if $todopages has no values 334 $pages = array(); 335 if(isset($todopages) && count($todopages)>0) { 336 foreach($todopages as $page) { 337 $todos = array(); 338 // contains 3 arrays: an array with complete matches and 2 arrays with subpatterns 339 foreach($page['matches'][1] as $todoindex => $todomatch) { 340 $todo = array_merge(array('todotitle' => trim($page['matches'][2][$todoindex]), 'todoindex' => $todoindex), $this->parseTodoArgs($todomatch), $data); 341 342 if($this->isRequestedTodo($todo)) { 343 $todos[] = $todo; 344 } 345 } 346 if(isset($todos) && count($todos) > 0) { 347 $pages[] = array('id' => $page['id'], 'todos' => $todos); 348 } 349 } 350 } 351 return $pages; 352 } 353 354 355 private function htmlShort($R, $todopages, $data) { 356 if (is_null($todopages)) return; 357 $done = 0; $todo = 0; 358 foreach($todopages as $page) { 359 foreach($page['todos'] as $value) { 360 $todo++; 361 if ($value['checked']) { 362 $done++; 363 } 364 } 365 } 366 367 $R->cdata("($done/$todo)"); 368 } 369 370 /** 371 * Create html for table with todos 372 * 373 * @param Doku_Renderer_xhtml $R 374 * @param array $todopages 375 * @param array $data array with rendering options 376 */ 377 private function htmlTodoTable($R, $todopages, $data) { 378 if (is_null($todopages)) return; 379 $R->table_open(); 380 foreach($todopages as $page) { 381 if ($data['header']!='none') { 382 $R->tablerow_open(); 383 $R->tableheader_open(); 384 $R->internallink(':'.$page['id'], ($data['header']=='firstheader' ? p_get_first_heading($page['id']) : $page['id'])); 385 $R->tableheader_close(); 386 $R->tablerow_close(); 387 } 388 foreach($page['todos'] as $todo) { 389 if(empty($todo['todotitle'])) 390 { 391 $todo['todotitle'] = '<no title>'; 392 } 393 $R->tablerow_open(); 394 $R->tablecell_open(); 395 $R->doc .= $this->createTodoItem($R, $page['id'], array_merge($todo, $data)); 396 $R->tablecell_close(); 397 $R->tablerow_close(); 398 } 399 } 400 $R->table_close(); 401 } 402 403 /** 404 * Check the conditions for adding a todoitem 405 * 406 * @param $data array the defined filters 407 * @param $checked bool completion status of task; true: finished, false: open 408 * @param $todouser string user username of user 409 * @return bool if the todoitem should be listed 410 */ 411 private function isRequestedTodo($data) { 412 //completion status 413 $condition1 = $data['completed'] === 'all' //all 414 || $data['completed'] === $data['checked']; //yes or no 415 416 // resolve placeholder in assignees 417 $requestedassignees = array(); 418 if(isset($data['assigned']) && is_array($data['assigned'])) { 419 $requestedassignees = array_map( array($this,"__todolistExpandAssignees"), $data['assigned'] ); 420 } 421 //assigned 422 $condition2 = $data['assigned'] === 'all' //all 423 || (is_bool($data['assigned']) && $data['assigned'] == $data['todouser']); //yes or no 424 425 if (!$condition2 && isset($data['assigned']) && is_array($data['assigned']) && isset($data['todousers']) && is_array($data['todousers'])) 426 foreach($data['todousers'] as $todouser) { 427 if(in_array($todouser, $requestedassignees)) { $condition2 = true; break; } 428 } 429 430 //completed by 431 if($condition2 && isset($data['completeduserlist']) && is_array($data['completeduserlist'])) 432 $condition2 = in_array($data['completeduser'], $data['completeduserlist']); 433 434 //compare start/due dates 435 if($condition1 && $condition2) { 436 $condition3s = true; $condition3d = true; 437 if(isset($data['startbefore']) || isset($data['startafter']) || isset($data['startat'])) { 438 if(isset($data['start'])) { 439 if($data['startignore'] != '!') { 440 if(isset($data['startbefore'])) { $condition3s = $condition3s && new DateTime($data['startbefore']) > $data['start']; } 441 if(isset($data['startafter'])) { $condition3s = $condition3s && new DateTime($data['startafter']) < $data['start']; } 442 if(isset($data['startat'])) { $condition3s = $condition3s && new DateTime($data['startat']) == $data['start']; } 443 } 444 } else { 445 if(!$data['startignore'] == '*') { $condition3s = false; } 446 if($data['startignore'] == '!') { $condition3s = false; } 447 } 448 } 449 450 if(isset($data['duebefore']) || isset($data['dueafter']) || isset($data['dueat'])) { 451 if(isset($data['due'])) { 452 if($data['dueignore'] != '!') { 453 if(isset($data['duebefore'])) { $condition3d = $condition3d && new DateTime($data['duebefore']) > $data['due']; } 454 if(isset($data['dueafter'])) { $condition3d = $condition3d && new DateTime($data['dueafter']) < $data['due']; } 455 if(isset($data['dueat'])) { $condition3d = $condition3d && new DateTime($data['dueat']) == $data['due']; } 456 } 457 } else { 458 if(!$data['dueignore'] == '*') { $condition3d = false; } 459 if($data['dueignore'] == '!') { $condition3d = false; } 460 } 461 } 462 $condition3 = $condition3s && $condition3d; 463 } 464 465 // compare completed date 466 $condition4 = true; 467 if(isset($data['completedbefore'])) { 468 $condition4 = $condition4 && new DateTime($data['completedbefore']) > $data['completeddate']; 469 } 470 if(isset($data['completedafter'])) { 471 $condition4 = $condition4 && new DateTime($data['completedafter']) < $data['completeddate']; 472 } 473 if(isset($data['completedat'])) { 474 $condition4 = $condition4 && new DateTime($data['completedat']) == $data['completeddate']; 475 } 476 477 return $condition1 AND $condition2 AND $condition3 AND $condition4; 478 } 479 480 481 /** 482 * Analyse of relative/absolute Date and return an absolute date 483 * 484 * @param $date string absolute/relative value of the date to analyse 485 * @return array absolute date or actual date if $date is invalid 486 */ 487 private function analyseDate($date) { 488 $result = array($date, ''); 489 if(is_string($date)) { 490 if($date == '!') { 491 $result = array('', '!'); 492 } elseif ($date =='*') { 493 $result = array('', '*'); 494 } else { 495 if(substr($date, -1) == '*') { 496 $date = substr($date, 0, -1); 497 $result = array($date, '*'); 498 } 499 500 if(date('Y-m-d', strtotime($date)) == $date) { 501 $result[0] = $date; 502 } elseif(preg_match('/^[\+\-]\d+$/', $date)) { // check if we have a valid relative value 503 $newdate = date_create(date('Y-m-d')); 504 date_modify($newdate, $date . ' day'); 505 $result[0] = date_format($newdate, 'Y-m-d'); 506 } else { 507 $result[0] = date('Y-m-d'); 508 } 509 } 510 } else { $result[0] = date('Y-m-d'); } 511 512 return $result; 513 } 514 515 516} 517