コード例 #1
0
ファイル: mtdb_base.php プロジェクト: Wilhansen/melody
 function &fetch_authors($args)
 {
     # Adds blog join
     $extend_column = '';
     if ($sql = $this->include_exclude_blogs($args)) {
         $blog_join = 'join mt_permission on permission_author_id = author_id and permission_blog_id ' . $sql;
         $unique_filter = 'distinct';
     } elseif (isset($args['blog_id'])) {
         $blog_id = intval($args['blog_id']);
         $blog_join = "join mt_permission on permission_author_id = author_id and permission_blog_id = {$blog_id}";
     }
     # Adds author filter
     if (isset($args['author_id'])) {
         $author_id = intval($args['author_id']);
         $author_filter = " and author_id = {$author_id}";
     }
     if (isset($args['author_nickname'])) {
         $author_filter .= " and author_nickname = '" . $args['author_nickname'] . "'";
     }
     if (isset($args['author_name'])) {
         $author_filter .= " and author_name = '" . $args['author_name'] . "'";
     }
     # Adds entry join and filter
     if ($args['need_entry']) {
         $entry_join = 'join mt_entry on author_id = entry_author_id';
         $unique_filter = 'distinct';
         $entry_filter = " and entry_status = 2";
         if ($blog_join) {
             $entry_filter .= " and entry_blog_id = permission_blog_id";
         } else {
             $entry_filter .= " and entry_blog_id = " . $args['blog_id'];
         }
     } else {
         if (!$args['any_type']) {
             $author_filter .= " and author_type = 1";
         }
     }
     # a context hash for filter routines
     $ctx = array();
     $filters = array();
     if (isset($args['status'])) {
         $status_arg = $args['status'];
         require_once "MTUtil.php";
         $status = array(array('name' => 'enabled', 'id' => 1), array('name' => 'disabled', 'id' => 2));
         $cexpr = create_status_expr_function($status_arg, $status);
         if ($cexpr) {
             $filters[] = $cexpr;
         }
     }
     if (isset($args['roles']) or isset($args['role'])) {
         $role_arg = isset($args['role']) ? $args['role'] : $args['roles'];
         require_once "MTUtil.php";
         $roles =& $this->fetch_all_roles();
         if (!is_array($roles)) {
             $roles = array();
         }
         $cexpr = create_role_expr_function($role_arg, $roles);
         if ($cexpr) {
             $rmap = array();
             $role_list = array();
             foreach ($roles as $role) {
                 $role_list[] = $role['role_id'];
             }
             $as =& $this->fetch_associations(array('blog_id' => $blog_id, 'role_id' => $role_list));
             if ($as) {
                 foreach ($as as $a) {
                     $rmap[$a['association_author_id']][$a['association_role_id']]++;
                 }
             }
             $ctx['r'] =& $rmap;
             $filters[] = $cexpr;
         }
     }
     # Adds a score or rate filter to the filters list.
     $re_sort = false;
     if (isset($args['namespace'])) {
         if (isset($args['scoring_to'])) {
             require_once "MTUtil.php";
             require_once "rating_lib.php";
             $type = $args['scoring_to'];
             $obj = $args['_scoring_to_obj'];
             $obj_id = $obj[$type . '_id'];
             if (isset($args['min_score'])) {
                 $expr = '$ctx = $c;if ($ctx == null) { global $mt; $ctx = $mt->context(); } $old = $ctx->mt->db->result; ';
                 $expr .= '$sc = get_score($ctx, ' . $obj_id . ', "' . $type . '", "' . $args['namespace'] . '", $e["author_id"]);';
                 $expr .= '$ret = $sc >= ' . $args['min_score'] . ';';
                 $expr .= ' $ctx->mt->db->result = $old; return $ret;';
                 $fn = create_function('&$e,&$c', $expr);
                 $filters[] = $fn;
             } elseif (isset($args['max_score'])) {
                 $expr = '$ctx = $c;if ($ctx == null) { global $mt; $ctx = $mt->context(); } $old = $ctx->mt->db->result; ';
                 $expr .= '$sc = get_score($ctx, ' . $obj_id . ', "' . $type . '", "' . $args['namespace'] . '", $e["author_id"]);';
                 $expr .= '$ret = $sc <= ' . $args['max_score'] . ';';
                 $expr .= ' $ctx->mt->db->result = $old; return $ret;';
                 $fn = create_function('&$e,&$c', $expr);
                 $filters[] = $fn;
             } else {
                 $expr = '$ctx = $c;if ($ctx == null) { global $mt; $ctx = $mt->context(); } $old = $ctx->mt->db->result; ';
                 $expr .= '$ret = !is_null($ctx->mt->db->fetch_score(' . $args['namespace'] . ',' . $obj_id . ', $e["author_id"],' . $type . '));';
                 $expr .= ' $ctx->mt->db->result = $old; return $ret;';
                 $fn = create_function('&$e,&$c', $expr);
                 $filters[] = $fn;
             }
         } else {
             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'], 'author');
                     if ($cexpr) {
                         $filters[] = $cexpr;
                         $re_sort = true;
                     } else {
                         return null;
                     }
                 }
             }
         }
     }
     # sort
     $join_score = "";
     if (isset($args['sort_by'])) {
         if ($args['sort_by'] == 'score' || $args['sort_by'] == 'rate') {
             $join_score = "join mt_objectscore on objectscore_object_id = author_id and objectscore_namespace='" . $args['namespace'] . "' and objectscore_object_ds='author'";
             $unique_filter = 'distinct';
             $order_sql = "order by author_created_on desc";
             $re_sort = true;
         } else {
             $sort_col = $args['sort_by'];
             $order = '';
             if (isset($args['sort_order'])) {
                 if ($args['sort_order'] == 'ascend') {
                     $order = 'asc';
                 } else {
                     $order = 'desc';
                 }
             }
             $order_sql = "order by {$sort_col} {$order}";
             if (isset($args['start_string'])) {
                 $val = $args['start_string'];
                 if ($order == 'asc') {
                     $val_order = '>';
                 } else {
                     $val_order = '<';
                 }
                 $sort_filter = " and {$sort_col} {$val_order} '{$val}'";
             }
             if (isset($args['start_num'])) {
                 $val = $args['start_num'];
                 if ($order == 'asc') {
                     $val_order = '>';
                 } else {
                     $val_order = '<';
                 }
                 $sort_filter .= " and {$sort_col} {$val_order} {$val}";
             }
         }
     }
     $limit = 0;
     $offset = 0;
     if (isset($args['lastn'])) {
         $limit = $args['lastn'];
     }
     if (isset($args['offset'])) {
         $limit = $args['offset'];
     }
     if ($re_sort) {
         $post_select_limit = $limit;
         $post_select_offset = $offset;
         $limit = 0;
         $offset = 0;
     }
     $sql = "\n            select {$unique_filter}\n                   mt_author.*\n                   {$extend_column}\n              from mt_author\n                   {$blog_join}\n                   {$entry_join}\n                   {$join_score}\n              where 1 = 1\n                {$author_filter}\n                {$entry_filter}\n                {$sort_filter}\n              {$order_sql}\n                   <LIMIT>\n        ";
     $sql = $this->apply_limit_sql($sql, $limit, $offset);
     $result = $this->query_start($sql);
     if (!$result) {
         return null;
     }
     $authors = array();
     if ($args['sort_by'] != 'score' && $args['sort_by'] != 'rate') {
         $offset = $post_select_offset ? $post_select_offset : 0;
         $limit = $post_select_limit ? $post_select_limit : 0;
     }
     $j = 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;
                 }
             }
         }
         $authors[] = $e;
         if ($limit > 0 && count($authors) >= $limit) {
             break;
         }
     }
     if (isset($args['sort_by']) && 'score' == $args['sort_by']) {
         $authors_tmp = array();
         $order = 'asc';
         if (isset($args['sort_order'])) {
             $order = $args['sort_order'] == 'ascend' ? 'asc' : 'desc';
         }
         foreach ($authors as $a) {
             $authors_tmp[$a['author_id']] = $a;
         }
         $scores = $this->fetch_sum_scores($args['namespace'], 'author', $order, $author_filter);
         $offset = $post_select_offset ? $post_select_offset : 0;
         $limit = $post_select_limit ? $post_select_limit : 0;
         $j = 0;
         $authors_sorted = array();
         foreach ($scores as $score) {
             if (array_key_exists($score['objectscore_object_id'], $authors_tmp)) {
                 if ($offset && $j++ < $offset) {
                     continue;
                 }
                 array_push($authors_sorted, $authors_tmp[$score['objectscore_object_id']]);
                 unset($authors_tmp[$score['objectscore_object_id']]);
                 if ($limit > 0 && count($authors_sorted) >= $limit) {
                     break;
                 }
             }
         }
         $authors = $authors_sorted;
     } elseif (isset($args['sort_by']) && 'rate' == $args['sort_by']) {
         $authors_tmp = array();
         $order = 'asc';
         if (isset($args['sort_order'])) {
             $order = $args['sort_order'] == 'ascend' ? 'asc' : 'desc';
         }
         foreach ($authors as $a) {
             $authors_tmp[$a['author_id']] = $a;
         }
         $scores = $this->fetch_avg_scores($args['namespace'], 'author', $order, $author_filter);
         $offset = $post_select_offset ? $post_select_offset : 0;
         $limit = $post_select_limit ? $post_select_limit : 0;
         $j = 0;
         $authors_sorted = array();
         foreach ($scores as $score) {
             if (array_key_exists($score['objectscore_object_id'], $authors_tmp)) {
                 if ($offset && $j++ < $offset) {
                     continue;
                 }
                 array_push($authors_sorted, $authors_tmp[$score['objectscore_object_id']]);
                 unset($authors_tmp[$score['objectscore_object_id']]);
                 if ($limit > 0 && count($authors_sorted) >= $limit) {
                     break;
                 }
             }
         }
         $authors = $authors_sorted;
     }
     return $authors;
 }
