adding some strings
[moodle-linuxchix.git] / mod / wiki / ewiki / plugins / moodle / sitemap.php
blob55abd3ba05b1c8f3cac7eb6932b6ac1686449a6e
1 <?php // $Id$
3 /*
4 This plugin will create a sitemap rooted at the given location
5 Written By: Jeffrey Engleman
6 */
8 define("EWIKI_PAGE_SITEMAP", "SiteMap");
9 define("EWIKI_SITEMAP_DEPTH", 10);
10 $ewiki_t["en"]["INVALIDROOT"] = "You are not authorized to access the current root page so no sitemap can be created.";
11 $ewiki_t["en"]["SITEMAPFOR"] = "Site map for ";
12 $ewiki_t["en"]["VIEWSMFOR"] = "View site map for ";
13 $ewiki_plugins["page"][EWIKI_PAGE_SITEMAP]="ewiki_page_sitemap";
14 $ewiki_plugins["action"]['sitemap']="ewiki_page_sitemap";
16 if(!isset($ewiki_config["SiteMap"]["RootList"])){
17 $ewiki_config["SiteMap"]["RootList"]=array(EWIKI_PAGE_INDEX);
20 /*
21 populates an array with all sites the current user is allowed to access
22 calls the sitemap creation function.
23 returns the sitemap to be displayed.
25 function ewiki_page_sitemap($id=0, $data=0, $action=0){
26 global $ewiki_config;
28 //**code hijacked from page_pageindex.php**
29 //creates a list of all of the valid wiki pages in the site
30 $str_null=NULL;
32 $a_validpages=ewiki_valid_pages(0,1);
34 //**end of hijacked code**
35 //$time_end=getmicrotime();
37 //creates the title bar on top of page
38 if($id == EWIKI_PAGE_SITEMAP){
39 $o = ewiki_make_title($id, $id, 2);
41 foreach($ewiki_config["SiteMap"]["RootList"] as $root){
42 if(isset($a_validpages[$root])){
43 $valid_root=TRUE;
44 $str_rootid=$root;
45 break;
49 }else{
50 $o = ewiki_make_title($id, ewiki_t("SITEMAPFOR")." ".$id, 2);
51 if(isset($a_validpages[$id])){
52 $valid_root=TRUE;
53 $str_rootid=$id;
57 $o .= "<p>".ewiki_t("VIEWSMFOR")." ";
59 foreach($ewiki_config["SiteMap"]["RootList"] as $root){
60 if(isset($a_validpages[$root])){
61 $o.='<a href="'.ewiki_script('sitemap/',$root).'">'.$root.'</a> ';
65 $o.="</p>";
67 //checks to see if the user is allowed to view the root page
68 if(!isset($a_validpages[$str_rootid])){
69 $o .= ewiki_t("INVALIDROOT");
70 return $o;
73 //$timesitemap=getmicrotime();
74 $a_sitemap=ewiki_sitemap_create($str_rootid, $a_validpages, EWIKI_SITEMAP_DEPTH);
76 $timer=array();
77 $level=-1;
78 $fordump=0;
79 $str_formatted="<ul>\n<li><a href=\"".EWIKI_SCRIPT.$str_rootid."\">".$str_rootid."</a></li>";
80 $fin_level=format_sitemap($a_sitemap, $str_rootid, $str_formatted, $level, $timer, $fordump);
81 $str_formatted.="</ul>".str_pad("", $fin_level*6, "</ul>\n");
82 $o.=$str_formatted;
84 //$timesitemap_end=getmicrotime();
86 //$o.="GetAll: ".($time_end-$time)."\n";
87 //$o.="SiteMap: ".($timesitemap_end-$timesitemap)."\n";
88 //$o.="Total: ".($timesitemap_end-$time);
91 return($o);
95 function ewiki_valid_pages($bool_allowimages=0, $virtual_pages=0){
96 //$time=getmicrotime();
97 global $ewiki_plugins;
98 $result = ewiki_database("GETALL", array("flags", "refs", "meta"));
99 while ($row = $result->get()) {
100 if (EWIKI_PROTECTED_MODE && EWIKI_PROTECTED_MODE_HIDING && !ewiki_auth($row["id"], $str_null, "view")) {
101 continue;
104 $isbinary= ($row["meta"]["class"]=="image"||$row["meta"]["class"]=="file")?true:false;
106 if (($row["flags"] & EWIKI_DB_F_TYPE) == EWIKI_DB_F_TEXT || ($bool_allowimages ? $isbinary : 0)) {
107 $temp_refs=explode("\n",$row["refs"]);
108 foreach($temp_refs as $key => $value) {
109 if(empty($value)) {
110 unset($temp_refs[$key]);
113 if($isbinary){
114 $a_validpages[$row["id"]]=$temp_array=array("refs" => $temp_refs, "type" => $row["meta"]["class"], "touched" => FALSE);
115 } else {
116 $a_validpages[$row["id"]]=$temp_array=array("refs" => $temp_refs, "type" => "page", "touched" => FALSE);
118 unset($temp_refs);
122 if($virtual_pages){
123 #-- include virtual pages to the sitemap.
124 $virtual = array_keys($ewiki_plugins["page"]);
125 foreach($virtual as $vp){
126 if(!EWIKI_PROTECTED_MODE || !EWIKI_PROTECTED_MODE_HIDING || ewiki_auth($vp, $str_null, "view")){
127 $a_validpages[$vp]=array("refs" => array(), "type" => "page", "touched" => FALSE);
131 return $a_validpages;
135 Adds each of the pages in the sitemap to an HTML list. Each site is a clickable link.
137 function format_sitemap($a_sitemap, $str_rootpage, &$str_formatted, &$prevlevel, &$timer, &$fordump){
139 //get all children of the root format them and store in $str_formatted array
140 $a_sitemap[$str_rootpage]["child"]= is_array($a_sitemap[$str_rootpage]["child"])?$a_sitemap[$str_rootpage]["child"]:array();
141 if($a_sitemap[$str_rootpage]["child"]){
142 while($str_child = current($a_sitemap[$str_rootpage]["child"])){
143 $str_mark="";
144 if($a_sitemap[$str_rootpage]["level"]>$prevlevel){
145 $str_mark="<ul>\n";
147 elseif ($a_sitemap[$str_rootpage]["level"]<$prevlevel){
148 //markup length is 6 characters
149 $str_mark=str_pad("", ($prevlevel-$a_sitemap[$str_rootpage]["level"])*6, "</ul>\n");
151 $prevlevel=$a_sitemap[$str_rootpage]["level"];
152 if($fordump){
153 $str_formatted.=($str_mark."<li><a href=\"".preg_replace(EWIKI_DUMP_FILENAME_REGEX, "", urlencode($str_child)).".html\">".$str_child."</a></li>\n");
154 } else {
155 $str_formatted.=($str_mark."<li><a href=\"".EWIKI_SCRIPT.$str_child."\">".$str_child."</a></li>\n");
157 array_shift($a_sitemap[$str_rootpage]["child"]);
158 format_sitemap($a_sitemap, $str_child, $str_formatted, $prevlevel, $timer, $fordump);
160 return ($prevlevel+1);
166 gets all children of the given root and stores them in the $a_children array
168 function ewiki_page_listallchildren($str_root, &$a_children, &$a_sitemap, &$a_validpages, $i_level, $i_maxdepth, $i_flatmap){
169 if(($i_level<$i_maxdepth) && is_array($a_validpages[$str_root]["refs"])){ //controls depth the sitemap will recurse into
170 foreach($a_validpages[$str_root]["refs"] as $str_refs){
171 if($str_refs){ //make sure $str_refs contains a value before doing anything
172 if(isset($a_validpages[$str_refs])){ //test page validity
173 if(!$a_validpages[$str_refs]["touched"]){ //check to see if page already exists
174 if($i_flatmap){
175 $a_sitemap[]=$str_refs;
177 $a_validpages[$str_refs]["touched"]=TRUE; //mark page as displayed
178 $a_children[$str_refs]="";
179 $a_currchildren[]=$str_refs;
184 if(!$i_flatmap){
185 if($a_currchildren){
186 $a_sitemap[$str_root]=array("level" => $i_level, "child" => $a_currchildren);
187 } else {
188 $a_sitemap[$str_root]=array("level" => $i_level);
196 Creates the sitemap. And sends the data to the format_sitemap function.
197 Returns the HTML formatted sitemap.
199 function ewiki_sitemap_create($str_rootid, $a_validpages, $i_maxdepth, $i_flatmap=0){
200 //map starts out with a depth of 0
201 $i_depth=0;
202 $forcelevel=FALSE;
204 //create entry for root in the sitemap array
205 if(!$i_flatmap){
206 $a_sitemap[$str_rootid]=array("parent" => "", "level" => $i_depth, "child" => $str_rootid);
207 } else {
208 $a_sitemap[]=$str_rootid;
210 //mark the root page as touched
211 $a_validpages[$str_rootid]["touched"]=TRUE;
212 //list all of the children of the root
213 ewiki_page_listallchildren($str_rootid, $a_children, $a_sitemap, $a_validpages, $i_depth, $i_maxdepth, $i_flatmap);
214 $i_depth++;
216 if($a_children){
217 end($a_children);
218 $str_nextlevel=key($a_children);
219 reset($a_children);
221 while($str_child = key($a_children)){
222 //list all children of the current child
223 ewiki_page_listallchildren($str_child, $a_children, $a_sitemap, $a_validpages, $i_depth, $i_maxdepth, $i_flatmap);
225 //if the child is the next level marker...
226 if($str_child==$str_nextlevel){
227 //increment the level counter
228 $i_depth++;
229 //determine which child marks the end of this level
230 end($a_children);
231 $str_nextlevel=key($a_children);
232 //reset the array counter to the beginning of the array
233 reset($a_children);
234 //we are done with this child...get rid of it
236 array_shift($a_children);
240 return $a_sitemap;