SiteReader.class.php (6750B)
1 <?php 2 3 class SiteReader 4 { 5 var $pages = array(); 6 var $pagesByDate = array(); 7 var $pagesByKeyword = array(); 8 var $pagesByUrl = array(); 9 var $pagesByCategory = array(); 10 var $keywordNames = array(); 11 var $categoryNames = array(); 12 13 /** 14 * Reads all pages of a site. 15 */ 16 function readSite() 17 { 18 $this->pages = $this->read_all_pages(''); 19 $this->filter_future_pages(); 20 $this->filter_unique_page_url(); 21 22 foreach( $this->pages as $page ) 23 { 24 $url = $page['url']; 25 $this->pagesByUrl[$url] = $page; 26 27 $date = $page['date']; 28 if ( !isset($this->pagesByDate[$date])) 29 $this->pagesByDate[$date] = array(); 30 $this->pagesByDate[$date][] = $page; 31 32 $keywords = $page['keywords']; 33 foreach( $keywords as $keyword ) { 34 if ( !isset($this->pagesByKeyword[$keyword])) 35 $this->pagesByKeyword[$keyword] = array(); 36 $this->pagesByKeyword[$keyword][] = $page; 37 } 38 39 if ( isset( $page['category'] ) ) 40 { 41 $category = $this->urlify($page['category']); 42 if ( !isset($this->pagesByCategory[$category])) 43 $this->pagesByCategory[$category] = array(); 44 $this->pagesByCategory[$category][] = $page; 45 $this->categoryNames[$category] = $page['config']['category']; 46 } 47 } 48 49 50 ksort($this->pagesByDate ); 51 ksort($this->pagesByKeyword ); 52 ksort($this->pagesByUrl ); 53 ksort($this->pagesByCategory); 54 ksort($this->keywordNames ); 55 ksort($this->categoryNames ); 56 } 57 58 59 /** 60 * Reads a content file. 61 * 62 * @param Filename $filename 63 * @return Array of pages 64 */ 65 function read_page($filename) 66 { 67 $file = file(SITE_DIR.SLASH.$filename); 68 $page = array(); 69 $fileParts = $this->explode_array('---',$file); 70 if (isset($fileParts[1])) 71 { 72 $pageYaml = spyc_load( implode("\n",$fileParts[1])); 73 $page['config'] = array_change_key_case($pageYaml); // force lowercase on page attributes 74 } 75 76 if (isset($fileParts[2])) 77 { 78 $page['value'] = implode("\n",$fileParts[2]); 79 } 80 $page['filename' ] = $filename; 81 $page['filemtime'] = filemtime(SITE_DIR.SLASH.$filename); 82 83 84 if (isset($page['config']['date'])) 85 $page['date'] = strtotime($page['config']['date']); 86 else 87 $page['date'] = $page['filemtime']; 88 89 // Category 90 if ( isset( $page['config']['category'] )) 91 $page['category'] = $this->urlify($page['config']['category']); 92 93 94 // keywords is similar to tags 95 if ( isset( $page['config']['keywords'] )) 96 $page['config']['tags'] = $page['config']['keywords']; 97 98 if ( isset( $page['config']['tags'] )) 99 if ( is_array($page['config']['tags']) ) 100 $page['keywords'] = $this->urlify_array($page['config']['tags']); 101 else 102 $page['keywords'] = $this->urlify_array(explode(',',$page['config']['tags'])); 103 else 104 $page['keywords'] = array(); 105 106 107 108 if (!empty($page['config']['url'])) 109 if ( substr($page['config']['url'],0,1)=='/') 110 $page['url'] = $this->urlify(substr($page['config']['url'],1)); 111 else 112 $page['url'] = dirname($page['filename']).SLASH.$this->urlify($page['config']['url']); 113 elseif (!empty($page['config']['title'])) 114 { 115 if (!empty($page['config']['category'])) 116 $page['url'] = $this->urlify($page['config']['category']).SLASH.$this->urlify($page['config']['title']); 117 else 118 $page['url'] = $this->urlify($page['config']['title']); 119 } 120 else 121 // no title available, so we need to use the filename instead. 122 if (!empty($page['config']['category'])) 123 $page['url'] = $this->urlify($page['config']['category']).SLASH.$this->urlify(substr(basename($page['filename']),0,-3)); 124 else 125 $page['url'] = $this->urlify(substr(basename($page['filename']),0,-3)); 126 127 if (isset($page['config']['title'])) 128 $page['title'] = $page['config']['title']; 129 else 130 $page['title'] = basename($page['filename']); 131 132 if (isset($page['config']['theme'])) 133 $page['theme'] = $page['config']['theme']; 134 else 135 $page['theme'] = THEME; // not supported - because bee.php doesnt know which theme should be searched for static resources. 136 137 if (isset($page['config']['author'])) 138 // Username from page config. 139 $page['author'] = $page['config']['author']; 140 else 141 { 142 // Fallback: Detect username from os. 143 $uid = posix_getpwuid( fileowner(SITE_DIR.SLASH.$filename) ); 144 $page['author'] = $uid['name']; 145 } 146 147 return $page; 148 } 149 150 151 /** 152 * removes pages which has a future date. 153 * @param unknown_type $pages 154 * @return multitype:unknown 155 */ 156 function filter_future_pages() 157 { 158 $validatedPages = array(); 159 160 foreach( $this->pages as $page ) 161 { 162 if ( $page['date'] <= time() ) 163 $validatedPages[] = $page; 164 else 165 ; // pages in the future are not being published yet 166 } 167 168 $this->pages = $validatedPages; 169 } 170 171 172 /** 173 * forces that every page url is unique. 174 * @param unknown_type $pages 175 * @return multitype:Ambigous <string, unknown> 176 */ 177 function filter_unique_page_url() 178 { 179 $urls = array(); 180 $validatedPages = array(); 181 182 $i=1; 183 184 foreach( $this->pages as $page ) 185 { 186 $url = $page['url']; 187 188 // Prüfung auf doppelte Dateinamen. Jeder Dateiname muss eindeutig sein. Ggf. wird ein Suffix ergänzt. 189 while( in_array($url, $urls) ) 190 { 191 $url = $page['url'].'-'.++$i; 192 $page['url'] = $url; 193 } 194 $urls[] = $url; 195 $validatedPages[] = $page; 196 } 197 $this->pages = $validatedPages; 198 } 199 200 201 202 function read_all_pages( $dir ) 203 { 204 $pages = array(); 205 if ( $handle = opendir(SITE_DIR.SLASH.$dir) ) 206 { 207 while (false !== ($entry = readdir($handle))) 208 { 209 if ( $entry[0] == '.' ) 210 continue; 211 if ( is_dir( SITE_DIR.SLASH.$dir.(!empty($dir)?SLASH:'').$entry )) 212 { 213 $pages = array_merge($pages,$this->read_all_pages($dir.(!empty($dir)?SLASH:'').$entry)); 214 } 215 if ( is_file( SITE_DIR.SLASH.$dir.(!empty($dir)?SLASH:'').$entry ) && substr($entry,-3)=='.md') 216 { 217 $page = $this->read_page($dir.(!empty($dir)?SLASH:'').$entry); 218 219 $pages[] = $page; 220 } 221 } 222 closedir($handle); 223 } 224 225 return $pages; 226 } 227 228 229 /** 230 * Creates a slug url out of the filename. 231 * @param $filename Name 232 * @return string 233 */ 234 function urlify( $filename ) 235 { 236 $slug = $filename; 237 $slug = iconv('utf-8', 'ascii//TRANSLIT', $slug); 238 $slug = preg_replace('/[^A-Za-z0-9-]+/', '-', $slug); 239 $slug = trim($slug, '-'); 240 $slug = strtolower($slug); 241 242 return $slug; 243 } 244 245 246 function urlify_array( $nameArray ) 247 { 248 $newNames = array(); 249 foreach( $nameArray as $name) 250 { 251 $newNames[] = $this->urlify($name); 252 } 253 return $newNames; 254 255 } 256 257 258 function explode_array($sep, $array) 259 { 260 $idx = 0; 261 $target = array(); 262 $target[$idx] = array(); 263 264 foreach( $array as $line ) 265 { 266 if ( trim($line) == $sep ) 267 { 268 $target[++$idx] = array(); 269 continue; 270 } 271 $target[$idx][] = $line; 272 } 273 274 return $target; 275 } 276 } 277 278 ?>