| Conditions | 14 |
| Paths | 387 |
| Total Lines | 136 |
| Code Lines | 77 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 31 | public function indexDocument(CDocument $document): ?int |
||
| 32 | { |
||
| 33 | $resourceNode = $document->getResourceNode(); |
||
| 34 | |||
| 35 | error_log('[Xapian] indexDocument: start for iid='.(string) $document->getIid() |
||
| 36 | .', resource_node_id='.($resourceNode ? $resourceNode->getId() : 'null') |
||
| 37 | .', filetype='.$document->getFiletype() |
||
| 38 | ); |
||
| 39 | |||
| 40 | // 1) Check if search is globally enabled |
||
| 41 | $enabled = (string) $this->settingsManager->getSetting('search.search_enabled', true); |
||
| 42 | error_log('[Xapian] indexDocument: search.search_enabled='.var_export($enabled, true)); |
||
| 43 | |||
| 44 | if ($enabled !== 'true') { |
||
| 45 | error_log('[Xapian] indexDocument: search is disabled, skipping indexing'); |
||
| 46 | |||
| 47 | return null; |
||
| 48 | } |
||
| 49 | |||
| 50 | if (!$resourceNode instanceof ResourceNode) { |
||
| 51 | error_log('[Xapian] indexDocument: missing ResourceNode, skipping'); |
||
| 52 | |||
| 53 | return null; |
||
| 54 | } |
||
| 55 | |||
| 56 | // Do not index folders |
||
| 57 | if ($document->getFiletype() === 'folder') { |
||
| 58 | error_log('[Xapian] indexDocument: skipping folder document, resource_node_id=' |
||
| 59 | .$resourceNode->getId() |
||
| 60 | ); |
||
| 61 | |||
| 62 | return null; |
||
| 63 | } |
||
| 64 | |||
| 65 | // 2) Resolve course, session and course root node ids |
||
| 66 | [$courseId, $sessionId, $courseRootNodeId] = $this->resolveCourseSessionAndRootNode($resourceNode); |
||
| 67 | |||
| 68 | error_log('[Xapian] indexDocument: courseId='.var_export($courseId, true) |
||
| 69 | .', sessionId='.var_export($sessionId, true) |
||
| 70 | .', courseRootNodeId='.var_export($courseRootNodeId, true) |
||
| 71 | ); |
||
| 72 | |||
| 73 | // 3) Get textual content if any |
||
| 74 | $content = (string) ($resourceNode->getContent() ?? ''); |
||
| 75 | error_log('[Xapian] indexDocument: content_length='.strlen($content)); |
||
| 76 | |||
| 77 | // 4) Build fields payload |
||
| 78 | $fields = [ |
||
| 79 | 'title' => (string) $document->getTitle(), |
||
| 80 | 'description' => (string) ($document->getComment() ?? ''), |
||
| 81 | 'content' => $content, |
||
| 82 | 'filetype' => (string) $document->getFiletype(), |
||
| 83 | 'resource_node_id' => (string) $resourceNode->getId(), |
||
| 84 | 'course_id' => $courseId !== null ? (string) $courseId : '', |
||
| 85 | 'session_id' => $sessionId !== null ? (string) $sessionId : '', |
||
| 86 | 'course_root_node_id'=> $courseRootNodeId !== null ? (string) $courseRootNodeId : '', |
||
| 87 | 'full_path' => $document->getFullPath(), |
||
| 88 | ]; |
||
| 89 | |||
| 90 | // 5) Base terms |
||
| 91 | $terms = ['Tdocument']; |
||
| 92 | |||
| 93 | if ($courseId !== null) { |
||
| 94 | $terms[] = 'C'.$courseId; |
||
| 95 | } |
||
| 96 | if ($sessionId !== null) { |
||
| 97 | $terms[] = 'S'.$sessionId; |
||
| 98 | } |
||
| 99 | |||
| 100 | // 6) Extra prefilter terms from config |
||
| 101 | $this->applyPrefilterConfigToTerms($terms, $courseId, $sessionId, $document); |
||
| 102 | |||
| 103 | error_log('[Xapian] indexDocument: terms='.json_encode($terms)); |
||
| 104 | |||
| 105 | // 7) Existing mapping? |
||
| 106 | /** @var SearchEngineRef|null $existingRef */ |
||
| 107 | $existingRef = $this->em |
||
| 108 | ->getRepository(SearchEngineRef::class) |
||
| 109 | ->findOneBy(['resourceNodeId' => $resourceNode->getId()]); |
||
| 110 | |||
| 111 | $existingDocId = $existingRef?->getSearchDid(); |
||
| 112 | error_log('[Xapian] indexDocument: existing SearchEngineRef id=' |
||
| 113 | .($existingRef?->getId() ?? 'null') |
||
| 114 | .', existing_did='.var_export($existingDocId, true) |
||
| 115 | ); |
||
| 116 | |||
| 117 | // 7.1) If we already had a doc in Xapian, try to delete it first |
||
| 118 | if ($existingDocId !== null) { |
||
| 119 | try { |
||
| 120 | $this->xapianIndexService->deleteDocument($existingDocId); |
||
| 121 | error_log('[Xapian] indexDocument: previous docId deleted=' |
||
| 122 | .var_export($existingDocId, true) |
||
| 123 | ); |
||
| 124 | } catch (\Throwable $e) { |
||
| 125 | error_log('[Xapian] indexDocument: failed to delete previous docId=' |
||
| 126 | .var_export($existingDocId, true) |
||
| 127 | .' error='.$e->getMessage() |
||
| 128 | ); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | // 8) Call Xapian (create new document) |
||
| 133 | try { |
||
| 134 | $docId = $this->xapianIndexService->indexDocument( |
||
| 135 | $fields, |
||
| 136 | $terms |
||
| 137 | ); |
||
| 138 | } catch (\Throwable $e) { |
||
| 139 | error_log('[Xapian] indexDocument: indexDocument() failed: '.$e->getMessage()); |
||
| 140 | |||
| 141 | return null; |
||
| 142 | } |
||
| 143 | |||
| 144 | error_log('[Xapian] indexDocument: XapianIndexService->indexDocument returned docId=' |
||
| 145 | .var_export($docId, true) |
||
| 146 | ); |
||
| 147 | |||
| 148 | // 9) Persist mapping resource_node_id <-> search_did |
||
| 149 | if ($existingRef instanceof SearchEngineRef) { |
||
| 150 | $existingRef->setSearchDid($docId); |
||
| 151 | error_log('[Xapian] indexDocument: updating existing SearchEngineRef id='.$existingRef->getId()); |
||
| 152 | } else { |
||
| 153 | $existingRef = new SearchEngineRef(); |
||
| 154 | $existingRef->setResourceNodeId((int) $resourceNode->getId()); |
||
| 155 | $existingRef->setSearchDid($docId); |
||
| 156 | $this->em->persist($existingRef); |
||
| 157 | error_log('[Xapian] indexDocument: creating new SearchEngineRef for resource_node_id=' |
||
| 158 | .$resourceNode->getId() |
||
| 159 | ); |
||
| 160 | } |
||
| 161 | |||
| 162 | $this->em->flush(); |
||
| 163 | |||
| 164 | error_log('[Xapian] indexDocument: SearchEngineRef saved with id='.$existingRef->getId()); |
||
| 165 | |||
| 166 | return $docId; |
||
| 167 | } |
||
| 318 |