Permalink
Browse files

Issue #501: Add pagination to link listings

* Links from People You Follow, Links from Favorites, and Photos from People You Follow now have pagination
* Updated LinkDAO interface and tests
  • Loading branch information...
1 parent b166464 commit 4d8f572e8ab540cff38174204f1c0ad79fda3ace @amygdala amygdala committed with ginatrapani Dec 28, 2010
@@ -243,6 +243,10 @@ public function testGetLinksByFriends(){
$this->assertEqual($val->container_post->in_reply_to_post_id, 0);
$this->assertTrue($posts[$num]['fr']);
}
+ // check pagination
+ $result = $this->DAO->getLinksByFriends(2, 'twitter', 5, 2);
+ $this->assertIsA($result, "array");
+ $this->assertEqual(count($result), 5);
}
@@ -274,6 +278,10 @@ public function testGetPhotosByFriends(){
$this->assertEqual($val->container_post->in_reply_to_post_id, 0);
$this->assertTrue($posts[$num]['fr']);
}
+ // check pagination
+ $result = $this->DAO->getPhotosByFriends(2, 'twitter', 5, 2);
+ $this->assertIsA($result, "array");
+ $this->assertEqual(count($result), 4);
}
/**
@@ -386,4 +394,45 @@ public function testGetFavoritedLinks() {
$lbuilders = null;
}
+ public function testGetFavoritedLinksPaging() {
+ $lbuilders = array();
+ $counter = 0;
+ while ($counter < 15) {
+ $post_id = $counter + 280;
+ $pseudo_minute = str_pad(($counter), 2, "0", STR_PAD_LEFT);
+ $lbuilders[] = FixtureBuilder::build('links', array('url'=>'http://example2.com/'.$counter,
+ 'title'=>'Link '.$counter, 'clicks'=>0, 'post_id'=>$post_id, 'network'=>'twitter', 'is_image'=>0,
+ 'expanded_url'=>'', 'error'=>''));
+ $counter++;
+ }
+ //create posts-- links will be associated with the first 15 of them
+ $counter = 0;
+ while ($counter < 30) {
+ $post_id = $counter + 280;
+ $user_id = ($counter * 5) + 2;
+ $pseudo_minute = str_pad(($counter), 2, "0", STR_PAD_LEFT);
+
+ $lbuilders[] = FixtureBuilder::build('posts', array('post_id'=>$post_id, 'author_user_id'=>$user_id,
+ 'author_username'=>"user$counter", 'author_fullname'=>"User$counter Name$counter",
+ 'author_avatar'=>'avatar.jpg', 'post_text'=>'This is post '.$post_id, 'pub_date'=>'2009-01-01 00:'.
+ $pseudo_minute.':00', 'network'=>'twitter',
+ 'in_reply_to_post_id'=>null, 'in_retweet_of_post_id'=>null, 'is_geo_encoded'=>0));
+
+ // user '20' favorites the first 20 of the test posts, only 15 of which will have links
+ if ($counter < 20) {
+ $lbuilders[] = FixtureBuilder::build('favorites', array('post_id'=>$post_id,
+ 'author_user_id'=>$user_id, 'fav_of_user_id'=>20, 'network'=>'twitter'));
+ }
+ $counter++;
+ }
+ // 1st page, default count is 15
+ $result = $this->DAO->getLinksByFavorites(20, 'twitter');
+ $this->assertIsA($result, "array");
+ $this->assertEqual(count($result), 15);
+ // 2nd page, ask for count of 10. So, there should be 5 favs returned.
+ $result = $this->DAO->getLinksByFavorites(20, 'twitter', 10, 2);
+ $this->assertEqual(count($result), 5);
+
+ $lbuilders = null;
+ }
}
@@ -108,7 +108,9 @@ public function update( $url, $expanded, $title, $post_id, $network, $is_image =
return $this->getUpdateCount($ps);
}
- public function getLinksByFriends($user_id, $network) {
+ public function getLinksByFriends($user_id, $network, $count = 15, $page = 1) {
+ $start_on_record = ($page - 1) * $count;
+
$q = "SELECT l.*, p.*, pub_date + interval #gmt_offset# hour AS adj_pub_date ";
$q .= "FROM #prefix#posts AS p ";
$q .= "INNER JOIN #prefix#links AS l ";
@@ -118,10 +120,12 @@ public function getLinksByFriends($user_id, $network) {
$q .= " WHERE f.follower_id=:user_id AND f.active=1 AND f.network=:network ";
$q .= ")";
$q .= "ORDER BY l.post_id DESC ";
- $q .= "LIMIT 15 ";
+ $q .= "LIMIT :start_on_record, :limit";
$vars = array(
':user_id'=>$user_id,
- ':network'=>$network
+ ':network'=>$network,
+ ':limit'=>$count,
+ ':start_on_record'=>(int)$start_on_record
);
$ps = $this->execute($q, $vars);
$all_rows = $this->getDataRowsAsArrays($ps);
@@ -144,23 +148,28 @@ private function setLinkWithPost($row) {
return $link;
}
- public function getLinksByFavorites($user_id, $network) {
+ public function getLinksByFavorites($user_id, $network, $count = 15, $page = 1) {
+ $start_on_record = ($page - 1) * $count;
+
$q = "SELECT l.*, p.*, pub_date - interval 8 hour AS adj_pub_date ";
$q .= "FROM #prefix#posts as p, #prefix#favorites as f, #prefix#links as l WHERE f.post_id = p.post_id ";
- // $q .= "INNER JOIN #prefix#links AS l ";
$q .= "AND p.post_id = l.post_id AND p.network = l.network ";
$q .= "AND l.network = :network AND f.fav_of_user_id = :user_id ";
$q .= "ORDER BY l.post_id DESC ";
- $q .= "LIMIT 15 ";
+ $q .= "LIMIT :start_on_record, :limit";
$vars = array(
':user_id'=>$user_id,
- ':network'=>$network
+ ':network'=>$network,
+ ':limit'=>$count,
+ ':start_on_record'=>(int)$start_on_record
);
$ps = $this->execute($q, $vars);
return $this->getDataRowsAsObjects($ps, "Link");
}
- public function getPhotosByFriends($user_id, $network) {
+ public function getPhotosByFriends($user_id, $network, $count = 15, $page = 1) {
+ $start_on_record = ($page - 1) * $count;
+
$q = "SELECT l.*, p.*, pub_date + interval #gmt_offset# hour as adj_pub_date ";
$q .= "FROM #prefix#links AS l ";
$q .= "INNER JOIN #prefix#posts p ";
@@ -169,10 +178,12 @@ public function getPhotosByFriends($user_id, $network) {
$q .= " SELECT user_id FROM #prefix#follows AS f ";
$q .= " WHERE f.follower_id=:user_id AND f.active=1 AND f.network = :network) ";
$q .= "ORDER BY l.post_id DESC ";
- $q .= "LIMIT 15 ";
+ $q .= "LIMIT :start_on_record, :limit";
$vars = array(
':user_id'=>$user_id,
- ':network'=>$network
+ ':network'=>$network,
+ ':limit'=>$count,
+ ':start_on_record'=>(int)$start_on_record
);
$ps = $this->execute($q, $vars);
$all_rows = $this->getDataRowsAsArrays($ps);
@@ -60,7 +60,7 @@ public function saveExpandedURL($url, $expanded, $title = '', $is_image = false
public function saveExpansionError($url, $error_text);
/**
- * Updates a URL in storage
+ * Updates a URL in storage.
* @param str $url
* @param str $expanded
* @param str $title
@@ -72,24 +72,38 @@ public function saveExpansionError($url, $error_text);
public function update($url, $expanded, $title, $post_id, $network, $is_image = false );
/**
- * Get the links posted by a users friends
+ * Get the links posted by a user's friends.
* @param int $user_id
* @param str $network
+ * @param int $count
+ * @param int $page
* @return array with Link objects
*/
- public function getLinksByFriends($user_id, $network);
+ public function getLinksByFriends($user_id, $network, $count = 15, $page = 1);
/**
- * Get the images posted by a users friends
+ * Get the links in a user's favorites.
* @param int $user_id
* @param str $network
+ * @param int $count
+ * @param int $page
+ * @return array with Link objects
+ */
+ public function getLinksByFavorites($user_id, $network, $count = 15, $page = 1);
+
+ /**
+ * Get the images posted by a user's friends.
+ * @param int $user_id
+ * @param str $network
+ * @param int $count
+ * @param int $page
* @return array numbered keys, with Link objects
*/
- public function getPhotosByFriends($user_id, $network);
+ public function getPhotosByFriends($user_id, $network, $count = 15, $page = 1);
/**
* Gets a number of links that has not been expanded.
- * Non standard output - Sceduled for deprecation.
+ * Non standard output - Scheduled for deprecation.
* @param int $limit
* @return array with numbered keys, with strings
*/
@@ -313,19 +313,22 @@ public function getDashboardMenuItems($instance) {
//Links from friends
$fltab = new MenuItem('Links from People You Follow', 'Links your friends posted', $twitter_data_tpl, 'Links');
- $fltabds = new Dataset("links", 'LinkDAO', "getLinksByFriends", array($instance->network_user_id, 'twitter'));
+ $fltabds = new Dataset("links", 'LinkDAO', "getLinksByFriends",
+ array($instance->network_user_id, 'twitter', 15, '#page_number#'));
$fltab->addDataset($fltabds);
$menus["links-friends"] = $fltab;
//Links from favorites
$lftab = new MenuItem('Links From Favorites', 'Links in posts you favorited', $twitter_data_tpl);
- $lftabds = new Dataset("links", 'LinkDAO', "getLinksByFavorites", array($instance->network_user_id, 'twitter'));
+ $lftabds = new Dataset("links", 'LinkDAO', "getLinksByFavorites",
+ array($instance->network_user_id, 'twitter', 15, '#page_number#'));
$lftab->addDataset($lftabds);
$menus["links-favorites"] = $lftab;
//Photos
$ptab = new MenuItem("Photos from People You Follow", 'Photos your friends have posted', $twitter_data_tpl);
- $ptabds = new Dataset("links", 'LinkDAO', "getPhotosByFriends", array($instance->network_user_id, 'twitter'));
+ $ptabds = new Dataset("links", 'LinkDAO', "getPhotosByFriends",
+ array($instance->network_user_id, 'twitter', 15, '#page_number#'));
$ptab->addDataset($ptabds);
$menus["links-photos"] = $ptab;

0 comments on commit 4d8f572

Please sign in to comment.