1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mQueue\Model; |
4
|
|
|
|
5
|
|
|
use DOMDocument; |
6
|
|
|
use DOMXPath; |
7
|
|
|
use Exception; |
8
|
|
|
use Zend_Date; |
9
|
|
|
use Zend_Registry; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* A movie |
13
|
|
|
*/ |
14
|
|
|
class Movie extends AbstractModel |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* All known IMDb hostnames indexed by their language |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
public static $imdbHostnames = [ |
22
|
|
|
'en' => 'www.imdb.com', |
23
|
|
|
'fr' => 'www.imdb.fr', |
24
|
|
|
'de' => 'www.imdb.de', |
25
|
|
|
'es' => 'www.imdb.es', |
26
|
|
|
'it' => 'www.imdb.it', |
27
|
|
|
'pt' => 'www.imdb.pt', |
28
|
|
|
'akas' => 'akas.imdb.com', |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Extract IMDb id from URL |
33
|
|
|
* |
34
|
|
|
* @param string $string |
35
|
|
|
* |
36
|
|
|
* @return null|string the id extracted |
37
|
|
|
*/ |
38
|
2 |
|
public static function extractId($string) |
39
|
|
|
{ |
40
|
2 |
|
preg_match_all("/(\d{7})/", $string, $r); |
|
|
|
|
41
|
2 |
|
if (isset($r[1][0])) { |
42
|
1 |
|
return $r[1][0]; |
43
|
|
|
} |
44
|
|
|
|
45
|
2 |
|
return null; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Returns the title, if needed fetch the title from IMDb |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
1 |
|
public function getTitle() |
54
|
|
|
{ |
55
|
|
|
// If we didn't get the title yet, fetch it and save in our database |
56
|
1 |
|
if (!($this->title)) { |
|
|
|
|
57
|
|
|
$this->fetchData(); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
return $this->title; |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Fetch data from IMDb and store in database (possibly overwriting) |
65
|
|
|
*/ |
66
|
|
|
public function fetchData(): void |
67
|
|
|
{ |
68
|
|
|
$ch = curl_init($this->getImdbUrl('akas')); |
69
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept-Language: en-US,en;q=0.8']); |
70
|
|
|
curl_setopt($ch, CURLOPT_HEADER, false); |
71
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
72
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
73
|
|
|
|
74
|
|
|
$file = curl_exec($ch); |
75
|
|
|
curl_close($ch); |
76
|
|
|
|
77
|
|
|
$document = new DOMDocument(); |
78
|
|
|
@$document->loadHTML($file); |
79
|
|
|
$xpath = new DOMXPath($document); |
80
|
|
|
|
81
|
|
|
// Extract title |
82
|
|
|
$titleEntries = $xpath->evaluate('//meta[contains(@property, "og:title")]/@content'); |
83
|
|
|
if ($titleEntries->length == 1) { |
84
|
|
|
$this->title = $titleEntries->item(0)->value; |
|
|
|
|
85
|
|
|
} else { |
86
|
|
|
$this->title = '[title not available, could not fetch from IMDb]'; |
|
|
|
|
87
|
|
|
|
88
|
|
|
return; // If there is not even title give up everything |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// Extract release date |
92
|
|
|
$dateReleaseEntries = $xpath->evaluate('//*[@id="overview-top"]//meta[contains(@itemprop, "datePublished")]/@content'); |
93
|
|
|
if ($dateReleaseEntries->length == 1) { |
94
|
|
|
$this->dateRelease = $dateReleaseEntries->item(0)->value; |
|
|
|
|
95
|
|
|
} else { |
96
|
|
|
$this->dateRelease = null; |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->dateUpdate = Zend_Date::now()->get(Zend_Date::ISO_8601); |
|
|
|
|
100
|
|
|
$this->setReadOnly(false); // If the movie is coming from a joined query, we need to set non-readonly before saving |
101
|
|
|
$this->save(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Sets the ID for the movie from any string containing a valid ID |
106
|
|
|
* |
107
|
|
|
* @param string $id |
108
|
|
|
* |
109
|
|
|
* @return \mQueue\Model\Movie |
110
|
|
|
*/ |
111
|
|
|
public function setId($id) |
112
|
|
|
{ |
113
|
|
|
$extractedId = self::extractId($id); |
114
|
|
|
if (!$extractedId) { |
115
|
|
|
throw new Exception(sprintf('Invalid Id for movie. Given "%1$s", extracted "%2$s"', $id, $extractedId)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$this->id = $extractedId; |
|
|
|
|
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Returns the IMDb url for the movie |
125
|
|
|
* |
126
|
|
|
* @param string $lang suggested language for hostname |
|
|
|
|
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
public function getImdbUrl($lang = null) |
131
|
|
|
{ |
132
|
|
|
if ($lang == null) { |
133
|
|
|
$lang = Zend_Registry::get('Zend_Locale')->getLanguage(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if (isset(self::$imdbHostnames[$lang])) { |
137
|
|
|
$hostname = self::$imdbHostnames[$lang]; |
138
|
|
|
} else { |
139
|
|
|
$hostname = reset(self::$imdbHostnames); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return 'http://' . $hostname . '/title/tt' . $this->id . '/'; |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Returns the status for this movie and the specified user |
147
|
|
|
* |
148
|
|
|
* @param \mQueue\Model\User $user |
|
|
|
|
149
|
|
|
* |
150
|
|
|
* @return \mQueue\Model\Status |
151
|
|
|
*/ |
152
|
|
|
public function getStatus(User $user = null) |
153
|
|
|
{ |
154
|
|
|
return StatusMapper::find($this->id, $user); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Set the status for the specified user |
159
|
|
|
* |
160
|
|
|
* @param \mQueue\Model\User $user |
161
|
|
|
* @param int $rating @see \mQueue\Model\Status |
162
|
|
|
* |
163
|
|
|
* @return \mQueue\Model\Status |
164
|
|
|
*/ |
165
|
|
|
public function setStatus(User $user, $rating) |
166
|
|
|
{ |
167
|
|
|
$status = StatusMapper::set($this, $user, $rating); |
168
|
|
|
|
169
|
|
|
return $status; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Set the source for the movie if any. In any case record the search date and count |
174
|
|
|
* |
175
|
|
|
* @param array|false $source |
176
|
|
|
*/ |
177
|
|
|
public function setSource($source): void |
178
|
|
|
{ |
179
|
|
|
$this->dateSearch = Zend_Date::now()->get(Zend_Date::ISO_8601); |
|
|
|
|
180
|
|
|
++$this->searchCount; |
|
|
|
|
181
|
|
|
if ($source && @$source['score']) { |
182
|
|
|
$this->identity = $source['identity']; |
|
|
|
|
183
|
|
|
$this->quality = $source['quality']; |
|
|
|
|
184
|
|
|
$this->score = $source['score']; |
|
|
|
|
185
|
|
|
$this->source = $source['link']; |
|
|
|
|
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.