Index: trunk/extensions/Score/Score.php |
— | — | @@ -70,7 +70,7 @@ |
71 | 71 | /** |
72 | 72 | * Init routine. |
73 | 73 | * |
74 | | - * @param $parser Mediawiki parser |
| 74 | + * @param $parser Parser Mediawiki parser |
75 | 75 | * |
76 | 76 | * @return true if initialisation was successful, false otherwise. |
77 | 77 | */ |
Index: trunk/extensions/ParserFunctions/Expr.php |
— | — | @@ -157,6 +157,8 @@ |
158 | 158 | * The algorithm here is based on the infix to RPN algorithm given in |
159 | 159 | * http://montcs.bloomu.edu/~bobmon/Information/RPN/infix2rpn.shtml |
160 | 160 | * It's essentially the same as Dijkstra's shunting yard algorithm. |
| 161 | + * @param $expr string |
| 162 | + * @return string |
161 | 163 | */ |
162 | 164 | function doExpression( $expr ) { |
163 | 165 | $operands = array(); |
— | — | @@ -371,181 +373,260 @@ |
372 | 374 | return implode( "<br />\n", $operands ); |
373 | 375 | } |
374 | 376 | |
| 377 | + /** |
| 378 | + * @param $op int |
| 379 | + * @param $stack array |
| 380 | + * @throws ExprError |
| 381 | + */ |
375 | 382 | function doOperation( $op, &$stack ) { |
376 | 383 | switch ( $op ) { |
377 | 384 | case EXPR_NEGATIVE: |
378 | | - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 385 | + if ( count( $stack ) < 1 ) { |
| 386 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 387 | + } |
379 | 388 | $arg = array_pop( $stack ); |
380 | 389 | $stack[] = -$arg; |
381 | 390 | break; |
382 | 391 | case EXPR_POSITIVE: |
383 | | - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 392 | + if ( count( $stack ) < 1 ) { |
| 393 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 394 | + } |
384 | 395 | break; |
385 | 396 | case EXPR_TIMES: |
386 | | - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 397 | + if ( count( $stack ) < 2 ) { |
| 398 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 399 | + } |
387 | 400 | $right = array_pop( $stack ); |
388 | 401 | $left = array_pop( $stack ); |
389 | 402 | $stack[] = $left * $right; |
390 | 403 | break; |
391 | 404 | case EXPR_DIVIDE: |
392 | | - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 405 | + if ( count( $stack ) < 2 ) { |
| 406 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 407 | + } |
393 | 408 | $right = array_pop( $stack ); |
394 | 409 | $left = array_pop( $stack ); |
395 | | - if ( $right == 0 ) throw new ExprError( 'division_by_zero', $this->names[$op] ); |
| 410 | + if ( $right == 0 ) { |
| 411 | + throw new ExprError( 'division_by_zero', $this->names[$op] ); |
| 412 | + } |
396 | 413 | $stack[] = $left / $right; |
397 | 414 | break; |
398 | 415 | case EXPR_MOD: |
399 | | - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 416 | + if ( count( $stack ) < 2 ) { |
| 417 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 418 | + } |
400 | 419 | $right = array_pop( $stack ); |
401 | 420 | $left = array_pop( $stack ); |
402 | | - if ( $right == 0 ) throw new ExprError( 'division_by_zero', $this->names[$op] ); |
| 421 | + if ( $right == 0 ) { |
| 422 | + throw new ExprError( 'division_by_zero', $this->names[$op] ); |
| 423 | + } |
403 | 424 | $stack[] = $left % $right; |
404 | 425 | break; |
405 | 426 | case EXPR_PLUS: |
406 | | - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 427 | + if ( count( $stack ) < 2 ) { |
| 428 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 429 | + } |
407 | 430 | $right = array_pop( $stack ); |
408 | 431 | $left = array_pop( $stack ); |
409 | 432 | $stack[] = $left + $right; |
410 | 433 | break; |
411 | 434 | case EXPR_MINUS: |
412 | | - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 435 | + if ( count( $stack ) < 2 ) { |
| 436 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 437 | + } |
413 | 438 | $right = array_pop( $stack ); |
414 | 439 | $left = array_pop( $stack ); |
415 | 440 | $stack[] = $left - $right; |
416 | 441 | break; |
417 | 442 | case EXPR_AND: |
418 | | - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 443 | + if ( count( $stack ) < 2 ) { |
| 444 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 445 | + } |
419 | 446 | $right = array_pop( $stack ); |
420 | 447 | $left = array_pop( $stack ); |
421 | 448 | $stack[] = ( $left && $right ) ? 1 : 0; |
422 | 449 | break; |
423 | 450 | case EXPR_OR: |
424 | | - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 451 | + if ( count( $stack ) < 2 ) { |
| 452 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 453 | + } |
425 | 454 | $right = array_pop( $stack ); |
426 | 455 | $left = array_pop( $stack ); |
427 | 456 | $stack[] = ( $left || $right ) ? 1 : 0; |
428 | 457 | break; |
429 | 458 | case EXPR_EQUALITY: |
430 | | - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 459 | + if ( count( $stack ) < 2 ) { |
| 460 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 461 | + } |
431 | 462 | $right = array_pop( $stack ); |
432 | 463 | $left = array_pop( $stack ); |
433 | 464 | $stack[] = ( $left == $right ) ? 1 : 0; |
434 | 465 | break; |
435 | 466 | case EXPR_NOT: |
436 | | - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 467 | + if ( count( $stack ) < 1 ) { |
| 468 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 469 | + } |
437 | 470 | $arg = array_pop( $stack ); |
438 | 471 | $stack[] = ( !$arg ) ? 1 : 0; |
439 | 472 | break; |
440 | 473 | case EXPR_ROUND: |
441 | | - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 474 | + if ( count( $stack ) < 2 ) { |
| 475 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 476 | + } |
442 | 477 | $digits = intval( array_pop( $stack ) ); |
443 | 478 | $value = array_pop( $stack ); |
444 | 479 | $stack[] = round( $value, $digits ); |
445 | 480 | break; |
446 | 481 | case EXPR_LESS: |
447 | | - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 482 | + if ( count( $stack ) < 2 ) { |
| 483 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 484 | + } |
448 | 485 | $right = array_pop( $stack ); |
449 | 486 | $left = array_pop( $stack ); |
450 | 487 | $stack[] = ( $left < $right ) ? 1 : 0; |
451 | 488 | break; |
452 | 489 | case EXPR_GREATER: |
453 | | - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 490 | + if ( count( $stack ) < 2 ) { |
| 491 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 492 | + } |
454 | 493 | $right = array_pop( $stack ); |
455 | 494 | $left = array_pop( $stack ); |
456 | 495 | $stack[] = ( $left > $right ) ? 1 : 0; |
457 | 496 | break; |
458 | 497 | case EXPR_LESSEQ: |
459 | | - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 498 | + if ( count( $stack ) < 2 ) { |
| 499 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 500 | + } |
460 | 501 | $right = array_pop( $stack ); |
461 | 502 | $left = array_pop( $stack ); |
462 | 503 | $stack[] = ( $left <= $right ) ? 1 : 0; |
463 | 504 | break; |
464 | 505 | case EXPR_GREATEREQ: |
465 | | - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 506 | + if ( count( $stack ) < 2 ) { |
| 507 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 508 | + } |
466 | 509 | $right = array_pop( $stack ); |
467 | 510 | $left = array_pop( $stack ); |
468 | 511 | $stack[] = ( $left >= $right ) ? 1 : 0; |
469 | 512 | break; |
470 | 513 | case EXPR_NOTEQ: |
471 | | - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 514 | + if ( count( $stack ) < 2 ) { |
| 515 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 516 | + } |
472 | 517 | $right = array_pop( $stack ); |
473 | 518 | $left = array_pop( $stack ); |
474 | 519 | $stack[] = ( $left != $right ) ? 1 : 0; |
475 | 520 | break; |
476 | 521 | case EXPR_EXPONENT: |
477 | | - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 522 | + if ( count( $stack ) < 2 ) { |
| 523 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 524 | + } |
478 | 525 | $right = array_pop( $stack ); |
479 | 526 | $left = array_pop( $stack ); |
480 | 527 | $stack[] = $left * pow( 10, $right ); |
481 | 528 | break; |
482 | 529 | case EXPR_SINE: |
483 | | - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 530 | + if ( count( $stack ) < 1 ) { |
| 531 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 532 | + } |
484 | 533 | $arg = array_pop( $stack ); |
485 | 534 | $stack[] = sin( $arg ); |
486 | 535 | break; |
487 | 536 | case EXPR_COSINE: |
488 | | - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 537 | + if ( count( $stack ) < 1 ) { |
| 538 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 539 | + } |
489 | 540 | $arg = array_pop( $stack ); |
490 | 541 | $stack[] = cos( $arg ); |
491 | 542 | break; |
492 | 543 | case EXPR_TANGENS: |
493 | | - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 544 | + if ( count( $stack ) < 1 ) { |
| 545 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 546 | + } |
494 | 547 | $arg = array_pop( $stack ); |
495 | 548 | $stack[] = tan( $arg ); |
496 | 549 | break; |
497 | 550 | case EXPR_ARCSINE: |
498 | | - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 551 | + if ( count( $stack ) < 1 ) { |
| 552 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 553 | + } |
499 | 554 | $arg = array_pop( $stack ); |
500 | | - if ( $arg < -1 || $arg > 1 ) throw new ExprError( 'invalid_argument', $this->names[$op] ); |
| 555 | + if ( $arg < -1 || $arg > 1 ) { |
| 556 | + throw new ExprError( 'invalid_argument', $this->names[$op] ); |
| 557 | + } |
501 | 558 | $stack[] = asin( $arg ); |
502 | 559 | break; |
503 | 560 | case EXPR_ARCCOS: |
504 | | - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 561 | + if ( count( $stack ) < 1 ) { |
| 562 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 563 | + } |
505 | 564 | $arg = array_pop( $stack ); |
506 | | - if ( $arg < -1 || $arg > 1 ) throw new ExprError( 'invalid_argument', $this->names[$op] ); |
| 565 | + if ( $arg < -1 || $arg > 1 ) { |
| 566 | + throw new ExprError( 'invalid_argument', $this->names[$op] ); |
| 567 | + } |
507 | 568 | $stack[] = acos( $arg ); |
508 | 569 | break; |
509 | 570 | case EXPR_ARCTAN: |
510 | | - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 571 | + if ( count( $stack ) < 1 ) { |
| 572 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 573 | + } |
511 | 574 | $arg = array_pop( $stack ); |
512 | 575 | $stack[] = atan( $arg ); |
513 | 576 | break; |
514 | 577 | case EXPR_EXP: |
515 | | - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 578 | + if ( count( $stack ) < 1 ) { |
| 579 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 580 | + } |
516 | 581 | $arg = array_pop( $stack ); |
517 | 582 | $stack[] = exp( $arg ); |
518 | 583 | break; |
519 | 584 | case EXPR_LN: |
520 | | - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 585 | + if ( count( $stack ) < 1 ) { |
| 586 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 587 | + } |
521 | 588 | $arg = array_pop( $stack ); |
522 | | - if ( $arg <= 0 ) throw new ExprError( 'invalid_argument_ln', $this->names[$op] ); |
| 589 | + if ( $arg <= 0 ) { |
| 590 | + throw new ExprError( 'invalid_argument_ln', $this->names[$op] ); |
| 591 | + } |
523 | 592 | $stack[] = log( $arg ); |
524 | 593 | break; |
525 | 594 | case EXPR_ABS: |
526 | | - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 595 | + if ( count( $stack ) < 1 ) { |
| 596 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 597 | + } |
527 | 598 | $arg = array_pop( $stack ); |
528 | 599 | $stack[] = abs( $arg ); |
529 | 600 | break; |
530 | 601 | case EXPR_FLOOR: |
531 | | - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 602 | + if ( count( $stack ) < 1 ) { |
| 603 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 604 | + } |
532 | 605 | $arg = array_pop( $stack ); |
533 | 606 | $stack[] = floor( $arg ); |
534 | 607 | break; |
535 | 608 | case EXPR_TRUNC: |
536 | | - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 609 | + if ( count( $stack ) < 1 ) { |
| 610 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 611 | + } |
537 | 612 | $arg = array_pop( $stack ); |
538 | 613 | $stack[] = (int)$arg; |
539 | 614 | break; |
540 | 615 | case EXPR_CEIL: |
541 | | - if ( count( $stack ) < 1 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 616 | + if ( count( $stack ) < 1 ) { |
| 617 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 618 | + } |
542 | 619 | $arg = array_pop( $stack ); |
543 | 620 | $stack[] = ceil( $arg ); |
544 | 621 | break; |
545 | 622 | case EXPR_POW: |
546 | | - if ( count( $stack ) < 2 ) throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 623 | + if ( count( $stack ) < 2 ) { |
| 624 | + throw new ExprError( 'missing_operand', $this->names[$op] ); |
| 625 | + } |
547 | 626 | $right = array_pop( $stack ); |
548 | 627 | $left = array_pop( $stack ); |
549 | | - if ( false === ( $stack[] = pow( $left, $right ) ) ) throw new ExprError( 'division_by_zero', $this->names[$op] ); |
| 628 | + if ( false === ( $stack[] = pow( $left, $right ) ) ) { |
| 629 | + throw new ExprError( 'division_by_zero', $this->names[$op] ); |
| 630 | + } |
550 | 631 | break; |
551 | 632 | default: |
552 | 633 | // Should be impossible to reach here. |
Index: trunk/extensions/ParserFunctions/Convert.php |
— | — | @@ -54,6 +54,8 @@ |
55 | 55 | # The last value converted, which will be used for PLURAL evaluation |
56 | 56 | protected $lastValue; |
57 | 57 | |
| 58 | + protected $precision; |
| 59 | + |
58 | 60 | /** |
59 | 61 | * Reset the parser so it isn't contaminated by the results of previous parses |
60 | 62 | */ |