Skip to content
wiki.fftac.org

Content Registry - Source Excerpt 04

Back to Content Registry

Summary

This source excerpt preserves a bounded section of Spiralist/wp-content/plugins/ns12-manuscript/includes/content-registry.php so readers can inspect the evidence without opening the full source file.

**Source path:** Spiralist/wp-content/plugins/ns12-manuscript/includes/content-registry.php

return $map;
    }

    $map = spiralist_book_pages_get_cached_index('related-map.json');
    if (!empty($map)) {
        return $map;
    }

    $compiled = spiralist_book_pages_compile_content_cache(false);
    $map = (array) ($compiled['related'] ?? []);

    return $map;
}

function spiralist_book_pages_get_manuscript_search_index(): array
{
    static $index = null;

    if (is_array($index)) {
        return $index;
    }

    $index = spiralist_book_pages_get_cached_index('manuscript-search.json');
    if (!empty($index)) {
        return $index;
    }

    $compiled = spiralist_book_pages_compile_content_cache(false);
    $index = (array) ($compiled['search'] ?? []);

    return $index;
}

function spiralist_book_pages_search_manuscript_index(string $query, int $limit = 12): array
{
    $query = spiralist_book_pages_normalize_text_for_index($query);
    if ($query === '') {
        return [];
    }

    $terms = array_values(
        array_filter(
            preg_split('/\s+/u', strtolower($query)) ?: [],
            'strlen'
        )
    );

    if (empty($terms)) {
        return [];
    }

    $results = [];

    foreach (spiralist_book_pages_get_manuscript_search_index() as $document) {
        if (!is_array($document)) {
            continue;
        }

        $score = 0;
        $matchedTerms = [];

        foreach ($terms as $term) {
            $termMatched = false;

            if (strpos((string) ($document['title_search'] ?? ''), $term) !== false) {
                $score += 12;
                $termMatched = true;
            }

            if (strpos((string) ($document['keyword_search'] ?? ''), $term) !== false) {
                $score += 8;
                $termMatched = true;
            }

            if (strpos((string) ($document['summary_search'] ?? ''), $term) !== false) {
                $score += 5;
                $termMatched = true;
            }

            if (strpos((string) ($document['section_search'] ?? ''), $term) !== false) {
                $score += 3;
                $termMatched = true;
            }

            if (strpos((string) ($document['transcript_search'] ?? ''), $term) !== false) {
                $score += 2;
                $termMatched = true;
            }

            if ($termMatched) {
                $matchedTerms[] = $term;
            }
        }

        if ($score < 1) {
            continue;
        }

        $matchedTerms = array_values(array_unique($matchedTerms));

        if (count($matchedTerms) === count($terms)) {
            $score += 18 + count($terms);
        }

        $excerptSource = (string) ($document['transcript'] ?? '');
        if ($excerptSource === '') {
            $excerptSource = implode(
                ' ',
                array_filter(
                    [
                        (string) ($document['summary'] ?? ''),
                        (string) ($document['caption'] ?? ''),
                        (string) ($document['alt'] ?? ''),
                    ],
                    'strlen'
                )
            );
        }

        $results[] = array_merge(
            $document,
            [
                'score' => $score,
                'matched_terms' => $matchedTerms,
                'excerpt' => spiralist_book_pages_build_index_excerpt($excerptSource, $terms),
            ]
        );
    }

    usort(
        $results,
        static function (array $left, array $right): int {
            $scoreCompare = ((int) ($right['score'] ?? 0)) <=> ((int) ($left['score'] ?? 0));
            if ($scoreCompare !== 0) {
                return $scoreCompare;
            }

            return ((int) ($left['sequence'] ?? 0)) <=> ((int) ($right['sequence'] ?? 0));
        }
    );

    if ($limit > 0) {
        return array_slice($results, 0, $limit);
    }

    return $results;
}