/** * Get posts by info * - has:info => a post info key or array of post info keys, which should be present * - all:info => a post info key and value pair or array of post info key and value pairs, which should all be present and match * - not:all:info => a post info key and value pair or array of post info key and value pairs, to exclude if all are present and match * - any:info => a post info key and value pair or array of post info key and value pairs, any of which can match * - not:any:info => a post info key and value pair or array of post info key and value pairs, to exclude if any are present and match */ public function test_get_posts_by_info() { // setup $informationless_post = Post::create(array('title' => 'This is a Post without information', 'content' => 'The real point of this post is to make sure that there is at least one countable post without info for the sake of testing.', 'user_id' => $this->user->id, 'status' => Post::status('published'), 'content_type' => Post::type('entry'), 'pubdate' => DateTime::date_create(time()))); $seven_things = array("one", "two", "red", "blue", "black", "old", "new"); // create some posts with info for ($i = 1; $i < 42; $i++) { $post = Post::create(array('title' => 'This Post has Info', 'content' => 'If this were really a post, would it have such useless information?', 'user_id' => $this->user->id, 'status' => Post::status('published'), 'content_type' => Post::type('entry'), 'pubdate' => DateTime::date_create(time()))); $post->info->testing_info = 1; $post->info->{$seven_things}[$i % 7] = 1; $post->info->i = $i; $post->info->commit(); } // has:info $count = DB::get_value("SELECT COUNT(*) FROM {posts}\n\t\t\t\tLEFT JOIN {postinfo} pi1 ON\n\t\t\t\t\t{posts}.id = pi1.post_id AND\n\t\t\t\t\tpi1.name = 'red'\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tpi1.name <> ''\n\t\t"); $count_info_posts = Posts::get(array('ignore_permissions' => true, 'has:info' => 'testing_info', 'count' => 'DISTINCT {posts}.id', 'nolimit' => 1)); $this->assert_not_equal(Posts::count_total(), $count_info_posts); $count_posts = Posts::get(array('ignore_permissions' => true, 'has:info' => array('red'), 'count' => 'DISTINCT {posts}.id', 'nolimit' => 1)); $this->assert_equal($count_posts, $count); $count = DB::get_value("SELECT COUNT(*) FROM {posts}\n\t\t\t\tLEFT JOIN {postinfo} pi1 ON\n\t\t\t\t\t{posts}.id = pi1.post_id AND\n\t\t\t\t\tpi1.name = 'testing_info'\n\t\t\t\tLEFT JOIN {postinfo} pi2 ON\n\t\t\t\t\t{posts}.id = pi2.post_id AND\n\t\t\t\t\tpi2.name = 'red'\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tpi1.name <> '' OR\n\t\t\t\t\t\tpi2.name <> ''\n\t\t"); $count_posts = Posts::get(array('ignore_permissions' => true, 'has:info' => array('testing_info', 'red'), 'count' => 1, 'nolimit' => 1)); $this->assert_equal($count_posts, $count); // $query = Posts::get( array( 'has:info' => array( 'testing_info', 'red' ), 'nolimit' => 1, 'fetch_fn' => 'get_query' ) ); // Utils::debug( $query );die(); // all:info $count = DB::get_value("SELECT COUNT(*) FROM {posts}\n\t\t\t\tLEFT JOIN {postinfo} pi1 ON\n\t\t\t\t\t{posts}.id = pi1.post_id AND\n\t\t\t\t\tpi1.name = 'blue' AND pi1.value = 1\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tpi1.name <> ''\n\t\t"); $count_posts = Posts::get(array('ignore_permissions' => true, 'all:info' => array('blue' => 1), 'count' => 1, 'nolimit' => 1)); $this->assert_equal($count_posts, $count); $count = DB::get_value("SELECT COUNT(*) FROM {posts}\n\t\t\t\tLEFT JOIN {postinfo} pi1 ON\n\t\t\t\t\t{posts}.id = pi1.post_id AND\n\t\t\t\t\tpi1.name = 'blue' AND pi1.value = 1\n\t\t\t\tLEFT JOIN {postinfo} pi2 ON\n\t\t\t\t\t{posts}.id = pi2.post_id AND\n\t\t\t\t\tpi2.name = 'two' AND pi2.value = 1\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tpi1.name <> '' AND\n\t\t\t\t\t\tpi2.name <> ''\n\t\t"); $count_posts = Posts::get(array('ignore_permissions' => true, 'all:info' => array('blue' => true, 'two' => true), 'count' => 1, 'nolimit' => 1)); $this->assert_equal($count_posts, $count); // any:info $count = DB::get_value("SELECT COUNT(*) FROM {posts}\n\t\t\t\tLEFT JOIN {postinfo} pi1 ON\n\t\t\t\t\t{posts}.id = pi1.post_id AND\n\t\t\t\t\tpi1.name = 'black' AND\n\t\t\t\t\tpi1.value = 1\n\t\t\t\tLEFT JOIN {postinfo} pi2 ON\n\t\t\t\t\t{posts}.id = pi2.post_id AND\n\t\t\t\t\tpi2.name = 'blue' AND\n\t\t\t\t\tpi2.value = 1\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tpi1.name <> '' OR\n\t\t\t\t\t\tpi2.name <> ''\n\t\t"); $count_posts = Posts::get(array('ignore_permissions' => true, 'any:info' => array('black' => 1, 'blue' => 1), 'count' => 1, 'nolimit' => 1)); $this->assert_equal($count_posts, $count); $count = Posts::get(array('ignore_permissions' => true, 'all:info' => array('black' => 1), 'count' => 1, 'nolimit' => 1)) + Posts::get(array('all:info' => array('blue' => true), 'count' => 1, 'nolimit' => 1)); $this->assert_equal($count_posts, $count); $count = DB::get_value("SELECT COUNT(*) FROM {posts}\n\t\t\t\tLEFT JOIN {postinfo} pi1 ON\n\t\t\t\t\t{posts}.id = pi1.post_id AND\n\t\t\t\t\tpi1.name = 'i' AND\n\t\t\t\t\tpi1.value IN ( 0,1,2,3,4,5 )\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tpi1.name <> ''\n\t\t"); $params = array('ignore_permissions' => true, 'any:info' => array('i' => array(1, 2, 3, 4, 5)), 'count' => 1, 'nolimit' => 1); //$this->output(Posts::get(array_merge($params, array('fetch_fn' => 'get_query')))); $count_posts = Posts::get($params); $this->assert_equal($count_posts, $count); // not:all:info $count = DB::get_value("SELECT COUNT(*) FROM {posts} WHERE\n\t\t\t\t{posts}.id NOT IN (\n\t\t\t\t\tSELECT post_id FROM {postinfo}\n\t\t\t\t\t\tWHERE ( name = 'testing_info' AND value = 1 )\n\t\t\t\t\t\tGROUP BY post_id\n\t\t\t\t\t\tHAVING COUNT(*) = 1\n\t\t\t\t)\n\t\t"); $count_posts = Posts::get(array('ignore_permissions' => true, 'not:all:info' => array('testing_info' => 1), 'nolimit' => 1, 'count' => 1)); $this->assert_equal($count_posts, $count, _t('not:all:info expected %d, got %d', array($count, $count_posts))); $count = DB::get_value("SELECT COUNT(*) FROM {posts} WHERE\n\t\t\t\t{posts}.id NOT IN (\n\t\t\t\t\tSELECT post_id FROM {postinfo}\n\t\t\t\t\t\tWHERE ( name = 'one' AND value = 1 )\n\t\t\t\t\t\tGROUP BY post_id\n\t\t\t\t\t\tHAVING COUNT(*) = 1\n\t\t\t\t)\n\t\t"); $count_posts = Posts::get(array('ignore_permissions' => true, 'not:all:info' => array('one' => 1), 'count' => 1, 'nolimit' => 1)); $this->assert_equal($count_posts, $count); $count = DB::get_value("SELECT COUNT(*) FROM {posts} WHERE\n\t\t\t\t{posts}.id NOT IN (\n\t\t\t\t\tSELECT post_id FROM {postinfo}\n\t\t\t\t\t\tWHERE ( name = 'old' AND value = 1 OR\n\t\t\t\t\t\t name = 'new' AND value = 1 )\n\t\t\t\t\t\tGROUP BY post_id\n\t\t\t\t\t\tHAVING COUNT(*) = 2\n\t\t\t\t)\n\t\t"); $count_posts = Posts::get(array('ignore_permissions' => true, 'not:all:info' => array('old' => 1, 'new' => 1), 'count' => 1, 'nolimit' => 1)); $this->assert_equal($count_posts, $count); // not:any:info $count = DB::get_value("SELECT COUNT(*) FROM {posts} WHERE\n\t\t\t\t{posts}.id NOT IN (\n\t\t\t\t\tSELECT post_id FROM {postinfo}\n\t\t\t\t\t\tWHERE ( {postinfo}.name = 'two' AND {postinfo}.value = 1 )\n\t\t\t\t)\n\t\t"); $count_posts = Posts::get(array('ignore_permissions' => true, 'not:any:info' => array('two' => 1), 'count' => 1, 'nolimit' => 1)); $this->assert_equal($count_posts, $count); $count = DB::get_value("SELECT COUNT(*) FROM {posts} WHERE\n\t\t\t\t{posts}.id NOT IN (\n\t\t\t\t\tSELECT post_id FROM {postinfo}\n\t\t\t\t\t\tWHERE ( {postinfo}.name = 'black' AND {postinfo}.value = 1 OR\n\t\t\t\t\t\t {postinfo}.name = 'blue' AND {postinfo}.value = 1 )\n\t\t\t\t)\n\t\t"); $count_posts = Posts::get(array('ignore_permissions' => true, 'not:any:info' => array('black' => 1, 'blue' => 1), 'count' => 1, 'nolimit' => 1)); $this->assert_equal($count_posts, $count); // $query = Posts::get( array( 'not:any:info' => array( 'comments_disabled' => 1, 'html_title' => 'Chili, The Breakfast of Champions' ), 'nolimit' => 1, 'fetch_fn' => 'get_query' ) ); // Utils::debug( $query );die(); // teardown Posts::get(array('ignore_permissions' => true, 'has:info' => 'testing_info', 'nolimit' => 1))->delete(); $informationless_post->delete(); }
/** * Output a post collection based on the provided parameters. * * @param array $params An array of parameters as passed to Posts::get() to retrieve posts. */ public function get_collection($params = array()) { // Store handler vars since we'll be using them a lot. $handler_vars = Controller::get_handler_vars(); // Retrieve the current matched rule and store its name and argument values. $rr = URL::get_matched_rule(); $rr_name = $rr->name; $rr_args = $rr->named_arg_values; // Assign alternate links based on the matched rule. $alternate_rules = array('atom_feed_tag' => 'display_entries_by_tag', 'atom_feed' => 'display_home', 'atom_entry' => 'display_entry', 'atom_feed_entry_comments' => 'display_entry', 'atom_feed_page_comments' => 'display_entry', 'atom_feed_comments' => 'display_home'); $alternate_rules = Plugins::filter('atom_get_collection_alternate_rules', $alternate_rules); $alternate = URL::get($alternate_rules[$rr_name], $handler_vars, false); // Assign self link based on the matched rule. $self = URL::get($rr_name, $rr_args, false); $id = isset($rr_args_values['tag']) ? $rr_args_values['tag'] : 'atom'; $xml = $this->create_atom_wrapper($alternate, $self, $id); $xml = $this->add_pagination_links($xml, Posts::count_total(Post::status('published'))); // Get posts to put in the feed $page = isset($rr_args['page']) ? $rr_args['page'] : 1; if ($page > 1) { $params['page'] = $page; } if (!isset($params['content_type'])) { $params['content_type'] = Post::type('entry'); } $params['content_type'] = Plugins::filter('atom_get_collection_content_type', $params['content_type']); $params['status'] = Post::status('published'); $params['orderby'] = 'updated DESC'; $params['limit'] = Options::get('atom_entries'); $params = array_merge($params, $rr_args); if (array_key_exists('tag', $params)) { $params['tag_slug'] = Utils::slugify($params['tag']); unset($params['tag']); } $posts = Posts::get($params); $xml = $this->add_posts($xml, $posts); Plugins::act('atom_get_collection', $xml, $params, $handler_vars); $xml = $xml->asXML(); ob_clean(); header('Content-Type: application/atom+xml'); print $xml; }
/** * Output a post collection based on the provided parameters. * * @param array $params An array of parameters as passed to Posts::get() to retrieve posts. */ public function get_collection($params = array()) { // Store handler vars since we'll be using them a lot. $handler_vars = Controller::get_handler_vars(); // Retrieve the current matched rule and store its name and argument values. $rr = URL::get_matched_rule(); $rr_name = $rr->name; $rr_args = $rr->named_arg_values; // Assign alternate links based on the matched rule. $alternate_rules = array('atom_feed_tag' => 'display_entries_by_tag', 'atom_feed' => 'display_home', 'atom_entry' => 'display_entry', 'atom_feed_entry_comments' => 'display_entry', 'atom_feed_page_comments' => 'display_entry', 'atom_feed_comments' => 'display_home'); $alternate_rules = Plugins::filter('atom_get_collection_alternate_rules', $alternate_rules); $alternate = URL::get($alternate_rules[$rr_name], $handler_vars, false); // Assign self link based on the matched rule. $self = URL::get($rr_name, $rr_args, false); // Get posts to put in the feed $page = isset($rr_args['page']) ? $rr_args['page'] : 1; if ($page > 1) { $params['page'] = $page; } if (!isset($params['content_type'])) { $params['content_type'] = Post::type('entry'); } $params['content_type'] = Plugins::filter('atom_get_collection_content_type', $params['content_type']); $params['status'] = $this->is_auth() ? 'any' : Post::status('published'); $params['orderby'] = 'updated DESC'; $params['limit'] = Options::get('atom_entries'); $params = array_merge($params, $rr_args); if (array_key_exists('tag', $params)) { $id = urlencode($params['tag']); $tags = explode(' ', $params['tag']); foreach ($tags as $tag) { if ($tag[0] == '-') { $tag = substr($tag, 1); $params['vocabulary'][Tags::vocabulary()->name . ':not:term'][] = Utils::slugify($tag); } else { $params['vocabulary'][Tags::vocabulary()->name . ':all:term'][] = Utils::slugify($tag); } } unset($params['tag']); } else { $id = 'atom'; } $posts = Posts::get($params); if (count($posts)) { $updated = $posts[0]->updated; } else { $updated = null; } $xml = $this->create_atom_wrapper($alternate, $self, $id, $updated); if ($this->is_auth()) { $xml = $this->add_pagination_links($xml, Posts::count_total()); } else { $xml = $this->add_pagination_links($xml, Posts::count_total(Post::status('published'))); } $xml = $this->add_posts($xml, $posts); Plugins::act('atom_get_collection', $xml, $params, $handler_vars); $xml = $xml->asXML(); ob_clean(); header('Content-Type: application/atom+xml'); print $this->tidy_xml($xml); }