page: rename article method "body" to "story"
[minimedit.git] / article.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[2]), $parts[1] > 0 ? $monthname[intval($parts[1])] : '', $parts[0],
13                 count($parts) > 5 ? "$parts[3]:$parts[4]" : '',
14         ]));
15 }
16
17 class ArchiveArticle
18 {
19         function __construct($path)
20         {
21                 $this->page = $path;
22                 $this->link = preg_replace('{(?:/index)?\.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                 if (!file_exists($this->page)) return;
33                 return fopen($this->page, 'r');
34         }
35
36         function title()
37         {
38                 return preg_replace('{<h2>(.*)</h2>\s*}', '\1', fgets($this->file));
39         }
40
41         function safetitle()
42         {
43                 return trim(strip_tags($this->title));
44         }
45
46         function name()
47         {
48                 return $this->safetitle ?: $this->link;
49         }
50
51         function last()
52         {
53                 return filemtime($this->page);
54         }
55
56         function lastiso()
57         {
58                 return date(DATE_ATOM, $this->last);
59         }
60
61         function dateparts()
62         {
63                 preg_match('< / (\d{4}) [/-] (\d{2}) (?:- (\d{2}) )? - >x', $this->page, $ymd);
64                 array_shift($ymd);
65                 return $ymd;
66         }
67
68         function dateiso()
69         {
70                 return implode('-', $this->dateparts()) . 'T12:00:00+02:00';
71         }
72
73         function date()
74         {
75                 return showdate($this->dateparts);
76         }
77
78         function story()
79         {
80                 $this->title;
81                 $rest = fread($this->file, filesize($this->page));
82                 if ( preg_match('{
83                         \n (?: < (?: p | figure [^>]* ) >\s* )+ (<img\ [^>]*>) | \n <hr\ />
84                 }x', $rest, $img, PREG_OFFSET_CAPTURE) ) {
85                         # strip part after matching divider (image)
86                         if (isset($img[1])) {
87                                 $this->img = $img[1][0];
88                         }
89                         return substr($rest, 0, $img[0][1]);
90                 }
91                 return $rest;
92         }
93
94         function teaser()
95         {
96                 if (preg_match('{<p>(.*?)</p>}s', $this->story, $bodyp)) {
97                         return $bodyp[1];
98                 }
99         }
100
101         function img()
102         {
103                 $this->img = NULL;
104                 $this->story;
105                 return $this->img;
106         }
107
108         function image()
109         {
110                 if ( preg_match('/\bsrc="([^"]*)"/', $this->img, $src) ) {
111                         return $src[1];
112                 }
113         }
114
115         function thumb($size = '300x')
116         {
117                 if (!$this->image or $this->image[0] !== '/') return;
118                 return preg_replace(
119                         ['{^(?:/thumb/[^/]*)?}', '/\.groot(?=\.\w+$)/'], ["thumb/$size", ''],
120                         $this->image
121                 );
122         }
123 }