Skip to content

Commit

Permalink
Merge pull request #3984 from wordpress-mobile/issue/3940-reader-tag-…
Browse files Browse the repository at this point in the history
…titles

Issue/3940 reader tag titles
  • Loading branch information
roundhill committed Apr 20, 2016
2 parents cf946c5 + a181dce commit 6d7b724
Show file tree
Hide file tree
Showing 18 changed files with 209 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
public class ReaderDatabase extends SQLiteOpenHelper {
protected static final String DB_NAME = "wpreader.db";
private static final int DB_VERSION = 112;
private static final int DB_VERSION = 114;

/*
* version history
Expand Down Expand Up @@ -64,6 +64,8 @@ public class ReaderDatabase extends SQLiteOpenHelper {
* 110 - added xpost_post_id and xpost_blog_id to tbl_posts
* 111 - added author_first_name to tbl_posts
* 112 - no structural change, just reset db
* 113 - added tag_title to tag tables
* 114 - renamed tag_name to tag_slug in tag tables
*/

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ private static int purgePostsForTag(SQLiteDatabase db, ReaderTag tag) {
}

int numToPurge = numPosts - MAX_POSTS_PER_TAG;
String[] args = {tag.getTagName(), Integer.toString(tag.tagType.toInt()), Integer.toString(numToPurge)};
String[] args = {tag.getTagSlug(), Integer.toString(tag.tagType.toInt()), Integer.toString(numToPurge)};
String where = "pseudo_id IN ("
+ " SELECT tbl_posts.pseudo_id FROM tbl_posts, tbl_post_tags"
+ " WHERE tbl_posts.pseudo_id = tbl_post_tags.pseudo_id"
Expand Down Expand Up @@ -233,7 +233,7 @@ public static int getNumPostsWithTag(ReaderTag tag) {
if (tag == null) {
return 0;
}
String[] args = {tag.getTagName(), Integer.toString(tag.tagType.toInt())};
String[] args = {tag.getTagSlug(), Integer.toString(tag.tagType.toInt())};
return SqlUtils.intForQuery(ReaderDatabase.getReadableDb(),
"SELECT count(*) FROM tbl_post_tags WHERE tag_name=? AND tag_type=?",
args);
Expand Down Expand Up @@ -385,7 +385,7 @@ public static int deletePostsWithTag(final ReaderTag tag) {
}

// first delete posts from tbl_post_tags, and if any were deleted next delete posts in tbl_posts that no longer exist in tbl_post_tags
String[] args = {tag.getTagName(), Integer.toString(tag.tagType.toInt())};
String[] args = {tag.getTagSlug(), Integer.toString(tag.tagType.toInt())};
int numDeleted = ReaderDatabase.getWritableDb().delete("tbl_post_tags",
"tag_name=? AND tag_type=?",
args);
Expand Down Expand Up @@ -415,7 +415,7 @@ public static String getOldestPubDateWithTag(final ReaderTag tag) {
+ " WHERE tbl_posts.post_id = tbl_post_tags.post_id AND tbl_posts.blog_id = tbl_post_tags.blog_id"
+ " AND tbl_post_tags.tag_name=? AND tbl_post_tags.tag_type=?"
+ " ORDER BY published LIMIT 1";
String[] args = {tag.getTagName(), Integer.toString(tag.tagType.toInt())};
String[] args = {tag.getTagSlug(), Integer.toString(tag.tagType.toInt())};
return SqlUtils.stringForQuery(ReaderDatabase.getReadableDb(), sql, args);
}

Expand All @@ -439,7 +439,7 @@ public static String getOldestPubDateInFeed(long feedId) {
public static void removeGapMarkerForTag(final ReaderTag tag) {
if (tag == null) return;

String[] args = {tag.getTagName(), Integer.toString(tag.tagType.toInt())};
String[] args = {tag.getTagSlug(), Integer.toString(tag.tagType.toInt())};
String sql = "UPDATE tbl_post_tags SET has_gap_marker=0 WHERE has_gap_marker!=0 AND tag_name=? AND tag_type=?";
ReaderDatabase.getWritableDb().execSQL(sql, args);
}
Expand All @@ -452,7 +452,7 @@ public static ReaderBlogIdPostId getGapMarkerForTag(final ReaderTag tag) {
return null;
}

String[] args = {tag.getTagName(), Integer.toString(tag.tagType.toInt())};
String[] args = {tag.getTagSlug(), Integer.toString(tag.tagType.toInt())};
String sql = "SELECT blog_id, post_id FROM tbl_post_tags WHERE has_gap_marker!=0 AND tag_name=? AND tag_type=?";
Cursor cursor = ReaderDatabase.getReadableDb().rawQuery(sql, args);
try {
Expand All @@ -474,7 +474,7 @@ public static void setGapMarkerForTag(long blogId, long postId, ReaderTag tag) {
String[] args = {
Long.toString(blogId),
Long.toString(postId),
tag.getTagName(),
tag.getTagSlug(),
Integer.toString(tag.tagType.toInt())
};
String sql = "UPDATE tbl_post_tags SET has_gap_marker=1 WHERE blog_id=? AND post_id=? AND tag_name=? AND tag_type=?";
Expand Down Expand Up @@ -511,7 +511,7 @@ public static void deletePostsOlderThanGapMarkerForTag(ReaderTag tag) {
long timestamp = getGapMarkerTimestampForTag(tag);
if (timestamp == 0) return;

String[] args = {Long.toString(timestamp), tag.getTagName(), Integer.toString(tag.tagType.toInt())};
String[] args = {Long.toString(timestamp), tag.getTagSlug(), Integer.toString(tag.tagType.toInt())};
String where = "pseudo_id IN (SELECT tbl_posts.pseudo_id FROM tbl_posts, tbl_post_tags"
+ " WHERE tbl_posts.timestamp < ?"
+ " AND tbl_posts.pseudo_id = tbl_post_tags.pseudo_id"
Expand Down Expand Up @@ -550,10 +550,10 @@ private static void setFollowStatusForPosts(long blogId, long feedId, boolean is
if (!isFollowed) {
if (blogId != 0) {
db.delete("tbl_post_tags", "blog_id=? AND tag_name=?",
new String[]{Long.toString(blogId), ReaderTag.TAG_NAME_FOLLOWED_SITES});
new String[]{Long.toString(blogId), ReaderTag.TAG_TITLE_FOLLOWED_SITES});
} else {
db.delete("tbl_post_tags", "feed_id=? AND tag_name=?",
new String[]{Long.toString(feedId), ReaderTag.TAG_NAME_FOLLOWED_SITES});
new String[]{Long.toString(feedId), ReaderTag.TAG_TITLE_FOLLOWED_SITES});
}
}

Expand Down Expand Up @@ -647,7 +647,7 @@ public static void addOrUpdatePosts(final ReaderTag tag, ReaderPostList posts) {

// now add to tbl_post_tags if a tag was passed
if (tag != null) {
String tagName = tag.getTagName();
String tagName = tag.getTagSlug();
int tagType = tag.tagType.toInt();
for (ReaderPost post: posts) {
stmtTags.bindLong (1, post.postId);
Expand Down Expand Up @@ -697,7 +697,7 @@ public static ReaderPostList getPostsWithTag(ReaderTag tag, int maxPosts, boolea
sql += " LIMIT " + Integer.toString(maxPosts);
}

String[] args = {tag.getTagName(), Integer.toString(tag.tagType.toInt())};
String[] args = {tag.getTagSlug(), Integer.toString(tag.tagType.toInt())};
Cursor cursor = ReaderDatabase.getReadableDb().rawQuery(sql, args);
try {
return getPostListFromCursor(cursor);
Expand Down Expand Up @@ -767,7 +767,7 @@ public static ReaderBlogIdPostIdList getBlogIdPostIdsWithTag(ReaderTag tag, int
sql += " LIMIT " + Integer.toString(maxPosts);
}

String[] args = {tag.getTagName(), Integer.toString(tag.tagType.toInt())};
String[] args = {tag.getTagSlug(), Integer.toString(tag.tagType.toInt())};
Cursor cursor = ReaderDatabase.getReadableDb().rawQuery(sql, args);
try {
if (cursor != null && cursor.moveToFirst()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,20 @@ public class ReaderTagTable {

protected static void createTables(SQLiteDatabase db) {
db.execSQL("CREATE TABLE tbl_tags ("
+ " tag_name TEXT COLLATE NOCASE,"
+ " tag_slug TEXT COLLATE NOCASE,"
+ " tag_title TEXT COLLATE NOCASE,"
+ " tag_type INTEGER DEFAULT 0,"
+ " endpoint TEXT,"
+ " date_updated TEXT,"
+ " PRIMARY KEY (tag_name, tag_type)"
+ " PRIMARY KEY (tag_slug, tag_type)"
+ ")");

db.execSQL("CREATE TABLE tbl_tags_recommended ("
+ " tag_name TEXT COLLATE NOCASE,"
+ " tag_slug TEXT COLLATE NOCASE,"
+ " tag_title TEXT COLLATE NOCASE,"
+ " tag_type INTEGER DEFAULT 0,"
+ " endpoint TEXT,"
+ " PRIMARY KEY (tag_name, tag_type)"
+ " PRIMARY KEY (tag_slug, tag_type)"
+ ")");
}

Expand Down Expand Up @@ -92,13 +94,14 @@ private static void addOrUpdateTags(ReaderTagList tagList) {
SQLiteStatement stmt = null;
try {
stmt = ReaderDatabase.getWritableDb().compileStatement(
"INSERT OR REPLACE INTO tbl_tags (tag_name, tag_type, endpoint) VALUES (?1,?2,?3)"
"INSERT OR REPLACE INTO tbl_tags (tag_slug, tag_title, tag_type, endpoint) VALUES (?1,?2,?3,?4)"
);

for (ReaderTag tag: tagList) {
stmt.bindString(1, tag.getTagName());
stmt.bindLong (2, tag.tagType.toInt());
stmt.bindString(3, tag.getEndpoint());
stmt.bindString(1, tag.getTagSlug());
stmt.bindString(2, tag.getTagTitle());
stmt.bindLong (3, tag.tagType.toInt());
stmt.bindString(4, tag.getEndpoint());
stmt.execute();
}

Expand All @@ -114,53 +117,50 @@ public static boolean tagExists(ReaderTag tag) {
if (tag == null) {
return false;
}
String[] args = {tag.getTagName(), Integer.toString(tag.tagType.toInt())};
String[] args = {tag.getTagSlug(), Integer.toString(tag.tagType.toInt())};
return SqlUtils.boolForQuery(ReaderDatabase.getReadableDb(),
"SELECT 1 FROM tbl_tags WHERE tag_name=?1 AND tag_type=?2",
"SELECT 1 FROM tbl_tags WHERE tag_slug=?1 AND tag_type=?2",
args);
}

/*
* returns true if the passed tag exists and it has the passed type
*/
private static boolean tagExistsOfType(String tagName, ReaderTagType tagType) {
if (TextUtils.isEmpty(tagName) || tagType == null) {
private static boolean tagExistsOfType(String tagSlug, ReaderTagType tagType) {
if (TextUtils.isEmpty(tagSlug) || tagType == null) {
return false;
}

String[] args = {tagName, Integer.toString(tagType.toInt())};
String[] args = {tagSlug, Integer.toString(tagType.toInt())};
return SqlUtils.boolForQuery(ReaderDatabase.getReadableDb(),
"SELECT 1 FROM tbl_tags WHERE tag_name=?1 AND tag_type=?2",
"SELECT 1 FROM tbl_tags WHERE tag_slug=?1 AND tag_type=?2",
args);
}

public static boolean isFollowedTagName(String tagName) {
return tagExistsOfType(tagName, ReaderTagType.FOLLOWED);
}

public static boolean isDefaultTagName(String tagName) {
return tagExistsOfType(tagName, ReaderTagType.DEFAULT);
public static boolean isFollowedTagName(String tagSlug) {
return tagExistsOfType(tagSlug, ReaderTagType.FOLLOWED);
}

private static ReaderTag getTagFromCursor(Cursor c) {
if (c == null) {
throw new IllegalArgumentException("null tag cursor");
}

String tagName = c.getString(c.getColumnIndex("tag_name"));
String tagSlug = c.getString(c.getColumnIndex("tag_slug"));
String tagTitle = c.getString(c.getColumnIndex("tag_title"));
String endpoint = c.getString(c.getColumnIndex("endpoint"));
ReaderTagType tagType = ReaderTagType.fromInt(c.getInt(c.getColumnIndex("tag_type")));

return new ReaderTag(tagName, endpoint, tagType);
return new ReaderTag(tagSlug, tagTitle, endpoint, tagType);
}

public static ReaderTag getTag(String tagName, ReaderTagType tagType) {
if (TextUtils.isEmpty(tagName)) {
public static ReaderTag getTag(String tagSlug, ReaderTagType tagType) {
if (TextUtils.isEmpty(tagSlug)) {
return null;
}

String[] args = {tagName, Integer.toString(tagType.toInt())};
Cursor c = ReaderDatabase.getReadableDb().rawQuery("SELECT * FROM tbl_tags WHERE tag_name=? AND tag_type=? LIMIT 1", args);
String[] args = {tagSlug, Integer.toString(tagType.toInt())};
Cursor c = ReaderDatabase.getReadableDb().rawQuery("SELECT * FROM tbl_tags WHERE tag_slug=? AND tag_type=? LIMIT 1", args);
try {
if (!c.moveToFirst()) {
return null;
Expand All @@ -175,9 +175,9 @@ public static String getEndpointForTag(ReaderTag tag) {
if (tag == null) {
return null;
}
String[] args = {tag.getTagName(), Integer.toString(tag.tagType.toInt())};
String[] args = {tag.getTagSlug(), Integer.toString(tag.tagType.toInt())};
return SqlUtils.stringForQuery(ReaderDatabase.getReadableDb(),
"SELECT endpoint FROM tbl_tags WHERE tag_name=? AND tag_type=?",
"SELECT endpoint FROM tbl_tags WHERE tag_slug=? AND tag_type=?",
args);
}

Expand All @@ -191,7 +191,7 @@ public static ReaderTagList getFollowedTags() {

private static ReaderTagList getTagsOfType(ReaderTagType tagType) {
String[] args = {Integer.toString(tagType.toInt())};
Cursor c = ReaderDatabase.getReadableDb().rawQuery("SELECT * FROM tbl_tags WHERE tag_type=? ORDER BY tag_name", args);
Cursor c = ReaderDatabase.getReadableDb().rawQuery("SELECT * FROM tbl_tags WHERE tag_type=? ORDER BY tag_slug", args);
try {
ReaderTagList tagList = new ReaderTagList();
if (c.moveToFirst()) {
Expand All @@ -206,7 +206,7 @@ private static ReaderTagList getTagsOfType(ReaderTagType tagType) {
}

static ReaderTagList getAllTags() {
Cursor c = ReaderDatabase.getReadableDb().rawQuery("SELECT * FROM tbl_tags ORDER BY tag_name", null);
Cursor c = ReaderDatabase.getReadableDb().rawQuery("SELECT * FROM tbl_tags ORDER BY tag_slug", null);
try {
ReaderTagList tagList = new ReaderTagList();
if (c.moveToFirst()) {
Expand All @@ -224,18 +224,18 @@ public static void deleteTag(ReaderTag tag) {
if (tag == null) {
return;
}
String[] args = {tag.getTagName(), Integer.toString(tag.tagType.toInt())};
ReaderDatabase.getWritableDb().delete("tbl_tags", "tag_name=? AND tag_type=?", args);
String[] args = {tag.getTagSlug(), Integer.toString(tag.tagType.toInt())};
ReaderDatabase.getWritableDb().delete("tbl_tags", "tag_slug=? AND tag_type=?", args);
}


public static String getTagLastUpdated(ReaderTag tag) {
if (tag == null) {
return "";
}
String[] args = {tag.getTagName(), Integer.toString(tag.tagType.toInt())};
String[] args = {tag.getTagSlug(), Integer.toString(tag.tagType.toInt())};
return SqlUtils.stringForQuery(ReaderDatabase.getReadableDb(),
"SELECT date_updated FROM tbl_tags WHERE tag_name=? AND tag_type=?",
"SELECT date_updated FROM tbl_tags WHERE tag_slug=? AND tag_type=?",
args);
}

Expand All @@ -245,11 +245,11 @@ public static void setTagLastUpdated(ReaderTag tag) {
}

String date = DateTimeUtils.javaDateToIso8601(new Date());
String sql = "UPDATE tbl_tags SET date_updated=?1 WHERE tag_name=?2 AND tag_type=?3";
String sql = "UPDATE tbl_tags SET date_updated=?1 WHERE tag_slug=?2 AND tag_type=?3";
SQLiteStatement stmt = ReaderDatabase.getWritableDb().compileStatement(sql);
try {
stmt.bindString(1, date);
stmt.bindString(2, tag.getTagName());
stmt.bindString(2, tag.getTagSlug());
stmt.bindLong (3, tag.tagType.toInt());
stmt.execute();
} finally {
Expand Down Expand Up @@ -294,9 +294,9 @@ private static int minutesSinceLastUpdate(ReaderTag tag) {
public static ReaderTagList getRecommendedTags(boolean excludeSubscribed) {
Cursor c;
if (excludeSubscribed) {
c = ReaderDatabase.getReadableDb().rawQuery("SELECT * FROM tbl_tags_recommended WHERE tag_name NOT IN (SELECT tag_name FROM tbl_tags) ORDER BY tag_name", null);
c = ReaderDatabase.getReadableDb().rawQuery("SELECT * FROM tbl_tags_recommended WHERE tag_slug NOT IN (SELECT tag_slug FROM tbl_tags) ORDER BY tag_slug", null);
} else {
c = ReaderDatabase.getReadableDb().rawQuery("SELECT * FROM tbl_tags_recommended ORDER BY tag_name", null);
c = ReaderDatabase.getReadableDb().rawQuery("SELECT * FROM tbl_tags_recommended ORDER BY tag_slug", null);
}
try {
ReaderTagList tagList = new ReaderTagList();
Expand All @@ -318,7 +318,7 @@ public static void setRecommendedTags(ReaderTagList tagList) {

SQLiteDatabase db = ReaderDatabase.getWritableDb();
SQLiteStatement stmt = db.compileStatement
("INSERT INTO tbl_tags_recommended (tag_name, tag_type, endpoint) VALUES (?1,?2,?3)");
("INSERT INTO tbl_tags_recommended (tag_slug, tag_title, tag_type, endpoint) VALUES (?1,?2,?3,?4)");
db.beginTransaction();
try {
try {
Expand All @@ -327,9 +327,10 @@ public static void setRecommendedTags(ReaderTagList tagList) {

// then insert the passed ones
for (ReaderTag tag: tagList) {
stmt.bindString(1, tag.getTagName());
stmt.bindLong (2, tag.tagType.toInt());
stmt.bindString(3, tag.getEndpoint());
stmt.bindString(1, tag.getTagSlug());
stmt.bindString(2, tag.getTagTitle());
stmt.bindLong (3, tag.tagType.toInt());
stmt.bindString(4, tag.getEndpoint());
stmt.execute();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ private static void assignTagsFromJson(ReaderPost post, JSONObject jsonTags) {
int postCount = jsonThisTag.optInt("post_count");
if (postCount > popularCount) {
nextMostPopularTag = mostPopularTag;
mostPopularTag = JSONUtils.getStringDecoded(jsonThisTag, "name");
mostPopularTag = JSONUtils.getStringDecoded(jsonThisTag, "slug");
popularCount = postCount;
}
}
Expand Down Expand Up @@ -424,7 +424,7 @@ public String getPrimaryTag() {
}
public void setPrimaryTag(String tagName) {
// this is a bit of a hack to avoid setting the primary tag to one of the defaults
if (!ReaderTag.isDefaultTagName(tagName)) {
if (!ReaderTag.isDefaultTagTitle(tagName)) {
this.primaryTag = StringUtils.notNullStr(tagName);
}
}
Expand All @@ -436,7 +436,7 @@ public String getSecondaryTag() {
return StringUtils.notNullStr(secondaryTag);
}
public void setSecondaryTag(String tagName) {
if (!ReaderTag.isDefaultTagName(tagName)) {
if (!ReaderTag.isDefaultTagTitle(tagName)) {
this.secondaryTag = StringUtils.notNullStr(tagName);
}
}
Expand Down
Loading

0 comments on commit 6d7b724

Please sign in to comment.