1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mQueue\Model; |
4
|
|
|
|
5
|
|
|
use Zend_Date; |
6
|
|
|
use Zend_Db_Expr; |
7
|
|
|
|
8
|
|
|
abstract class MovieMapper extends AbstractMapper |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Returns a movie by its ID |
12
|
|
|
* @param int $id |
13
|
|
|
* @return \mQueue\Model\Movie|null |
14
|
|
|
*/ |
15
|
16 |
|
public static function find($id) |
16
|
|
|
{ |
17
|
16 |
|
$result = self::getDbTable()->find($id); |
18
|
|
|
|
19
|
16 |
|
return $result->current(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Returns all movies |
24
|
|
|
* @return \mQueue\Model\Movie[] |
25
|
|
|
*/ |
26
|
|
|
public static function fetchAll() |
27
|
|
|
{ |
28
|
|
|
$resultSet = self::getDbTable()->fetchAll(); |
29
|
|
|
|
30
|
|
|
return $resultSet; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Returns movies for search |
35
|
|
|
* @return \mQueue\Model\Movie[] |
36
|
|
|
*/ |
37
|
|
|
public static function findAllForSearch() |
38
|
|
|
{ |
39
|
|
|
$futureYears = []; |
40
|
|
|
$date = Zend_Date::now(); |
41
|
|
|
for ($i = 0; $i < 10; ++$i) { |
42
|
|
|
$date->addYear(1); |
43
|
|
|
$futureYears[] = $date->get(Zend_Date::YEAR_8601); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$select = self::getDbTable()->select()->setIntegrityCheck(false) |
47
|
|
|
->from('movie') |
48
|
|
|
->join('status', 'status.idMovie = movie.id AND status.isLatest AND rating = ' . Status::Need, []) |
49
|
|
|
->where('source IS NULL') |
50
|
|
|
->where('dateSearch IS NULL OR dateSearch < DATE_SUB(NOW(), INTERVAL searchCount MONTH)') // Search for same movie with an incrementally longer interval (1 month, 2 month, 3 month, etc.) |
51
|
|
|
->where('dateRelease IS NOT NULL') // Movie must be released ... |
52
|
|
|
->where('dateRelease < DATE_SUB(NOW(), INTERVAL 1 MONTH)') // ...at least released one month ago, or longer |
53
|
|
|
->group('movie.id') |
54
|
|
|
->order('COUNT(movie.id) DESC') // First, order by popularity, so get the most needed first |
55
|
|
|
->order('RAND() ASC') // Then, randomize a little bit so we don't always look for the same movies |
56
|
|
|
->limit(5); |
57
|
|
|
|
58
|
|
|
$records = self::getDbTable()->fetchAll($select); |
59
|
|
|
|
60
|
|
|
return $records; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns movies for data fetching |
65
|
|
|
* @param int $limit |
|
|
|
|
66
|
|
|
* @return \mQueue\Model\Movie[] |
67
|
|
|
*/ |
68
|
|
|
public static function findAllForFetching($limit = null) |
69
|
|
|
{ |
70
|
|
|
$select = self::getDbTable()->select()->setIntegrityCheck(false) |
71
|
|
|
->from('movie') |
72
|
|
|
->where('dateUpdate IS NULL OR dateUpdate < DATE_SUB(NOW(), INTERVAL 1 MONTH)') // Don't fetch data for movies, more than once a month |
73
|
|
|
->order('RAND()') // Randomize order, so we don't watch only old movies |
74
|
|
|
; |
75
|
|
|
|
76
|
|
|
if ($limit) { |
77
|
|
|
$select->limit(20); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$records = self::getDbTable()->fetchAll($select); |
81
|
|
|
|
82
|
|
|
return $records; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns a query filtered according to parameters. This query may be used with paginator. |
87
|
|
|
* @param array $filters |
88
|
|
|
* @param string $orderBy valid SQL sorting snippet |
89
|
|
|
* @return Zend_Db_Table_Select |
90
|
|
|
*/ |
91
|
1 |
|
public static function getFilteredQuery(array $filters, $orderBy) |
92
|
|
|
{ |
93
|
1 |
|
$orderBy = preg_replace('/^(status\d+)(.*)/', '\\1.rating\\2', $orderBy); |
94
|
|
|
|
95
|
1 |
|
$select = self::getDbTable()->select()->setIntegrityCheck(false) |
96
|
1 |
|
->from('movie') |
97
|
1 |
|
->order($orderBy); |
98
|
|
|
|
99
|
1 |
|
$i = 0; |
100
|
1 |
|
$maxDate = ''; |
101
|
1 |
|
$filtersDone = []; |
102
|
1 |
|
foreach ($filters as $key => $filter) { |
103
|
1 |
|
if (!is_array($filter)) { |
104
|
1 |
|
continue; |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
$filterUniqueId = $filter['user'] . $filter['status']; |
108
|
1 |
|
if (!preg_match('/^filter\d+$/', $key) || in_array($filterUniqueId, $filtersDone)) { |
109
|
|
|
continue; |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
$filtersDone [] = $filterUniqueId; |
113
|
|
|
|
114
|
1 |
|
$alias = 'status' . $i++; |
|
|
|
|
115
|
1 |
|
$allowNull = ($filter['status'] == 0 || $filter['status'] == -2 ? ' OR ' . $alias . '.idUser IS NULL' : ''); |
116
|
1 |
|
$select->joinLeft([$alias => 'status'], '(movie.id = ' . $alias . '.idMovie AND ' . $alias . '.idUser = ' . $filter['user'] . ')' . $allowNull, []); |
117
|
|
|
|
118
|
1 |
|
$select->where($alias . '.isLatest = 1 OR ' . $alias . '.isLatest IS NULL'); |
119
|
|
|
|
120
|
|
|
// Filter by status, not rated or a specific rating |
121
|
1 |
|
if ($filter['status'] >= 0 && $filter['status'] <= 5) { |
122
|
|
|
$select->where($alias . '.rating = ?' . $allowNull, $filter['status']); |
123
|
|
|
} |
124
|
|
|
// All rated |
125
|
1 |
|
elseif ($filter['status'] == -1) { |
126
|
1 |
|
$select->where($alias . '.rating <> ?' . $allowNull, 0); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// Filter by title |
130
|
1 |
|
if (isset($filter['title'])) { |
131
|
1 |
|
$title = $filter['title']; |
132
|
1 |
|
$id = Movie::extractId($title); |
133
|
1 |
|
$titles = explode(' ', trim($title)); |
134
|
1 |
|
foreach ($titles as $part) { |
135
|
1 |
|
if ($part) { |
136
|
1 |
|
$select->where('movie.title LIKE ? OR movie.id = "' . $id . '"', '%' . $part . '%'); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
// Filter by presence of source |
142
|
1 |
|
if (isset($filter['withSource']) && $filter['withSource']) { |
143
|
|
|
$select->where('movie.source IS NOT NULL'); |
144
|
|
|
} |
145
|
|
|
|
146
|
1 |
|
if ($maxDate) { |
147
|
|
|
$maxDate = 'IF(`' . $alias . '`.`dateUpdate` IS NULL OR `' . $alias . '`.`dateUpdate` < ' . $maxDate . ', ' . $maxDate . ', `' . $alias . '`.`dateUpdate`)'; |
148
|
|
|
} else { |
149
|
1 |
|
$maxDate = '`' . $alias . '`.`dateUpdate`'; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
1 |
|
$select->columns(['date' => new Zend_Db_Expr($maxDate)]); |
154
|
|
|
|
155
|
1 |
|
return $select; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Delete obsolete sources for all movies. |
160
|
|
|
* An obsolete source is either a source older than 3 months, or |
161
|
|
|
* a source which is not needed anymore (nobody need the movie anymore) |
162
|
|
|
*/ |
163
|
|
|
public static function deleteObsoleteSources() |
164
|
|
|
{ |
165
|
|
|
$db = self::getDbTable()->getAdapter(); |
|
|
|
|
166
|
|
|
$update = 'UPDATE `movie` SET dateUpdate = dateUpdate, dateSearch = NULL, searchCount = 0, identity = 0, quality = 0, score = 0, source = NULL'; |
167
|
|
|
|
168
|
|
|
// Delete sources older than 6 months |
169
|
|
|
$db->query($update . ' WHERE `source` IS NOT NULL AND `dateSearch` < DATE_SUB(NOW(), INTERVAL 6 MONTH)'); |
170
|
|
|
|
171
|
|
|
// Delete non-needed sources |
172
|
|
|
$db->query($update . ' WHERE `movie`.`id` NOT IN (SELECT `status`.`idMovie` FROM `status` WHERE `rating` = ? AND `isLatest`)', [Status::Need]); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.