This repository has been archived by the owner on Sep 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShortTeaser.php
466 lines (448 loc) · 15.4 KB
/
ShortTeaser.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
<?php
/**
* Short Teaser
*
* PHP version 5
*
* @category Contao
* @package ShortTeaser
* @author Benjamin Meier <gpl@code-meier.de>
* @copyright 2013 Benjamin Meier
* @license http://www.gnu.org/licenses/gpl.html GNU/GPL
* @link http://www.code-meier.de
*/
if (!defined('TL_ROOT')) die('You can not access this file directly!');
/**
* Class ShortTeaser
*
* @category Contao
* @package ShortTeaser
* @author Benjamin Meier <gpl@code-meier.de>
* @copyright 2013 Benjamin Meier
* @license http://www.gnu.org/licenses/gpl.html GNU/GPL
* @link http://www.code-meier.de
*/
class ShortTeaser extends Frontend
{
/**
* Check if Teaser could be shortened
*
* Check if this is a newslist and if it
* its teaser should be shortened
*
* @param object &$objTemplate containing template information
* @param object $objArticles containing newsdata
*
* @uses $GLOBALS['TL_CONFIG']['shortTeaserHTML']
*
* @return boolean
*/
public function shortNewsTeaser(&$objTemplate, $objArticles)
{
$mod = false;
foreach (debug_backtrace() as $b) {
if ($b['class'] == 'Contao\ModuleNewsList') {
$mod = $b['object'];
break;
}
}
if ($mod===false || !is_object($mod) || $mod->shortTeaser!=1) {
if ($mod===false) {
$this->debug(
'No parent ModuleNewsList found!',
__METHOD__
);
} else if (!is_object($mod)) {
$this->debug(
'No object found, we need PHP Version > 5.1.0!',
__METHOD__
);
} else {
$this->debug(
'shortTeaser not active!',
__METHOD__
);
}
return false;
}
$this->debug('Found ModuleNewsList and shortTeaser active.', __METHOD__);
$shortTeaserShorten = unserialize($mod->shortTeaserShorten);
$type = $shortTeaserShorten['unit'];
$count = $shortTeaserShorten['value'];
$teaser = $objTemplate->teaser;
$full = $objTemplate->text;
$id = $objTemplate->id;
$text = "";
switch ($mod->shortTeaserSource) {
case 'teaser':
$this->debug('Using teaser as source.', __METHOD__);
$text = $this->checkHTML($teaser);
break;
case 'full':
$this->debug('Using fulltext as source.', __METHOD__);
$text = $this->checkHTML($full);
break;
case 'teaser_or_full':
if (strlen($teaser)>0) {
$this->debug('Using teaser as source.', __METHOD__);
$text = $this->checkHTML($teaser);
} else {
$this->debug('Using fulltext as source.', __METHOD__);
$text = $this->checkHTML($full);
}
break;
case 'full_no_teaser':
if (strlen($teaser)===0) {
$this->debug('Using fulltext as source.', __METHOD__);
$text = $this->checkHTML($full);
} else {
$this->debug('Using uncut teaser as source.', __METHOD__);
$text = $teaser;
}
break;
default:
$this->debug(
'Unknown value for shortTeaserSource!',
__METHOD__, TL_ERROR
);
return false;
break;
}
$new_text = $text;
// check if shortening is activ
if ($count>0) {
switch ($type) {
case 'chars':
$this->debug('Short text to '.$count.' chars.', __METHOD__);
$new_text = $this->getChars($text, $count, $id);
// add some chars like ...
$new_text.= $GLOBALS['TL_LANG']['MSC']['shortTeaserEnd'];
break;
case 'words':
$this->debug('Short text to '.$count.' words.', __METHOD__);
$new_text = $this->getWords($text, $count, $id);
// add some chars like ...
$new_text.= $GLOBALS['TL_LANG']['MSC']['shortTeaserEnd'];
break;
case 'sentences':
$this->debug('Short text to '.$count.' sentences.', __METHOD__);
$new_text = $this->getSentences($text, $count, $id);
break;
case 'paragraphs':
$this->debug('Short text to '.$count.' paragraphs.', __METHOD__);
$new_text = $this->getParagraphs($text, $count, $id);
break;
case 'sections':
$this->debug('Short text to '.$count.' sections.', __METHOD__);
$new_text = $this->getSections($text, $count, $id);
break;
default:
$this->debug(
'Unknown value for shortTeaserShorten!',
__METHOD__, TL_ERROR
);
break;
}
}
$objTemplate->teaser = $new_text;
return true;
}
/**
* Reduces some unallowed HTML
*
* Removes all HTML tags, not explicitely allowed
* in global settings.
*
* @param string $text The text to be processed
*
* @return string Resulting html code.
*/
protected function checkHTML($text)
{
return strip_tags($text, $GLOBALS['TL_CONFIG']['shortTeaserHTML']);
}
/**
* Reduce text to characters
*
* Reduces the text $text to $count characters
*
* @param string $text text to reduce
* @param int $count number of chars
* @param int $id the id of the news item
*
* @uses $GLOBALS['TL_LANG']['MSC']['shortTeaserEnd']
* @uses $GLOBALS['TL_CONFIG']['shortTeaserHTML']
*
* @return string new teaser html code
*/
protected function getChars($text, $count, $id)
{
// working copy of $count
$real_count = $count;
// working copy of $text
$stext = $text;
// ignoring empty html elements
$empty_tags = str_replace(
array('<img>','<br>','<input>','<hr>'),
'',
$GLOBALS['TL_CONFIG']['shortTeaserHTML']
);
// tag stack to add missing closing tags
$tag_stack = array();
// internal counter for while
$c = 0;
// regular expression for html tags
$reg = '#<(/([a-zA-Z0-9]*)|([A-Za-z0-9]*)( [^>]*)?)>#';
// search all html tags and consider them
while (preg_match($reg, $stext, $matches, PREG_OFFSET_CAPTURE)
&& $c < $real_count
&& ($c + $matches[0][1]) < $real_count
) {
$cm = count($matches);
if ($cm == 3) {
// closing tag
$res = array_search($matches[2][0], array_reverse($tag_stack));
if ($res === false) {
$message = 'Closing tag "'.htmlentities($$matches[2][0]).'"';
$message .= ' found in News with id '.$id.' but no';
$message .= ' corresponding opening tag before.';
$this->debug($message, __METHOD__, TL_ERROR);
} else {
// remove last corresponding opening tag
$this->debug(
'Found closing tag '.$matches[2][0].'.',
__METHOD__
);
unset($tag_stack[count($tag_stack)-$res-1]);
}
} else if ($cm > 3) {
// opening tag
// remove empty tags
$tag = strip_tags($matches[0][0], $empty_tags);
if (strlen($tag)>0) {
// add opening tags to $tag_stack
array_push($tag_stack, $matches[3][0]);
$this->debug(
'Found opening tag '.$matches[3][0].'.',
__METHOD__
);
} else {
$message = 'Found tag '.$matches[3][0]
.' but ignoring tag_stack.';
$this->debug(
$message,
__METHOD__
);
}
} else {
$message = 'You may inform the developer of the module';
$message .= ' '.__CLASS__.' that error 42 occured.';
$message .= ' But this will never happen =)';
$this->debug($message, __METHOD__, TL_ERROR);
}
// get length of match
$matchlength = strlen($matches[0][0]);
$end_pos = $matchlength + $matches[0][1];
// extend range
$real_count += $matchlength;
// extend actual position
$c += $end_pos;
// cut already checked text
$stext = substr($stext, $end_pos);
}
if (strlen($text)<=$real_count) {
$this->debug(
'Nothing to short cause text has '.strlen($text)
.' chars, '.$real_count.' allowed!',
__METHOD__
);
} else {
$this->debug(
'Short Teaser to calculated '.$real_count.' chars.',
__METHOD__
);
}
// now cut real text
$text = substr($text, 0, $real_count);
// add missing closing tags in reverse order
foreach (array_reverse($tag_stack) as $tag) {
$text .= '</'.$tag.'>';
$this->debug('Add missing closing tag '.$tag.'.', __METHOD__);
}
return $text;
}
/**
* Reduce text to words
*
* Reduces the text $text to $count words
*
* @param string $text text to reduce
* @param int $count number of words
* @param int $id the id of the news item
*
* @uses getChars()
*
* @return string new teaser html code
*/
protected function getWords($text, $count, $id)
{
// working copy of $text without html tags
$stext = strip_tags($text);
// get $count words from the beginning
$words = explode(' ', $stext, $count+1);
// check if there are some more words
if (count($words)>$count) {
// remove last element (containing the rest of the string)
array_pop($words);
}
// calculate position of last word
$pos = array_sum(array_map('strlen', $words))+count($words)-1;
$this->debug(
'Reducing to '.count($words).' words is equal to '.$pos.' chars.',
__METHOD__
);
// now use getChars()
return $this->getChars($text, $pos, $id);
}
/**
* Reduce text to Sentences
*
* Reduces the text $text to $count sentences
*
* @param string $text text to reduce
* @param int $count number of sentences
* @param int $id the id of the news item
*
* @uses getChars()
*
* @return string new teaser html code
*/
protected function getSentences($text, $count, $id)
{
// working copy of text without html tags
$stext = strip_tags($text);
// replace ! and ? with . for explode.
$stext = str_replace(array('!', '?'), '.', $stext);
$sentences = explode('.', $stext, $count+1);
// check if there are some more sentences
if (count($sentences)>$count) {
// remove last element (containing the rest of the string)
array_pop($sentences);
}
// calculate position of last sentence with .,? or ! at the end
$pos = array_sum(array_map('strlen', $sentences))+count($sentences);
$this->debug(
'Reducing to '.count($sentences).' sentences'
.' is equal to '.$pos.' chars.',
__METHOD__
);
// now use getChars()
return $this->getChars($text, $pos, $id);
}
/**
* Reduce text to paragraphs
*
* Reduces the text $text to $count paragraphs
*
* @param string $text text to reduce
* @param int $count number of paragraphs
* @param int $id the id of the news item
*
* @uses getChars()
*
* @return string new teaser html code
*/
protected function getParagraphs($text, $count, $id)
{
// working copy of text without html tags except p
$stext = strip_tags($text, '<p>');
$paragraphs = explode('</p>', $stext, $count+1);
// check if there are some more paragraphs
if (count($paragraphs)>$count) {
// remove last element (containing the rest of the string)
array_pop($paragraphs);
}
// remove html tags
$paragraphs = array_map('strip_tags', $paragraphs);
// calculate position of last paragraph
$pos = array_sum(array_map('strlen', $paragraphs));
$this->debug(
'Reducing to '.count($paragraphs).' paragraphs'
.' is equal to '.$pos.' chars.',
__METHOD__
);
// now use getChars()
return $this->getChars($text, $pos, $id);
}
/**
* Reduce text to sections
*
* Reduces the text $text to $count sections
*
* @param string $text text to reduce
* @param int $count number of sections
* @param int $id the id of the news item
*
* @uses getChars()
*
* @return string new teaser html code
*/
protected function getSections($text, $count, $id)
{
// headlines
$hl = array();
// search for type of first heading
preg_match('#<h([1-6])[^>]*?>#', $text, $matches, PREG_OFFSET_CAPTURE);
// search all html tags and consider them
if (count($matches)==2) {
$hl = $matches[1][0];
} else {
// no section found
return $text;
}
// working copy of text
$stext = $text;
$sections = explode('</h'.$hl.'>', $stext, $count+2);
// check if there are some more sections
if (count($sections)>$count+1) {
// remove last element (containing the rest of the string)
array_pop($sections);
}
// check if heading of next section is present
if (count($sections)>$count) {
// remove heading of next section
$ls = $sections[$count];
$sections[$count] = substr($ls, 0, strrpos($ls, '<h'.$hl));
}
// remove html tags
$sections = array_map('strip_tags', $sections);
// calculate position of last section
$pos = array_sum(array_map('strlen', $sections));
$this->debug(
'Reducing to '.count($sections).' sections is equal to '.$pos.' chars.',
__METHOD__
);
// now use getChars()
return $this->getChars($text, $pos, $id);
}
/**
* Debug to system log
*
* Logs the debug message to the system log
*
* @param string $message text to log
* @param string $method caller method
* @param string $type type for logging message
*
* @uses $GLOBALS['TL_CONFIG']['shortTeaserDebug']
*
* @return boolean
*/
protected function debug($message, $method, $type=TL_GENERAL)
{
if ($type==TL_ERROR || $GLOBALS['TL_CONFIG']['shortTeaserDebug']==1) {
$this->log($message, __CLASS__.' '.$method, $type);
}
}
}
?>