| Index: trunk/extensions/HashTables/HashTables.php |
| — | — | @@ -469,8 +469,8 @@ |
| 470 | 470 | * Syntax: |
| 471 | 471 | * {{#hashtoarray:valArrayID |hashID |keyArrayID}} |
| 472 | 472 | */ |
| 473 | | - static function pf_hashtoarray( Parser &$parser, $valArrayId, $hashId, $keyArrayId = null) { |
| 474 | | - if( ! isset( $hashId ) || ! isset( $valArrayId ) ) { |
| | 473 | + static function pf_hashtoarray( Parser &$parser, $valArrayId, $hashId = null, $keyArrayId = null) { |
| | 474 | + if( ! isset( $hashId ) ) { |
| 475 | 475 | return ''; |
| 476 | 476 | } |
| 477 | 477 | |
| — | — | @@ -492,10 +492,10 @@ |
| 493 | 493 | } |
| 494 | 494 | } |
| 495 | 495 | |
| 496 | | - $wgArrayExtension->mArrayExtension[ trim( $valArrayId ) ] = $valArray; |
| | 496 | + $wgArrayExtension->mArrayExtension[ $valArrayId ] = $valArray; |
| 497 | 497 | if( $keyArrayId !== null ) { |
| 498 | 498 | // additional array for hash keys: |
| 499 | | - $wgArrayExtension->mArrayExtension[ trim( $keyArrayId ) ] = $keyArray; |
| | 499 | + $wgArrayExtension->mArrayExtension[ $keyArrayId ] = $keyArray; |
| 500 | 500 | } |
| 501 | 501 | return ''; |
| 502 | 502 | } |
| — | — | @@ -508,8 +508,9 @@ |
| 509 | 509 | * The 'keysArrayID' is optional. If set the items in this array will end up as keys in |
| 510 | 510 | * the new hash table. |
| 511 | 511 | */ |
| 512 | | - static function pf_arraytohash( Parser &$parser, $hashId, $valArrId, $keyArrId = null) { |
| 513 | | - if( ! isset( $hashId) ) { |
| | 512 | + static function pf_arraytohash( Parser &$parser, $hashId, $valArrId = null, $keyArrId = null) { |
| | 513 | + if( $valArrId === null ) { |
| | 514 | + self::get( $parser )->setHash( $hashId ); |
| 514 | 515 | return ''; |
| 515 | 516 | } |
| 516 | 517 | |
| — | — | @@ -524,7 +525,7 @@ |
| 525 | 526 | $newHash = array(); |
| 526 | 527 | |
| 527 | 528 | // if no key array is given OR the key array doesn't exist |
| 528 | | - if( ! isset($keyArrId) || ! array_key_exists( $keyArrId, $wgArrayExtension->mArrayExtension ) ) |
| | 529 | + if( $keyArrId === null || ! array_key_exists( $keyArrId, $wgArrayExtension->mArrayExtension ) ) |
| 529 | 530 | { |
| 530 | 531 | // Copy the whole array. Result will be a hash with numeric keys |
| 531 | 532 | $newHash = $arrExtValArray; |
| — | — | @@ -534,13 +535,14 @@ |
| 535 | 536 | $keyArray = $wgArrayExtension->mArrayExtension[ $keyArrId ]; |
| 536 | 537 | $valArray = $arrExtValArray; |
| 537 | 538 | |
| 538 | | - for( $i=0; $i < count($keyArray); $i++ ) { |
| | 539 | + for( $i=0; $i < count( $keyArray ); $i++ ) { |
| 539 | 540 | $currVal = array_key_exists( $i, $valArray ) ? trim( $valArray[ $i ] ) : ''; |
| 540 | | - $newHash[ $keyArray[ $i ] ] = $currVal; |
| | 541 | + $currKey = trim( $keyArray[ $i ] ); |
| | 542 | + $newHash[ $currKey ] = $currVal; |
| 541 | 543 | } |
| 542 | 544 | } |
| 543 | | - |
| 544 | | - self::get( $parser )->mHashTables[ $hashId ] = $newHash; |
| | 545 | + |
| | 546 | + self::get( $parser )->setHash( $hashId, $newHash ); |
| 545 | 547 | return ''; |
| 546 | 548 | } |
| 547 | 549 | |
| — | — | @@ -619,8 +621,9 @@ |
| 620 | 622 | } |
| 621 | 623 | $argHashId = trim( $frame->expand( $args[ $i ] ) ); |
| 622 | 624 | |
| | 625 | + // ignore all tables which do not exist |
| 623 | 626 | if( $this->hashExists( $argHashId ) ) { |
| 624 | | - $argHash = $this->mHashTables[ $argHashId ]; |
| | 627 | + $argHash = $this->getHash( $argHashId ); |
| 625 | 628 | if( $lastHash === null ) { |
| 626 | 629 | // first valid hash table, process together with second... |
| 627 | 630 | $lastHash = $argHash; |
| — | — | @@ -642,8 +645,8 @@ |
| 643 | 646 | if( $operationRan == false && $runFuncOnSingleHash ) { |
| 644 | 647 | $lastHash = $this->{ $operationFunc }( $lastHash ); |
| 645 | 648 | } |
| 646 | | - |
| 647 | | - $this->mHashTables[ $finalHashId ] = $lastHash; |
| | 649 | + |
| | 650 | + $this->setHash( $finalHashId, $lastHash ); |
| 648 | 651 | } |
| 649 | 652 | |
| 650 | 653 | |
| — | — | @@ -718,12 +721,19 @@ |
| 719 | 722 | * @param string $hashId |
| 720 | 723 | * @param array $hashTable |
| 721 | 724 | */ |
| 722 | | - public function setHash( $hashId, array $hashTable = array() ) { |
| | 725 | + public function createHash( $hashId, array $hashTable = array() ) { |
| 723 | 726 | $hashTable = array_map( 'trim', $hashTable ); // make it all string and trim |
| 724 | 727 | $this->mHashTables[ trim( $hashId ) ] = $hashTable; |
| 725 | 728 | } |
| 726 | 729 | |
| 727 | 730 | /** |
| | 731 | + * Same as public setHash() but without sanitizing the input array first (faster). |
| | 732 | + */ |
| | 733 | + protected function setHash( $hashId, array $hashTable = array() ) { |
| | 734 | + $this->mHashTables[ trim( $hashId ) ] = $hashTable; |
| | 735 | + } |
| | 736 | + |
| | 737 | + /** |
| 728 | 738 | * Returns a value within a hash. If key or hash do not exist, this will return null |
| 729 | 739 | * or another predefined default. |
| 730 | 740 | * |