article: find teaser paragraph from variable offset
[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         public $raw, $preface, $title, $body;
20         public $meta = [];
21
22         function __construct($path)
23         {
24                 $this->page = preg_replace('{^\.(?:/|$)}', '', $path);
25                 $this->link = preg_replace('{(?:(?:/|^)index)?\.html$}', '', $this->page);
26                 $this->raw($this->page);
27         }
28
29         function raw($page)
30         {
31                 if (!file_exists($page)) {
32                         return;
33                 }
34                 $this->raw = file_get_contents($page);
35
36                 if (preg_match_all('{
37                         \G <meta \s+ property="( [^"]+ )" \s+ content="( [^"]* )" > \s*
38                 }x', $this->raw, $meta)) {
39                         $matchlen = array_sum(array_map('strlen', $meta[0]));
40                         $this->raw = substr($this->raw, $matchlen); # delete matched contents
41                         $this->meta = array_combine($meta[1], $meta[2]); # [property => content]
42                 }
43
44                 @list ($this->preface, $this->title, $this->body) =
45                         preg_split('{<h2>(.*?)</h2>\s*}s', $this->raw, 2, PREG_SPLIT_DELIM_CAPTURE);
46         }
47
48         function __get($col)
49         {
50                 return $this->$col = $this->$col();  # run method and cache
51         }
52
53         function handler()
54         {
55                 $path = $this->link;
56                 $this->path = '';
57                 $this->restricted = FALSE;
58                 while (TRUE) {
59                         if (file_exists("$path/.private")) {
60                                 $this->restricted = $path;
61                         }
62
63                         if (file_exists("$path/index.php")) {
64                                 return $path;
65                         }
66
67                         $up = strrpos($path, '/');
68                         $this->path = substr($path, $up) . $this->path;
69                         $path = substr($path, 0, $up);
70                         if ($up === FALSE) {
71                                 break;
72                         }
73                 }
74                 return;
75         }
76
77         function safetitle()
78         {
79                 return trim($this->meta['og:title'] ?? strip_tags($this->title));
80         }
81         function name()
82         {
83                 return $this->safetitle ?: $this->link;
84         }
85
86         function last()
87         {
88                 return filemtime($this->page);
89         }
90         function lastiso()
91         {
92                 return date(DATE_ATOM, $this->last);
93         }
94
95         function dateparts()
96         {
97                 preg_match('< / (\d{4}) [/-] (\d{2}) (?:- (\d{2}) )? - >x', $this->page, $ymd);
98                 array_shift($ymd);
99                 return $ymd;
100         }
101         function dateiso()
102         {
103                 return implode('-', $this->dateparts()) . 'T12:00:00+02:00';
104         }
105         function date()
106         {
107                 return showdate($this->dateparts);
108         }
109
110         function story()
111         {
112                 if ( preg_match('{
113                         (?: < (?: p | figure [^>]* ) >\s* )+ (<img\ [^>]*>) | \n <hr\ />
114                 }x', $this->body, $img, PREG_OFFSET_CAPTURE) ) {
115                         # strip part after matching divider (image)
116                         if (isset($img[1])) {
117                                 $this->img = $img[1][0];
118                         }
119                         return substr($this->body, 0, $img[0][1]);
120                 }
121                 return $this->body;
122         }
123
124         function teaser()
125         {
126                 if ($override = @$this->meta['og:description']) {
127                         # prefer specific page description if found in metadata
128                         return $override;
129                 }
130
131                 # paragraph contents following the page header if any
132                 $offset = strpos($this->raw, '</h2>');
133                 $offset = $offset ? $offset + 5 : 0;
134                 if (preg_match('{
135                         \G (?: \s+ | <p\sclass="nav\b.*?</p> | <div [^>]*> )* <p> \s* (.*?) </p>
136                 }sx', $this->raw, $bodyp, 0, $offset)) {
137                         return $bodyp[1];
138                 }
139         }
140
141         function img()
142         {
143                 $this->img = NULL;
144                 $this->story;
145                 return $this->img;
146         }
147         function image()
148         {
149                 if ($override = @$this->meta['og:image']) {
150                         # prefer specific page image if found in metadata
151                         return $override;
152                 }
153
154                 if ( preg_match('/\bsrc="([^"]*)"/', $this->img, $src) ) {
155                         return $src[1];
156                 }
157         }
158         function thumb($size = '300x')
159         {
160                 if (!$this->image or $this->image[0] !== '/') return;
161                 return preg_replace(
162                         ['{^(?:/thumb/[^/]*)?}', '/\.groot(?=\.\w+$)/'], ["thumb/$size", ''],
163                         $this->image
164                 );
165         }
166 }
167
168 class PageSearch
169 {
170         function __construct($path = '.')
171         {
172                 $this->iterator = new RecursiveCallbackFilterIterator(
173                         new RecursiveDirectoryIterator($path),
174                         function ($current) {
175                                 if ($current->getFilename()[0] === '.') {
176                                         # skip hidden files and directories
177                                         return FALSE;
178                                 }
179                                 if ($current->isLink()) {
180                                         # ignore symlinks, original contents only
181                                         return FALSE;
182                                 }
183                                 if ($current->isDir()) {
184                                         # traverse subdirectories unless untracked in any amount
185                                         return !file_exists("$current/.gitignore");
186                                 }
187                                 # match **/*.html
188                                 return preg_match('/(?<!\.inc)\.html$/', $current->getFilename());
189                         }
190                 );
191         }
192
193         function files()
194         {
195                 # order alphabetically by link
196                 $dir = iterator_to_array(new RecursiveIteratorIterator($this->iterator));
197                 array_walk($dir, function (&$row, $name) {
198                         # prepare values for sorting (directory index first)
199                         $row = preg_replace('{/index\.html$}', '', $name);
200                 });
201                 asort($dir);
202                 return $dir;
203         }
204 }