Пример #1
0
 function fetch_assets($args)
 {
     # load assets
     if (isset($args['blog_id'])) {
         $blog_filter = 'and asset_blog_id = ' . intval($args['blog_id']);
     }
     # Adds a thumbnail filter to the filters list.
     if (isset($args['exclude_thumb']) && $args['exclude_thumb']) {
         $thumb_filter = ' and asset_parent is null';
     }
     # Adds a tag filter to the filters list.
     if (isset($args['tags']) or isset($args['tag'])) {
         $tag_arg = isset($args['tag']) ? $args['tag'] : $args['tags'];
         require_once "MTUtil.php";
         if (!preg_match('/\\b(AND|OR|NOT)\\b|\\(|\\)/i', $tag_arg)) {
             $not_clause = false;
         } else {
             $not_clause = preg_match('/\\bNOT\\b/i', $tag_arg);
         }
         $include_private = 0;
         $tag_array = tag_split($tag_arg);
         foreach ($tag_array as $tag) {
             if ($tag && substr($tag, 0, 1) == '@') {
                 $include_private = 1;
             }
         }
         $tags =& $this->fetch_asset_tags(array('blog_id' => $blog_id, 'tag' => $tag_arg, 'include_private' => $include_private));
         if (!is_array($tags)) {
             $tags = array();
         }
         $cexpr = create_tag_expr_function($tag_arg, $tags, 'asset');
         if ($cexpr) {
             $tmap = array();
             $tag_list = array();
             foreach ($tags as $tag) {
                 $tag_list[] = $tag['tag_id'];
             }
             $ot =& $this->fetch_objecttags(array('tag_id' => $tag_list, 'datasource' => 'asset'));
             if ($ot) {
                 foreach ($ot as $o) {
                     $tmap[$o['objecttag_object_id']][$o['objecttag_tag_id']]++;
                     if (!$not_clause) {
                         $asset_list[$o['objecttag_object_id']] = 1;
                     }
                 }
             }
             $ctx['t'] =& $tmap;
             $filters[] = $cexpr;
         } else {
             return null;
         }
     }
     # Adds an author filter
     if (isset($args['author'])) {
         $author_filter = 'and author_name = \'' . $this->escape($args['author']) . "'";
     }
     # Adds an entry filter
     if (isset($args['entry_id'])) {
         $entry_filter = 'and (objectasset_object_ds = \'entry\' and objectasset_object_id = \'' . $this->escape($args['entry_id']) . '\' and asset_id = objectasset_asset_id)';
         $entry_join = ', mt_objectasset.*';
         $entry_join_tbl = ', mt_objectasset';
     }
     # Adds an ID filter
     if (isset($args['id'])) {
         if ($args['id'] == '') {
             return null;
         }
         $id_filter = 'and asset_id = ' . $args['id'];
     }
     # Adds a days filter
     if (isset($args['days'])) {
         $day_filter = 'and ' . $this->limit_by_day_sql('asset_created_on', intval($args['days']));
     }
     # Adds a type filter
     if (isset($args['type'])) {
         $type_filter = "and asset_class ='" . $args['type'] . "'";
     }
     # Adds a file extension filter
     if (isset($args['file_ext'])) {
         $ext_filter = "and asset_file_ext ='" . $args['file_ext'] . "'";
     }
     # Adds a score or rate filter to the filters list.
     if (isset($args['namespace'])) {
         require_once "MTUtil.php";
         $arg_names = array('min_score', 'max_score', 'min_rate', 'max_rate', 'min_count', 'max_count');
         foreach ($arg_names as $n) {
             if (isset($args[$n])) {
                 $rating_args = $args[$n];
                 $cexpr = create_rating_expr_function($rating_args, $n, $args['namespace'], 'asset');
                 if ($cexpr) {
                     $filters[] = $cexpr;
                 } else {
                     return null;
                 }
             }
         }
         if (isset($args['scored_by'])) {
             $voter = $this->fetch_author_by_name($args['scored_by']);
             if (!$voter) {
                 echo "Invalid scored by filter: " . $args['scored_by'];
                 return null;
             }
             $cexpr = create_rating_expr_function($voter['author_id'], 'scored_by', $args['namespace'], 'asset');
             if ($cexpr) {
                 $filters[] = $cexpr;
             } else {
                 return null;
             }
         }
     }
     # Adds sort order
     $order = 'desc';
     $sort_by = 'asset_created_on';
     if (isset($args['sort_order'])) {
         if ($args['sort_order'] == 'ascend') {
             $order = 'asc';
         } else {
             if ($args['sort_order'] == 'descend') {
                 $order = 'desc';
             }
         }
     }
     if (isset($args['sort_by'])) {
         if ('score' != $args['sort_by'] && 'rate' != $args['sort_by']) {
             $sort_by = 'asset_' . $args['sort_by'];
         }
     }
     if (isset($args['lastn'])) {
         $order = 'desc';
     }
     $join_score = "";
     $distinct = "";
     if (isset($args['sort_by']) && ($args['sort_by'] == 'score' || $args['sort_by'] == 'rate')) {
         $join_score = "left join mt_objectscore on objectscore_object_id = asset_id";
         $distinct = " distinct";
     }
     $limit = 0;
     $offset = 0;
     if (isset($args['lastn'])) {
         $limit = $args['lastn'];
     }
     if (isset($args['limit'])) {
         $limit = $args['limit'];
     }
     if (isset($args['offset'])) {
         $offset = $args['offset'];
     }
     if (count($filters)) {
         $post_select_limit = $limit;
         $post_select_offset = $offset;
         $limit = 0;
         $offset = 0;
     }
     # Build SQL
     $sql = "\n            select {$distinct} mt_asset.*, mt_author.* {$entry_join}\n            from mt_asset {$join_score}, mt_author {$entry_join_tbl}\n            where\n                author_id = asset_created_by\n                {$id_filter}\n                {$blog_filter}\n                {$author_filter}\n                {$entry_filter}\n                {$day_filter}\n                {$type_filter}\n                {$ext_filter}\n                {$thumb_filter}\n            order by\n                {$sort_by} {$order}\n                <LIMIT>\n        ";
     # Added Limit and offset
     $sql = $this->apply_limit_sql($sql, $limit, $offset);
     # Fetch resultset
     $result = $this->query_start($sql);
     if (!$result) {
         return null;
     }
     $assets = array();
     $offset = $post_select_offset ? $post_select_offset : 0;
     $limit = $post_select_limit ? $post_select_limit : 0;
     while (true) {
         $e = $this->query_fetch(ARRAY_A);
         if ($offset && $j++ < $offset) {
             continue;
         }
         if (!isset($e)) {
             break;
         }
         if (count($filters)) {
             foreach ($filters as $f) {
                 if (!$f($e, $ctx)) {
                     continue 2;
                 }
             }
         }
         $e = $this->expand_meta($e);
         $assets[] = $e;
         if ($limit > 0 && count($assets) >= $limit) {
             break;
         }
     }
     $order = 'desc';
     if (isset($args['sort_order'])) {
         if ($args['sort_order'] == 'ascend') {
             $order = 'asc';
         } else {
             if ($args['sort_order'] == 'descend') {
                 $order = 'desc';
             }
         }
     }
     if (isset($args['sort_by']) && 'score' == $args['sort_by']) {
         $assets_tmp = array();
         foreach ($assets as $a) {
             $assets_tmp[$a['asset_id']] = $a;
         }
         $scores = $this->fetch_sum_scores($args['namespace'], 'asset', $order, $id_filter . "\n" . $blog_filter . "\n" . $author_filter . "\n" . $day_filter . "\n" . $type_filter . "\n" . $ext_filter . "\n" . $thumb_filter . "\n");
         $assets_sorted = array();
         foreach ($scores as $score) {
             if (array_key_exists($score['objectscore_object_id'], $assets_tmp)) {
                 array_push($assets_sorted, $assets_tmp[$score['objectscore_object_id']]);
                 unset($assets_tmp[$score['objectscore_object_id']]);
             }
         }
         foreach ($assets_tmp as $et) {
             if ($order == 'asc') {
                 array_unshift($assets_sorted, $et);
             } else {
                 array_push($assets_sorted, $et);
             }
         }
         $assets = $assets_sorted;
     } elseif (isset($args['sort_by']) && 'rate' == $args['sort_by']) {
         $assets_tmp = array();
         foreach ($assets as $a) {
             $assets_tmp[$a['asset_id']] = $a;
         }
         $scores = $this->fetch_avg_scores($args['namespace'], 'asset', $order, $id_filter . "\n" . $blog_filter . "\n" . $author_filter . "\n" . $day_filter . "\n" . $type_filter . "\n" . $ext_filter . "\n" . $thumb_filter . "\n");
         $assets_sorted = array();
         foreach ($scores as $score) {
             if (array_key_exists($score['objectscore_object_id'], $assets_tmp)) {
                 array_push($assets_sorted, $assets_tmp[$score['objectscore_object_id']]);
                 unset($assets_tmp[$score['objectscore_object_id']]);
             }
         }
         foreach ($assets_tmp as $et) {
             if ($order == 'asc') {
                 array_unshift($assets_sorted, $et);
             } else {
                 array_push($assets_sorted, $et);
             }
         }
         $assets = $assets_sorted;
     }
     return $assets;
 }
