import pymysql,time,sys
OUT=open('/opt/kkFileView-4.4.0/file/db_scan.txt','w',buffering=1)
def log(*a):
    print(*a,file=OUT)
def conn():
    return pymysql.connect(host="10.0.0.11",port=3306,user="java_osc",password="56Wp96Z6bS3kBpBf",database="oscdb",connect_timeout=5,read_timeout=280,write_timeout=60,charset="utf8mb4")
c=conn(); cur=c.cursor()
queries=[
 ("drafts_flag","SELECT id,space,title,LEFT(content,500),save_time FROM osc_drafts WHERE content LIKE '%flag{%' OR title LIKE '%flag%' LIMIT 20"),
 ("news_flag","SELECT id,title,LEFT(detail,500),create_time FROM osc_news WHERE detail LIKE '%flag{%' OR title LIKE '%flag%' LIMIT 20"),
 ("spaces_flag","SELECT id,ident,name,LEFT(detail,500),create_time FROM osc_spaces WHERE detail LIKE '%flag{%' OR title LIKE '%flag%' LIMIT 20"),
 ("userinfo_flag","SELECT id,name,LEFT(signature,300),LEFT(brief,500) FROM osc_user_infos WHERE signature LIKE '%flag{%' OR brief LIKE '%flag%' OR name LIKE '%flag{%' LIMIT 20"),
 ("questions_flag","SELECT id,user,subject,LEFT(body,300),create_time FROM osc_questions q LEFT JOIN osc_question_bodies b ON b.question=q.id WHERE q.subject LIKE '%flag%' OR b.body LIKE '%flag{%' LIMIT 20"),
 ("answers_flag","SELECT id,question,user,LEFT(body,500),create_time FROM osc_answers WHERE body LIKE '%flag{%' LIMIT 20"),
 ("blogs_flag_recent","SELECT id,space,title,LEFT(content,500),create_time FROM osc_blogs WHERE create_time>='2025-06-01' AND (content LIKE '%flag{%' OR title LIKE '%flag%') LIMIT 20"),
 ("blogs_flag_old","SELECT id,space,title,LEFT(content,500),create_time FROM osc_blogs WHERE content LIKE '%flag{%' LIMIT 20"),
 ("articles_flag","SELECT id,authorName,title,LEFT(articleDesc,500),pubDate FROM osc_articles WHERE articleDesc LIKE '%flag{%' OR title LIKE '%flag%' LIMIT 20"),
]
for label,sql in queries:
    t=time.time(); log("=====",label,"START",time.strftime('%F %T'))
    try:
        cur=c.cursor(); cur.execute(sql); rows=cur.fetchall()
        log("ROWS",len(rows))
        for r in rows: log(" R",str(r)[:1000])
    except Exception as e:
        log("ERR",type(e).__name__,str(e)[:200])
        try: c.close()
        except: pass
        try: c=conn(); cur=c.cursor()
        except Exception as e2: log("RECONN_ERR",str(e2)[:200]); break
    log("DONE",label,"TIME",round(time.time()-t,1))
log("ALL_DONE")
OUT.close()
