• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Freegle / iznik-server / #2450

25 Nov 2025 12:36PM UTC coverage: 90.579%. Remained the same
#2450

push

php-coveralls

edwh
Merge remote-tracking branch 'origin/master'

6 of 10 new or added lines in 3 files covered. (60.0%)

94 existing lines in 2 files now uncovered.

26335 of 29074 relevant lines covered (90.58%)

31.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.11
/include/message/Item.php
1
<?php
2
namespace Freegle\Iznik;
3

4

5

6
class Item extends Entity
7
{
8
    /** @var  $dbhm LoggedPDO */
9
    var $publicatts = array('id', 'name', 'popularity', 'weight', 'updated', 'suggestfromphoto', 'suggestfromtypeahead');
10
    var $settableatts = array('name', 'popularity', 'weight');
11

12
    /** @var  $log Log */
13
    private $log;
14

15
    function __construct(LoggedPDO $dbhr, LoggedPDO $dbhm, $id = NULL)
16
    {
17
        $this->fetch($dbhr, $dbhm, $id, 'items', 'item', $this->publicatts);
158✔
18
        $this->s = new Search($dbhr, $dbhm, 'items_index', 'itemid', 'popularity', 'words', 'categoryid', NULL, 'words_cache');
158✔
19
    }
20

21
    /**
22
     * @param LoggedPDO $dbhm
23
     */
24
    public function setDbhm($dbhm)
25
    {
26
        $this->dbhm = $dbhm;
1✔
27
    }
28

29
    public function create($name) {
30
        if (!$name || trim($name) === '') {
65✔
NEW
31
            return NULL;
×
32
        }
33

34
        $name = trim($name);
65✔
35

36
        try {
37
            # If we have a dup, update the name.  This is because the unique index is case insensitive, and it might be
38
            # that someone is correcting the case.
39
            $rc = $this->dbhm->preExec("INSERT INTO items (name) VALUES (?) ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), name = ?;", [ $name, $name ]);
65✔
40
            $id = $this->dbhm->lastInsertId();
64✔
41
        } catch (\Exception $e) {
1✔
42
            $id = NULL;
1✔
43
            $rc = 0;
1✔
44
        }
45

46
        if ($rc && $id) {
65✔
47
            $this->fetch($this->dbhm, $this->dbhm, $id, 'items', 'item', $this->publicatts);
64✔
48

49
            # Add into the search index.
50
            $this->index();
64✔
51

52
            # Create a weight estimate for this.
53
            $weight = $this->estimateWeight();
64✔
54
            $this->setWeight($weight);
64✔
55

56
            return($id);
64✔
57
        } else {
58
            return(NULL);
1✔
59
        }
60
    }
61

62
    public function index() {
63
        if ($this->id) {
64✔
64
            $this->s->delete($this->id);
64✔
65
            $this->s->add($this->id, $this->item['name'], $this->item['popularity'], NULL);
64✔
66
        }
67
    }
68

69
    public function typeahead($query, $minpop = 5) {
70
        $ctx = NULL;
2✔
71

72
        # Only look for exact words to make typeahead fast.
73
        $results = $this->s->search($query, $ctx, 10, NULL, NULL, TRUE, $minpop);
2✔
74
        foreach ($results as &$result) {
2✔
75
            $i = new Item($this->dbhr, $this->dbhm, $result['id']);
1✔
76

77
            if ($i->getPrivate('suggestfromtypeahead')) {
1✔
78
                $result['item'] = $i->getPublic();
1✔
79
            }
80
        }
81
        return($results);
2✔
82
    }
83

84
    public function estimateWeight() {
85
        # We scan the standard weights, looking for the entry with the most words in common with this one.
86
        $name = $this->item['name'];
64✔
87

88
        $weights = $this->dbhr->preQuery("SELECT CASE WHEN simplename IS NOT NULL THEN simplename ELSE name END AS name, weight FROM weights");
64✔
89
        $bestweight = NULL;
64✔
90
        $bestwic = NULL;
64✔
91
        $bestname = NULL;
64✔
92

93
        foreach ($weights as $weight) {
64✔
94
            $wic = Utils::wordsInCommon($name, $weight['name']);
64✔
95

96
            #error_log("$name vs {$weight['name']} = $wic");
97
            if (is_null($bestwic) || $wic > $bestwic) {
64✔
98
                $bestweight = $weight['weight'];
64✔
99
                $bestwic = $wic;
64✔
100
                $bestname = $weight['name'];
64✔
101
            }
102
        }
103

104
        $bestweight = $bestwic > 10 ? $bestweight : NULL;
64✔
105

106
        return($bestweight);
64✔
107
    }
108

109
    public function setWeight($weight) {
110
        if ($this->id) {
64✔
111
            $this->dbhm->preExec("UPDATE items SET weight = ? WHERE id = ?;", [
64✔
112
                $weight,
64✔
113
                $this->id
64✔
114
            ]);
64✔
115
        }
116
    }
117

118
    public function findByName($query) {
119
        $items = $this->dbhr->preQuery("SELECT * FROM items WHERE name LIKE ? AND suggestfromphoto = 1 ORDER BY popularity DESC limit 1;", [ $query ]);
121✔
120
        return($items);
121✔
121
    }
122

123
    public function delete() {
124
        $rc = FALSE;
1✔
125
        if ($this->id) {
1✔
126
            # Remove from the search index.
127
            $this->s->delete($this->id);
1✔
128

129
            $rc = $this->dbhm->preExec("DELETE FROM items WHERE id = ?;", [$this->id]);
1✔
130
        }
131

132
        return($rc);
1✔
133
    }
134
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc