page: separate method to load page contents
[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 safetitle()
54         {
55                 return trim($this->meta['og:title'] ?? strip_tags($this->title));
56         }
57         function name()
58         {
59                 return $this->safetitle ?: $this->link;
60         }
61
62         function last()
63         {
64                 return filemtime($this->page);
65         }
66         function lastiso()
67         {
68                 return date(DATE_ATOM, $this->last);
69         }
70
71         function dateparts()
72         {
73                 preg_match('< / (\d{4}) [/-] (\d{2}) (?:- (\d{2}) )? - >x', $this->page, $ymd);
74                 array_shift($ymd);
75                 return $ymd;
76         }
77         function dateiso()
78         {
79                 return implode('-', $this->dateparts()) . 'T12:00:00+02:00';
80         }
81         function date()
82         {
83                 return showdate($this->dateparts);
84         }
85
86         function story()
87         {
88                 if ( preg_match('{
89                         \n (?: < (?: p | figure [^>]* ) >\s* )+ (<img\ [^>]*>) | \n <hr\ />
90                 }x', $this->body, $img, PREG_OFFSET_CAPTURE) ) {
91                         # strip part after matching divider (image)
92                         if (isset($img[1])) {
93                                 $this->img = $img[1][0];
94                         }
95                         return substr($this->body, 0, $img[0][1]);
96                 }
97                 return $this->body;
98         }
99
100         function teaser()
101         {
102                 if ($override = @$this->meta['og:description']) {
103                         # prefer specific page description if found in metadata
104                         return $override;
105                 }
106
107                 if (preg_match('{
108                         </h2> (?: \s+ | <p\sclass="nav\b.*?</p> | <div[^>]*> )* <p> \s* (.*?) </p>
109                 }sx', $this->raw, $bodyp, PREG_OFFSET_CAPTURE)) {
110                         # fallback paragraph contents following the page header
111                         if ($bodyp[1][1] < 256) {
112                                 return $bodyp[1][0];
113                         }
114                 }
115
116                 # starting paragraph for documents without title (assumed simple/partial)
117                 if (strpos($this->raw, '<h2') === FALSE and preg_match('{
118                         \A (?: <div [^>]*> \s* )* <p> \s* (.*?) </p>
119                 }sx', $this->raw, $bodyp)) {
120                         return $bodyp[1];
121                 }
122         }
123
124         function img()
125         {
126                 $this->img = NULL;
127                 $this->story;
128                 return $this->img;
129         }
130         function image()
131         {
132                 if ($override = @$this->meta['og:image']) {
133                         # prefer specific page image if found in metadata
134                         return $override;
135                 }
136
137                 if ( preg_match('/\bsrc="([^"]*)"/', $this->img, $src) ) {
138                         return $src[1];
139                 }
140         }
141         function thumb($size = '300x')
142         {
143                 if (!$this->image or $this->image[0] !== '/') return;
144                 return preg_replace(
145                         ['{^(?:/thumb/[^/]*)?}', '/\.groot(?=\.\w+$)/'], ["thumb/$size", ''],
146                         $this->image
147                 );
148         }
149 }
150
151 class PageSearch
152 {
153         function __construct($path = '.')
154         {
155                 $this->iterator = new RecursiveCallbackFilterIterator(
156                         new RecursiveDirectoryIterator($path),
157                         function ($current) {
158                                 if ($current->getFilename()[0] === '.') {
159                                         # skip hidden files and directories
160                                         return FALSE;
161                                 }
162                                 if ($current->isLink()) {
163                                         # ignore symlinks, original contents only
164                                         return FALSE;
165                                 }
166                                 if ($current->isDir()) {
167                                         # traverse subdirectories unless untracked in any amount
168                                         return !file_exists("$current/.gitignore");
169                                 }
170                                 # match **/*.html
171                                 return preg_match('/(?<!\.inc)\.html$/', $current->getFilename());
172                         }
173                 );
174         }
175
176         function files()
177         {
178                 # order alphabetically by link
179                 $dir = iterator_to_array(new RecursiveIteratorIterator($this->iterator));
180                 array_walk($dir, function (&$row, $name) {
181                         # prepare values for sorting (directory index first)
182                         $row = preg_replace('{/index\.html$}', '', $name);
183                 });
184                 asort($dir);
185                 return $dir;
186         }
187 }