xref: /dokuwiki/inc/Search/Index/TupleOps.php (revision 596d5287d7a816d606ef4153ef9e0f4704bf8f73)
19bd7d62fSAndreas Gohr<?php
29bd7d62fSAndreas Gohr
39bd7d62fSAndreas Gohrnamespace dokuwiki\Search\Index;
49bd7d62fSAndreas Gohr
59bd7d62fSAndreas Gohr/**
69bd7d62fSAndreas Gohr * Provides operations on tuple records used in our indexes
79bd7d62fSAndreas Gohr *
89bd7d62fSAndreas Gohr * Tuples consist of a key (typically a RID from another Index) and a number (usually a count).
99bd7d62fSAndreas Gohr * Used to store page <-> word counts for example
109bd7d62fSAndreas Gohr */
119bd7d62fSAndreas Gohrclass TupleOps
129bd7d62fSAndreas Gohr{
139bd7d62fSAndreas Gohr    /**
149bd7d62fSAndreas Gohr     * Insert or replace a tuple in a line
159bd7d62fSAndreas Gohr     *
169bd7d62fSAndreas Gohr     * @param string $record This is the current row value to be modified
179bd7d62fSAndreas Gohr     * @param int|string $key The foreign rid or identifier
189bd7d62fSAndreas Gohr     * @param int $count The count to store
199bd7d62fSAndreas Gohr     * @return string A new row value
209bd7d62fSAndreas Gohr     * @author Tom N Harris <tnharris@whoopdedo.org>
219bd7d62fSAndreas Gohr     *
229bd7d62fSAndreas Gohr     */
239bd7d62fSAndreas Gohr    public static function updateTuple($record, $key, $count)
249bd7d62fSAndreas Gohr    {
259bd7d62fSAndreas Gohr        if ($record != '') {
269bd7d62fSAndreas Gohr            // remove any current version of the tuple
279bd7d62fSAndreas Gohr            $record = preg_replace('/(^|:)' . preg_quote($key, '/') . '\*\d*/', '', $record);
289bd7d62fSAndreas Gohr        }
299bd7d62fSAndreas Gohr        $record = trim($record, ':');
309bd7d62fSAndreas Gohr        if ($count) {
319bd7d62fSAndreas Gohr            if ($record) {
329bd7d62fSAndreas Gohr                return "{$key}*{$count}:" . $record;
339bd7d62fSAndreas Gohr            } else {
349bd7d62fSAndreas Gohr                return "{$key}*{$count}";
359bd7d62fSAndreas Gohr            }
369bd7d62fSAndreas Gohr        }
379bd7d62fSAndreas Gohr        return $record;
389bd7d62fSAndreas Gohr    }
399bd7d62fSAndreas Gohr
409bd7d62fSAndreas Gohr    /**
419bd7d62fSAndreas Gohr     * Sum the counts in a list of tuples
429bd7d62fSAndreas Gohr     *
439bd7d62fSAndreas Gohr     * @param string $record The row value to parse
449bd7d62fSAndreas Gohr     * @return int sum of all counts
459bd7d62fSAndreas Gohr     * @author Tom N Harris <tnharris@whoopdedo.org>
469bd7d62fSAndreas Gohr     */
479bd7d62fSAndreas Gohr    public static function aggregateTupleCounts($record)
489bd7d62fSAndreas Gohr    {
499bd7d62fSAndreas Gohr        $freq = 0;
509bd7d62fSAndreas Gohr        $parts = explode(':', $record);
519bd7d62fSAndreas Gohr        foreach ($parts as $tuple) {
529bd7d62fSAndreas Gohr            if ($tuple === '') continue;
539bd7d62fSAndreas Gohr            list(/* $key */, $cnt) = explode('*', $tuple);
549bd7d62fSAndreas Gohr            $freq += (int)$cnt;
559bd7d62fSAndreas Gohr        }
569bd7d62fSAndreas Gohr        return $freq;
579bd7d62fSAndreas Gohr    }
589bd7d62fSAndreas Gohr
599bd7d62fSAndreas Gohr    /**
609bd7d62fSAndreas Gohr     * Split a line into an array of tuples
619bd7d62fSAndreas Gohr     *
629bd7d62fSAndreas Gohr     * The given key of the given $filtermap defines which tuples to extract, the value
639bd7d62fSAndreas Gohr     * gives the name in the output array. This basically allows to map RIDs to their
649bd7d62fSAndreas Gohr     * respective real values. The result will contain the counts associated with the
659bd7d62fSAndreas Gohr     * mapped keys.
669bd7d62fSAndreas Gohr     *
67*596d5287SAndreas Gohr     * If no $filtermap is given (null), all tuples are returned keeping their original keys
68*596d5287SAndreas Gohr     *
699bd7d62fSAndreas Gohr     * @param string $record The row value to parse
70*596d5287SAndreas Gohr     * @param array|null $filtermap Associative array of ($key => $mapping), null for all tuples
719bd7d62fSAndreas Gohr     * @return array mapped counts
729bd7d62fSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
739bd7d62fSAndreas Gohr     * @author Tom N Harris <tnharris@whoopdedo.org>
749bd7d62fSAndreas Gohr     */
75*596d5287SAndreas Gohr    public static function parseTuples($record, $filtermap = null)
769bd7d62fSAndreas Gohr    {
779bd7d62fSAndreas Gohr        $result = array();
789bd7d62fSAndreas Gohr        if ($record == '') return $result;
799bd7d62fSAndreas Gohr        $parts = explode(':', $record);
809bd7d62fSAndreas Gohr        foreach ($parts as $tuple) {
819bd7d62fSAndreas Gohr            if ($tuple === '') continue;
829bd7d62fSAndreas Gohr            list($key, $cnt) = explode('*', $tuple);
839bd7d62fSAndreas Gohr            if (!$cnt) continue;
84*596d5287SAndreas Gohr            if (is_array($filtermap)) {
85*596d5287SAndreas Gohr                if (!isset($filtermap[$key])) continue;
869bd7d62fSAndreas Gohr                $mapped = $filtermap[$key];
87*596d5287SAndreas Gohr            } else {
88*596d5287SAndreas Gohr                $mapped = $key;
89*596d5287SAndreas Gohr            }
909bd7d62fSAndreas Gohr            $result[$mapped] = $cnt;
919bd7d62fSAndreas Gohr        }
929bd7d62fSAndreas Gohr        return $result;
939bd7d62fSAndreas Gohr    }
949bd7d62fSAndreas Gohr}
95