Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / search / ngrams / PhabricatorSearchNgrams.php
blob96dfa40c0636fce4e5243aba1ed670501b724884
1 <?php
3 abstract class PhabricatorSearchNgrams
4 extends PhabricatorSearchDAO {
6 protected $objectID;
7 protected $ngram;
9 private $value;
10 private $ngramEngine;
12 abstract public function getNgramKey();
13 abstract public function getColumnName();
15 final public function setValue($value) {
16 $this->value = $value;
17 return $this;
20 final public function getValue() {
21 return $this->value;
24 protected function getConfiguration() {
25 return array(
26 self::CONFIG_TIMESTAMPS => false,
27 self::CONFIG_COLUMN_SCHEMA => array(
28 'objectID' => 'uint32',
29 'ngram' => 'char3',
31 self::CONFIG_KEY_SCHEMA => array(
32 'key_ngram' => array(
33 'columns' => array('ngram', 'objectID'),
35 'key_object' => array(
36 'columns' => array('objectID'),
39 ) + parent::getConfiguration();
42 public function getTableName() {
43 $application = $this->getApplicationName();
44 $key = $this->getNgramKey();
45 return "{$application}_{$key}_ngrams";
48 final public function writeNgram($object_id) {
49 $ngram_engine = $this->getNgramEngine();
50 $ngrams = $ngram_engine->getTermNgramsFromString($this->getValue());
52 $conn_w = $this->establishConnection('w');
54 $sql = array();
55 foreach ($ngrams as $ngram) {
56 $sql[] = qsprintf(
57 $conn_w,
58 '(%d, %s)',
59 $object_id,
60 $ngram);
63 queryfx(
64 $conn_w,
65 'DELETE FROM %T WHERE objectID = %d',
66 $this->getTableName(),
67 $object_id);
69 if ($sql) {
70 queryfx(
71 $conn_w,
72 'INSERT INTO %T (objectID, ngram) VALUES %LQ',
73 $this->getTableName(),
74 $sql);
77 return $this;
80 private function getNgramEngine() {
81 if (!$this->ngramEngine) {
82 $this->ngramEngine = new PhabricatorSearchNgramEngine();
85 return $this->ngramEngine;