| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
| 17 |
#include "section_list_loader.h" |
#include "section_list_loader.h" |
| 18 |
|
#include "article_cache.h" |
| 19 |
#include "log.h" |
#include "log.h" |
| 20 |
#include "database.h" |
#include "database.h" |
| 21 |
#include "menu.h" |
#include "menu.h" |
| 181 |
} |
} |
| 182 |
|
|
| 183 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 184 |
"SELECT AID, TID, SID, CID, UID, visible, excerption, ontop, `lock`, " |
"SELECT bbs.AID, TID, SID, bbs.CID, UID, visible, excerption, ontop, `lock`, " |
| 185 |
"transship, username, nickname, title, UNIX_TIMESTAMP(sub_dt) AS sub_dt " |
"transship, username, nickname, title, UNIX_TIMESTAMP(sub_dt) AS sub_dt, " |
| 186 |
"FROM bbs WHERE AID >= %d ORDER BY AID LIMIT %d", |
"bbs_content.content " |
| 187 |
|
"FROM bbs INNER JOIN bbs_content ON bbs.CID = bbs_content.CID " |
| 188 |
|
"WHERE bbs.AID >= %d ORDER BY bbs.AID LIMIT %d", |
| 189 |
start_aid, article_count_limit); |
start_aid, article_count_limit); |
| 190 |
|
|
| 191 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 193 |
log_error("Query article list error: %s\n", mysql_error(db)); |
log_error("Query article list error: %s\n", mysql_error(db)); |
| 194 |
return -3; |
return -3; |
| 195 |
} |
} |
| 196 |
if ((rs = mysql_store_result(db)) == NULL) |
if ((rs = mysql_use_result(db)) == NULL) |
| 197 |
{ |
{ |
| 198 |
log_error("Get article list data failed\n"); |
log_error("Get article list data failed\n"); |
| 199 |
return -3; |
return -3; |
| 241 |
{ |
{ |
| 242 |
if ((ret = section_list_rw_unlock(p_section)) < 0) |
if ((ret = section_list_rw_unlock(p_section)) < 0) |
| 243 |
{ |
{ |
| 244 |
log_error("section_list_rw_unlock(sid = %d) error\n", p_section->sid); |
log_error("section_list_rw_unlock(sid=%d) error\n", p_section->sid); |
| 245 |
break; |
break; |
| 246 |
} |
} |
| 247 |
} |
} |
| 270 |
{ |
{ |
| 271 |
if ((ret = section_list_rw_lock(p_section)) < 0) |
if ((ret = section_list_rw_lock(p_section)) < 0) |
| 272 |
{ |
{ |
| 273 |
log_error("section_list_rw_lock(sid = 0) error\n"); |
log_error("section_list_rw_lock(sid=0) error\n"); |
| 274 |
break; |
break; |
| 275 |
} |
} |
| 276 |
} |
} |
| 280 |
|
|
| 281 |
if (section_list_append_article(p_section, &article) < 0) |
if (section_list_append_article(p_section, &article) < 0) |
| 282 |
{ |
{ |
| 283 |
log_error("section_list_append_article(sid = %d, aid = %d) error\n", |
log_error("section_list_append_article(sid=%d, aid=%d) error\n", |
| 284 |
p_section->sid, article.aid); |
p_section->sid, article.aid); |
| 285 |
ret = -3; |
ret = -3; |
| 286 |
break; |
break; |
| 288 |
|
|
| 289 |
article_count++; |
article_count++; |
| 290 |
|
|
| 291 |
// TODO: generate content cache |
if (article_cache_generate(VAR_ARTICLE_CACHE_DIR, &article, p_section, row[i++], 0) < 0) |
| 292 |
|
{ |
| 293 |
|
log_error("article_cache_generate(aid=%d, cid=%d) error\n", article.aid, article.cid); |
| 294 |
|
ret = -4; |
| 295 |
|
break; |
| 296 |
|
} |
| 297 |
} |
} |
| 298 |
|
|
| 299 |
// release lock of last section |
// release lock of last section |
| 301 |
{ |
{ |
| 302 |
if ((ret = section_list_rw_unlock(p_section)) < 0) |
if ((ret = section_list_rw_unlock(p_section)) < 0) |
| 303 |
{ |
{ |
| 304 |
log_error("section_list_rw_unlock(sid = %d) error\n", p_section->sid); |
log_error("section_list_rw_unlock(sid=%d) error\n", p_section->sid); |
| 305 |
} |
} |
| 306 |
} |
} |
| 307 |
|
|
| 310 |
{ |
{ |
| 311 |
if ((ret = section_list_rw_unlock(NULL)) < 0) |
if ((ret = section_list_rw_unlock(NULL)) < 0) |
| 312 |
{ |
{ |
| 313 |
log_error("section_list_rw_unlock(sid = 0) error\n"); |
log_error("section_list_rw_unlock(sid=0) error\n"); |
| 314 |
} |
} |
| 315 |
} |
} |
| 316 |
|
|
| 467 |
break; |
break; |
| 468 |
case 'M': // Modify article |
case 'M': // Modify article |
| 469 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 470 |
"SELECT CID FROM bbs WHERE AID = %d", |
"SELECT bbs.CID, bbs_content.content " |
| 471 |
|
"FROM bbs INNER JOIN bbs_content ON bbs.CID = bbs_content.CID " |
| 472 |
|
"WHERE bbs.AID = %d", |
| 473 |
p_article->aid); |
p_article->aid); |
| 474 |
|
|
| 475 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 478 |
ret = -3; |
ret = -3; |
| 479 |
break; |
break; |
| 480 |
} |
} |
| 481 |
if ((rs2 = mysql_store_result(db)) == NULL) |
if ((rs2 = mysql_use_result(db)) == NULL) |
| 482 |
{ |
{ |
| 483 |
log_error("Get article data failed\n"); |
log_error("Get article data failed\n"); |
| 484 |
ret = -3; |
ret = -3; |
| 487 |
if ((row2 = mysql_fetch_row(rs2))) |
if ((row2 = mysql_fetch_row(rs2))) |
| 488 |
{ |
{ |
| 489 |
p_article->cid = atoi(row2[0]); |
p_article->cid = atoi(row2[0]); |
| 490 |
|
|
| 491 |
|
if (article_cache_generate(VAR_ARTICLE_CACHE_DIR, p_article, p_section, row2[1], 0) < 0) |
| 492 |
|
{ |
| 493 |
|
log_error("article_cache_generate(aid=%d, cid=%d) error\n", p_article->aid, p_article->cid); |
| 494 |
|
ret = -4; |
| 495 |
|
} |
| 496 |
} |
} |
| 497 |
else |
else |
| 498 |
{ |
{ |
| 500 |
ret = -4; |
ret = -4; |
| 501 |
} |
} |
| 502 |
mysql_free_result(rs2); |
mysql_free_result(rs2); |
|
|
|
|
if (p_article->cid > 0) |
|
|
{ |
|
|
// TODO: generate content cache |
|
|
} |
|
|
|
|
| 503 |
break; |
break; |
| 504 |
case 'T': // Move article |
case 'T': // Move article |
| 505 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 756 |
return 0; |
return 0; |
| 757 |
} |
} |
| 758 |
|
|
| 759 |
int query_section_articles(SECTION_LIST *p_section, int32_t page_id, ARTICLE *p_articles[], int32_t *p_article_count) |
int query_section_articles(SECTION_LIST *p_section, int page_id, ARTICLE *p_articles[], int *p_article_count, int *p_page_count) |
| 760 |
{ |
{ |
| 761 |
ARTICLE *p_article; |
ARTICLE *p_article; |
| 762 |
ARTICLE *p_next_page_first_article; |
ARTICLE *p_next_page_first_article; |
| 763 |
int ret = 0; |
int ret = 0; |
| 764 |
|
|
| 765 |
if (p_section == NULL || p_articles == NULL || p_article_count == NULL) |
if (p_section == NULL || p_articles == NULL || p_article_count == NULL || p_page_count == NULL) |
| 766 |
{ |
{ |
| 767 |
log_error("query_section_articles() NULL pointer error\n"); |
log_error("query_section_articles() NULL pointer error\n"); |
| 768 |
return -1; |
return -1; |
| 775 |
return -2; |
return -2; |
| 776 |
} |
} |
| 777 |
|
|
| 778 |
|
*p_page_count = p_section->page_count; |
| 779 |
|
|
| 780 |
if (p_section->visible_article_count == 0) |
if (p_section->visible_article_count == 0) |
| 781 |
{ |
{ |
| 782 |
*p_article_count = 0; |
*p_article_count = 0; |
| 819 |
|
|
| 820 |
return ret; |
return ret; |
| 821 |
} |
} |
| 822 |
|
|
| 823 |
|
int locate_article_in_section(SECTION_LIST *p_section, const ARTICLE *p_article_cur, int direction, |
| 824 |
|
int *p_page_id, int *p_visible_offset, int *p_article_count) |
| 825 |
|
{ |
| 826 |
|
const ARTICLE *p_article = NULL; |
| 827 |
|
ARTICLE *p_tmp; |
| 828 |
|
int32_t aid = 0; |
| 829 |
|
int page_id; |
| 830 |
|
int offset; |
| 831 |
|
int ret = 0; |
| 832 |
|
int i; |
| 833 |
|
|
| 834 |
|
if (p_section == NULL || p_article_cur == NULL || p_page_id == NULL || p_visible_offset == NULL || p_article_count == NULL) |
| 835 |
|
{ |
| 836 |
|
log_error("locate_article_in_section() NULL pointer error\n"); |
| 837 |
|
return -1; |
| 838 |
|
} |
| 839 |
|
|
| 840 |
|
// acquire lock of section |
| 841 |
|
if ((ret = section_list_rd_lock(p_section)) < 0) |
| 842 |
|
{ |
| 843 |
|
log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid); |
| 844 |
|
return -2; |
| 845 |
|
} |
| 846 |
|
|
| 847 |
|
if (direction == 0) |
| 848 |
|
{ |
| 849 |
|
aid = p_article_cur->aid; |
| 850 |
|
} |
| 851 |
|
else if (direction == 1) |
| 852 |
|
{ |
| 853 |
|
p_article = p_article_cur; |
| 854 |
|
do |
| 855 |
|
{ |
| 856 |
|
p_article = p_article->p_topic_next; |
| 857 |
|
} while (p_article != p_article_cur && p_article->visible == 0); |
| 858 |
|
|
| 859 |
|
aid = (p_article->aid > p_article_cur->aid ? p_article->aid : 0); |
| 860 |
|
} |
| 861 |
|
else if (direction == -1) |
| 862 |
|
{ |
| 863 |
|
p_article = p_article_cur; |
| 864 |
|
do |
| 865 |
|
{ |
| 866 |
|
p_article = p_article->p_topic_prior; |
| 867 |
|
} while (p_article != p_article_cur && p_article->visible == 0); |
| 868 |
|
|
| 869 |
|
aid = (p_article->aid < p_article_cur->aid ? p_article->aid : 0); |
| 870 |
|
} |
| 871 |
|
|
| 872 |
|
p_article = NULL; |
| 873 |
|
|
| 874 |
|
if (aid > 0) |
| 875 |
|
{ |
| 876 |
|
p_article = section_list_find_article_with_offset(p_section, aid, &page_id, &offset, &p_tmp); |
| 877 |
|
if (p_article != NULL) |
| 878 |
|
{ |
| 879 |
|
*p_article_count = (page_id == p_section->page_count - 1 ? p_section->last_page_visible_article_count : BBS_article_limit_per_page); |
| 880 |
|
|
| 881 |
|
p_article = p_section->p_page_first_article[page_id]; |
| 882 |
|
for (i = 0; i < *p_article_count;) |
| 883 |
|
{ |
| 884 |
|
if (p_article->visible) |
| 885 |
|
{ |
| 886 |
|
if (p_article->aid == aid) |
| 887 |
|
{ |
| 888 |
|
*p_page_id = page_id; |
| 889 |
|
*p_visible_offset = i; |
| 890 |
|
break; |
| 891 |
|
} |
| 892 |
|
i++; |
| 893 |
|
if (i >= *p_article_count) |
| 894 |
|
{ |
| 895 |
|
log_error("Visible article (aid=%d) not found in page %d\n", aid, page_id); |
| 896 |
|
p_article = NULL; |
| 897 |
|
} |
| 898 |
|
} |
| 899 |
|
p_article = p_article->p_next; |
| 900 |
|
} |
| 901 |
|
} |
| 902 |
|
} |
| 903 |
|
|
| 904 |
|
// release lock of section |
| 905 |
|
if (section_list_rd_unlock(p_section) < 0) |
| 906 |
|
{ |
| 907 |
|
log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid); |
| 908 |
|
ret = -2; |
| 909 |
|
} |
| 910 |
|
|
| 911 |
|
return (ret < 0 ? ret : (p_article == NULL ? 0 : 1)); |
| 912 |
|
} |