changed git call from https to git readonly
[atutor.git] / mods / wiki / plugins / page / sitemap.php
1 <?php
2
3 /*
4     This plugin will create a sitemap rooted at the given location
5     Written By: Jeffrey Engleman
6 */
7
8 define("EWIKI_PAGE_SITEMAP", "SiteMap");
9 define("EWIKI_SITEMAP_DEPTH", 1);
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";
15
16 if(!isset($ewiki_config["SiteMap"]["RootList"])){
17   $ewiki_config["SiteMap"]["RootList"]=array(EWIKI_PAGE_INDEX);
18 }
19
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.
24 */
25 function ewiki_page_sitemap($id=0, $data=0, $action=0){
26   global $ewiki_config;
27
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;
31
32   $a_validpages=ewiki_valid_pages(0,1);
33   
34   //**end of hijacked code**
35   //$time_end=getmicrotime();
36
37   //creates the title bar on top of page 
38   if($id == EWIKI_PAGE_SITEMAP){
39     $o = ewiki_make_title($id, $id, 2);  
40
41     foreach($ewiki_config["SiteMap"]["RootList"] as $root){
42       if(isset($a_validpages[$root])){
43         $valid_root=TRUE;
44         $str_rootid=$root;
45         break;
46       }
47     }
48     
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;
54     }    
55   }
56
57   $o .= "<p>".ewiki_t("VIEWSMFOR");
58
59   foreach($ewiki_config["SiteMap"]["RootList"] as $root){
60     if(isset($a_validpages[$root])){
61       $o.='<a href="'.ewiki_script('sitemap/',$root).'">'.$root.'</a> ';
62     }
63   }
64   
65   $o.="</p>";
66
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;
71   }
72   
73   //$timesitemap=getmicrotime();
74   $a_sitemap=ewiki_sitemap_create($str_rootid, $a_validpages, EWIKI_SITEMAP_DEPTH);
75
76   $timer=array();
77   $level=-1;
78   $fordump=0;
79   $str_formatted="<ul>\n<li><a href=\"?page=".$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;
83   
84   //$timesitemap_end=getmicrotime();
85   
86   //$o.="GetAll: ".($time_end-$time)."\n";
87   //$o.="SiteMap: ".($timesitemap_end-$timesitemap)."\n";
88   //$o.="Total: ".($timesitemap_end-$time);
89   
90   
91   return($o);
92     
93 }
94
95 function ewiki_valid_pages($bool_allowimages=0, $virtual_pages=0){
96   //$time=getmicrotime();
97   global $ewiki_plugins;
98   $result = ewiki_db::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;
102     }   
103     if (($row["flags"] & EWIKI_DB_F_TYPE) == EWIKI_DB_F_TEXT || ($bool_allowimages ? $row["meta"]["class"]=="image" : 0)) {
104       $temp_refs=explode("\n",$row["refs"]);
105       foreach($temp_refs as $key => $value) {
106         if(empty($value)) {
107           unset($temp_refs[$key]);
108         }
109       }
110       if($row["meta"]["class"]=="image"){
111         $a_validpages[$row["id"]]=$temp_array=array("refs" => $temp_refs, "type" => "image", "touched" => FALSE);
112       } else {
113         $a_validpages[$row["id"]]=$temp_array=array("refs" => $temp_refs, "type" => "page", "touched" => FALSE);
114       }
115       unset($temp_refs);
116     }
117   }
118
119   if($virtual_pages){
120     #-- include virtual pages to the sitemap.
121     $virtual = array_keys($ewiki_plugins["page"]);
122     foreach($virtual as $vp){
123       if(!EWIKI_PROTECTED_MODE || !EWIKI_PROTECTED_MODE_HIDING || ewiki_auth($vp, $str_null, "view")){
124         $a_validpages[$vp]=array("refs" => array(), "type" => "page", "touched" => FALSE);
125       }
126     }
127   }
128   return $a_validpages;
129 }
130
131 /*
132   Adds each of the pages in the sitemap to an HTML list.  Each site is a clickable link.
133 */
134 function format_sitemap($a_sitemap, $str_rootpage, &$str_formatted, &$prevlevel, &$timer, &$fordump){
135
136   //get all children of the root format them and store in $str_formatted array
137   if($a_sitemap[$str_rootpage]["child"]){
138     while($str_child = current($a_sitemap[$str_rootpage]["child"])){
139       $str_mark="";
140       if($a_sitemap[$str_rootpage]["level"]>$prevlevel){
141         $str_mark="<ul>\n";
142       } 
143       elseif ($a_sitemap[$str_rootpage]["level"]<$prevlevel){
144         //markup length is 6 characters
145         $str_mark=str_pad("", ($prevlevel-$a_sitemap[$str_rootpage]["level"])*6, "</ul>\n");
146       }
147       $prevlevel=$a_sitemap[$str_rootpage]["level"];
148       if($fordump){
149         $str_formatted.=($str_mark."<li><a href=\"".preg_replace(EWIKI_DUMP_FILENAME_REGEX, "", urlencode($str_child)).".html\">".$str_child."</a></li>\n");
150       } else {
151         $str_formatted.=($str_mark."<li><a href=\"?page=".$str_child."\">".$str_child."</a></li>\n");
152       }
153       array_shift($a_sitemap[$str_rootpage]["child"]);
154       format_sitemap($a_sitemap, $str_child, $str_formatted, $prevlevel, $timer, $fordump);
155     }
156     return ($prevlevel+1);
157   }
158 }
159
160
161 /*
162   gets all children of the given root and stores them in the $a_children array
163 */
164 function ewiki_page_listallchildren($str_root, &$a_children, &$a_sitemap, &$a_validpages, $i_level, $i_maxdepth, $i_flatmap){
165   if(($i_level<$i_maxdepth) && is_array($a_validpages[$str_root]["refs"])){ //controls depth the sitemap will recurse into
166     foreach($a_validpages[$str_root]["refs"] as $str_refs){
167       if($str_refs){ //make sure $str_refs contains a value before doing anything
168         if(isset($a_validpages[$str_refs])){ //test page validity
169           if(!$a_validpages[$str_refs]["touched"]){ //check to see if page already exists
170             if($i_flatmap){
171               $a_sitemap[]=$str_refs;
172             }
173             $a_validpages[$str_refs]["touched"]=TRUE; //mark page as displayed
174             $a_children[$str_refs]="";
175             $a_currchildren[]=$str_refs;
176           }
177         }
178       }
179     }
180     if(!$i_flatmap){
181       if($a_currchildren){
182         $a_sitemap[$str_root]=array("level" => $i_level, "child" => $a_currchildren);
183       } else {
184         $a_sitemap[$str_root]=array("level" => $i_level);
185       }
186     } 
187   }
188 }   
189
190
191 /*
192   Creates the sitemap. And sends the data to the format_sitemap function.
193   Returns the HTML formatted sitemap.
194 */
195 function ewiki_sitemap_create($str_rootid, $a_validpages, $i_maxdepth, $i_flatmap=0){
196   //map starts out with a depth of 0
197   $i_depth=0;
198   $forcelevel=FALSE;
199
200   //create entry for root in the sitemap array
201   if(!$i_flatmap){
202     $a_sitemap[$str_rootid]=array("parent" => "", "level" => $i_depth, "child" => $str_rootid);
203   } else {
204     $a_sitemap[]=$str_rootid;
205   }
206   //mark the root page as touched
207   $a_validpages[$str_rootid]["touched"]=TRUE;
208   //list all of the children of the root
209   ewiki_page_listallchildren($str_rootid, $a_children, $a_sitemap, $a_validpages, $i_depth, $i_maxdepth, $i_flatmap);
210   $i_depth++;    
211   
212   if($a_children){
213     end($a_children);
214     $str_nextlevel=key($a_children);
215     reset($a_children);
216     
217     while($str_child = key($a_children)){
218       //list all children of the current child
219       ewiki_page_listallchildren($str_child, $a_children, $a_sitemap, $a_validpages, $i_depth, $i_maxdepth, $i_flatmap);
220       
221       //if the child is the next level marker...
222       if($str_child==$str_nextlevel){
223         //increment the level counter
224         $i_depth++;
225         //determine which child marks the end of this level
226         end($a_children);
227         $str_nextlevel=key($a_children);
228         //reset the array counter to the beginning of the array
229         reset($a_children);
230         //we are done with this child...get rid of it 
231       }
232       array_shift($a_children);
233     }
234   }
235
236   return $a_sitemap;
237 }
238 ?>