コード例 #2
0
ファイル: mtdb.base.php プロジェクト: OCMO/movabletype
 public function fetch_authors($args)
 {
     # Adds blog join
     $extras = array();
     $blog_ids = $this->include_exclude_blogs($args);
     $mt = MT::get_instance();
     $ctx = $mt->context();
     $blog_ids or $blog_ids = " = " . $ctx->stash('blog_id');
     # Adds author filter
     if (isset($args['author_id'])) {
         $author_id = intval($args['author_id']);
         $author_filter = " and author_id = {$author_id}";
     }
     if (isset($args['author_nickname'])) {
         $author_filter .= " and author_nickname = '" . $args['author_nickname'] . "'";
     }
     if (isset($args['author_name'])) {
         $author_filter .= " and author_name = '" . $args['author_name'] . "'";
     }
     # Adds entry join and filter
     if (isset($args['any_type']) && $args['any_type'] && !isset($args['need_entry'])) {
         $args['need_entry'] = 0;
     }
     if (!isset($args['need_entry'])) {
         $args['need_entry'] = 1;
     }
     if ($args['need_entry']) {
         $extras['join']['mt_entry'] = array('condition' => "author_id = entry_author_id");
         $extras['distinct'] = 'distinct';
         $entry_filter = " and entry_status = 2";
         if ($blog_ids) {
             $entry_filter .= " and entry_blog_id " . $blog_ids;
         }
     } else {
         $extras['distinct'] = 'distinct';
         if (!isset($args['roles']) and !isset($args['role'])) {
             $join_sql = "permission_author_id = author_id";
             if (isset($args['need_association']) && $args['need_association']) {
                 $join_sql .= " and permission_blog_id" . $blog_ids;
             }
             if (!$args['any_type']) {
                 $join_sql .= "\n                        and (\n                            (\n                                permission_blog_id {$blog_ids}\n                                and (\n                                    permission_permissions like '%create_post%' or\n                                    permission_permissions like '%publish_post%'\n                                )\n                            ) or (\n                                permission_blog_id = 0\n                                and \n                                    permission_permissions like '%administer%'  \n                           )\n                       )\n                    ";
             } else {
                 $join_sql .= "\n                        and\n                            (\n                                (\n                                    permission_blog_id {$blog_ids}\n                                    and permission_permissions is not null\n                                )\n                            or\n                                (\n                                    permission_blog_id = 0\n                                    and\n                                        (\n                                            permission_permissions like '%administer%'\n                                            or\n                                            permission_permissions like '%comment%'\n                                        )\n                                )\n                            )\n                        ";
             }
             $sql = $join_sql;
             $extras['join']['mt_permission'] = array('condition' => $sql);
         }
     }
     # a context hash for filter routines
     $ctx = array();
     $filters = array();
     if (isset($args['status'])) {
         $status_arg = $args['status'];
         require_once "MTUtil.php";
         $status = array(array('name' => 'enabled', 'id' => 1), array('name' => 'disabled', 'id' => 2));
         $cexpr = create_status_expr_function($status_arg, $status);
         if ($cexpr) {
             $filters[] = $cexpr;
         }
     }
     if (isset($args['roles']) or isset($args['role'])) {
         $role_arg = isset($args['role']) ? $args['role'] : $args['roles'];
         require_once "MTUtil.php";
         $roles = $this->fetch_all_roles();
         if (!is_array($roles)) {
             $roles = array();
         }
         $cexpr = create_role_expr_function($role_arg, $roles);
         if ($cexpr) {
             $rmap = array();
             $role_list = array();
             foreach ($roles as $role) {
                 $role_list[] = $role->role_id;
             }
             $as = $this->fetch_associations(array('blog_id' => $blog_id, 'role_id' => $role_list));
             foreach ($as as $a) {
                 if ($a->association_type == 2 || $a->association_type == 5) {
                     $as2 = $this->fetch_associations(array('group_id' => array($a->association_group_id), 'type' => 3, 'blog_id' => 0));
                     foreach ($as2 as $a2) {
                         $rmap[$a2->association_author_id][$a->association_role_id]++;
                     }
                 } else {
                     $rmap[$a->association_author_id][$a->association_role_id]++;
                 }
             }
             $ctx['r'] =& $rmap;
             $filters[] = $cexpr;
         }
     }
     # Adds a score or rate filter to the filters list.
     $re_sort = false;
     if (isset($args['namespace'])) {
         if (isset($args['scoring_to'])) {
             require_once "MTUtil.php";
             require_once "rating_lib.php";
             $type = $args['scoring_to'];
             $obj = $args['_scoring_to_obj'];
             $field_name = $type . '_id';
             $obj_id = $obj->{$field_name};
             if (isset($args['min_score'])) {
                 $expr = '$ctx = $c;if ($ctx == null) { $mt = MT::get_instance(); $ctx = $mt->context(); } ';
                 $expr .= '$sc = get_score($ctx, ' . $obj_id . ', "' . $type . '", "' . $args['namespace'] . '", $e->author_id);';
                 $expr .= '$ret = $sc >= ' . $args['min_score'] . ';';
                 $expr .= ' return $ret;';
                 $fn = create_function('&$e,&$c', $expr);
                 $filters[] = $fn;
             } elseif (isset($args['max_score'])) {
                 $expr = '$ctx = $c;if ($ctx == null) { $mt = MT::get_instance(); $ctx = $mt->context(); } ';
                 $expr .= '$sc = get_score($ctx, ' . $obj_id . ', "' . $type . '", "' . $args['namespace'] . '", $e->author_id);';
                 $expr .= '$ret = $sc <= ' . $args['max_score'] . ';';
                 $expr .= ' return $ret;';
                 $fn = create_function('&$e,&$c', $expr);
                 $filters[] = $fn;
             } else {
                 $expr = '$ctx = $c;if ($ctx == null) { $mt = MT::get_instance(); $ctx = $mt->context(); } ';
                 $expr .= '$ret = !is_null($ctx->mt->db()->fetch_score(' . $args['namespace'] . ',' . $obj_id . ', $e->author_id,' . $type . '));';
                 $expr .= ' return $ret;';
                 $fn = create_function('&$e,&$c', $expr);
                 $filters[] = $fn;
             }
         } else {
             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'], 'author');
                     if ($cexpr) {
                         $filters[] = $cexpr;
                         $re_sort = true;
                     } else {
                         return null;
                     }
                 }
             }
         }
     }
     # sort
     $join_score = "";
     if (isset($args['sort_by'])) {
         if ($args['sort_by'] == 'score' || $args['sort_by'] == 'rate') {
             $extras['join']['mt_objectscore'] = array('condition' => "objectscore_object_id = author_id and objectscore_namespace='" . $args['namespace'] . "' and objectscore_object_ds='author'");
             $extras['distinct'] = "distinct";
             $order_sql = "order by author_created_on desc";
             $re_sort = true;
         } else {
             $sort_col = $args['sort_by'];
             if (strtolower($sort_col) == 'display_name') {
                 $sort_col = 'nickname';
             }
             if (!preg_match('/^author_/i', $sort_col)) {
                 $sort_col = 'author_' . $sort_col;
             }
             $order = '';
             if (isset($args['sort_order'])) {
                 if ($args['sort_order'] == 'ascend') {
                     $order = 'asc';
                 } else {
                     $order = 'desc';
                 }
             }
             $order_sql = "order by {$sort_col} {$order}";
             if (isset($args['start_string'])) {
                 $val = $args['start_string'];
                 if ($order == 'asc') {
                     $val_order = '>';
                 } else {
                     $val_order = '<';
                 }
                 $sort_filter = " and {$sort_col} {$val_order} '{$val}'";
             }
             if (isset($args['start_num'])) {
                 $val = $args['start_num'];
                 if ($order == 'asc') {
                     $val_order = '>';
                 } else {
                     $val_order = '<';
                 }
                 $sort_filter .= " and {$sort_col} {$val_order} {$val}";
             }
         }
     }
     $limit = 0;
     if (isset($args['limit'])) {
         $limit = $args['limit'];
     }
     $lastn = isset($args['lastn']) ? $args['lastn'] : 0;
     if ($re_sort) {
         $post_select_limit = $lastn;
         $lastn = 0;
         $post_select_offset = isset($args['offset']) ? $args['offset'] : 0;
     }
     $where = "1 = 1\n                  {$author_filter}\n                  {$entry_filter}\n                  {$sort_filter}\n                  {$order_sql}\n        ";
     if ($limit) {
         $extras['limit'] = $limit;
     }
     require_once 'class.mt_author.php';
     $author = new Author();
     $results = $author->Find($where, false, false, $extras);
     $authors = array();
     if ($args['sort_by'] != 'score' && $args['sort_by'] != 'rate') {
         $offset = $post_select_offset ? $post_select_offset : 0;
         $limit = $post_select_limit ? $post_select_limit : 0;
     }
     $j = 0;
     $i = -1;
     while (true) {
         $i++;
         $e = $results[$i];
         if ($offset && $j++ < $offset) {
             continue;
         }
         if (empty($e)) {
             break;
         }
         if (count($filters)) {
             foreach ($filters as $f) {
                 if (!$f($e, $ctx)) {
                     continue 2;
                 }
             }
         }
         $authors[] = $e;
         if ($lastn > 0 && count($authors) >= $lastn) {
             break;
         }
     }
     if (isset($args['sort_by']) && 'score' == $args['sort_by']) {
         $authors_tmp = array();
         $order = 'desc';
         if (isset($args['sort_order'])) {
             $order = $args['sort_order'] == 'ascend' ? 'asc' : 'desc';
         }
         foreach ($authors as $a) {
             $authors_tmp[$a->author_id] = $a;
         }
         $scores = $this->fetch_sum_scores($args['namespace'], 'author', $order, $author_filter);
         $offset = $post_select_offset ? $post_select_offset : 0;
         $limit = $post_select_limit ? $post_select_limit : 0;
         $j = 0;
         $authors_sorted = array();
         foreach ($scores as $score) {
             if (array_key_exists($score['objectscore_object_id'], $authors_tmp)) {
                 if ($offset && $j++ < $offset) {
                     continue;
                 }
                 array_push($authors_sorted, $authors_tmp[$score['objectscore_object_id']]);
                 unset($authors_tmp[$score['objectscore_object_id']]);
                 if ($limit > 0 && count($authors_sorted) >= $limit) {
                     break;
                 }
             }
         }
         $authors = $authors_sorted;
     } elseif (isset($args['sort_by']) && 'rate' == $args['sort_by']) {
         $authors_tmp = array();
         $order = 'asc';
         if (isset($args['sort_order'])) {
             $order = $args['sort_order'] == 'ascend' ? 'asc' : 'desc';
         }
         foreach ($authors as $a) {
             $authors_tmp[$a->author_id] = $a;
         }
         $scores = $this->fetch_avg_scores($args['namespace'], 'author', $order, $author_filter);
         $offset = $post_select_offset ? $post_select_offset : 0;
         $limit = $post_select_limit ? $post_select_limit : 0;
         $j = 0;
         $authors_sorted = array();
         foreach ($scores as $score) {
             if (array_key_exists($score['objectscore_object_id'], $authors_tmp)) {
                 if ($offset && $j++ < $offset) {
                     continue;
                 }
                 array_push($authors_sorted, $authors_tmp[$score['objectscore_object_id']]);
                 unset($authors_tmp[$score['objectscore_object_id']]);
                 if ($limit > 0 && count($authors_sorted) >= $limit) {
                     break;
                 }
             }
         }
         $authors = $authors_sorted;
     }
     return $authors;
 }