4 * Class Videos_model models videos information from the DB
7 * @author Călin-Andrei Burloiu
9 class Videos_model extends CI_Model {
12 public function __construct()
14 if ($this->db === NULL)
16 $this->load->library('singleton_db');
17 $this->db = $this->singleton_db->connect();
22 * Retrieves information about a set of videos which are going to be
23 * displayed in the catalog.
25 * @param int $category_id DB category ID
28 * @param string $ordering control videos ording by these
31 * <li><strong>'hottest':</strong> newest most appreciated first. An
32 * appreciated video is one which has a bigger
33 * score = views + likes - dislikes.</li>
34 * <li><strong>'newest':</strong> newest first.</li>
35 * <li><strong>'alphabetically':</strong> sort alphabetically.</li>
37 * @return array a list of videos, each one being an assoc array with:
39 * <li>id, name, title, duration, thumbs_count, default_thumb, views => from DB</li>
40 * <li>shorted_title => ellipsized title</li>
41 * <li>video_url => P2P-Tube video URl</li>
42 * <li>TODO: user_id, user_name</li>
43 * <li>thumbs => thumbnail images' URLs</li>
46 public function get_videos_summary($category_id, $offset, $count, $ordering = 'hottest')
48 $this->load->helper('text');
54 $order_statement = "ORDER BY date DESC, score DESC, RAND()";
57 $order_statement = "ORDER BY date DESC";
59 case 'alphabetically':
60 $order_statement = "ORDER BY title";
64 $order_statement = "";
67 $query = $this->db->query(
68 "SELECT id, name, title, duration, user_id, views, thumbs_count,
69 default_thumb, (views + likes - dislikes) AS score
74 array(intval($category_id), $offset, $count));
76 if ($query->num_rows() > 0)
77 $videos = $query->result_array();
81 foreach ($videos as & $video)
84 $video['video_url'] = site_url(sprintf("watch/%d/%s",
85 $video['id'], $video['name']));
88 $video['thumbs'] = $this->get_thumbs($video['name'],
89 $video['thumbs_count']);
92 //$video['shorted_title'] = ellipsize($video['title'], 45, 0.75);
93 $video['shorted_title'] = character_limiter($video['title'], 50);
95 // TODO: user information
96 $video['user_name'] = 'TODO';
102 public function get_videos_count($category_id)
104 $query = $this->db->query(
105 'SELECT COUNT(*) count
107 WHERE category_id = ?',
110 if ($query->num_rows() > 0)
111 return $query->row()->count;
118 * Retrieves information about a video.
120 * If $name does not match with the video's `name` from the DB an error is
121 * marked in the key 'err'. If it's NULL it is ignored.
124 * @param string $id video's `id` column from `videos` DB table
125 * @param string $name video's `name` column from `videos` DB
126 * table. NULL means there is no name provided.
127 * @return array an associative list with information about a video
128 * with the following keys:
130 * <li>all columns form DB with some exceptions that are overwritten or new</li>
131 * <li>content is moved in assets</li>
132 * <li>assets => list of associative lists where each one represents a</li>
133 * video asset having keys: "src", "res", "par" and "ext". Value of key
134 * "src" is the video torrent formated as
135 * {name}_{format}.{video_ext}.{default_torrent_ext}</li>
136 * <li>user_name => TODO: user name from `users` table</li>
137 * <li>category_title => a human-friendly category name</li>
138 * <li>tags => associative list of "tag => score"</li>
139 * <li>date => date and time when the video was created</li>
140 * <li>thumbs => thumbnail images' URLs</li>
143 public function get_video($id, $name = NULL)
145 $this->load->helper('video');
147 $query = $this->db->query('SELECT *
152 if ($query->num_rows() > 0)
154 $video = $query->row_array();
155 if ($name !== NULL && $video['name'] != $name)
156 $video['err'] = 'INVALID_NAME';
160 $video['err'] = 'INVALID_ID';
164 // Convert JSON encoded string to arrays.
165 $video['assets'] = json_decode($video['formats'], TRUE);
166 unset($video['formats']);
167 $video['tags'] = json_decode($video['tags'], TRUE);
168 asort($video['tags']);
169 $video['tags'] = array_reverse($video['tags'], TRUE);
171 // Sort assets by their megapixels number.
172 function access_function($a) { return $a['res']; }
173 function assets_cmp($a, $b)
174 { return megapixels_cmp($a, $b, "access_function"); }
175 usort($video['assets'], "assets_cmp");
178 $video['url'] = array();
179 foreach ($video['assets'] as & $asset)
181 $def = substr($asset['res'], strpos($asset['res'], 'x') + 1) . 'p';
182 $asset['src'] = site_url('data/torrents/'. $video['name'] . '_'
183 . $def . '.'. $asset['ext']
184 . '.'. $this->config->item('default_torrent_ext'));
188 $categories = $this->config->item('categories');
189 $category_name = $categories[ intval($video['category_id']) ];
190 $video['category_title'] = $category_name ?
191 $this->lang->line("ui_categ_$category_name") : $category_name;
194 $video['thumbs'] = $this->get_thumbs($video['name'], $video['thumbs_count']);
196 // TODO: user information
197 $video['user_name'] = 'TODO';
203 * Increment a video parameter from DB: `views`, `likes` or `dislikes`.
205 * @param int $id DB video id
206 * @param string $param DB parameter column name.
209 public function inc_video_var($id, $var)
211 // TODO error report if query returns FALSE
212 $this->db->query('UPDATE `videos` '
213 . 'SET `'. $var. '`=`'. $var. '`+1 '
217 public function get_thumbs($name, $count)
221 for ($i=0; $i < $count; $i++)
222 $thumbs[] = site_url(sprintf("data/thumbs/%s_t%02d.jpg", $name, $i));
228 * Searches videos in DB based on a search query string and returns an
229 * associative array of results.
230 * If count is zero the function only return the number of results.
231 * @param string $search_query
234 * @param int $category_id if NULL, all categories are searched
235 * @return array an associative array with the same keys as that from
236 * get_videos_summary's result, but with two additional keys:
237 * description and date.
239 public function search_videos($search_query, $offset = 0, $count = 0,
242 $search_query = trim($search_query);
243 $search_query = str_replace("'", " ", $search_query);
245 // Search word fragments.
246 // sfc = search fragment condition
248 // sfr = serach fragment relevation
251 $fragm = strtok($search_query, $sep);
252 while ($fragm !== FALSE)
254 $sfc .= "(title LIKE '%$fragm%'
255 OR description LIKE '%$fragm%'
256 OR tags LIKE '%$fragm%') OR ";
258 // Frament relevations are half of boolean relevations such
259 // that they will appear at the end of the results.
260 $sfr .= "0.25 * (title LIKE '%$fragm%')
261 + 0.1 * (description LIKE '%$fragm%')
262 + 0.15 * (tags LIKE '%$fragm%') + ";
264 $fragm = strtok($sep);
266 $sfc = substr($sfc, 0, -4) . " )";
267 $sfr = substr($sfr, 0, -3) . " )";
269 if (! $this->is_advanced_search_query($search_query))
271 $search_cond = "MATCH (title, description, tags)
272 AGAINST ('$search_query') OR $sfc";
273 $relevance = "( MATCH (title, description, tags)
274 AGAINST ('$search_query') + $sfr ) AS relevance";
279 $against = "AGAINST ('$search_query' IN BOOLEAN MODE)";
280 $search_cond = "( MATCH (title, description, tags)
282 $relevance = "( 0.5 * (MATCH(title) $against)
283 + 0.3 * (MATCH(tags) $against)
284 + 0.2 * (MATCH(description) $against)
285 + $sfr) AS relevance";
290 $selected_columns = "COUNT(*) count";
296 // TODO select data, description if details are needed
297 $selected_columns = "id, name, title, duration, user_id, views,
298 thumbs_count, default_thumb,
299 (views + likes - dislikes) AS score,
301 $order = "ORDER BY relevance DESC, score DESC";
302 $limit = "LIMIT $offset, $count";
305 if ($category_id !== NULL)
306 $category_cond = "category_id = '$category_id' AND ";
310 $str_query = "SELECT $selected_columns
312 WHERE $category_cond ( $search_cond )
315 // echo "<p>$str_query</p>";
316 $query = $this->db->query($str_query);
318 if ($query->num_rows() > 0)
321 return $query->row()->count;
323 $videos = $query->result_array();
328 $this->load->helper('text');
330 foreach ($videos as & $video)
332 // P2P-Tube Video URL
333 $video['video_url'] = site_url(sprintf("watch/%d/%s",
334 $video['id'], $video['name']));
337 $video['thumbs'] = $this->get_thumbs($video['name'],
338 $video['thumbs_count']);
341 //$video['shorted_title'] = ellipsize($video['title'], 45, 0.75);
342 $video['shorted_title'] = character_limiter($video['title'], 50);
344 // TODO: user information
345 $video['user_name'] = 'TODO';
351 public function decode_search_query($search_query)
353 $search_query = urldecode($search_query);
355 $search_query = str_replace('_AST_', '*', $search_query);
356 $search_query = str_replace('_AND_', '+', $search_query);
357 $search_query = str_replace('_GT_', '>', $search_query);
358 $search_query = str_replace('_LT_', '<', $search_query);
359 $search_query = str_replace('_PO_', '(', $search_query);
360 $search_query = str_replace('_PC_', ')', $search_query);
361 $search_query = str_replace('_LOW_', '~', $search_query);
362 $search_query = str_replace('_QUO_', '"', $search_query);
364 return $search_query;
367 public function encode_search_query($search_query)
369 $search_query = str_replace('*', '_AST_', $search_query);
370 $search_query = str_replace('+', '_AND_', $search_query);
371 $search_query = str_replace('>', '_GT_', $search_query);
372 $search_query = str_replace('<', '_LT_', $search_query);
373 $search_query = str_replace('(', '_PO_', $search_query);
374 $search_query = str_replace(')', '_PC_', $search_query);
375 $search_query = str_replace('~', '_LOW_', $search_query);
376 $search_query = str_replace('"', '_QUO_', $search_query);
378 $search_query = urlencode($search_query);
380 return $search_query;
384 * Return TRUE if it contains any special caracter from an advanced search
386 * @param string $search_query
389 public function is_advanced_search_query($search_query)
391 return (preg_match('/\*|\+|\-|>|\<|\(|\)|~|"/', $search_query) == 0
396 /* End of file videos_model.php */
397 /* Location: ./application/models/videos_model.php */