summaryrefslogtreecommitdiff
blob: 516a31bde303a2b41b22fbf56e57cd8ea37d845f (plain)
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
<?php
declare( strict_types = 1 );

namespace MediaWiki\Extension\Translate\PageTranslation;

use MediaWiki\Extension\Translate\TranslatorInterface\Insertable\Insertable;
use MediaWiki\Extension\Translate\TranslatorInterface\Insertable\MediaWikiInsertablesSuggester;

/**
 * Special insertables for translatable pages.
 * @author Niklas Laxström
 * @license GPL-2.0-or-later
 * @since 2013.11
 */
class TranslatablePageInsertablesSuggester extends MediaWikiInsertablesSuggester {
	public function getInsertables( string $text ): array {
		$insertables = parent::getInsertables( $text );

		// Translatable pages allow naming the variables. Basically anything is
		// allowed in a variable name, but here we are stricter to avoid too many
		// false positives.
		$matches = [];
		preg_match_all( '/\$([a-zA-Z0-9-_]+)/', $text, $matches, PREG_SET_ORDER );

		$new = array_map( function ( $match ) {
			// Numerical ones are already handled by parent
			if ( ctype_digit( $match[1] ) ) {
				return null;
			}

			return new Insertable( $match[0], $match[0] );
		}, $matches );

		$new = array_filter( $new );
		$insertables = array_merge( $insertables, $new );

		return $insertables;
	}
}

class_alias(
	TranslatablePageInsertablesSuggester::class,
	'\MediaWiki\Extensions\Translate\TranslatablePageInsertablesSuggester'
);