#include #include #include #include #include #include #include "tlmpdoc.m" #include #include #include using namespace std; enum DOCOPER { DOCOPER_NONE, DOCOPER_UPDDOC, DOCOPER_DELUSER, DOCOPER_DELUSERDOC, DOCOPER_DELDOC }; static bool dumphtml=false; static int docprog_upddoc (int argc, char *argv[], bool test, const char *webargs) { glocal bool test = test; int ret = 0; for (int i=0; i> sections; glocal vector> contents; glocal shared_ptr lastsect = nullptr; glocal shared_ptr lastcont = nullptr; glocal int level = 0; glocal bool modified = false; string cmd; char *pt = argv[i]; if (*pt == '/') pt++; cmd = string_f ("tlmpweb.test.sh %s 'buildkey=1%s%s'",pt ,webargs==NULL ? "" : "&" ,webargs==NULL ? "" : webargs); (cmd,20); close_input(); fprintf (stderr,"%s\n",line); return 0; if (dumphtml) fprintf (stderr,"LINE :%s\n",line); if (strncmp(line,"##__##",6)==0){ glocal.id = line+6; NSQL_ENCODE enc; ("select docid,allocsectionid from documents" " where document='%s'" ,enc.enc(line+6)); if (!glocal.test){ NSQL_ENCODE enc; sql_action ("insert into documents (document,dater,allocsectionid)" " values ('%s',now(),0)" ,enc.enc(glocal.id)); // Retrieve the docid (auto-increment field) ("select docid from documents" " where document='%s'" ,enc.enc(glocal.id)); fprintf (stderr,"No document ID, quitting\n"); exit (-1); glocal.docid = atoi(row[0]); if (!dialog_silent){ printf (MSG_U(I_NEWDOCUMENT ,"New Document %s -> %d\n") ,glocal.id.c_str(),glocal.docid); } } glocal.docid = atoi(row[0]); glocal.allocsectionid = atoi(row[1]); if (!dialog_silent){ printf (MSG_U(I_OLDDOCUMENT ,"Document %s -> %d\n"),glocal.id.c_str() ,glocal.docid); } }else if (strncmp(line,"##--##",6)==0){ // for (int i=0; i(line+6); glocal.lastcont = make_shared(); glocal.sections.push_back (glocal.lastsect); glocal.contents.push_back (glocal.lastcont); glocal.level++; }else if (strncmp(line,"##++##",6)==0){ // Now we compare this node with the database NSQL_ENCODE enc; ("select content from revisions" " where section = '%s' and docid=%d" ,enc.enc(glocal.lastsect->c_str()),glocal.docid); // New section if (!dialog_silent){ printf (MSG_U(I_NEWSECTION,"New section: %s\n") ,glocal.lastsect->c_str()); } if (!glocal.test){ NSQL_ENCODE enc; sql_action ("insert into revisions (docid,section,content,dater,sectionid)" " values (%d,'%s','%s',now(),%d)" ,glocal.docid ,enc.enc(glocal.lastsect->c_str()) ,enc.enc(glocal.lastcont->c_str()) ,glocal.allocsectionid++); glocal.modified = true; } if (strcasecmp(glocal.lastcont->c_str(),row[0])!=0){ // Ok, this node changed if (!dialog_silent){ printf (MSG_U(I_MODSECTION,"Section changed: %s\n") ,glocal.lastsect->c_str()); } if (!glocal.test){ NSQL_ENCODE enc; sql_action ("update revisions set dater=now(),content='%s'" " where section = '%s' and docid=%d" ,enc.enc(glocal.lastcont->c_str()),enc.enc(glocal.lastsect->c_str()) ,glocal.docid); glocal.modified = true; } } glocal.level--; glocal.contents.erase (glocal.contents.begin()+glocal.level); glocal.sections.erase (glocal.sections.begin()+glocal.level); glocal.lastcont = glocal.contents[glocal.level-1]; glocal.lastsect = glocal.sections[glocal.level-1]; }else{ if (glocal.lastcont != NULL){ (*glocal.lastcont) +=string_f ("%s\n",line); } } return 0; if (glocal.modified){ sql_action ("update documents set dater=now(),allocsectionid=%d" " where docid=%d",glocal.allocsectionid,glocal.docid); } } return ret; } int main (int argc, char *argv[]) { glocal DOCOPER oper = DOCOPER_NONE; glocal bool test = false; glocal const char *dbserv = "localhost"; glocal const char *db = "docsession"; glocal const char *dbuser = NULL; glocal const char *dbpass = NULL; glocal bool askpass = false; glocal const char *webargs = NULL; static const char *tbdic[]={"tlmpweb","tlmpsql",NULL}; int ret = (argc,argv,tbdic); extern const char *version; fprintf (stderr,MSG_U(I_TLMPDOCVER ,"tlmpdoc version %s\n" "tlmpdoc [ options ] task documents ...\n" "\n" "Update the docsession database with the revision of\n" "each documents. They are located in the web area.\n" "\n" "Tasks:\n" "--upddoc : Update documents in the database.\n" "--deluser : Delete all reference to a userid in the database.\n" "--deluserdoc: Delete all reference to a userid/document in the database.\n" "--deldoc : Delete a document from the database.\n" "\n" "Options:\n" "--silent : Do not print information about the job done.\n" "--dump : Show the html produced by the tlmpdoc application.\n" "--test : Do not update the database.\n" "--db : Database.\n" "--dbserv : Database server.\n" "--dbuser : Database user.\n" "--dbpass : Password to use.\n" "--askpass: Ask for the password.\n" "--webargs: Option passed to the document.\n") ,version); int ret = -1; if (strcmp(opt,"--upddoc")==0){ glocal.oper = DOCOPER_UPDDOC; ret = 0; }else if (strcmp(opt,"--deldoc")==0){ glocal.oper = DOCOPER_DELDOC; ret = 0; }else if (strcmp(opt,"--deluser")==0){ glocal.oper = DOCOPER_DELUSER; ret = 0; }else if (strcmp(opt,"--deluserdoc")==0){ glocal.oper = DOCOPER_DELUSERDOC; ret = 0; }else if (strcmp(opt,"--test")==0){ ret = 0; glocal.test = true; }else if (strcmp(opt,"--silent")==0){ ret = 0; dialog_silent = true; }else if (strcmp(opt,"--dbserv")==0){ ret = 1; glocal.dbserv = val; }else if (strcmp(opt,"--db")==0){ ret = 1; glocal.db = val; }else if (strcmp(opt,"--dbuser")==0){ ret = 1; glocal.dbuser = val; }else if (strcmp(opt,"--dbpass")==0){ ret = 1; glocal.dbpass = val; }else if (strcmp(opt,"--askpass")==0){ glocal.askpass = true; ret = 0; }else if (strcmp(opt,"--dump")==0){ dumphtml = true; ret = 0; }else if (strcmp(opt,"--webargs")==0){ glocal.webargs = val; ret = 1; } return ret; int ret = -1; if (argc == 0){ usage(); }else if (glocal.oper == DOCOPER_NONE){ fprintf (stderr,MSG_U(E_NOOPER ,"You must specify one task option\n")); fprintf (stderr,"\n"); usage(); }else{ SSTRING pass; if (glocal.askpass){ DIALOG dia; dia.newf_pass (MSG_U(F_DBPASS,"Database password"),pass); if (dia.edit("","",help_nil)==MENU_ACCEPT){ glocal.dbpass = pass.get(); }else{ return -1; } dialog_end(); } query_setdefaultdb (glocal.dbserv,glocal.db,glocal.dbuser,glocal.dbpass); if (glocal.oper == DOCOPER_UPDDOC){ ret = docprog_upddoc (argc,argv,glocal.test,glocal.webargs); }else if (glocal.oper == DOCOPER_DELUSER){ for (int i=0; i("select docid from documents" " where document='%s'",enc.enc(argv[i])); fprintf (stderr,MSG_R(E_NODOCUMENT),glocal.document); sql_action ("delete from userview where docid=%s",row[0]); } }else if (glocal.oper == DOCOPER_DELDOC){ for (int i=0; i("select docid from documents" " where document='%s'" ,enc.enc(argv[i])); fprintf (stderr,MSG_U(E_NODOCUMENT ,"No document %s\n"),glocal.document); sql_action ("delete from documents" " where docid='%s'",row[0]); sql_action ("delete from revisions" " where docid='%s'",row[0]); sql_action ("delete from userview" " where docid=%s",row[0]); } } } return ret; return ret; }