| | 576 | |
| | 577 | /** |
| | 578 | * Parses a search string for status, type, author, and tag keywords. Returns |
| | 579 | * an associative array which can be passed to Comments::get(). If multiple |
| | 580 | * authors, statuses, or types are specified, we assume an implicit OR |
| | 581 | * such that (e.g.) any author that matches would be returned. |
| | 582 | * |
| | 583 | * @param string $search_string The search string |
| | 584 | * @return array An associative array which can be passed to Comments::get() |
| | 585 | */ |
| | 586 | public static function search_to_get( $search_string ) { |
| | 587 | $keywords= array('author' => 1, 'status' => 1, 'type' => 1 ); |
| | 588 | // Comments::list_comment_statuses and list_comment_types return associative arrays with key/values |
| | 589 | // in the opposite order of the equivalent functions in Posts. Maybe we should change this? |
| | 590 | // In any case, we need to flip them for our purposes |
| | 591 | $statuses= array_flip( Comment::list_comment_statuses() ); |
| | 592 | $types= array_flip( Comment::list_comment_types() ); |
| | 593 | $arguments= array( |
| | 594 | 'name' => array(), |
| | 595 | 'status' => array(), |
| | 596 | 'type' => array() |
| | 597 | ); |
| | 598 | $criteria= ''; |
| | 599 | |
| | 600 | $tokens= explode( ' ', $search_string ); |
| | 601 | |
| | 602 | foreach( $tokens as $token ) { |
| | 603 | // check for a keyword:value pair |
| | 604 | if ( preg_match( '/^\w+:\S+$/', $token ) ) { |
| | 605 | list( $keyword, $value )= explode( ':', $token ); |
| | 606 | |
| | 607 | $keyword= strtolower( $keyword ); |
| | 608 | switch ( $keyword ) { |
| | 609 | case 'author': |
| | 610 | $arguments['name'][]= $value; |
| | 611 | break; |
| | 612 | case 'status': |
| | 613 | if ( isset( $statuses[ucfirst( $value )] ) ) { |
| | 614 | $arguments['status'][]= (int) $statuses[ucfirst( $value )]; |
| | 615 | } |
| | 616 | break; |
| | 617 | case 'type': |
| | 618 | if ( isset( $types[ucfirst( $value )] ) ) { |
| | 619 | $arguments['type'][]= (int) $types[ucfirst( $value )]; |
| | 620 | } |
| | 621 | break; |
| | 622 | } |
| | 623 | } |
| | 624 | else { |
| | 625 | $criteria .= $token . ' '; |
| | 626 | } |
| | 627 | } |
| | 628 | // flatten keys that have single-element or no-element arrays |
| | 629 | foreach ( $arguments as $key => $arg ) { |
| | 630 | switch ( count( $arg ) ) { |
| | 631 | case 0: |
| | 632 | unset( $arguments[$key] ); |
| | 633 | break; |
| | 634 | case 1: |
| | 635 | $arguments[$key]= $arg[0]; |
| | 636 | break; |
| | 637 | } |
| | 638 | } |
| | 639 | |
| | 640 | if ( $criteria != '' ) { |
| | 641 | $arguments['criteria']= $criteria; |
| | 642 | } |
| | 643 | |
| | 644 | return $arguments; |
| | 645 | |
| | 646 | } |