Ensure variables in database classes are defined, used and correctly cased
[lhc/web/wiklou.git] / languages / utils / CLDRPluralRuleEvaluator.php
1 <?php
2 /**
3 * Parse and evaluate a plural rule.
4 *
5 * UTS #35 Revision 33
6 * http://www.unicode.org/reports/tr35/tr35-33/tr35-numbers.html#Language_Plural_Rules
7 *
8 * @author Niklas Laxstrom, Tim Starling
9 *
10 * @copyright Copyright © 2010-2012, Niklas Laxström
11 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0
12 * or later
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 * http://www.gnu.org/copyleft/gpl.html
28 *
29 *
30 * @file
31 * @since 1.20
32 */
33 class CLDRPluralRuleEvaluator {
34 /**
35 * Evaluate a number against a set of plural rules. If a rule passes,
36 * return the index of plural rule.
37 *
38 * @param int The number to be evaluated against the rules
39 * @param array The associative array of plural rules in pluralform => rule format.
40 * @return int The index of the plural form which passed the evaluation
41 */
42 public static function evaluate( $number, array $rules ) {
43 $rules = self::compile( $rules );
44 return self::evaluateCompiled( $number, $rules );
45 }
46
47 /**
48 * Convert a set of rules to a compiled form which is optimised for
49 * fast evaluation. The result will be an array of strings, and may be cached.
50 *
51 * @param array $rules The rules to compile
52 * @return array An array of compile rules.
53 */
54 public static function compile( array $rules ) {
55 // We can't use array_map() for this because it generates a warning if
56 // there is an exception.
57 foreach ( $rules as &$rule ) {
58 $rule = CLDRPluralRuleConverter::convert( $rule );
59 }
60 return $rules;
61 }
62
63 /**
64 * Evaluate a compiled set of rules returned by compile(). Do not allow
65 * the user to edit the compiled form, or else PHP errors may result.
66 *
67 * @param string The number to be evaluated against the rules, in English, or it
68 * may be a type convertible to string.
69 * @param array The associative array of plural rules in pluralform => rule format.
70 * @return int The index of the plural form which passed the evaluation
71 */
72 public static function evaluateCompiled( $number, array $rules ) {
73 // Calculate the values of the operand symbols
74 $number = strval( $number );
75 if ( !preg_match( '/^ -? ( ([0-9]+) (?: \. ([0-9]+) )? )$/x', $number, $m ) ) {
76 wfDebug( __METHOD__.': invalid number input, returning "other"' );
77 return count( $rules );
78 }
79 if ( !isset( $m[3] ) ) {
80 $operandSymbols = array(
81 'n' => intval( $m[1] ),
82 'i' => intval( $m[1] ),
83 'v' => 0,
84 'w' => 0,
85 'f' => 0,
86 't' => 0
87 );
88 } else {
89 $absValStr = $m[1];
90 $intStr = $m[2];
91 $fracStr = $m[3];
92 $operandSymbols = array(
93 'n' => floatval( $absValStr ),
94 'i' => intval( $intStr ),
95 'v' => strlen( $fracStr ),
96 'w' => strlen( rtrim( $fracStr, '0' ) ),
97 'f' => intval( $fracStr ),
98 't' => intval( rtrim( $fracStr, '0' ) ),
99 );
100 }
101
102 // The compiled form is RPN, with tokens strictly delimited by
103 // spaces, so this is a simple RPN evaluator.
104 foreach ( $rules as $i => $rule ) {
105 $stack = array();
106 $zero = ord( '0' );
107 $nine = ord( '9' );
108 foreach ( StringUtils::explode( ' ', $rule ) as $token ) {
109 $ord = ord( $token );
110 if ( isset( $operandSymbols[$token] ) ) {
111 $stack[] = $operandSymbols[$token];
112 } elseif ( $ord >= $zero && $ord <= $nine ) {
113 $stack[] = intval( $token );
114 } else {
115 $right = array_pop( $stack );
116 $left = array_pop( $stack );
117 $result = self::doOperation( $token, $left, $right );
118 $stack[] = $result;
119 }
120 }
121 if ( $stack[0] ) {
122 return $i;
123 }
124 }
125 // None of the provided rules match. The number belongs to category
126 // 'other', which comes last.
127 return count( $rules );
128 }
129
130 /**
131 * Do a single operation
132 *
133 * @param string $token The token string
134 * @param mixed $left The left operand. If it is an object, its state may be destroyed.
135 * @param mixed $right The right operand
136 * @throws CLDRPluralRuleError
137 * @return mixed The operation result
138 */
139 private static function doOperation( $token, $left, $right ) {
140 if ( in_array( $token, array( 'in', 'not-in', 'within', 'not-within' ) ) ) {
141 if ( !( $right instanceof CLDRPluralRuleEvaluator_Range ) ) {
142 $right = new CLDRPluralRuleEvaluator_Range( $right );
143 }
144 }
145 switch ( $token ) {
146 case 'or':
147 return $left || $right;
148 case 'and':
149 return $left && $right;
150 case 'is':
151 return $left == $right;
152 case 'is-not':
153 return $left != $right;
154 case 'in':
155 return $right->isNumberIn( $left );
156 case 'not-in':
157 return !$right->isNumberIn( $left );
158 case 'within':
159 return $right->isNumberWithin( $left );
160 case 'not-within':
161 return !$right->isNumberWithin( $left );
162 case 'mod':
163 if ( is_int( $left ) ) {
164 return (int)fmod( $left, $right );
165 }
166 return fmod( $left, $right );
167 case ',':
168 if ( $left instanceof CLDRPluralRuleEvaluator_Range ) {
169 $range = $left;
170 } else {
171 $range = new CLDRPluralRuleEvaluator_Range( $left );
172 }
173 $range->add( $right );
174 return $range;
175 case '..':
176 return new CLDRPluralRuleEvaluator_Range( $left, $right );
177 default:
178 throw new CLDRPluralRuleError( "Invalid RPN token" );
179 }
180 }
181 }
182
183 /**
184 * Evaluator helper class representing a range list.
185 */
186 class CLDRPluralRuleEvaluator_Range {
187 /**
188 * The parts
189 *
190 * @var array
191 */
192 public $parts = array();
193
194 /**
195 * Initialize a new instance of CLDRPluralRuleEvaluator_Range
196 *
197 * @param int $start The start of the range
198 * @param int|bool $end The end of the range, or false if the range is not bounded.
199 */
200 function __construct( $start, $end = false ) {
201 if ( $end === false ) {
202 $this->parts[] = $start;
203 } else {
204 $this->parts[] = array( $start, $end );
205 }
206 }
207
208 /**
209 * Determine if the given number is inside the range.
210 *
211 * @param int $number The number to check
212 * @param bool $integerConstraint If true, also asserts the number is an integer; otherwise, number simply has to be inside the range.
213 * @return bool True if the number is inside the range; otherwise, false.
214 */
215 function isNumberIn( $number, $integerConstraint = true ) {
216 foreach ( $this->parts as $part ) {
217 if ( is_array( $part ) ) {
218 if ( ( !$integerConstraint || floor( $number ) === (float)$number )
219 && $number >= $part[0] && $number <= $part[1]
220 ) {
221 return true;
222 }
223 } else {
224 if ( $number == $part ) {
225 return true;
226 }
227 }
228 }
229 return false;
230 }
231
232 /**
233 * Readable alias for isNumberIn( $number, false ), and the implementation
234 * of the "within" operator.
235 *
236 * @param int $number The number to check
237 * @return bool True if the number is inside the range; otherwise, false.
238 */
239 function isNumberWithin( $number ) {
240 return $this->isNumberIn( $number, false );
241 }
242
243 /**
244 * Add another part to this range.
245 *
246 * @param mixed The part to add, either a range object itself or a single number.
247 */
248 function add( $other ) {
249 if ( $other instanceof self ) {
250 $this->parts = array_merge( $this->parts, $other->parts );
251 } else {
252 $this->parts[] = $other;
253 }
254 }
255
256 /**
257 * Returns the string representation of the rule evaluator range.
258 * The purpose of this method is to help debugging.
259 *
260 * @return string The string representation of the rule evaluator range
261 */
262 function __toString() {
263 $s = 'Range(';
264 foreach ( $this->parts as $i => $part ) {
265 if ( $i ) {
266 $s .= ', ';
267 }
268 if ( is_array( $part ) ) {
269 $s .= $part[0] . '..' . $part[1];
270 } else {
271 $s .= $part;
272 }
273 }
274 $s .= ')';
275 return $s;
276 }
277
278 }
279
280 /**
281 * Helper class for converting rules to reverse polish notation (RPN).
282 */
283 class CLDRPluralRuleConverter {
284 /**
285 * The input string
286 *
287 * @var string
288 */
289 public $rule;
290
291 /**
292 * The current position
293 *
294 * @var int
295 */
296 public $pos;
297
298 /**
299 * The past-the-end position
300 *
301 * @var int
302 */
303 public $end;
304
305 /**
306 * The operator stack
307 *
308 * @var array
309 */
310 public $operators = array();
311
312 /**
313 * The operand stack
314 *
315 * @var array
316 */
317 public $operands = array();
318
319 /**
320 * Precedence levels. Note that there's no need to worry about associativity
321 * for the level 4 operators, since they return boolean and don't accept
322 * boolean inputs.
323 */
324 static $precedence = array(
325 'or' => 2,
326 'and' => 3,
327 'is' => 4,
328 'is-not' => 4,
329 'in' => 4,
330 'not-in' => 4,
331 'within' => 4,
332 'not-within' => 4,
333 'mod' => 5,
334 ',' => 6,
335 '..' => 7,
336 );
337
338 /**
339 * A character list defining whitespace, for use in strspn() etc.
340 */
341 const WHITESPACE_CLASS = " \t\r\n";
342
343 /**
344 * Same for digits. Note that the grammar given in UTS #35 doesn't allow
345 * negative numbers or decimal separators.
346 */
347 const NUMBER_CLASS = '0123456789';
348
349 /**
350 * A character list of symbolic operands.
351 */
352 const OPERAND_SYMBOLS = 'nivwft';
353
354 /**
355 * An anchored regular expression which matches a word at the current offset.
356 */
357 const WORD_REGEX = '/[a-zA-Z@]+/A';
358
359 /**
360 * Convert a rule to RPN. This is the only public entry point.
361 *
362 * @param $rule The rule to convert
363 * @return string The RPN representation of the rule
364 */
365 public static function convert( $rule ) {
366 $parser = new self( $rule );
367 return $parser->doConvert();
368 }
369
370 /**
371 * Private constructor.
372 */
373 protected function __construct( $rule ) {
374 $this->rule = $rule;
375 $this->pos = 0;
376 $this->end = strlen( $rule );
377 }
378
379 /**
380 * Do the operation.
381 *
382 * @return string The RPN representation of the rule (e.g. "5 3 mod n is")
383 */
384 protected function doConvert() {
385 $expectOperator = true;
386
387 // Iterate through all tokens, saving the operators and operands to a
388 // stack per Dijkstra's shunting yard algorithm.
389 while ( false !== ( $token = $this->nextToken() ) ) {
390 // In this grammar, there are only binary operators, so every valid
391 // rule string will alternate between operator and operand tokens.
392 $expectOperator = !$expectOperator;
393
394 if ( $token instanceof CLDRPluralRuleConverter_Expression ) {
395 // Operand
396 if ( $expectOperator ) {
397 $token->error( 'unexpected operand' );
398 }
399 $this->operands[] = $token;
400 continue;
401 } else {
402 // Operator
403 if ( !$expectOperator ) {
404 $token->error( 'unexpected operator' );
405 }
406 // Resolve higher precedence levels
407 $lastOp = end( $this->operators );
408 while ( $lastOp && self::$precedence[$token->name] <= self::$precedence[$lastOp->name] ) {
409 $this->doOperation( $lastOp, $this->operands );
410 array_pop( $this->operators );
411 $lastOp = end( $this->operators );
412 }
413 $this->operators[] = $token;
414 }
415 }
416
417 // Finish off the stack
418 while ( $op = array_pop( $this->operators ) ) {
419 $this->doOperation( $op, $this->operands );
420 }
421
422 // Make sure the result is sane. The first case is possible for an empty
423 // string input, the second should be unreachable.
424 if ( !count( $this->operands ) ) {
425 $this->error( 'condition expected' );
426 } elseif ( count( $this->operands ) > 1 ) {
427 $this->error( 'missing operator or too many operands' );
428 }
429
430 $value = $this->operands[0];
431 if ( $value->type !== 'boolean' ) {
432 $this->error( 'the result must have a boolean type' );
433 }
434
435 return $this->operands[0]->rpn;
436 }
437
438 /**
439 * Fetch the next token from the input string.
440 *
441 * @return CLDRPluralRuleConverter_Fragment The next token
442 */
443 protected function nextToken() {
444 if ( $this->pos >= $this->end ) {
445 return false;
446 }
447
448 // Whitespace
449 $length = strspn( $this->rule, self::WHITESPACE_CLASS, $this->pos );
450 $this->pos += $length;
451
452 if ( $this->pos >= $this->end ) {
453 return false;
454 }
455
456 // Number
457 $length = strspn( $this->rule, self::NUMBER_CLASS, $this->pos );
458 if ( $length !== 0 ) {
459 $token = $this->newNumber( substr( $this->rule, $this->pos, $length ), $this->pos );
460 $this->pos += $length;
461 return $token;
462 }
463
464 // Two-character operators
465 $op2 = substr( $this->rule, $this->pos, 2 );
466 if ( $op2 === '..' || $op2 === '!=' ) {
467 $token = $this->newOperator( $op2, $this->pos, 2 );
468 $this->pos += 2;
469 return $token;
470 }
471
472 // Single-character operators
473 $op1 = $this->rule[$this->pos];
474 if ( $op1 === ',' || $op1 === '=' || $op1 === '%' ) {
475 $token = $this->newOperator( $op1, $this->pos, 1 );
476 $this->pos ++;
477 return $token;
478 }
479
480 // Word
481 if ( !preg_match( self::WORD_REGEX, $this->rule, $m, 0, $this->pos ) ) {
482 $this->error( 'unexpected character "' . $this->rule[$this->pos] . '"' );
483 }
484 $word1 = strtolower( $m[0] );
485 $word2 = '';
486 $nextTokenPos = $this->pos + strlen( $word1 );
487 if ( $word1 === 'not' || $word1 === 'is' ) {
488 // Look ahead one word
489 $nextTokenPos += strspn( $this->rule, self::WHITESPACE_CLASS, $nextTokenPos );
490 if ( $nextTokenPos < $this->end
491 && preg_match( self::WORD_REGEX, $this->rule, $m, 0, $nextTokenPos )
492 ) {
493 $word2 = strtolower( $m[0] );
494 $nextTokenPos += strlen( $word2 );
495 }
496 }
497
498 // Two-word operators like "is not" take precedence over single-word operators like "is"
499 if ( $word2 !== '' ) {
500 $bothWords = "{$word1}-{$word2}";
501 if ( isset( self::$precedence[$bothWords] ) ) {
502 $token = $this->newOperator( $bothWords, $this->pos, $nextTokenPos - $this->pos );
503 $this->pos = $nextTokenPos;
504 return $token;
505 }
506 }
507
508 // Single-word operators
509 if ( isset( self::$precedence[$word1] ) ) {
510 $token = $this->newOperator( $word1, $this->pos, strlen( $word1 ) );
511 $this->pos += strlen( $word1 );
512 return $token;
513 }
514
515 // The single-character operand symbols
516 if ( strpos( self::OPERAND_SYMBOLS, $word1 ) !== false ) {
517 $token = $this->newNumber( $word1, $this->pos );
518 $this->pos ++;
519 return $token;
520 }
521
522 // Samples
523 if ( $word1 === '@integer' || $word1 === '@decimal' ) {
524 // Samples are like comments, they have no effect on rule evaluation.
525 // They run from the first sample indicator to the end of the string.
526 $this->pos = $this->end;
527 return false;
528 }
529
530 $this->error( 'unrecognised word' );
531 }
532
533 /**
534 * For the binary operator $op, pop its operands off the stack and push
535 * a fragment with rpn and type members describing the result of that
536 * operation.
537 */
538 protected function doOperation( $op ) {
539 if ( count( $this->operands ) < 2 ) {
540 $op->error( 'missing operand' );
541 }
542 $right = array_pop( $this->operands );
543 $left = array_pop( $this->operands );
544 $result = $op->operate( $left, $right );
545 $this->operands[] = $result;
546 }
547
548 /**
549 * Create a numerical expression object
550 *
551 * @return CLDRPluralRuleConverter_Expression The numerical expression
552 */
553 protected function newNumber( $text, $pos ) {
554 return new CLDRPluralRuleConverter_Expression( $this, 'number', $text, $pos, strlen( $text ) );
555 }
556
557 /**
558 * Create a binary operator
559 *
560 * @return CLDRPluralRuleConverter_Operator The operator
561 */
562 protected function newOperator( $type, $pos, $length ) {
563 return new CLDRPluralRuleConverter_Operator( $this, $type, $pos, $length );
564 }
565
566 /**
567 * Throw an error
568 */
569 protected function error( $message ) {
570 throw new CLDRPluralRuleError( $message );
571 }
572 }
573
574 /**
575 * Helper for CLDRPluralRuleConverter.
576 * The base class for operators and expressions, describing a region of the input string.
577 */
578 class CLDRPluralRuleConverter_Fragment {
579 public $parser, $pos, $length, $end;
580
581 function __construct( $parser, $pos, $length ) {
582 $this->parser = $parser;
583 $this->pos = $pos;
584 $this->length = $length;
585 $this->end = $pos + $length;
586 }
587
588 public function error( $message ) {
589 $text = $this->getText();
590 throw new CLDRPluralRuleError( "$message at position " . ( $this->pos + 1 ) . ": \"$text\"" );
591 }
592
593 public function getText() {
594 return substr( $this->parser->rule, $this->pos, $this->length );
595 }
596 }
597
598 /**
599 * Helper for CLDRPluralRuleConverter.
600 * An expression object, representing a region of the input string (for error
601 * messages), the RPN notation used to evaluate it, and the result type for
602 * validation.
603 */
604 class CLDRPluralRuleConverter_Expression extends CLDRPluralRuleConverter_Fragment {
605 public $type, $rpn;
606
607 function __construct( $parser, $type, $rpn, $pos, $length ) {
608 parent::__construct( $parser, $pos, $length );
609 $this->type = $type;
610 $this->rpn = $rpn;
611 }
612
613 public function isType( $type ) {
614 if ( $type === 'range' && ( $this->type === 'range' || $this->type === 'number' ) ) {
615 return true;
616 }
617 if ( $type === $this->type ) {
618 return true;
619 }
620 return false;
621 }
622 }
623
624 /**
625 * Helper for CLDRPluralRuleConverter.
626 * An operator object, representing a region of the input string (for error
627 * messages), and the binary operator at that location.
628 */
629 class CLDRPluralRuleConverter_Operator extends CLDRPluralRuleConverter_Fragment {
630 /**
631 * The name
632 *
633 * @var string
634 */
635 public $name;
636
637 /**
638 * Each op type has three characters: left operand type, right operand type and result type
639 *
640 * b = boolean
641 * n = number
642 * r = range
643 *
644 * A number is a kind of range.
645 *
646 * @var array
647 */
648 static $opTypes = array(
649 'or' => 'bbb',
650 'and' => 'bbb',
651 'is' => 'nnb',
652 'is-not' => 'nnb',
653 'in' => 'nrb',
654 'not-in' => 'nrb',
655 'within' => 'nrb',
656 'not-within' => 'nrb',
657 'mod' => 'nnn',
658 ',' => 'rrr',
659 '..' => 'nnr',
660 );
661
662 /**
663 * Map converting from the abbrevation to the full form.
664 *
665 * @var array
666 */
667 static $typeSpecMap = array(
668 'b' => 'boolean',
669 'n' => 'number',
670 'r' => 'range',
671 );
672
673 /**
674 * Map for converting the new operators introduced in Rev 33 to the old forms
675 */
676 static $aliasMap = array(
677 '%' => 'mod',
678 '!=' => 'not-in',
679 '=' => 'in'
680 );
681
682 /**
683 * Initialize a new instance of a CLDRPluralRuleConverter_Operator object
684 *
685 * @param CLDRPluralRuleConverter $parser The parser
686 * @param string $name The operator name
687 * @param int $pos The position
688 * @param int $pos The length
689 */
690 function __construct( $parser, $name, $pos, $length ) {
691 parent::__construct( $parser, $pos, $length );
692 if ( isset( self::$aliasMap[$name] ) ) {
693 $name = self::$aliasMap[$name];
694 }
695 $this->name = $name;
696 }
697
698 /**
699 * Compute the operation
700 *
701 * @param CLDRPluralRuleConverter_Expression $left The left part of the expression
702 * @param CLDRPluralRuleConverter_Expression $right The right part of the expression
703 * @return CLDRPluralRuleConverter_Expression The result of the operation
704 */
705 public function operate( $left, $right ) {
706 $typeSpec = self::$opTypes[$this->name];
707
708 $leftType = self::$typeSpecMap[$typeSpec[0]];
709 $rightType = self::$typeSpecMap[$typeSpec[1]];
710 $resultType = self::$typeSpecMap[$typeSpec[2]];
711
712 $start = min( $this->pos, $left->pos, $right->pos );
713 $end = max( $this->end, $left->end, $right->end );
714 $length = $end - $start;
715
716 $newExpr = new CLDRPluralRuleConverter_Expression( $this->parser, $resultType,
717 "{$left->rpn} {$right->rpn} {$this->name}",
718 $start, $length );
719
720 if ( !$left->isType( $leftType ) ) {
721 $newExpr->error( "invalid type for left operand: expected $leftType, got {$left->type}" );
722 }
723
724 if ( !$right->isType( $rightType ) ) {
725 $newExpr->error( "invalid type for right operand: expected $rightType, got {$right->type}" );
726 }
727 return $newExpr;
728 }
729 }
730
731 /**
732 * The exception class for all the classes in this file. This will be thrown
733 * back to the caller if there is any validation error.
734 */
735 class CLDRPluralRuleError extends MWException {
736 function __construct( $message ) {
737 parent::__construct( 'CLDR plural rule error: ' . $message );
738 }
739 }