1<?php
2/**
3 * Plugin metadisplay"
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Myron Turner <turnermm02@shaw.ca
7 */
8if(!defined('DOKU_INC')) die();
9global $timezone, $current,$conf;
10
11class helper_plugin_metadisplay_plaintext extends DokuWiki_Plugin {
12private $subdir = "";
13private $page;
14private $match = false;
15private $exact_page_match = false;
16private $timestamp;
17private $t_when;
18private $dtype;
19private $search;
20private $fuzzy;
21private $ltype = "";
22
23//function init($subdir="", $page="", $exact="off", $search="", $fuzzy="", $tm="", $dtype="") {
24function init($options) {
25   global $conf;
26 // $subdir=""; $page=""; $exact="off"; $search=""; $fuzzy=""; $tm=""; $dtype="";
27  $subdir=$options['namespace'];
28  $page=$options['page'];
29  $exact=$options['exact'];
30  $search=$options['search'];
31  $fuzzy=$options['fuzzy'];
32  $tm=$options['tm'];
33  $dtype=$options['dtype'];
34  $ltype=$options['ltype'];
35
36  if($conf['savedir'] == './data') {
37      chdir(DOKU_INC . trim($conf['savedir'],'.\/') . '/meta');
38      define ('PAGES', DOKU_INC . trim( $conf['savedir'],"\/\\\.") . '/pages');
39  }
40   else {
41      chdir( '/'.trim( $conf['savedir'],"\/\\") . '/meta');
42      define ('PAGES',  '/'.trim( $conf['savedir'],"\/\\") . '/pages') ;
43   }
44    if($subdir == '.') $subdir = "";
45    $this->page=str_replace(':', "",$page);
46    if($subdir) {
47         $subdir = trim($subdir,':\\\/');
48         $this->subdir ="/$subdir";
49         chdir($subdir);
50    }
51    if($exact == 'on') $this->exact_page_match = true;
52	if($tm) {
53	 list($this->timestamp,$this->t_when) = explode(':',$tm);
54	 $this->dtype = $dtype;
55	}
56    if($search) {
57       $this->search = $search;
58	}
59    else if($fuzzy) {
60       $this->fuzzy = $this->get_regex($fuzzy);
61    }
62    if($search || $fuzzy) {
63       if($ltype) $this->ltype = $ltype;
64    }
65
66    ob_start();
67    $this->recurse('.');
68    if(!$this->match){
69        if($page) $page = ":$page";
70        if($subdir) $subdir = "for $subdir";
71        echo "No match  $subdir$page" ."\n";
72    }
73    $contents = ob_get_contents();
74    ob_end_clean();
75     echo $contents;
76 }
77
78function recurse($dir) {
79
80    $dh = opendir($dir);
81    if (!$dh) return;
82
83    while (($file = readdir($dh)) !== false) {
84        if ($file == '.' || $file == '..') continue;
85        if (is_dir("$dir/$file")) $this->recurse("$dir/$file");
86        if (preg_match("/\.meta$/", $file)) {
87             if($this->page && !preg_match("/" . $this->page ."/",$file)) continue;
88             if($this->exact_page_match) {
89                if(!preg_match("/^" . $this->page ."\.meta$/",$file)) continue;
90             }
91
92             $store_name = preg_replace('/^\./', $this->subdir, "$dir/$file");
93             $id_name = PAGES . preg_replace("/\.meta$/","",$store_name) . '.txt';
94             if(!file_exists($id_name)) continue;
95             $success = $this->get_data("$dir/$file","$id_name",$store_name);
96             if($success) {
97                 $this->match = true;
98                 echo "\n";
99        }
100        }
101    }
102
103    closedir($dh);
104}
105
106/*
107  @param string $file, the meta file
108  @param string $id_path, path to dokuwiki page
109*/
110function get_data($file,$id_path,$store_name="") {
111    global $current;
112    $description = "";
113    $data = file_get_contents($file);
114    $data_array = @unserialize(file_get_contents($file));
115    $creator =""; $creator_id="";
116    $contributors = array();
117    if ($data_array === false || !is_array($data_array)) return;
118    if (!isset($data_array['current'])) return false;
119
120    $current = $data_array['current'];
121	if($this->t_when) {
122		$tmstmp = $this->getcurrent('date', $this->dtype);
123		if($this->t_when == 'b' && $tmstmp > $this->timestamp) {
124			return false;
125		}
126		else if($this->t_when == 'a' && $tmstmp < $this->timestamp) {
127			return false;
128		}
129	}
130
131    $search = "";
132    $regex = "";
133    if($this->fuzzy) {
134        $search = $this->fuzzy;
135        $regex = '/(' . $search . ')/im';
136    }
137    else if($this->search) {
138        $search = $this->search;
139        $regex = '/(' . $search . ')/m';
140    }
141    if($regex) {
142        if($this->ltype == 'descr') {
143        $description = $this->getcurrent('description','abstract');
144        if(!preg_match($regex,$description)){
145            return false;
146		}
147            $description = preg_replace($regex,"<span style='color:blue'>$1</span>",$description);
148        }
149        else if($this->ltype == 'media') {
150            $media = $this->check_listtypes('media',$regex);
151            if(!$media) return false;
152        }
153        else if($this->ltype == 'links') {
154            $references = $this->check_listtypes('references',$regex);
155            if(!$references) return false;
156        }
157        else if($this->ltype == 'contrib') {
158            $contribs = $this->getcurrent('contributor', null);
159            if(!$contribs) return false;
160            if(!array_key_exists($search,$contribs)) return;
161            $val = $contribs[$search];
162            unset($contribs[$search]);
163            $search = "<span style='color:blue'>$search</span>";
164            $contribs[$search] = $val;
165            $contributors = $contribs;
166
167        }
168        else if($this->ltype == 'creator') {
169             $creator = $this->getcurrent('creator', null);
170             $creator_id = $this->getcurrent('user', null);
171             if(!$creator && !$creator_id) return false;
172             if(!preg_match($regex,$creator) && !preg_match($regex,$creator_id)) return;
173             $creator=preg_replace($regex,"<span style='color:blue'>$1</span>",$creator);
174             $creator_id=preg_replace($regex,"<span style='color:blue'>$1</span>",$creator_id);
175         }
176
177
178
179	}
180
181    $this->match = true;
182    echo "\n----------------\n$store_name";
183    echo "\n$id_path\n";
184
185    $keys =  array('title','date','creator','last_change','relation', 'description','contributor');
186    foreach ($keys AS $header) {
187        switch($header) {
188            case 'title':
189                 $title = $this->getcurrent($header, null);
190                 echo "\n[Title: $title]";
191                 break;
192            case 'date':
193                 $this->process_dates($this->getcurrent('date', 'created'),$this->getcurrent('date', 'modified'));
194                 break;
195            case 'user':
196                if($creator || $creator_id) break;
197            case 'creator':
198            /*
199                creator: string, full name of the user who created the page
200                user: string, the login name of the user who created the page
201            */
202                if(!$creator) {
203                $creator = $this->getcurrent('creator', null);
204                }
205                if(!$creator_id) {
206                $creator_id = $this->getcurrent('user', null);
207                }
208                $this->process_users($creator,$creator_id);
209                 break;
210
211            case 'last_change':
212                $last_change = $this->getSimpleKeyValue($this->getcurrent($header, null),"last_change");
213                 if($last_change) {
214                    echo "[Last Change] \n$last_change\n";
215                }
216                break;
217            case 'contributor':
218                 if(empty($contributors)) {
219                   $contributors = $this->getcurrent($header, null);
220                 }
221                 if(!$contributors) break;
222                  echo "<tr><th colspan='2'>Contributors</th>\n";
223                  echo '<tr><td>';
224                  foreach($contributors as $userid=>$name) {
225                      $this->process_users($name,$userid, '');
226                  }
227                  echo '</td></tr>';
228                 break;
229            case 'relation':
230                $isreferencedby = $this->getcurrent($header,'isreferencedby');
231                if(!$references) {
232                $references = $this->getcurrent($header,'references');
233                }
234                if(!$media) {
235                $media = $this->getcurrent($header,'media');
236                }
237                $firstimage = $this->getcurrent($header,'firstimage');
238                $haspart = $this->getcurrent($header,'haspart');
239                $subject = $this->getcurrent($header,'subject');
240                $this->process_relation($isreferencedby,$references,$media,$firstimage,$haspart,$subject);
241                break;
242             case 'description':
243                echo "[Description]\n";
244                if(!$description) {
245                    $description = htmlentities($this->getcurrent($header,'abstract'));
246                }
247                $description = preg_replace("/[\n]+/","\n", $description);
248                echo "$description\n";
249                break;
250            default:
251                 break;
252            }
253
254        }
255       echo "\n\n";
256       $current = array();
257}
258
259/*
260*  @param array $ar metadata field
261*  @param string $which which field
262*/
263function getSimpleKeyValue($ar,$which="") {
264    $retv = "";
265
266    $types = array('C'=>'>Create','E'=>'>Edit','e' =>'minor >edit','D'=>'>Delete',
267    'R'=>'>Revert');
268    if(!is_array($ar)) return false;
269    foreach ($ar As $key=>$val) {
270        if(!empty($val)) {
271           if($which == 'last_change')  {
272               if($key == 'date') {
273                   $val = date("r", $val);
274                }
275                if($key == 'type')  {
276                    $val = $types[$val];
277                }
278           }
279
280           $retv .= "$key: $val\n";
281       }
282    }
283    return $retv;
284}
285
286function process_users($creator,$user, $label = 'Created by') {
287        if(empty($creator)) {
288            echo "\n"; return;
289         }
290        if ($label) {
291            $label .= ':';
292            echo "\n$label $creator (userid: $user)\n";
293        }
294        else echo " $creator (userid: $user)\n";
295}
296
297function process_dates($created, $modified) {
298    $retv = "";
299
300    if ($created) {
301        $rfc_cr = date("r", $created);
302        echo "\nDate created:".$rfc_cr.
303        "   $created\n";
304        }
305
306    if ($modified) {
307        $rfc_mod = date("r", $modified);
308        echo "Last modified: " . $rfc_mod .
309        "  $modified\n";
310     }
311
312}
313
314function insertListInTable($list,$type) {
315    if($list) echo "$type $list\n";
316}
317function process_relation($isreferencedby,$references,$media,$firstimage,$haspart,$subject) {
318
319    if(!empty($isreferencedby)) {
320        $list = $this->create_list(array_keys($isreferencedby));
321        $this->insertListInTable($list,'Backlinks');
322    }
323    if(!empty($references)) {
324       $list = $this->create_list(array_keys($references));
325       $this->insertListInTable($list,'[Links]');
326    }
327    if(!empty($media)) {
328       $list = $this->create_list(array_keys($media));
329       $this->insertListInTable($list,'[Media]');
330    }
331    if(!empty($firstimage)) {
332       echo "First Image] \n$firstimage\n";
333    }
334    if(!empty($haspart)) {
335       $list = $this->create_list(array_keys($haspart));
336      $this->insertListInTable($list,'haspart');
337    }
338    if(!empty($subject)) {
339       $list = create_list(array_keys($subject));
340       $this->insertListInTable($list,'Subject');
341    }
342
343}
344
345function create_list($ar) {
346    $list = "\n";
347    for($i=0; $i<count($ar); $i++) {
348        $list .= "$i) ". $ar[$i] . "\n";
349    }
350     $list .= "\n";
351     return $list;
352}
353function getcurrent($which, $other) {
354    global $current;
355    if (!isset($current)) return "";
356    if ($other) {
357        if (isset($current[$which][$other])) {
358            return $current[$which][$other];
359        }
360    }
361    if (isset($current[$which]) && $other === null) {
362        return $current[$which];
363    }
364    return "";
365}
366
367function get_regex($str) {
368    $str = preg_replace('{([aeiou])\1+}','$1',$str);
369    $a = str_split($str);
370
371    for($i = 0; $i < count($a); $i++) {
372        if(preg_match("/[aeiou]/",$a[$i])) {
373            $a[$i] = '[aeiou]+';
374        }
375    }
376    return implode("",$a);
377}
378
379function check_listtypes($which,$regex) {
380    if($which == 'references' || $which == 'media') {
381        $ar = $this->getcurrent('relation',$which);
382       // cli_plugin_metadisplay::write_debug($ar ."\n" . print_r($ar,1));
383       if(is_array($ar)) {
384        $references = array_keys($ar); // references here refers to either images or links
385       }
386       else $references = $ar;
387        if(!empty($references)) {
388            $str = implode('|',$references);
389            if(preg_match($regex,$str)) {
390                $str = preg_replace($regex,"<span style='color:blue'>$1</span>",$str);
391                if($str) {
392                    $arr = explode('|', $str);
393                    $vals = array_values($ar);
394                    $val_str = implode('|',$vals);
395                    $val_str = preg_replace($regex,"<span style='color:blue'>$1</span>",$val_str);
396                    $vals = explode('|',$val_str);
397                    return array_combine($arr,$vals);
398                }
399            }
400                return false;
401         }
402        return false;
403    }
404   return $ar;
405}
406
407
408}