Skip to content

Commit 69eb1c5

Browse files
committed
Add snippet length limiting
1 parent ae48ddf commit 69eb1c5

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

Snippet.body.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class Snippet {
1212
* @return String: processed text
1313
*/
1414
static function parseText( $text ) {
15+
global $wgSnippetMaxLength;
16+
1517
$parts = explode( '==', $text );
1618
$text = $parts[0]; // remove anything after the first heading
1719

@@ -27,6 +29,12 @@ static function parseText( $text ) {
2729
$text = preg_replace( '/{{[^{]+?}}/', '', $text ); // remove 2nd layer templates
2830
$text = preg_replace( '/{{[^{]+?}}/', '', $text ); // remove 1st layer templates
2931

32+
$text = substr( $text, 0, $wgSnippetMaxLength ); // limit to $wgSnippetMaxLength chars
33+
34+
$parts = explode( '.', $text );
35+
array_pop( $parts ); // remove anything after the last full stop
36+
$text = implode( '.', $parts ) . '.';
37+
3038
$text = trim( $text );
3139

3240
return $text;

Snippet.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@
1414

1515
$wgAPIModules['snippet'] = 'SnippetAPI';
1616

17-
$wgExtensionMessagesFiles['Snippet'] = __DIR__ . '/Snippet.i18n.php';
17+
$wgExtensionMessagesFiles['Snippet'] = __DIR__ . '/Snippet.i18n.php';
18+
19+
$wgSnippetMaxLength = 600;

0 commit comments

Comments
 (0)