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 parent::__construct();
16 if ($this->db === NULL)
18 $this->load->library('singleton_db');
19 $this->db = $this->singleton_db->connect();
24 * Retrieves a set of videos information which can be used for displaying
25 * that videos as a list with few details.
27 * @param int $category_id DB category ID; pass NULL for all
29 * @param mixed $user an user_id (as int) or an username
30 * (as string); pass NULL for all users
33 * @param string $ordering control videos ording by these
36 * <li><strong>'hottest':</strong> newest most appreciated first. An
37 * appreciated video is one which has a bigger
38 * score = views + likes - dislikes.</li>
39 * <li><strong>'newest':</strong> newest first.</li>
40 * <li><strong>'alphabetically':</strong> sort alphabetically.</li>
42 * @return array a list of videos, each one being an assoc array with:
44 * <li>id, name, title, duration, thumbs_count, default_thumb, views => from DB</li>
45 * <li>shorted_title => ellipsized title</li>
46 * <li>video_url => P2P-Tube video URl</li>
47 * <li>user_id, user_name</li>
48 * <li>thumbs => thumbnail images' URLs</li>
51 public function get_videos_summary($category_id, $user, $offset, $count,
52 $ordering = 'hottest')
54 $this->load->helper('text');
60 $order_statement = "ORDER BY date DESC, score DESC, RAND()";
63 $order_statement = "ORDER BY date DESC";
65 case 'alphabetically':
66 $order_statement = "ORDER BY title";
70 $order_statement = "";
74 if ($category_id === NULL)
78 $category_id = intval($category_id);
79 $cond_category = "category_id = $category_id";
88 $cond_user = "v.user_id = $user";
89 else if (is_string($user))
90 $cond_user = "u.username = '$user'";
93 $query = $this->db->query(
94 "SELECT v.id, name, title, duration, user_id, u.username, views,
95 thumbs_count, default_thumb,
96 (views + likes - dislikes) AS score
97 FROM `videos` v, `users` u
98 WHERE v.user_id = u.id AND $cond_category AND $cond_user
100 LIMIT $offset, $count");
102 if ($query->num_rows() > 0)
103 $videos = $query->result_array();
107 foreach ($videos as & $video)
109 // P2P-Tube Video URL
110 $video['video_url'] = site_url(sprintf("watch/%d/%s",
111 $video['id'], $video['name']));
114 $video['thumbs'] = $this->get_thumbs($video['name'],
115 $video['thumbs_count']);
118 //$video['shorted_title'] = ellipsize($video['title'], 45, 0.75);
119 $video['shorted_title'] = character_limiter($video['title'], 50);
126 * Returns the number of videos from database from a specific category or
128 * NULL parameters count videos from all categories and / or all users.
130 * @param int $category_id
131 * @param mixed $user an user_id (as int) or an username (as string)
132 * @return int number of videos or NULL if an error occured
134 public function get_videos_count($category_id = NULL, $user = NULL)
136 if ($category_id === NULL)
137 $cond_category = "1";
139 $cond_category = "category_id = $category_id";
146 $cond_user = "v.user_id = $user";
147 else if(is_string($user))
148 $cond_user = "u.username = '$user'";
151 $query = $this->db->query(
152 "SELECT COUNT(*) count
153 FROM `videos` v, `users` u
154 WHERE v.user_id = u.id AND $cond_category AND $cond_user");
156 if ($query->num_rows() > 0)
157 return $query->row()->count;
164 * Retrieves information about a video.
166 * If $name does not match with the video's `name` from the DB an error is
167 * marked in the key 'err'. If it's NULL it is ignored.
170 * @param string $id video's `id` column from `videos` DB table
171 * @param string $name video's `name` column from `videos` DB
172 * table. NULL means there is no name provided.
173 * @return array an associative list with information about a video
174 * with the following keys:
176 * <li>all columns form DB with some exceptions that are overwritten or new</li>
177 * <li>content is moved in assets</li>
178 * <li>assets => list of associative lists where each one represents a</li>
179 * video asset having keys: "src", "res", "par" and "ext". Value of key
180 * "src" is the video torrent formated as
181 * {name}_{format}.{video_ext}.{default_torrent_ext}</li>
182 * <li>username => user name from `users` table</li>
183 * <li>category_title => a human-friendly category name</li>
184 * <li>tags => associative list of "tag => score"</li>
185 * <li>date => date and time when the video was created</li>
186 * <li>thumbs => thumbnail images' URLs</li>
189 public function get_video($id, $name = NULL)
191 $this->load->helper('video');
193 $query = $this->db->query("SELECT v.*, u.username
194 FROM `videos` v, `users` u
195 WHERE v.user_id = u.id AND v.id = $id");
198 if ($query->num_rows() > 0)
200 $video = $query->row_array();
201 if ($name !== NULL && $video['name'] != $name)
202 $video['err'] = 'INVALID_NAME';
206 $video['err'] = 'INVALID_ID';
210 // Convert JSON encoded string to arrays.
211 $video['assets'] = json_decode($video['formats'], TRUE);
212 unset($video['formats']);
213 $video['tags'] = json_decode($video['tags'], TRUE);
214 asort($video['tags']);
215 $video['tags'] = array_reverse($video['tags'], TRUE);
217 // Sort assets by their megapixels number.
218 function access_function($a) { return $a['res']; }
219 function assets_cmp($a, $b)
220 { return megapixels_cmp($a, $b, "access_function"); }
221 usort($video['assets'], "assets_cmp");
224 $video['url'] = array();
225 foreach ($video['assets'] as & $asset)
227 $def = substr($asset['res'], strpos($asset['res'], 'x') + 1) . 'p';
228 $asset['src'] = site_url('data/torrents/'. $video['name'] . '_'
229 . $def . '.'. $asset['ext']
230 . '.'. $this->config->item('default_torrent_ext'));
234 $categories = $this->config->item('categories');
235 $category_name = $categories[ intval($video['category_id']) ];
236 $video['category_title'] = $category_name ?
237 $this->lang->line("ui_categ_$category_name") : $category_name;
240 $video['thumbs'] = $this->get_thumbs($video['name'], $video['thumbs_count']);
246 * Increment a video parameter from DB: `views`, `likes` or `dislikes`.
248 * @param int $id DB video id
249 * @param string $param DB parameter column name.
252 public function inc_video_var($id, $var)
254 // TODO error report if query returns FALSE
255 $this->db->query('UPDATE `videos` '
256 . 'SET `'. $var. '`=`'. $var. '`+1 '
260 public function get_thumbs($name, $count)
264 for ($i=0; $i < $count; $i++)
265 $thumbs[] = site_url(sprintf("data/thumbs/%s_t%02d.jpg", $name, $i));
271 * Searches videos in DB based on a search query string and returns an
272 * associative array of results.
273 * If count is zero the function only return the number of results.
274 * @param string $search_query
277 * @param int $category_id if NULL, all categories are searched
278 * @return array an associative array with the same keys as that from
279 * get_videos_summary's result, but with two additional keys:
280 * description and date.
282 public function search_videos($search_query, $offset = 0, $count = 0,
285 $search_query = trim($search_query);
286 $search_query = str_replace("'", " ", $search_query);
288 // Search word fragments.
289 // sfc = search fragment condition
291 // sfr = serach fragment relevation
294 $fragm = strtok($search_query, $sep);
295 while ($fragm !== FALSE)
297 $sfc .= "(title LIKE '%$fragm%'
298 OR description LIKE '%$fragm%'
299 OR tags LIKE '%$fragm%') OR ";
301 // Frament relevations are half of boolean relevations such
302 // that they will appear at the end of the results.
303 $sfr .= "0.25 * (title LIKE '%$fragm%')
304 + 0.1 * (description LIKE '%$fragm%')
305 + 0.15 * (tags LIKE '%$fragm%') + ";
307 $fragm = strtok($sep);
309 $sfc = substr($sfc, 0, -4) . " )";
310 $sfr = substr($sfr, 0, -3) . " )";
312 if (! $this->is_advanced_search_query($search_query))
314 $search_cond = "MATCH (title, description, tags)
315 AGAINST ('$search_query') OR $sfc";
316 $relevance = "( MATCH (title, description, tags)
317 AGAINST ('$search_query') + $sfr ) AS relevance";
322 $against = "AGAINST ('$search_query' IN BOOLEAN MODE)";
323 $search_cond = "( MATCH (title, description, tags)
325 $relevance = "( 0.5 * (MATCH(title) $against)
326 + 0.3 * (MATCH(tags) $against)
327 + 0.2 * (MATCH(description) $against)
328 + $sfr) AS relevance";
333 $selected_columns = "COUNT(*) count";
339 // TODO select data, description if details are needed
340 $selected_columns = "id, name, title, duration, user_id, views,
341 thumbs_count, default_thumb,
342 (views + likes - dislikes) AS score,
344 $order = "ORDER BY relevance DESC, score DESC";
345 $limit = "LIMIT $offset, $count";
348 if ($category_id !== NULL)
349 $category_cond = "category_id = '$category_id' AND ";
353 $str_query = "SELECT $selected_columns
355 WHERE $category_cond ( $search_cond )
358 // echo "<p>$str_query</p>";
359 $query = $this->db->query($str_query);
361 if ($query->num_rows() > 0)
364 return $query->row()->count;
366 $videos = $query->result_array();
371 $this->load->helper('text');
373 foreach ($videos as & $video)
375 // P2P-Tube Video URL
376 $video['video_url'] = site_url(sprintf("watch/%d/%s",
377 $video['id'], $video['name']));
380 $video['thumbs'] = $this->get_thumbs($video['name'],
381 $video['thumbs_count']);
384 //$video['shorted_title'] = ellipsize($video['title'], 45, 0.75);
385 $video['shorted_title'] = character_limiter($video['title'], 50);
387 // TODO: user information
388 $video['user_name'] = 'TODO';
394 public function decode_search_query($search_query)
396 $search_query = urldecode($search_query);
398 $search_query = str_replace('_AST_', '*', $search_query);
399 $search_query = str_replace('_AND_', '+', $search_query);
400 $search_query = str_replace('_GT_', '>', $search_query);
401 $search_query = str_replace('_LT_', '<', $search_query);
402 $search_query = str_replace('_PO_', '(', $search_query);
403 $search_query = str_replace('_PC_', ')', $search_query);
404 $search_query = str_replace('_LOW_', '~', $search_query);
405 $search_query = str_replace('_QUO_', '"', $search_query);
407 return $search_query;
410 public function encode_search_query($search_query)
412 $search_query = str_replace('*', '_AST_', $search_query);
413 $search_query = str_replace('+', '_AND_', $search_query);
414 $search_query = str_replace('>', '_GT_', $search_query);
415 $search_query = str_replace('<', '_LT_', $search_query);
416 $search_query = str_replace('(', '_PO_', $search_query);
417 $search_query = str_replace(')', '_PC_', $search_query);
418 $search_query = str_replace('~', '_LOW_', $search_query);
419 $search_query = str_replace('"', '_QUO_', $search_query);
421 $search_query = urlencode($search_query);
423 return $search_query;
427 * Return TRUE if it contains any special caracter from an advanced search
429 * @param string $search_query
432 public function is_advanced_search_query($search_query)
434 return (preg_match('/\*|\+|\-|>|\<|\(|\)|~|"/', $search_query) == 0
439 /* End of file videos_model.php */
440 /* Location: ./application/models/videos_model.php */