1<?php 2/** 3 * ToDo Plugin: Creates a checkbox based todo list 4 * 5 * Syntax: <todo [@username] [#]>Name of Action</todo> - 6 * Creates a Checkbox with the "Name of Action" as 7 * the text associated with it. The hash (#, optional) 8 * will cause the checkbox to be checked by default. 9 * The @ sign followed by a username can be used to assign this todo to a user. 10 * examples: 11 * A todo without user assignment 12 * <todo>Something todo</todo> 13 * A completed todo without user assignment 14 * <todo #>Completed todo</todo> 15 * A todo assigned to user User 16 * <todo @leo>Something todo for Leo</todo> 17 * A completed todo assigned to user User 18 * <todo @leo #>Todo completed for Leo</todo> 19 * 20 * In combination with dokuwiki searchpattern plugin version (at least v20130408), 21 * it is a lightweight solution for a task management system based on dokuwiki. 22 * use this searchpattern expression for open todos: 23 * ~~SEARCHPATTERN#'/<todo[^#>]*>.*?<\/todo[\W]*?>/'?? _ToDo ??~~ 24 * use this searchpattern expression for completed todos: 25 * ~~SEARCHPATTERN#'/<todo[^#>]*#[^>]*>.*?<\/todo[\W]*?>/'?? _ToDo ??~~ 26 * do not forget the no-cache option 27 * ~~NOCACHE~~ 28 * 29 * Compatibility: 30 * Release 2013-03-06 "Weatherwax RC1" 31 * Release 2012-10-13 "Adora Belle" 32 * 33 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 34 * @author Babbage <babbage@digitalbrink.com>; Leo Eibler <dokuwiki@sprossenwanne.at> 35 */ 36 37/** 38 * ChangeLog: 39 * 40 * [05/11/2014] : by Markus Gschwendt <markus@runout.at> 41 * add options for list rendering: username:user|real|none checkbox:yes|no 42 ** [04/13/2013]: by Leo Eibler <dokuwiki@sprossenwanne.at> / http://www.eibler.at 43 ** bugfix: config option Strikethrough 44 * [04/11/2013]: by Leo Eibler <dokuwiki@sprossenwanne.at> / http://www.eibler.at 45 * bugfix: encoding html code (security risk <todo><script>alert('hi')</script></todo>) - bug reported by Andreas 46 * bugfix: use correct <todo> tag if there are more than 1 in the same line. 47 * [04/08/2013]: by Leo Eibler <dokuwiki@sprossenwanne.at> / http://www.eibler.at 48 * migrate changes made by Christian Marg to current version of plugin 49 * [04/08/2013]: by Christian Marg <marg@rz.tu-clausthal.de> 50 * changed behaviour - when multiple todo-items have the same text, only the clicked one is checked. 51 * [04/08/2013]: by Leo Eibler <dokuwiki@sprossenwanne.at> / http://www.eibler.at 52 * add description / comments and syntax howto about integration with searchpattern 53 * check compatibility with dokuwiki release 2012-10-13 "Adora Belle" 54 * remove getInfo() call because it's done by plugin.info.txt (since dokuwiki 2009-12-25 "Lemming") 55 * [04/07/2013]: by Leo Eibler <dokuwiki@sprossenwanne.at> / http://www.eibler.at 56 * add handler method _searchpatternHandler() for dokuwiki searchpattern extension. 57 * add user assignment for todos (with @username syntax in todo tag e.g. <todo @leo>do something</todo>) 58 * [04/05/2013]: by Leo Eibler <dokuwiki@sprossenwanne.at> / http://www.eibler.at 59 * upgrade plugin to work with newest version of dokuwiki (tested version Release 2013-03-06 Weatherwax RC1). 60 * [08/16/2010]: Fixed another bug where javascript would not decode the action 61 * text properly (replaced unescape with decodeURIComponent). 62 * [04/03/2010]: Fixed a bug where javascript would not decode the action text 63 * properly. 64 * [03/31/2010]: Fixed a bug where checking or unchecking an action whose text 65 * appeared outside of the todo tags, would result in mangling the 66 * code on your page. Also added support for using the ampersand 67 * character (&) and html entities inside of your todo action. 68 * [02/27/2010]: Created an action plugin to insert a ToDo button into the 69 * editor toolbar. 70 * [10/14/2009]: Added the feature so that if you have Links turned off and you 71 * click on the text of an action, it will check that action off. 72 * Thanks to Tero for the suggestion! (Plugin Option: CheckboxText) 73 * [10/08/2009]: I am no longer using the short open php tag (<?) for my 74 * ajax.php file. This was causing some problems for people who had 75 * short_open_tags=Off in their php.ini file (thanks Marcus!) 76 * [10/01/2009]: Updated javascript to use .nextSibling instead of .nextElementSibling 77 * to make it compatible with older versions of Firefox and IE. 78 * [09/13/2009]: Replaced ':' with a '-' in the action link so as not to create 79 * unnecessary namespaces (if the links option is active) 80 * [09/10/2009]: Removed unnecessary function calls (urlencode) in _createLink() function 81 * [09/09/2009]: Added ability for user to choose where Action links point to 82 * [08/30/2009]: Initial Release 83 */ 84 85if(!defined('DOKU_INC')) die(); 86 87/** 88 * All DokuWiki plugins to extend the parser/rendering mechanism 89 * need to inherit from this class 90 */ 91class syntax_plugin_todo_todo extends DokuWiki_Syntax_Plugin { 92 93 /** 94 * Get the type of syntax this plugin defines. 95 * 96 * @return String 97 */ 98 public function getType() { 99 return 'substition'; 100 } 101 102 /** 103 * Paragraph Type 104 * 105 * 'normal' - The plugin can be used inside paragraphs 106 * 'block' - Open paragraphs need to be closed before plugin output 107 * 'stack' - Special case. Plugin wraps other paragraphs. 108 */ 109 function getPType(){ 110 return 'normal'; 111 } 112 113 /** 114 * Where to sort in? 115 * 116 * @return Integer 117 */ 118 public function getSort() { 119 return 999; 120 } 121 122 /** 123 * Connect lookup pattern to lexer. 124 * 125 * @param $mode String The desired rendermode. 126 * @return void 127 * @see render() 128 */ 129 public function connectTo($mode) { 130 $this->Lexer->addEntryPattern('<todo[\s]*?.*?>(?=.*?</todo>)', $mode, 'plugin_todo_todo'); 131 } 132 133 public function postConnect() { 134 $this->Lexer->addExitPattern('</todo>', 'plugin_todo_todo'); 135 } 136 137 /** 138 * Handler to prepare matched data for the rendering process. 139 * 140 * @param $match string The text matched by the patterns. 141 * @param $state int The lexer state for the match. 142 * @param $pos int The character position of the matched text. 143 * @param &$handler Doku_Handler Reference to the Doku_Handler object. 144 * @return int The current lexer state for the match. 145 */ 146 public function handle($match, $state, $pos, Doku_Handler &$handler) { 147 switch($state) { 148 case DOKU_LEXER_ENTER : 149 #Search to see if the '#' is in the todotag (if so, this means the Action has been completed) 150 $x = preg_match('%<todo([^>]*)>%i', $match, $tododata); 151 if($x) { 152 list($handler->checked, $handler->todo_user) = $this->parseTodoArgs($tododata[1]); 153 } 154 if(!is_numeric($handler->todo_index)) { 155 $handler->todo_index = 0; 156 } 157 break; 158 case DOKU_LEXER_MATCHED : 159 break; 160 case DOKU_LEXER_UNMATCHED : 161 /** 162 * Structure: 163 * input(checkbox) 164 * <span> 165 * -<a> (if links is on) or <span> (if links is off) 166 * --<del> (if strikethrough is on) or --NOTHING-- 167 * -</a> or </span> 168 * </span> 169 */ 170 171 #Make sure there is actually an action to create 172 if(trim($match) != '') { 173 174 $data = array($state, $match, $handler->todo_index, $handler->todo_user, $handler->checked); 175 176 $handler->todo_index++; 177 return $data; 178 } 179 180 break; 181 case DOKU_LEXER_EXIT : 182 #Delete temporary checked variable 183 unset($handler->todo_user); 184 unset($handler->checked); 185 //unset($handler->todo_index); 186 break; 187 case DOKU_LEXER_SPECIAL : 188 break; 189 } 190 return array(); 191 } 192 193 /** 194 * Handle the actual output creation. 195 * 196 * @param $mode String The output format to generate. 197 * @param &$renderer Doku_Renderer A reference to the renderer object. 198 * @param $data Array The data created by the <tt>handle()</tt> method. 199 * @return Boolean true: if rendered successfully, or false: otherwise. 200 */ 201 public function render($mode, Doku_Renderer &$renderer, $data) { 202 global $ID; 203 list($state, $todotitle, $todoindex, $todouser, $checked) = $data; 204 if($mode == 'xhtml') { 205 /** @var $renderer Doku_Renderer_xhtml */ 206 if($state == DOKU_LEXER_UNMATCHED) { 207 208 #Output our result 209 $renderer->doc .= $this->createTodoItem($renderer, $todotitle, $todoindex, $todouser, $checked, $ID, array('checkbox'=>'yes', 'username'=>'user')); 210 return true; 211 } 212 213 } elseif($mode == 'metadata') { 214 /** @var $renderer Doku_Renderer_metadata */ 215 if($state == DOKU_LEXER_UNMATCHED) { 216 $id = $this->_composePageid($todotitle); 217 $renderer->internallink($id, $todotitle); 218 } 219 } 220 return false; 221 } 222 223 /** 224 * Parse the arguments of todotag 225 * 226 * @param string $todoargs 227 * @return array(bool, false|string) with checked and user 228 */ 229 protected function parseTodoArgs($todoargs) { 230 $checked = $todouser = false; 231 232 if(strpos($todoargs, '#') !== false) { 233 $checked = true; 234 } 235 if(($userStartPos = strpos($todoargs, '@')) !== false) { 236 $userraw = substr($todoargs, $userStartPos); 237 // @date 20140317 le: add support for email addresses 238 $x = preg_match('%@([-.\w@]+)%i', $userraw, $usermatch); 239 if($x) { 240 $todouser = $usermatch[1]; 241 } 242 } 243 return array($checked, $todouser); 244 } 245 246 /** 247 * @param Doku_Renderer_xhtml &$renderer 248 * @param string $todotitle Title of todoitem 249 * @param int $todoindex which number the todoitem has, null is when not at the page 250 * @param string $todouser User assigned to todoitem 251 * @param bool $checked whether item is done 252 * @param string $id of page 253 * @param array $data data for rendering options 254 * @return string html of an item 255 */ 256 protected function createTodoItem(&$renderer, $todotitle, $todoindex, $todouser, $checked, $id, $data) { 257 //set correct context 258 global $ID, $INFO; 259 $oldID = $ID; 260 $ID = $id; 261 262 263 if($data['checkbox']) { 264 $return = '<input type="checkbox" class="todocheckbox"' 265 . ' data-index="' . $todoindex . '"' 266 . ' data-date="' . hsc(@filemtime(wikiFN($ID))) . '"' 267 . ' data-pageid="' . hsc($ID) . '"' 268 . ' data-strikethrough="' . ($this->getConf("Strikethrough") ? '1' : '0') . '"' 269 . ($checked ? 'checked="checked"' : '') . ' /> '; 270 } 271 switch ($data['username']) { 272 case "user": break; 273 case "real": $todouser = $INFO['userinfo']['name']; break; 274 case "none": unset($todouser); break; 275 } 276 if($todouser) { 277 $return .= '<span class="todouser">[' . hsc($todouser) . ']</span>'; 278 } 279 280 $spanclass = 'todotext'; 281 if($this->getConf("CheckboxText") && !$this->getConf("AllowLinks")) { 282 $spanclass .= ' clickabletodo todohlght'; 283 } 284 $return .= '<span class="' . $spanclass . '">'; 285 286 if($checked && $this->getConf("Strikethrough")) { 287 $return .= '<del>'; 288 } 289 290 $return .= '<span class="todoinnertext">'; 291 if($this->getConf("AllowLinks")) { 292 $return .= $this->_createLink($renderer, $todotitle, $todotitle); 293 } else { 294 $return .= hsc($todotitle); 295 } 296 $return .= '</span>'; 297 298 if($checked && $this->getConf("Strikethrough")) { 299 $return .= '</del>'; 300 } 301 302 $return .= '</span>'; 303 304 //restore page ID 305 $ID = $oldID; 306 return $return; 307 } 308 309 /** 310 * Generate links from our Actions if necessary. 311 * 312 * @param Doku_Renderer_xhtml &$renderer 313 * @param string $pagename 314 * @param string $name 315 * @return string 316 */ 317 private function _createLink(&$renderer, $pagename, $name = NULL) { 318 $id = $this->_composePageid($pagename); 319 320 return $renderer->internallink($id, $name, null, true); 321 } 322 323 /** 324 * Compose the pageid of the pages linked by a todoitem 325 * 326 * @param string $pagename 327 * @return string page id 328 */ 329 private function _composePageid($pagename) { 330 #Get the ActionNamespace and make sure it ends with a : (if not, add it) 331 $actionNamespace = $this->getConf("ActionNamespace"); 332 if(strlen($actionNamespace) == 0 || substr($actionNamespace, -1) != ':') { 333 $actionNamespace .= ":"; 334 } 335 336 #Replace ':' in $pagename so we don't create unnecessary namespaces 337 $pagename = str_replace(':', '-', $pagename); 338 339 //resolve and build link 340 $id = $actionNamespace . $pagename; 341 return $id; 342 } 343 344 /** 345 * @brief this function can be called by dokuwiki plugin searchpattern to process the todos found by searchpattern. 346 * use this searchpattern expression for open todos: 347 * ~~SEARCHPATTERN#'/<todo[^#>]*>.*?<\/todo[\W]*?>/'?? _ToDo ??~~ 348 * use this searchpattern expression for completed todos: 349 * ~~SEARCHPATTERN#'/<todo[^#>]*#[^>]*>.*?<\/todo[\W]*?>/'?? _ToDo ??~~ 350 * this handler method uses the table and layout with css classes from searchpattern plugin 351 * 352 * @param $type string type of the request from searchpattern plugin 353 * (wholeoutput, intable:whole, intable:prefix, intable:match, intable:count, intable:suffix) 354 * wholeoutput = all output is done by THIS plugin (no output will be done by search pattern) 355 * intable:whole = the left side of table (page name) is done by searchpattern, the right side 356 * of the table will be done by THIS plugin 357 * intable:prefix = on the right side of table - THIS plugin will output a prefix header and 358 * searchpattern will continue it's default output 359 * intable:match = if regex, right side of table - THIS plugin will format the current 360 * outputvalue ($value) and output it instead of searchpattern 361 * intable:count = if normal, right side of table - THIS plugin will format the current 362 * outputvalue ($value) and output it instead of searchpattern 363 * intable:suffix = on the right side of table - THIS plugin will output a suffix footer and 364 * searchpattern will continue it's default output 365 * @param Doku_Renderer_xhtml &$renderer current rendering object (use $renderer->doc .= 'text' to output text) 366 * @param array $data whole data multidemensional array( array( $page => $countOfMatches ), ... ) 367 * @param array $matches whole regex matches multidemensional array( array( 0 => '1st Match', 1 => '2nd Match', ... ), ... ) 368 * @param string $page id of current page 369 * @param array $params the parameters set by searchpattern (see search pattern documentation) 370 * @param string $value value which should be outputted by searchpattern 371 * @return bool true if THIS method is responsible for the output (using $renderer->doc) OR false if searchpattern should output it's default 372 */ 373 public function _searchpatternHandler($type, &$renderer, $data, $matches, $params = array(), $page = null, $value = null) { 374 $renderer->nocache(); 375 376 $type = strtolower($type); 377 switch($type) { 378 case 'wholeoutput': 379 // $matches should hold an array with all <todo>matches</todo> or <todo #>matches</todo> 380 if(!is_array($matches)) { 381 return false; 382 } 383 //file_put_contents( dirname(__FILE__).'/debug.txt', print_r($matches,true), FILE_APPEND ); 384 //file_put_contents( dirname(__FILE__).'/debug.txt', print_r($params,true), FILE_APPEND ); 385 $renderer->doc .= '<div class="sp_main">'; 386 $renderer->doc .= '<table class="inline sp_main_table">'; //create table 387 388 foreach($matches as $page => $allTodosPerPage) { 389 $renderer->doc .= '<tr class="sp_title"><th class="sp_title" colspan="2"><a href="' . wl($page) . '">' . $page . '</a></td></tr>'; 390 //entry 0 contains all whole matches 391 foreach($allTodosPerPage[0] as $todoindex => $todomatch) { 392 $x = preg_match('%<todo([^>]*)>(.*)</[\W]*todo[\W]*>%i', $todomatch, $tododata); 393 394 if($x) { 395 list($checked, $todouser) = $this->parseTodoArgs($tododata[1]); 396 $todotitle = trim($tododata[2]); 397 if(empty($todotitle)) { 398 continue; 399 } 400 $renderer->doc .= '<tr class="sp_result"><td class="sp_page" colspan="2">'; 401 402 // in case of integration with searchpattern there is no chance to find the index of an element 403 $renderer->doc .= $this->createTodoItem($renderer, $todotitle, $todoindex, $todouser, $checked, $page, array('checkbox'=>'yes', 'username'=>'user')); 404 405 $renderer->doc .= '</td></tr>'; 406 } 407 } 408 } 409 $renderer->doc .= '</table>'; //end table 410 $renderer->doc .= '</div>'; 411 // true means, that this handler method does the output (searchpattern plugin has nothing to do) 412 return true; 413 break; 414 case 'intable:whole': 415 break; 416 case 'intable:prefix': 417 //$renderer->doc .= '<b>Start on Page '.$page.'</b>'; 418 break; 419 case 'intable:match': 420 //$renderer->doc .= 'regex match on page '.$page.': <pre>'.$value.'</pre>'; 421 break; 422 case 'intable:count': 423 //$renderer->doc .= 'normal count on page '.$page.': <pre>'.$value.'</pre>'; 424 break; 425 case 'intable:suffix': 426 //$renderer->doc .= '<b>End on Page '.$page.'</b>'; 427 break; 428 default: 429 break; 430 } 431 // false means, that this handler method does not output anything. all should be done by searchpattern plugin 432 return false; 433 } 434} 435 436//Setup VIM: ex: et ts=4 enc=utf-8 : 437