| Conditions | 36 |
| Paths | 7789 |
| Total Lines | 286 |
| Code Lines | 182 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| Bugs | 2 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 429 | #[Override] |
||
| 430 | public function search($store, $entryid, $action, $actionType) { |
||
| 431 | $useSearchFolder = $action["use_searchfolder"] ?? false; |
||
| 432 | $this->logFtsDebug('Search requested', [ |
||
| 433 | 'store_entryid' => $action['store_entryid'] ?? null, |
||
| 434 | 'entryid' => $this->formatEntryIdForLog($entryid), |
||
| 435 | 'subfolders' => $action['subfolders'] ?? null, |
||
| 436 | 'use_searchfolder' => (bool) $useSearchFolder, |
||
| 437 | 'restriction_present' => array_key_exists('restriction', $action), |
||
| 438 | ]); |
||
| 439 | if (!$useSearchFolder) { |
||
| 440 | $this->logFtsDebug('Search fallback: store does not support search folders', []); |
||
| 441 | /* |
||
| 442 | * store doesn't support search folders so we can't use this |
||
| 443 | * method instead we will pass restriction to messageList and |
||
| 444 | * it will give us the restricted results |
||
| 445 | */ |
||
| 446 | return parent::messageList($store, $entryid, $action, "list"); |
||
| 447 | } |
||
| 448 | $store_props = mapi_getprops($store, [PR_MDB_PROVIDER, PR_DEFAULT_STORE, PR_IPM_SUBTREE_ENTRYID]); |
||
| 449 | $this->logFtsDebug('Resolved store properties for search', [ |
||
| 450 | 'provider' => isset($store_props[PR_MDB_PROVIDER]) ? bin2hex((string) $store_props[PR_MDB_PROVIDER]) : null, |
||
| 451 | 'default_store' => $store_props[PR_DEFAULT_STORE] ?? null, |
||
| 452 | ]); |
||
| 453 | if ($store_props[PR_MDB_PROVIDER] == ZARAFA_STORE_PUBLIC_GUID) { |
||
| 454 | $this->logFtsDebug('Search fallback: public store does not support search folders', []); |
||
| 455 | // public store does not support search folders |
||
| 456 | return parent::messageList($store, $entryid, $action, "search"); |
||
| 457 | } |
||
| 458 | if ($GLOBALS['entryid']->compareEntryIds(bin2hex($entryid), bin2hex(TodoList::getEntryId()))) { |
||
| 459 | $this->logFtsDebug('Search fallback: todo list uses legacy restriction path', []); |
||
| 460 | // todo list do not need to perform full text index search |
||
| 461 | return parent::messageList($store, $entryid, $action, "list"); |
||
| 462 | } |
||
| 463 | |||
| 464 | $this->searchFolderList = true; // Set to indicate this is not the normal folder, but a search folder |
||
| 465 | $this->restriction = false; |
||
| 466 | |||
| 467 | // Parse Restriction |
||
| 468 | $this->parseRestriction($action); |
||
| 469 | if ($this->restriction == false) { |
||
| 470 | $this->logFtsDebug('Restriction parsing failed', [ |
||
| 471 | 'action_type' => $actionType, |
||
| 472 | ]); |
||
| 473 | // if error in creating restriction then send error to client |
||
| 474 | $errorInfo = []; |
||
| 475 | $errorInfo["error_message"] = _("Error in search, please try again") . "."; |
||
| 476 | $errorInfo["original_error_message"] = "Error in parsing restrictions."; |
||
| 477 | |||
| 478 | return $this->sendSearchErrorToClient($store, $entryid, $action, $errorInfo); |
||
| 479 | } |
||
| 480 | $ftsDescriptor = $this->buildFtsDescriptor($this->restriction); |
||
| 481 | if (empty($ftsDescriptor['ast'])) { |
||
| 482 | $this->logFtsDebug('Failed to translate restriction to FTS descriptor', [ |
||
| 483 | 'restriction_sample' => array_keys($this->restriction), |
||
| 484 | ]); |
||
| 485 | $errorInfo = []; |
||
| 486 | $errorInfo["error_message"] = _("Error in search, please try again") . "."; |
||
| 487 | $errorInfo["original_error_message"] = "Unable to translate search query to full-text expression."; |
||
| 488 | |||
| 489 | return $this->sendSearchErrorToClient($store, $entryid, $action, $errorInfo); |
||
| 490 | } |
||
| 491 | $serializedRestriction = serialize($this->restriction); |
||
| 492 | $restrictionSignature = md5($serializedRestriction); |
||
| 493 | $this->logFtsDebug('Restriction parsed for full-text search', [ |
||
| 494 | 'restriction_signature' => $restrictionSignature, |
||
| 495 | 'fts_descriptor' => $ftsDescriptor, |
||
| 496 | ]); |
||
| 497 | |||
| 498 | $isSetSearchFolderEntryId = isset($action['search_folder_entryid']); |
||
| 499 | if ($isSetSearchFolderEntryId) { |
||
| 500 | $this->sessionData['searchFolderEntryId'] = $action['search_folder_entryid']; |
||
| 501 | } |
||
| 502 | |||
| 503 | if (isset($action['forceCreateSearchFolder']) && $action['forceCreateSearchFolder']) { |
||
| 504 | $isSetSearchFolderEntryId = false; |
||
| 505 | } |
||
| 506 | |||
| 507 | // create or open search folder |
||
| 508 | $searchFolder = $this->createSearchFolder($store, $isSetSearchFolderEntryId); |
||
| 509 | if ($searchFolder === false) { |
||
| 510 | if ($store_props[PR_MDB_PROVIDER] == ZARAFA_STORE_DELEGATE_GUID) { |
||
| 511 | $this->messageList($store, $entryid, $action, "search"); |
||
| 512 | |||
| 513 | return true; |
||
| 514 | } |
||
| 515 | // if error in creating search folder then send error to client |
||
| 516 | $errorInfo = []; |
||
| 517 | |||
| 518 | $errorInfo["error_message"] = match (mapi_last_hresult()) { |
||
| 519 | MAPI_E_NO_ACCESS => _("Unable to perform search query, no permissions to create search folder."), |
||
| 520 | MAPI_E_NOT_FOUND => _("Unable to perform search query, search folder not found."), |
||
| 521 | default => _("Unable to perform search query, store might not support searching."), |
||
| 522 | }; |
||
| 523 | |||
| 524 | $errorInfo["original_error_message"] = _("Error in creating search folder."); |
||
| 525 | |||
| 526 | return $this->sendSearchErrorToClient($store, $entryid, $action, $errorInfo); |
||
| 527 | } |
||
| 528 | |||
| 529 | $subfolder_flag = 0; |
||
| 530 | $recursive = false; |
||
| 531 | if (isset($action["subfolders"]) && $action["subfolders"] == "true") { |
||
| 532 | $recursive = true; |
||
| 533 | $subfolder_flag = RECURSIVE_SEARCH; |
||
| 534 | } |
||
| 535 | |||
| 536 | if (!is_array($entryid)) { |
||
| 537 | $entryids = [$entryid]; |
||
| 538 | } |
||
| 539 | else { |
||
| 540 | $entryids = $entryid; |
||
| 541 | } |
||
| 542 | |||
| 543 | $searchFolderEntryId = $this->sessionData['searchFolderEntryId']; |
||
| 544 | |||
| 545 | // check if searchcriteria has changed |
||
| 546 | $restrictionCheck = md5($serializedRestriction . $searchFolderEntryId . $subfolder_flag); |
||
| 547 | |||
| 548 | // check if there is need to set searchcriteria again |
||
| 549 | if (!isset($this->sessionData['searchCriteriaCheck']) || $restrictionCheck != $this->sessionData['searchCriteriaCheck']) { |
||
| 550 | if (!empty($this->sessionData['searchOriginalEntryids']) && |
||
| 551 | isset($action['entryid']) && |
||
| 552 | in_array($action['entryid'], $this->sessionData['searchOriginalEntryids']) |
||
| 553 | ) { |
||
| 554 | // get entryids of original folders, and use it to set new search criteria |
||
| 555 | $entryids = []; |
||
| 556 | $entryIdsCount = count($this->sessionData['searchOriginalEntryids']); |
||
| 557 | for ($index = 0; $index < $entryIdsCount; ++$index) { |
||
| 558 | $entryids[] = hex2bin((string) $this->sessionData['searchOriginalEntryids'][$index]); |
||
| 559 | } |
||
| 560 | } |
||
| 561 | else { |
||
| 562 | // store entryids of original folders, so that can be used for re-setting the search criteria if needed |
||
| 563 | $this->sessionData['searchOriginalEntryids'] = []; |
||
| 564 | for ($index = 0, $len = count($entryids); $index < $len; ++$index) { |
||
| 565 | $this->sessionData['searchOriginalEntryids'][] = bin2hex($entryids[$index]); |
||
| 566 | } |
||
| 567 | } |
||
| 568 | // we never start the search folder because we will populate the search folder by ourselves |
||
| 569 | mapi_folder_setsearchcriteria($searchFolder, $this->restriction, $entryids, $subfolder_flag | STOP_SEARCH); |
||
| 570 | $this->logFtsDebug('Search criteria updated', [ |
||
| 571 | 'search_folder_entryid' => $searchFolderEntryId, |
||
| 572 | 'restriction_signature' => $restrictionSignature, |
||
| 573 | 'recursive' => $recursive, |
||
| 574 | 'scope_entryids' => $this->formatEntryIdForLog($entryids), |
||
| 575 | ]); |
||
| 576 | $this->sessionData['searchCriteriaCheck'] = $restrictionCheck; |
||
| 577 | } |
||
| 578 | |||
| 579 | if (isset($this->sessionData['searchCriteriaCheck']) || $restrictionCheck == $this->sessionData['searchCriteriaCheck']) { |
||
| 580 | $folderEntryid = bin2hex($entryid); |
||
| 581 | if ($this->sessionData['searchOriginalEntryids'][0] !== $folderEntryid) { |
||
| 582 | $this->sessionData['searchOriginalEntryids'][0] = $folderEntryid; |
||
| 583 | // we never start the search folder because we will populate the search folder by ourselves |
||
| 584 | mapi_folder_setsearchcriteria($searchFolder, $this->restriction, [$entryid], $subfolder_flag | STOP_SEARCH); |
||
| 585 | $this->logFtsDebug('Search criteria refreshed for active folder', [ |
||
| 586 | 'search_folder_entryid' => $searchFolderEntryId, |
||
| 587 | 'restriction_signature' => $restrictionSignature, |
||
| 588 | 'target_entryid' => $this->formatEntryIdForLog($entryid), |
||
| 589 | 'recursive' => $recursive, |
||
| 590 | ]); |
||
| 591 | } |
||
| 592 | } |
||
| 593 | |||
| 594 | // Sort |
||
| 595 | $this->parseSortOrder($action); |
||
| 596 | // Initialize search patterns with default values |
||
| 597 | if (is_array($ftsDescriptor['message_classes']) && |
||
| 598 | count($ftsDescriptor['message_classes']) >= 7) { |
||
| 599 | $ftsDescriptor['message_classes'] = null; |
||
| 600 | } |
||
| 601 | |||
| 602 | $username = null; |
||
| 603 | if ($store_props[PR_MDB_PROVIDER] == ZARAFA_STORE_DELEGATE_GUID) { |
||
| 604 | $eidObj = $GLOBALS["entryid"]->createMsgStoreEntryIdObj(hex2bin((string) $action['store_entryid'])); |
||
| 605 | $username = $eidObj['ServerShortname']; |
||
| 606 | $session = $GLOBALS["mapisession"]->getSession(); |
||
| 607 | |||
| 608 | if ($username) { |
||
| 609 | $indexDB = new IndexSqlite($username, $session, $store); |
||
| 610 | } |
||
| 611 | } |
||
| 612 | else { |
||
| 613 | $indexDB = new IndexSqlite(); |
||
| 614 | } |
||
| 615 | $this->logFtsDebug('Dispatching search to index backend', [ |
||
| 616 | 'search_folder_entryid' => $searchFolderEntryId, |
||
| 617 | 'restriction_signature' => $restrictionSignature, |
||
| 618 | 'recursive' => $recursive, |
||
| 619 | 'delegate_username' => $username, |
||
| 620 | 'message_classes' => $ftsDescriptor['message_classes'] ?? null, |
||
| 621 | 'filters' => [ |
||
| 622 | 'date_start' => $ftsDescriptor['date_start'] ?? null, |
||
| 623 | 'date_end' => $ftsDescriptor['date_end'] ?? null, |
||
| 624 | 'unread' => !empty($ftsDescriptor['unread']), |
||
| 625 | 'has_attachments' => !empty($ftsDescriptor['has_attachments']), |
||
| 626 | ], |
||
| 627 | 'ast' => $ftsDescriptor['ast'] ?? null, |
||
| 628 | ]); |
||
| 629 | |||
| 630 | $search_result = $indexDB->search(hex2bin((string) $searchFolderEntryId), $ftsDescriptor, $entryid, $recursive); |
||
| 631 | if ($search_result == false) { |
||
| 632 | $this->logFtsDebug('Index search returned no data', [ |
||
| 633 | 'search_folder_entryid' => $searchFolderEntryId, |
||
| 634 | 'restriction_signature' => $restrictionSignature, |
||
| 635 | 'recursive' => $recursive, |
||
| 636 | ]); |
||
| 637 | $errorInfo = []; |
||
| 638 | $errorInfo["error_message"] = _("Unable to perform search query, store might not support searching."); |
||
| 639 | $errorInfo["original_error_message"] = "Full-text search backend returned no result set."; |
||
| 640 | |||
| 641 | return $this->sendSearchErrorToClient($store, $entryid, $action, $errorInfo); |
||
| 642 | } |
||
| 643 | |||
| 644 | unset($action["restriction"]); |
||
| 645 | |||
| 646 | // Get the table and merge the arrays |
||
| 647 | $table = $GLOBALS["operations"]->getTable($store, hex2bin((string) $searchFolderEntryId), $this->properties, $this->sort, $this->start); |
||
| 648 | $this->logFtsDebug('Search folder table retrieved', [ |
||
| 649 | 'search_folder_entryid' => $searchFolderEntryId, |
||
| 650 | 'table_item_count' => isset($table['item']) ? count($table['item']) : null, |
||
| 651 | 'start' => $this->start, |
||
| 652 | 'sort' => $this->sort, |
||
| 653 | ]); |
||
| 654 | // Create the data array, which will be send back to the client |
||
| 655 | $data = []; |
||
| 656 | $data = array_merge($data, $table); |
||
| 657 | |||
| 658 | $this->getDelegateFolderInfo($store); |
||
| 659 | $data = $this->filterPrivateItems($data); |
||
| 660 | $this->logFtsDebug('Search results filtered for privacy', [ |
||
| 661 | 'search_folder_entryid' => $searchFolderEntryId, |
||
| 662 | 'item_count_after_filter' => isset($data['item']) ? count($data['item']) : null, |
||
| 663 | ]); |
||
| 664 | |||
| 665 | // remember which entryid's are send to the client |
||
| 666 | $searchResults = []; |
||
| 667 | foreach ($table["item"] as $item) { |
||
| 668 | // store entryid => last_modification_time mapping |
||
| 669 | $searchResults[$item["entryid"]] = $item["props"]["last_modification_time"]; |
||
| 670 | } |
||
| 671 | |||
| 672 | // store search results into session data |
||
| 673 | if (!isset($this->sessionData['searchResults'])) { |
||
| 674 | $this->sessionData['searchResults'] = []; |
||
| 675 | } |
||
| 676 | $this->sessionData['searchResults'][$searchFolderEntryId] = $searchResults; |
||
| 677 | $this->logFtsDebug('Search results stored in session', [ |
||
| 678 | 'search_folder_entryid' => $searchFolderEntryId, |
||
| 679 | 'result_count' => count($searchResults), |
||
| 680 | ]); |
||
| 681 | |||
| 682 | $result = mapi_folder_getsearchcriteria($searchFolder); |
||
| 683 | $this->logFtsDebug('Search folder state retrieved', [ |
||
| 684 | 'search_folder_entryid' => $searchFolderEntryId, |
||
| 685 | 'searchstate' => $result['searchstate'] ?? null, |
||
| 686 | ]); |
||
| 687 | |||
| 688 | $data["search_meta"] = []; |
||
| 689 | $data["search_meta"]["searchfolder_entryid"] = $searchFolderEntryId; |
||
| 690 | $data["search_meta"]["search_store_entryid"] = $action["store_entryid"]; |
||
| 691 | $data["search_meta"]["searchstate"] = $result["searchstate"]; |
||
| 692 | $data["search_meta"]["results"] = count($searchResults); |
||
| 693 | |||
| 694 | // Reopen the search folder, because otherwise the suggestion property will |
||
| 695 | // not have been updated |
||
| 696 | $searchFolder = $this->createSearchFolder($store, true); |
||
| 697 | $storeProps = mapi_getprops($searchFolder, [PR_EC_SUGGESTION]); |
||
| 698 | if (isset($storeProps[PR_EC_SUGGESTION])) { |
||
| 699 | $data["search_meta"]["suggestion"] = $storeProps[PR_EC_SUGGESTION]; |
||
| 700 | $this->logFtsDebug('Search suggestion ready', [ |
||
| 701 | 'search_folder_entryid' => $searchFolderEntryId, |
||
| 702 | 'suggestion' => $storeProps[PR_EC_SUGGESTION], |
||
| 703 | ]); |
||
| 704 | } |
||
| 705 | |||
| 706 | $this->addActionData("search", $data); |
||
| 707 | $GLOBALS["bus"]->addData($this->getResponseData()); |
||
| 708 | |||
| 709 | $this->logFtsDebug('Search response dispatched to client', [ |
||
| 710 | 'search_folder_entryid' => $searchFolderEntryId, |
||
| 711 | 'items_returned' => isset($data['item']) ? count($data['item']) : null, |
||
| 712 | 'search_meta' => $data['search_meta'] ?? null, |
||
| 713 | ]); |
||
| 714 | return true; |
||
| 715 | } |
||
| 754 |