MagicWordArray.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. /**
  3. * See docs/magicword.txt.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Parser
  22. */
  23. use MediaWiki\Logger\LoggerFactory;
  24. use MediaWiki\MediaWikiServices;
  25. /**
  26. * Class for handling an array of magic words
  27. * @ingroup Parser
  28. */
  29. class MagicWordArray {
  30. /** @var string[] */
  31. public $names = [];
  32. /** @var MagicWordFactory */
  33. private $factory;
  34. /** @var array */
  35. private $hash;
  36. private $baseRegex;
  37. private $regex;
  38. /**
  39. * @param string[] $names
  40. * @param MagicWordFactory|null $factory
  41. */
  42. public function __construct( $names = [], MagicWordFactory $factory = null ) {
  43. $this->names = $names;
  44. $this->factory = $factory ?: MediaWikiServices::getInstance()->getMagicWordFactory();
  45. }
  46. /**
  47. * Add a magic word by name
  48. *
  49. * @param string $name
  50. */
  51. public function add( $name ) {
  52. $this->names[] = $name;
  53. $this->hash = $this->baseRegex = $this->regex = null;
  54. }
  55. /**
  56. * Add a number of magic words by name
  57. *
  58. * @param string[] $names
  59. */
  60. public function addArray( $names ) {
  61. $this->names = array_merge( $this->names, array_values( $names ) );
  62. $this->hash = $this->baseRegex = $this->regex = null;
  63. }
  64. /**
  65. * Get a 2-d hashtable for this array
  66. * @return array
  67. */
  68. public function getHash() {
  69. if ( is_null( $this->hash ) ) {
  70. $this->hash = [ 0 => [], 1 => [] ];
  71. foreach ( $this->names as $name ) {
  72. $magic = $this->factory->get( $name );
  73. $case = intval( $magic->isCaseSensitive() );
  74. foreach ( $magic->getSynonyms() as $syn ) {
  75. if ( !$case ) {
  76. $syn = $this->factory->getContentLanguage()->lc( $syn );
  77. }
  78. $this->hash[$case][$syn] = $name;
  79. }
  80. }
  81. }
  82. return $this->hash;
  83. }
  84. /**
  85. * Get the base regex
  86. * @return string[]
  87. */
  88. public function getBaseRegex() {
  89. if ( is_null( $this->baseRegex ) ) {
  90. $this->baseRegex = [ 0 => '', 1 => '' ];
  91. $allGroups = [];
  92. foreach ( $this->names as $name ) {
  93. $magic = $this->factory->get( $name );
  94. $case = intval( $magic->isCaseSensitive() );
  95. foreach ( $magic->getSynonyms() as $i => $syn ) {
  96. // Group name must start with a non-digit in PCRE 8.34+
  97. $it = strtr( $i, '0123456789', 'abcdefghij' );
  98. $groupName = $it . '_' . $name;
  99. $group = '(?P<' . $groupName . '>' . preg_quote( $syn, '/' ) . ')';
  100. // look for same group names to avoid same named subpatterns in the regex
  101. if ( isset( $allGroups[$groupName] ) ) {
  102. throw new MWException(
  103. __METHOD__ . ': duplicate internal name in magic word array: ' . $name
  104. );
  105. }
  106. $allGroups[$groupName] = true;
  107. if ( $this->baseRegex[$case] === '' ) {
  108. $this->baseRegex[$case] = $group;
  109. } else {
  110. $this->baseRegex[$case] .= '|' . $group;
  111. }
  112. }
  113. }
  114. }
  115. return $this->baseRegex;
  116. }
  117. /**
  118. * Get an unanchored regex that does not match parameters
  119. * @return string[]
  120. */
  121. public function getRegex() {
  122. if ( is_null( $this->regex ) ) {
  123. $base = $this->getBaseRegex();
  124. $this->regex = [ '', '' ];
  125. if ( $this->baseRegex[0] !== '' ) {
  126. $this->regex[0] = "/{$base[0]}/iuS";
  127. }
  128. if ( $this->baseRegex[1] !== '' ) {
  129. $this->regex[1] = "/{$base[1]}/S";
  130. }
  131. }
  132. return $this->regex;
  133. }
  134. /**
  135. * Get a regex for matching variables with parameters
  136. *
  137. * @return string[]
  138. */
  139. public function getVariableRegex() {
  140. return str_replace( "\\$1", "(.*?)", $this->getRegex() );
  141. }
  142. /**
  143. * Get a regex anchored to the start of the string that does not match parameters
  144. *
  145. * @return string[]
  146. */
  147. public function getRegexStart() {
  148. $base = $this->getBaseRegex();
  149. $newRegex = [ '', '' ];
  150. if ( $base[0] !== '' ) {
  151. $newRegex[0] = "/^(?:{$base[0]})/iuS";
  152. }
  153. if ( $base[1] !== '' ) {
  154. $newRegex[1] = "/^(?:{$base[1]})/S";
  155. }
  156. return $newRegex;
  157. }
  158. /**
  159. * Get an anchored regex for matching variables with parameters
  160. *
  161. * @return string[]
  162. */
  163. public function getVariableStartToEndRegex() {
  164. $base = $this->getBaseRegex();
  165. $newRegex = [ '', '' ];
  166. if ( $base[0] !== '' ) {
  167. $newRegex[0] = str_replace( "\\$1", "(.*?)", "/^(?:{$base[0]})$/iuS" );
  168. }
  169. if ( $base[1] !== '' ) {
  170. $newRegex[1] = str_replace( "\\$1", "(.*?)", "/^(?:{$base[1]})$/S" );
  171. }
  172. return $newRegex;
  173. }
  174. /**
  175. * @since 1.20
  176. * @return string[]
  177. */
  178. public function getNames() {
  179. return $this->names;
  180. }
  181. /**
  182. * Parse a match array from preg_match
  183. * Returns array(magic word ID, parameter value)
  184. * If there is no parameter value, that element will be false.
  185. *
  186. * @param array $m
  187. *
  188. * @throws MWException
  189. * @return array
  190. */
  191. public function parseMatch( $m ) {
  192. reset( $m );
  193. while ( ( $key = key( $m ) ) !== null ) {
  194. $value = current( $m );
  195. next( $m );
  196. if ( $key === 0 || $value === '' ) {
  197. continue;
  198. }
  199. $parts = explode( '_', $key, 2 );
  200. if ( count( $parts ) != 2 ) {
  201. // This shouldn't happen
  202. // continue;
  203. throw new MWException( __METHOD__ . ': bad parameter name' );
  204. }
  205. list( /* $synIndex */, $magicName ) = $parts;
  206. $paramValue = next( $m );
  207. return [ $magicName, $paramValue ];
  208. }
  209. // This shouldn't happen either
  210. throw new MWException( __METHOD__ . ': parameter not found' );
  211. }
  212. /**
  213. * Match some text, with parameter capture
  214. * Returns an array with the magic word name in the first element and the
  215. * parameter in the second element.
  216. * Both elements are false if there was no match.
  217. *
  218. * @param string $text
  219. *
  220. * @return array
  221. */
  222. public function matchVariableStartToEnd( $text ) {
  223. $regexes = $this->getVariableStartToEndRegex();
  224. foreach ( $regexes as $regex ) {
  225. if ( $regex !== '' ) {
  226. $m = [];
  227. if ( preg_match( $regex, $text, $m ) ) {
  228. return $this->parseMatch( $m );
  229. }
  230. }
  231. }
  232. return [ false, false ];
  233. }
  234. /**
  235. * Match some text, without parameter capture
  236. * Returns the magic word name, or false if there was no capture
  237. *
  238. * @param string $text
  239. *
  240. * @return string|bool False on failure
  241. */
  242. public function matchStartToEnd( $text ) {
  243. $hash = $this->getHash();
  244. if ( isset( $hash[1][$text] ) ) {
  245. return $hash[1][$text];
  246. }
  247. $lc = $this->factory->getContentLanguage()->lc( $text );
  248. return $hash[0][$lc] ?? false;
  249. }
  250. /**
  251. * Returns an associative array, ID => param value, for all items that match
  252. * Removes the matched items from the input string (passed by reference)
  253. *
  254. * @param string &$text
  255. *
  256. * @return array
  257. */
  258. public function matchAndRemove( &$text ) {
  259. $found = [];
  260. $regexes = $this->getRegex();
  261. foreach ( $regexes as $regex ) {
  262. if ( $regex === '' ) {
  263. continue;
  264. }
  265. $matches = [];
  266. $res = preg_match_all( $regex, $text, $matches, PREG_SET_ORDER );
  267. if ( $res === false ) {
  268. LoggerFactory::getInstance( 'parser' )->warning( 'preg_match_all returned false', [
  269. 'code' => preg_last_error(),
  270. 'regex' => $regex,
  271. 'text' => $text,
  272. ] );
  273. } elseif ( $res ) {
  274. foreach ( $matches as $m ) {
  275. list( $name, $param ) = $this->parseMatch( $m );
  276. $found[$name] = $param;
  277. }
  278. }
  279. $res = preg_replace( $regex, '', $text );
  280. if ( $res === null ) {
  281. LoggerFactory::getInstance( 'parser' )->warning( 'preg_replace returned null', [
  282. 'code' => preg_last_error(),
  283. 'regex' => $regex,
  284. 'text' => $text,
  285. ] );
  286. }
  287. $text = $res;
  288. }
  289. return $found;
  290. }
  291. /**
  292. * Return the ID of the magic word at the start of $text, and remove
  293. * the prefix from $text.
  294. * Return false if no match found and $text is not modified.
  295. * Does not match parameters.
  296. *
  297. * @param string &$text
  298. *
  299. * @return int|bool False on failure
  300. */
  301. public function matchStartAndRemove( &$text ) {
  302. $regexes = $this->getRegexStart();
  303. foreach ( $regexes as $regex ) {
  304. if ( $regex === '' ) {
  305. continue;
  306. }
  307. if ( preg_match( $regex, $text, $m ) ) {
  308. list( $id, ) = $this->parseMatch( $m );
  309. if ( strlen( $m[0] ) >= strlen( $text ) ) {
  310. $text = '';
  311. } else {
  312. $text = substr( $text, strlen( $m[0] ) );
  313. }
  314. return $id;
  315. }
  316. }
  317. return false;
  318. }
  319. }