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 // very short queries
243 if (strlen($search_query) < 4)
245 $search_cond = "(title LIKE '%$search_query%'
246 OR description LIKE '%$search_query%'
247 OR tags LIKE '%$search_query%')";
248 $relevance = "( 0.5 * (title LIKE '%git%')
249 + 0.2 * (description LIKE '%git%')
250 + 0.3 * (tags LIKE '%git%') ) AS relevance";
252 // natural language mode
253 else if (! $this->is_advanced_search_query($search_query))
255 $search_cond = "MATCH (title, description, tags)
256 AGAINST ('$search_query')";
257 $relevance = "$search_cond AS relevance";
262 $against = "AGAINST ('$search_query' IN BOOLEAN MODE)";
263 $search_cond = "MATCH (title, description, tags)
265 $relevance = "( (0.5 * (MATCH(title) $against))
266 + (0.3 * (MATCH(tags) $against))
267 + (0.2 * (MATCH(description) $against)) ) AS relevance";
272 $selected_columns = "COUNT(*) count";
278 // TODO select data, description if details are needed
279 $selected_columns = "id, name, title, duration, user_id, views,
280 thumbs_count, default_thumb,
281 (views + likes - dislikes) AS score,
283 $order = "ORDER BY relevance DESC, score DESC";
284 $limit = "LIMIT $offset, $count";
287 if ($category_id !== NULL)
288 $category_cond = "category_id = '$category_id' AND ";
292 $search_query = trim($search_query);
294 $str_query = "SELECT $selected_columns
296 WHERE $category_cond $search_cond
299 $query = $this->db->query($str_query);
301 if ($query->num_rows() > 0)
304 return $query->row()->count;
306 $videos = $query->result_array();
311 $this->load->helper('text');
313 foreach ($videos as & $video)
315 // P2P-Tube Video URL
316 $video['video_url'] = site_url(sprintf("watch/%d/%s",
317 $video['id'], $video['name']));
320 $video['thumbs'] = $this->get_thumbs($video['name'],
321 $video['thumbs_count']);
324 //$video['shorted_title'] = ellipsize($video['title'], 45, 0.75);
325 $video['shorted_title'] = character_limiter($video['title'], 50);
327 // TODO: user information
328 $video['user_name'] = 'TODO';
334 public function decode_search_query($search_query)
336 $search_query = urldecode($search_query);
338 $search_query = str_replace('_AST_', '*', $search_query);
339 $search_query = str_replace('_AND_', '+', $search_query);
340 $search_query = str_replace('_GT_', '>', $search_query);
341 $search_query = str_replace('_LT_', '<', $search_query);
342 $search_query = str_replace('_PO_', '(', $search_query);
343 $search_query = str_replace('_PC_', ')', $search_query);
344 $search_query = str_replace('_LOW_', '~', $search_query);
345 $search_query = str_replace('_QUO_', '"', $search_query);
347 return $search_query;
351 * Return TRUE if it contains any special caracter from an advanced search
353 * @param string $search_query
356 public function is_advanced_search_query($search_query)
358 return (preg_match('/\*|\+|\-|>|\<|\(|\)|~|"/', $search_query) == 0
363 /* End of file videos_model.php */
364 /* Location: ./application/models/videos_model.php */