changed git call from https to git readonly
[atutor.git] / mods / wiki / plugins / aview / linktree.php
1 <?php
2
3 #
4 # Generates a page tree from the currently viewed page up to
5 # the "root_page" and prints it below the EditThisPage-line.
6 # Usually this "root_page" is the same as the FrontPage of
7 # your Wiki (EWIKI_PAGE_INDEX), but this can be overriden with
8 # $ewiki_config["root_page"] or EWIKI_LINKTREE_DEST.
9 #
10 # Modified by AndyFundinger (http://erfurtwiki.sourceforge.net/?id=AndyFundinger)
11 #
12
13 define("EWIKI_LINKTREE_UL", 0);         // else a link::list will be printed
14
15 #-- register
16 $ewiki_plugins["view_append"][] = "ewiki_view_append_linktree";
17
18
19 #-- plugin func
20 function ewiki_view_append_linktree($id, $data, $action) {
21    global $ewiki_config;
22
23    $refs = ewiki_db::GETALL(array("refs"));
24    $refs = ewiki_f_parent_refs($refs);
25
26    #-- $dest
27    if (empty($ewiki_config["root_page"])) {
28       if (defined("EWIKI_LINKTREE_DEST")) {
29          $ewiki_config["root_page"] = EWIKI_LINKTREE_DEST;
30       }
31       else {
32          $ewiki_config["root_page"] = EWIKI_PAGE_INDEX;
33       }
34    }
35    $dest = &$ewiki_config["root_page"];
36
37    $depth = 0;
38    $paths = array($id=>$id);
39    $current = $id;   
40 /*
41  *   $paths["Current"] = "Current";
42  *   $paths["WorldWideWeb\nWikiWikiWeb\nErfurtWiki"] = "ErfurtWiki";
43  */
44
45    #-- retry until at least one $path is found
46    while ( (!in_array($dest, $paths)) && ($depth <= 15) && (count($paths)<=100000)) {
47
48       $depth++;
49
50       #-- expand every last path entry
51       foreach ($paths as $pathkey=>$uu) {
52
53          #-- mk subkey from pathkey
54          if ($p = strrpos($pathkey, "\n")) {
55             $lkey = substr($pathkey, $p+1);
56          }
57          else {
58             $lkey = $pathkey;
59          }
60
61          #-- append tree leafs
62          if ($walk = $refs[$lkey]) {
63             foreach ($walk as $add=>$uu) {
64                $paths[$pathkey."\n".$add] = $add;
65             }
66             unset($refs[$lkey]);
67          }
68       }
69    }
70
71    #-- print results
72    foreach ($paths as $key => $name) {
73
74       $tree = array_reverse(explode("\n", $key));
75       $GLOBALS["ewiki_page_sections"] = array();
76
77       if (($name == $dest) && (count($tree) >= 2))  {
78
79          $GLOBALS["ewiki_page_sections"][] = $tree[1];
80
81          if (EWIKI_LINKTREE_UL) {
82             $o .= ewiki_f_tree($tree, 0);
83          } else {
84             $o .= ewiki_f_tree2($tree, 0);
85          }
86
87       }
88    }
89
90    ($o) && ($o = "<div class=\"link-tree\">$o</div>\n");
91
92
93    return($o);
94 }
95
96
97 #-- outputs the given pages in a treelist
98 function ewiki_f_tree(&$pages, $n=1) {
99
100    if ($id = $pages[0]) {
101
102       $o .= "<ul>";
103       $o .= ($n ? "<li>" : "") .
104             '<a href="'.ewiki_script("",$id).'">'.$id.'</a>' .
105             ($n ? "</li>" : "") . "\n";
106       $o .= ewiki_f_tree(array_slice($pages, 1));
107       $o .= "</ul>\n";
108    }
109
110    return($o);
111 }
112
113
114 #-- outputs a flat link list
115 function ewiki_f_tree2(&$pages, $n=1) {
116
117    foreach ($pages as $id) {
118       $o[] = '<a href="'.ewiki_script("",$id).'">'.$id.'</a>';
119    }
120
121    // "::" instead of "&rarr;" may also look nice
122    return(implode(" &rarr; ", $o) . "<br />");
123 }
124
125
126
127 #-- build parents array of (reverse) string $refs from the database
128 function ewiki_f_parent_refs($refs) {
129
130    $pages = array();
131
132    #-- decode refs
133    while ($row = $refs->get()) {
134       $parent = $row["id"];
135       foreach (explode("\n", $row["refs"]) as $page) {
136
137          if (strlen($page)) {
138             $pages[$page][$parent]=1;
139          }
140
141          //echo("($page,$parent) ");
142       }
143       //echo("\n");
144    }
145
146    return($pages);
147 }
148
149
150 ?>