This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
/
News.php
412 lines (342 loc) · 9.19 KB
/
News.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/
namespace Contao;
/**
* Provide methods regarding news archives.
*
* @author Leo Feyer <https://github.com/leofeyer>
*/
class News extends \Frontend
{
/**
* Update a particular RSS feed
*
* @param integer $intId
*/
public function generateFeed($intId)
{
$objFeed = \NewsFeedModel::findByPk($intId);
if ($objFeed === null)
{
return;
}
$objFeed->feedName = $objFeed->alias ?: 'news' . $objFeed->id;
// Delete XML file
if (\Input::get('act') == 'delete')
{
$this->import('Files');
$this->Files->delete($objFeed->feedName . '.xml');
}
// Update XML file
else
{
$this->generateFiles($objFeed->row());
$this->log('Generated news feed "' . $objFeed->feedName . '.xml"', __METHOD__, TL_CRON);
}
}
/**
* Delete old files and generate all feeds
*/
public function generateFeeds()
{
$this->import('Automator');
$this->Automator->purgeXmlFiles();
$objFeed = \NewsFeedModel::findAll();
if ($objFeed !== null)
{
while ($objFeed->next())
{
$objFeed->feedName = $objFeed->alias ?: 'news' . $objFeed->id;
$this->generateFiles($objFeed->row());
$this->log('Generated news feed "' . $objFeed->feedName . '.xml"', __METHOD__, TL_CRON);
}
}
}
/**
* Generate all feeds including a certain archive
* #
* @param integer $intId
*/
public function generateFeedsByArchive($intId)
{
$objFeed = \NewsFeedModel::findByArchive($intId);
if ($objFeed !== null)
{
while ($objFeed->next())
{
$objFeed->feedName = $objFeed->alias ?: 'news' . $objFeed->id;
// Update the XML file
$this->generateFiles($objFeed->row());
$this->log('Generated news feed "' . $objFeed->feedName . '.xml"', __METHOD__, TL_CRON);
}
}
}
/**
* Generate an XML files and save them to the root directory
*
* @param array $arrFeed
*/
protected function generateFiles($arrFeed)
{
$arrArchives = deserialize($arrFeed['archives']);
if (!is_array($arrArchives) || empty($arrArchives))
{
return;
}
$strType = ($arrFeed['format'] == 'atom') ? 'generateAtom' : 'generateRss';
$strLink = $arrFeed['feedBase'] ?: \Environment::get('base');
$strFile = $arrFeed['feedName'];
$objFeed = new \Feed($strFile);
$objFeed->link = $strLink;
$objFeed->title = $arrFeed['title'];
$objFeed->description = $arrFeed['description'];
$objFeed->language = $arrFeed['language'];
$objFeed->published = $arrFeed['tstamp'];
// Get the items
if ($arrFeed['maxItems'] > 0)
{
$objArticle = \NewsModel::findPublishedByPids($arrArchives, null, $arrFeed['maxItems']);
}
else
{
$objArticle = \NewsModel::findPublishedByPids($arrArchives);
}
// Parse the items
if ($objArticle !== null)
{
$arrUrls = array();
while ($objArticle->next())
{
/** @var \PageModel $objPage */
$objPage = $objArticle->getRelated('pid');
$jumpTo = $objPage->jumpTo;
// No jumpTo page set (see #4784)
if (!$jumpTo)
{
continue;
}
// Get the jumpTo URL
if (!isset($arrUrls[$jumpTo]))
{
$objParent = \PageModel::findWithDetails($jumpTo);
// A jumpTo page is set but does no longer exist (see #5781)
if ($objParent === null)
{
$arrUrls[$jumpTo] = false;
}
else
{
$arrUrls[$jumpTo] = $objParent->getAbsoluteUrl((\Config::get('useAutoItem') && !\Config::get('disableAlias')) ? '/%s' : '/items/%s');
}
}
// Skip the event if it requires a jumpTo URL but there is none
if ($arrUrls[$jumpTo] === false && $objArticle->source == 'default')
{
continue;
}
$strUrl = $arrUrls[$jumpTo];
$objItem = new \FeedItem();
$objItem->title = $objArticle->headline;
$objItem->link = $this->getLink($objArticle, $strUrl);
$objItem->published = $objArticle->date;
$objItem->author = $objArticle->authorName;
// Prepare the description
if ($arrFeed['source'] == 'source_text')
{
$strDescription = '';
$objElement = \ContentModel::findPublishedByPidAndTable($objArticle->id, 'tl_news');
if ($objElement !== null)
{
// Overwrite the request (see #7756)
$strRequest = \Environment::get('request');
\Environment::set('request', $objItem->link);
while ($objElement->next())
{
$strDescription .= $this->getContentElement($objElement->current());
}
\Environment::set('request', $strRequest);
}
}
else
{
$strDescription = $objArticle->teaser;
}
$strDescription = $this->replaceInsertTags($strDescription, false);
$objItem->description = $this->convertRelativeUrls($strDescription, $strLink);
// Add the article image as enclosure
if ($objArticle->addImage)
{
$objFile = \FilesModel::findByUuid($objArticle->singleSRC);
if ($objFile !== null)
{
$objItem->addEnclosure($objFile->path, $strLink);
}
}
// Enclosures
if ($objArticle->addEnclosure)
{
$arrEnclosure = deserialize($objArticle->enclosure, true);
if (is_array($arrEnclosure))
{
$objFile = \FilesModel::findMultipleByUuids($arrEnclosure);
if ($objFile !== null)
{
while ($objFile->next())
{
$objItem->addEnclosure($objFile->path, $strLink);
}
}
}
}
$objFeed->addItem($objItem);
}
}
// Create the file
\File::putContent('share/' . $strFile . '.xml', $this->replaceInsertTags($objFeed->$strType(), false));
}
/**
* Add news items to the indexer
*
* @param array $arrPages
* @param integer $intRoot
* @param boolean $blnIsSitemap
*
* @return array
*/
public function getSearchablePages($arrPages, $intRoot=0, $blnIsSitemap=false)
{
$arrRoot = array();
if ($intRoot > 0)
{
$arrRoot = $this->Database->getChildRecords($intRoot, 'tl_page');
}
$arrProcessed = array();
$time = \Date::floorToMinute();
// Get all news archives
$objArchive = \NewsArchiveModel::findByProtected('');
// Walk through each archive
if ($objArchive !== null)
{
while ($objArchive->next())
{
// Skip news archives without target page
if (!$objArchive->jumpTo)
{
continue;
}
// Skip news archives outside the root nodes
if (!empty($arrRoot) && !in_array($objArchive->jumpTo, $arrRoot))
{
continue;
}
// Get the URL of the jumpTo page
if (!isset($arrProcessed[$objArchive->jumpTo]))
{
$objParent = \PageModel::findWithDetails($objArchive->jumpTo);
// The target page does not exist
if ($objParent === null)
{
continue;
}
// The target page has not been published (see #5520)
if (!$objParent->published || ($objParent->start != '' && $objParent->start > $time) || ($objParent->stop != '' && $objParent->stop <= ($time + 60)))
{
continue;
}
if ($blnIsSitemap)
{
// The target page is protected (see #8416)
if ($objParent->protected)
{
continue;
}
// The target page is exempt from the sitemap (see #6418)
if ($objParent->sitemap == 'map_never')
{
continue;
}
}
// Generate the URL
$arrProcessed[$objArchive->jumpTo] = $objParent->getAbsoluteUrl((\Config::get('useAutoItem') && !\Config::get('disableAlias')) ? '/%s' : '/items/%s');
}
$strUrl = $arrProcessed[$objArchive->jumpTo];
// Get the items
$objArticle = \NewsModel::findPublishedDefaultByPid($objArchive->id);
if ($objArticle !== null)
{
while ($objArticle->next())
{
$arrPages[] = $this->getLink($objArticle, $strUrl);
}
}
}
}
return $arrPages;
}
/**
* Return the link of a news article
*
* @param \NewsModel $objItem
* @param string $strUrl
* @param string $strBase
*
* @return string
*/
protected function getLink($objItem, $strUrl, $strBase='')
{
switch ($objItem->source)
{
// Link to an external page
case 'external':
return $objItem->url;
break;
// Link to an internal page
case 'internal':
if (($objTarget = $objItem->getRelated('jumpTo')) !== null)
{
/** @var \PageModel $objTarget */
return $objTarget->getAbsoluteUrl();
}
break;
// Link to an article
case 'article':
if (($objArticle = \ArticleModel::findByPk($objItem->articleId, array('eager'=>true))) !== null && ($objPid = $objArticle->getRelated('pid')) !== null)
{
/** @var \PageModel $objPid */
return ampersand($objPid->getAbsoluteUrl('/articles/' . ((!\Config::get('disableAlias') && $objArticle->alias != '') ? $objArticle->alias : $objArticle->id)));
}
break;
}
// Backwards compatibility (see #8329)
if ($strBase != '' && !preg_match('#^https?://#', $strUrl))
{
$strUrl = $strBase . $strUrl;
}
// Link to the default page
return sprintf($strUrl, (($objItem->alias != '' && !\Config::get('disableAlias')) ? $objItem->alias : $objItem->id));
}
/**
* Return the names of the existing feeds so they are not removed
*
* @return array
*/
public function purgeOldFeeds()
{
$arrFeeds = array();
$objFeeds = \NewsFeedModel::findAll();
if ($objFeeds !== null)
{
while ($objFeeds->next())
{
$arrFeeds[] = $objFeeds->alias ?: 'news' . $objFeeds->id;
}
}
return $arrFeeds;
}
}