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