| 1 |
<?php |
| 2 |
declare( strict_types = 1 ); |
| 3 |
|
| 4 |
namespace Wikimedia\Parsoid\Ext; |
| 5 |
|
| 6 |
use Wikimedia\Parsoid\Config\SiteConfig; |
| 7 |
use Wikimedia\Parsoid\Utils\Utils as U; |
| 8 |
|
| 9 |
/** |
| 10 |
* This class provides sundry helpers needed by extensions. |
| 11 |
*/ |
| 12 |
class Utils { |
| 13 |
/** |
| 14 |
* Entity-escape anything that would decode to a valid wikitext entity. |
| 15 |
* |
| 16 |
* Note that HTML5 allows certain "semicolon-less" entities, like |
| 17 |
* `¶`; these aren't allowed in wikitext and won't be escaped |
| 18 |
* by this function. |
| 19 |
* |
| 20 |
* @param string $text |
| 21 |
* @return string |
| 22 |
*/ |
| 23 |
public static function escapeWtEntities( string $text ): string { |
| 24 |
return U::escapeWtEntities( $text ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Decode HTML5 entities in wikitext. |
| 29 |
* |
| 30 |
* NOTE that wikitext only allows semicolon-terminated entities, while |
| 31 |
* HTML allows a number of "legacy" entities to be decoded without |
| 32 |
* a terminating semicolon. This function deliberately does not |
| 33 |
* decode these HTML-only entity forms. |
| 34 |
* |
| 35 |
* @param string $text |
| 36 |
* @return string |
| 37 |
*/ |
| 38 |
public static function decodeWtEntities( string $text ): string { |
| 39 |
return U::decodeWtEntities( $text ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Parse media dimensions |
| 44 |
* |
| 45 |
* @param SiteConfig $siteConfig |
| 46 |
* @param string $str media dimension string to parse |
| 47 |
* @param bool $onlyOne If set, returns null if multiple dimenstions are present |
| 48 |
* @param bool $localized Defaults to false; set to true if the $str |
| 49 |
* has already been matched against `img_width` to localize the `px` |
| 50 |
* suffix. |
| 51 |
* @return array{x:int,y?:int}|null |
| 52 |
*/ |
| 53 |
public static function parseMediaDimensions( |
| 54 |
SiteConfig $siteConfig, string $str, bool $onlyOne = false, |
| 55 |
bool $localized = false |
| 56 |
): ?array { |
| 57 |
return U::parseMediaDimensions( $siteConfig, $str, $onlyOne, $localized ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Encode all characters as entity references. This is done to make |
| 62 |
* characters safe for wikitext (regardless of whether they are |
| 63 |
* HTML-safe). Typically only called with single-codepoint strings. |
| 64 |
* @param string $s |
| 65 |
* @return string |
| 66 |
*/ |
| 67 |
public static function entityEncodeAll( string $s ): string { |
| 68 |
return U::entityEncodeAll( $s ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Validate media parameters |
| 73 |
* More generally, this is defined by the media handler in core |
| 74 |
* @param ?int $num |
| 75 |
* @return bool |
| 76 |
*/ |
| 77 |
public static function validateMediaParam( ?int $num ): bool { |
| 78 |
return U::validateMediaParam( $num ); |
| 79 |
} |
| 80 |
} |