nieuws/feed: atom export of articles
[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 last()
46         {
47                 return filemtime($this->page);
48         }
49
50         function lastiso()
51         {
52                 return date(DATE_ATOM, $this->last);
53         }
54
55         function dateparts()
56         {
57                 preg_match('</(\d{4})/(\d{2})-(\d{2})->', $this->page, $ymd);
58                 return $ymd;
59         }
60
61         function dateiso()
62         {
63                 return implode('-', $this->dateparts());
64         }
65
66         function date()
67         {
68                 return showdate($this->dateparts);
69         }
70
71         function body()
72         {
73                 return fread($this->file, filesize($this->page));
74         }
75
76         function image()
77         {
78                 foreach (['jpg', 'png'] as $ext) {
79                         if (file_exists("{$this->link}.$ext")) {
80                                 return "{$this->link}.$ext";
81                         }
82                 }
83         }
84
85         function thumb($size = '300x')
86         {
87                 if ($this->image)
88                 return "thumb/$size/{$this->image}";
89         }
90 }
91
92 function shownews($input, $limit = 1000)
93 {
94         if (!is_array($input)) $input = glob("$input/*.html");
95         print '<ul class="left">'."\n\n";
96         foreach (array_reverse($input) as $filename) {
97                 $article = new ArchiveArticle($filename);
98                 print '<li>';
99                 if ($article->thumb) {
100                         printf('<img src="/%s" class="left" />', $article->thumb);
101                 }
102                 print '<article>';
103                 printf(
104                         '<h3><a href="/%s">%s <small class="date">%s</small></a></h3>',
105                         $article->link, $article->title, $article->date
106                 );
107                 print $article->body;
108                 print "</article></li>\n\n";
109
110                 if (--$limit <= 0) break;
111         }
112         print "</ul>\n\n";
113 }
114
115 function printtoc($input)
116 {
117         if (!is_array($input)) $input = glob("$input/*.html");
118         print '<ul>';
119         foreach (array_reverse($input) as $page) {
120                 $article = new ArchiveArticle($page);
121                 printf('<li><a href="/%s">%s <small class="date">%s</small></a></li>',
122                         $article->link, $article->safetitle, $article->date);
123         }
124         print "</ul>\n";
125 }