Пример #2
0
 function fetch_assets($args)
 {
     # load assets
     $extras = array();
     if (isset($args['blog_id'])) {
         $blog_filter = 'and asset_blog_id = ' . intval($args['blog_id']);
     }
     # Adds a thumbnail filter to the filters list.
     if (isset($args['exclude_thumb']) && $args['exclude_thumb']) {
         $thumb_filter = ' and asset_parent is null';
     }
     # Adds a tag filter to the filters list.
     if (isset($args['tags']) or isset($args['tag'])) {
         $tag_arg = isset($args['tag']) ? $args['tag'] : $args['tags'];
         require_once "MTUtil.php";
         if (!preg_match('/\\b(AND|OR|NOT)\\b|\\(|\\)/i', $tag_arg)) {
             $not_clause = false;
         } else {
             $not_clause = preg_match('/\\bNOT\\b/i', $tag_arg);
         }
         $include_private = 0;
         $tag_array = tag_split($tag_arg);
         foreach ($tag_array as $tag) {
             if ($tag && substr($tag, 0, 1) == '@') {
                 $include_private = 1;
             }
         }
         $tags = $this->fetch_asset_tags(array('blog_id' => $blog_id, 'tag' => $tag_arg, 'include_private' => $include_private));
         if (!is_array($tags)) {
             $tags = array();
         }
         $cexpr = create_tag_expr_function($tag_arg, $tags, 'asset');
         if ($cexpr) {
             $tmap = array();
             $tag_list = array();
             foreach ($tags as $tag) {
                 $tag_list[] = $tag->tag_id;
             }
             $ot = $this->fetch_objecttags(array('tag_id' => $tag_list, 'datasource' => 'asset'));
             if (!empty($ot)) {
                 foreach ($ot as $o) {
                     $tmap[$o->objecttag_object_id][$o->objecttag_tag_id]++;
                     if (!$not_clause) {
                         $asset_list[$o->objecttag_object_id] = 1;
                     }
                 }
             }
             $ctx['t'] =& $tmap;
             $filters[] = $cexpr;
         } else {
             return null;
         }
     }
     # Adds an author filter
     if (isset($args['author'])) {
         $author_filter = "and author_name = '" . $this->escape($args['author']) . "'";
         $extras['join']['mt_author'] = array('condition' => "author_id = asset_created_by");
     }
     # Adds an entry filter
     if (isset($args['entry_id'])) {
         $extras['join']['mt_objectasset'] = array('condition' => "(objectasset_object_ds = 'entry' and objectasset_object_id = " . intval($this->escape($args['entry_id'])) . " and asset_id = objectasset_asset_id)");
     }
     # Adds an ID filter
     if (isset($args['id'])) {
         if ($args['id'] == '') {
             return null;
         }
         $id_filter = 'and asset_id = ' . intval($args['id']);
     }
     # Adds a days filter
     if (isset($args['days'])) {
         $day_filter = 'and ' . $this->limit_by_day_sql('asset_created_on', intval($args['days']));
     }
     # Adds a type filter
     if (isset($args['type'])) {
         $type_filter = "and asset_class ='" . $args['type'] . "'";
     }
     # Adds a file extension filter
     if (isset($args['file_ext'])) {
         $ext_filter = "and asset_file_ext ='" . $args['file_ext'] . "'";
     }
     $date_filter = $args['ignore_archive_context'] ? '' : $this->build_date_filter($args, 'asset_created_on');
     # Adds a score or rate filter to the filters list.
     if (isset($args['namespace'])) {
         require_once "MTUtil.php";
         $arg_names = array('min_score', 'max_score', 'min_rate', 'max_rate', 'min_count', 'max_count');
         foreach ($arg_names as $n) {
             if (isset($args[$n])) {
                 $rating_args = $args[$n];
                 $cexpr = create_rating_expr_function($rating_args, $n, $args['namespace'], 'asset');
                 if ($cexpr) {
                     $filters[] = $cexpr;
                 } else {
                     return null;
                 }
             }
         }
         if (isset($args['scored_by'])) {
             $voter = $this->fetch_author_by_name($args['scored_by']);
             if (!$voter) {
                 echo "Invalid scored by filter: " . $args['scored_by'];
                 return null;
             }
             $cexpr = create_rating_expr_function($voter->author_id, 'scored_by', $args['namespace'], 'asset');
             if ($cexpr) {
                 $filters[] = $cexpr;
             } else {
                 return null;
             }
         }
     }
     # Adds sort order
     $order = 'desc';
     $sort_by = 'asset_created_on';
     if (isset($args['sort_order'])) {
         if ($args['sort_order'] == 'ascend') {
             $order = 'asc';
         } else {
             if ($args['sort_order'] == 'descend') {
                 $order = 'desc';
             }
         }
     }
     if (isset($args['sort_by'])) {
         if ('score' != $args['sort_by'] && 'rate' != $args['sort_by']) {
             $sort_by = 'asset_' . $args['sort_by'];
         }
     }
     if (isset($args['lastn'])) {
         $order = 'desc';
     }
     $join_score = "";
     $distinct = "";
     if (isset($args['sort_by']) && ($args['sort_by'] == 'score' || $args['sort_by'] == 'rate')) {
         $extras['join']['mt_objectscore'] = array('type' => 'left', 'condition' => "objectscore_object_id = asset_id");
         $extras['distinct'] = 'distinct';
     }
     $limit = 0;
     $offset = 0;
     if (isset($args['lastn'])) {
         $limit = $args['lastn'];
     }
     if (isset($args['limit'])) {
         $limit = $args['limit'];
     }
     if (isset($args['offset'])) {
         $offset = $args['offset'];
     }
     if (count($filters)) {
         $post_select_limit = $limit;
         $post_select_offset = $offset;
         $limit = 0;
         $offset = 0;
     }
     if ($limit) {
         $extras['limit'] = $limit;
     }
     if ($offset) {
         $extras['offset'] = $offset;
     }
     # Build SQL
     $where = "1 = 1\n                {$id_filter}\n                {$blog_filter}\n                {$author_filter}\n                {$entry_filter}\n                {$day_filter}\n                {$type_filter}\n                {$ext_filter}\n                {$thumb_filter}\n                {$date_filter}\n            order by\n                {$sort_by} {$order}\n        ";
     require_once 'class.mt_asset.php';
     $asset = new Asset();
     $result = $asset->Find($where, false, false, $extras);
     if (empty($result)) {
         return null;
     }
     $assets = array();
     $offset = $post_select_offset ? $post_select_offset : 0;
     $limit = $post_select_limit ? $post_select_limit : 0;
     $i = -1;
     while (true) {
         $i++;
         $e = $result[$i];
         if ($offset && $j++ < $offset) {
             continue;
         }
         if (!isset($e)) {
             break;
         }
         if (count($filters)) {
             foreach ($filters as $f) {
                 if (!$f($e, $ctx)) {
                     continue 2;
                 }
             }
         }
         $assets[] = $e;
         if ($limit > 0 && count($assets) >= $limit) {
             break;
         }
     }
     $no_resort = 0;
     if (isset($args['sort_by']) && $asset->has_column($args['sort_by'])) {
         $no_resort = 1;
     }
     if (isset($args['lastn'])) {
         if (isset($args['sort_by'])) {
             $no_resort = 0;
         }
     } else {
         if (!isset($args['sort_by'])) {
             $no_resort = 1;
         }
     }
     $order = 'desc';
     if (isset($args['sort_order'])) {
         if ($args['sort_order'] == 'ascend') {
             $order = 'asc';
         } else {
             if ($args['sort_order'] == 'descend') {
                 $order = 'desc';
             }
         }
     }
     # Resort assets
     if (isset($args['sort_by']) && 'score' == $args['sort_by']) {
         $assets_tmp = array();
         foreach ($assets as $a) {
             $assets_tmp[$a->asset_id] = $a;
         }
         $scores = $this->fetch_sum_scores($args['namespace'], 'asset', $order, $id_filter . "\n" . $blog_filter . "\n" . $author_filter . "\n" . $day_filter . "\n" . $type_filter . "\n" . $ext_filter . "\n" . $thumb_filter . "\n");
         $assets_sorted = array();
         foreach ($scores as $score) {
             if (array_key_exists($score['objectscore_object_id'], $assets_tmp)) {
                 array_push($assets_sorted, $assets_tmp[$score['objectscore_object_id']]);
                 unset($assets_tmp[$score['objectscore_object_id']]);
             }
         }
         foreach ($assets_tmp as $et) {
             if ($order == 'asc') {
                 array_unshift($assets_sorted, $et);
             } else {
                 array_push($assets_sorted, $et);
             }
         }
         $assets = $assets_sorted;
     } elseif (isset($args['sort_by']) && 'rate' == $args['sort_by']) {
         $assets_tmp = array();
         foreach ($assets as $a) {
             $assets_tmp[$a->asset_id] = $a;
         }
         $scores = $this->fetch_avg_scores($args['namespace'], 'asset', $order, $id_filter . "\n" . $blog_filter . "\n" . $author_filter . "\n" . $day_filter . "\n" . $type_filter . "\n" . $ext_filter . "\n" . $thumb_filter . "\n");
         $assets_sorted = array();
         foreach ($scores as $score) {
             if (array_key_exists($score['objectscore_object_id'], $assets_tmp)) {
                 array_push($assets_sorted, $assets_tmp[$score['objectscore_object_id']]);
                 unset($assets_tmp[$score['objectscore_object_id']]);
             }
         }
         foreach ($assets_tmp as $et) {
             if ($order == 'asc') {
                 array_unshift($assets_sorted, $et);
             } else {
                 array_push($assets_sorted, $et);
             }
         }
         $assets = $assets_sorted;
     } elseif (!$no_resort) {
         $sort_field = 'asset_created_on';
         if (isset($args['sort_by']) && $asset->has_column($args['sort_by'])) {
             if (preg_match('/^field[:\\.](.+)$/', $args['sort_by'], $match)) {
                 $sort_field = 'asset_field.' . $match[1];
             } else {
                 $sort_field = 'asset_' . $args['sort_by'];
             }
         }
         if ($sort_field == 'asset_blog_id' || $sort_field == 'asset_created_by' || $sort_field == 'asset_created_on' || $sort_field == 'asset_id' || $sort_field == 'asset_parent') {
             $sort_fn = "if (\$a->{$sort_field} == \$b->{$sort_field}) return 0; return \$a->{$sort_field} < \$b->{$sort_field} ? -1 : 1;";
         } else {
             $sort_fn = "\$f = '" . addslashes($sort_field) . "'; return strcmp(\$a->\$f,\$b->\$f);";
         }
         $sorter = create_function($order == 'asc' ? '$a,$b' : '$b,$a', $sort_fn);
         usort($assets, $sorter);
     }
     return $assets;
 }