nieuws: ArchiveArticle class to access news pages
[minimedit.git] / nieuws.inc.php
1 <?php
2 global $monthname;
3 $monthname = ['?',
4         'januari', 'februari', 'maart', 'april', 'mei', 'juni',
5         'juli', 'augustus', 'september', 'oktober', 'november', 'december',
6 ];
7
8 function showdate($parts)
9 {
10         global $monthname;
11         return implode(' ', array_filter([
12                 intval($parts[3]), $parts[2] > 0 ? $monthname[intval($parts[2])] : '', $parts[1],
13                 count($parts) > 6 ? "$parts[4]:$parts[5]" : '',
14         ]));
15 }
16
17 class ArchiveArticle
18 {
19         function __construct($path)
20         {
21                 $this->page = $path;
22                 $this->link = preg_replace('/\.html$/', '', $path);
23         }
24
25         function __get($col)
26         {
27                 return $this->$col = $this->$col();  # run method and cache
28         }
29
30         function file()
31         {
32                 return fopen($this->page, 'r');
33         }
34
35         function title()
36         {
37                 return preg_replace('{<h2>(.*)</h2>\s*}', '\1', fgets($this->file));
38         }
39
40         function safetitle()
41         {
42                 return strip_tags($this->title);
43         }
44
45         function date()
46         {
47                 if (!preg_match('</(\d{4})/(\d{2})-(\d{2})->', $this->page, $parts)) return;
48                 return showdate($parts);
49         }
50
51         function body()
52         {
53                 return fread($this->file, filesize($this->page));
54         }
55
56         function image()
57         {
58                 foreach (['jpg', 'png'] as $ext) {
59                         if (file_exists("{$this->link}.$ext")) {
60                                 return "{$this->link}.$ext";
61                         }
62                 }
63         }
64
65         function thumb($size = '300x')
66         {
67                 if ($this->image)
68                 return "thumb/$size/{$this->image}";
69         }
70 }
71
72 function shownews($input, $limit = 1000)
73 {
74         if (!is_array($input)) $input = glob("$input/*.html");
75         print '<ul class="left">'."\n\n";
76         foreach (array_reverse($input) as $filename) {
77                 $article = new ArchiveArticle($filename);
78                 print '<li>';
79                 if ($article->thumb) {
80                         printf('<img src="/%s" class="left" />', $article->thumb);
81                 }
82                 print '<article>';
83                 printf(
84                         '<h3><a href="/%s">%s <small class="date">%s</small></a></h3>',
85                         $article->link, $article->title, $article->date
86                 );
87                 print $article->body;
88                 print "</article></li>\n\n";
89
90                 if (--$limit <= 0) break;
91         }
92         print "</ul>\n\n";
93 }
94
95 function printtoc($input)
96 {
97         if (!is_array($input)) $input = glob("$input/*.html");
98         print '<ul>';
99         foreach (array_reverse($input) as $page) {
100                 $article = new ArchiveArticle($page);
101                 printf('<li><a href="/%s">%s <small class="date">%s</small></a></li>',
102                         $article->link, $article->safetitle, $article->date);
103         }
104         print "</ul>\n";
105 }