Fossil

Diff
Login

Differences From Artifact [c07694441f]:

To Artifact [f8ab2686b7]:


48
49
50
51
52
53
54
55



56
57
58
59
60
61
62
*/
static int nameCmpr(const void *a, const void *b){
  return strcmp(*(char**)a, *(char**)b);
}

/*
** Obtain a list of all fields of the TICKET table.  Put them 
** in sorted order.



*/
static void getAllTicketFields(void){
  Stmt q;
  if( nField>0 ) return;
  db_prepare(&q, "PRAGMA table_info(ticket)");
  while( db_step(&q)==SQLITE_ROW ){
    const char *zField = db_column_text(&q, 1);







|
>
>
>







48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
*/
static int nameCmpr(const void *a, const void *b){
  return strcmp(*(char**)a, *(char**)b);
}

/*
** Obtain a list of all fields of the TICKET table.  Put them 
** in sorted order in azField[].
**
** Also allocate space for azValue[] and azAppend[] and initialize
** all the values there to zero.
*/
static void getAllTicketFields(void){
  Stmt q;
  if( nField>0 ) return;
  db_prepare(&q, "PRAGMA table_info(ticket)");
  while( db_step(&q)==SQLITE_ROW ){
    const char *zField = db_column_text(&q, 1);
74
75
76
77
78
79
80

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  qsort(azField, nField, sizeof(azField[0]), nameCmpr);
  azAppend = &azField[nField];
  memset(azAppend, 0, sizeof(azAppend[0])*nField*2);
  azValue = &azAppend[nField];
}

/*

** Return true if zField is a field within the TICKET table.
*/
static int isTicketField(const char *zField){
  int i;
  for(i=0; i<nField; i++){
    if( strcmp(azField[i], zField)==0 ) return 1;
  }
  return 0;
}

/*
** Query the database for all TICKET fields for the specific
** ticket whose name is given by the "name" CGI parameter.
** Load the values for all fields into the interpreter.
**







>
|

|


|

|







77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  qsort(azField, nField, sizeof(azField[0]), nameCmpr);
  azAppend = &azField[nField];
  memset(azAppend, 0, sizeof(azAppend[0])*nField*2);
  azValue = &azAppend[nField];
}

/*
** Return the index into azField[] of the given field name.
** Return -1 if zField is not in azField[].
*/
static int fieldId(const char *zField){
  int i;
  for(i=0; i<nField; i++){
    if( strcmp(azField[i], zField)==0 ) return i;
  }
  return -1;
}

/*
** Query the database for all TICKET fields for the specific
** ticket whose name is given by the "name" CGI parameter.
** Load the values for all fields into the interpreter.
**
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
  blob_zero(&sql);
  blob_appendf(&sql, "UPDATE ticket SET tkt_mtime=:mtime");
  zSep = "SET";
  for(i=0; i<p->nField; i++){
    const char *zName = p->aField[i].zName;
    if( zName[0]=='+' ){
      zName++;
      if( !isTicketField(zName) ) continue;
      blob_appendf(&sql,", %s=%s || %Q", zName, zName, p->aField[i].zValue);
    }else{
      if( !isTicketField(zName) ) continue;
      blob_appendf(&sql,", %s=%Q", zName, p->aField[i].zValue);
    }
  }
  blob_appendf(&sql, " WHERE tkt_uuid='%s' AND tkt_mtime<:mtime",
                     p->zTicketUuid);
  db_prepare(&q, "%s", blob_str(&sql));
  db_bind_double(&q, ":mtime", p->rDate);







|


|







191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
  blob_zero(&sql);
  blob_appendf(&sql, "UPDATE ticket SET tkt_mtime=:mtime");
  zSep = "SET";
  for(i=0; i<p->nField; i++){
    const char *zName = p->aField[i].zName;
    if( zName[0]=='+' ){
      zName++;
      if( fieldId(zName)<0 ) continue;
      blob_appendf(&sql,", %s=%s || %Q", zName, zName, p->aField[i].zValue);
    }else{
      if( fieldId(zName)<0 ) continue;
      blob_appendf(&sql,", %s=%Q", zName, p->aField[i].zValue);
    }
  }
  blob_appendf(&sql, " WHERE tkt_uuid='%s' AND tkt_mtime<:mtime",
                     p->zTicketUuid);
  db_prepare(&q, "%s", blob_str(&sql));
  db_bind_double(&q, ":mtime", p->rDate);
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
  zScript = (char*)SbS_Fetch(pInterp, "tktview_template", -1, &nScript);
  zScript = mprintf("%.*s", nScript, zScript);
  SbS_Render(pInterp, zScript);
  style_footer();
}

/*
** Subscript command:   LABEL submit_new_ticket
**
** If the variable named LABEL exists, then submit a new ticket
** based on the values of other defined variables.
*/
static int submitNewCmd(struct Subscript *p, void *pNotify){
  const char *zLabel;
  int nLabel, size;







|







314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
  zScript = (char*)SbS_Fetch(pInterp, "tktview_template", -1, &nScript);
  zScript = mprintf("%.*s", nScript, zScript);
  SbS_Render(pInterp, zScript);
  style_footer();
}

/*
** Subscript command:   submit_new_ticket
**
** If the variable named LABEL exists, then submit a new ticket
** based on the values of other defined variables.
*/
static int submitNewCmd(struct Subscript *p, void *pNotify){
  const char *zLabel;
  int nLabel, size;
367
368
369
370
371
372
373


































































































374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391


392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564

565
566
567
568
569
570
571
    }
    manifest_crosslink(rid, &tktchng);
    return SBS_RETURN;
  }
  SbS_Pop(p, 1);
  return SBS_OK;  
}



































































































/*
** WEBPAGE: tktnew
*/
void tktnew_page(void){
  char *zScript;
  int nScript;
  char *zNewUuid = 0;

  login_check_credentials();
  if( !g.okNewTkt ){ login_needed(); return; }
  style_header("New Ticket");
  ticket_init();
  getAllTicketFields();
  initializeVariablesFromCGI();
  @ <form method="POST" action="%s(g.zBaseURL)/tktnew">
  zScript = (char*)SbS_Fetch(pInterp, "tktnew_template", -1, &nScript);
  zScript = mprintf("%.*s", nScript, zScript);


  SbS_AddVerb(pInterp, "submit_new_ticket", submitNewCmd, (void*)&zNewUuid);
  if( SbS_Render(pInterp, zScript)==SBS_RETURN && zNewUuid ){
    cgi_redirect(mprintf("%s/tktview/%s", g.zBaseURL, zNewUuid));
    return;
  }
  @ </form>
  style_footer();
}



/*
** Subscript command:   STR1 STR2 USERVAR APPENDVAR FIELD append_remark
**
** FIELD is the name of a database column to which we might want
** to append text.  APPENDVAR is the name of a CGI parameter which
** (if it exists) contains the text to be appended.  The append
** operation will only happen if APPENDVAR exists.  USERVAR is
** a CGI parameter which contains the name that the user wants to
** to be known by.  STR1 and STR2 are prefixes that are prepended
** to the text in the APPENDVAR CGI parameter.  STR1 is used if
** USERVAR is the same as g.zLogin or if USERVAR does not exist.
** STR2 is used if USERVAR exists and is different than g.zLogin.
** Within STR1 and STR2, the following substitutions occur:
**
**     %LOGIN%    The value of g.zLogin
**     %USER%     The value of the USERVAR CGI parameter
**     %DATE%     The current date and time
**
** The concatenation STR1 or STR2 with the content of APPENDVAR
** is written into azApnd[] in the FIELD slot so that it will be
** picked up and used by the submit_ticket_change command.
*/
static int appendRemarkCmd(struct Subscript *p, void *notUsed){
  int i, j, idx;
  const char *zField, *zAppendVar, *zUserVar, *zStr, *zValue, *zUser;
  int nField, nAppendVar, nUserVar, nStr, nValue, nUser;

  if( SbS_RequireStack(p, 5, "append_remark") ) return 1;
  zField = SbS_StackValue(p, 0, &nField);
  for(idx=0; idx<nField; idx++){
    if( strncmp(azField[idx], zField, nField)==0 && azField[idx][nField]==0 ){
      break;
    }
  }
  if( idx>=nField ){
    SbS_SetErrorMessage(p, "no such TICKET column: %.*s", nField, zField);
    return SBS_ERROR;
  }
  zAppendVar = SbS_StackValue(p, 1, &nAppendVar);
  zValue = SbS_Fetch(p, zAppendVar, nAppendVar, &nValue);
  if( zValue ){
    Blob out;
    blob_zero(&out);
    zUserVar = SbS_StackValue(p, 2, &nUserVar);
    zUser = SbS_Fetch(p, zUserVar, nUserVar, &nUser);
    if( zUser && (strncmp(zUser, g.zLogin, nUser) || g.zLogin[nUser]!=0) ){
      zStr = SbS_StackValue(p, 3, &nStr);
    }else{
      zStr = SbS_StackValue(p, 4, &nStr);
    }
    for(i=j=0; i<nStr; i++){
      if( zStr[i]!='%' ) continue;
      if( i>j ){
        blob_append(&out, &zStr[j], i-j);
        j = i;
      }
      if( strncmp(&zStr[j], "%USER%", 6)==0 ){
        blob_appendf(&out, "%z", htmlize(zUser, nUser));
        i += 5;
        j = i+1;
      }else if( strncmp(&zStr[j], "%LOGIN%", 7)==0 ){
        blob_appendf(&out, "%z", htmlize(g.zLogin, -1));
        i += 6;
        j = i+1;
      }else if( strncmp(&zStr[j], "%DATE%", 6)==0 ){
        blob_appendf(&out, "%z", db_text(0, "SELECT datetime('now')"));
        i += 5;
        j = i+1;
      }
    }
    if( i>j ){
      blob_append(&out, &zStr[j], i-j);     
    }
    blob_append(&out, zValue, nValue);
    azAppend[idx] = blob_str(&out);
  }
  SbS_Pop(p, 5);
  return SBS_OK;
}

/*
** Subscript command:   LABEL submit_ticket_change
**
** If the variable named LABEL exists, then submit a change to
** the ticket identified by the "name" CGI parameter.
*/
static int submitEditCmd(struct Subscript *p, void *pNotify){
  const char *zLabel;
  int nLabel, size;

  if( SbS_RequireStack(p, 1, "submit_ticket_change") ) return 1;
  zLabel = SbS_StackValue(p, 0, &nLabel);
  if( SbS_Fetch(p, zLabel, nLabel, &size)!=0 ){
    char *zDate, *zUuid;
    int i;
    int rid;
    Blob tktchng, cksum;

    (*(int*)pNotify) = 1;
    blob_zero(&tktchng);
    zDate = db_text(0, "SELECT datetime('now')");
    zDate[10] = 'T';
    blob_appendf(&tktchng, "D %s\n", zDate);
    free(zDate);
    for(i=0; i<nField; i++){
      const char *zValue;
      int nValue;
      if( azAppend[i] ){
        blob_appendf(&tktchng, "J +%s %z\n", azField[i],
                     fossilize(azAppend[i], -1));
      }else{
        zValue = SbS_Fetch(p, azField[i], -1, &nValue);
        if( zValue ){
          while( nValue>0 && isspace(zValue[nValue-1]) ){ nValue--; }
          if( strncmp(zValue, azValue[i], nValue)
                  || strlen(azValue[i])!=nValue ){
            blob_appendf(&tktchng, "J %s %z\n",
               azField[i], fossilize(zValue,nValue));
          }
        }
      }
    }
    zUuid = db_text(0, 
       "SELECT tkt_uuid FROM ticket WHERE tkt_uuid GLOB '%s*'",
       P("name")
    );
    blob_appendf(&tktchng, "K %s\n", zUuid);
    (*(char**)pNotify) = zUuid;
    blob_appendf(&tktchng, "U %F\n", g.zLogin ? g.zLogin : "");
    md5sum_blob(&tktchng, &cksum);
    blob_appendf(&tktchng, "Z %b\n", &cksum);

#if 1
    @ <hr><pre>
    @ %h(blob_str(&tktchng))
    @ </pre><hr>
    blob_zero(&tktchng);
    SbS_Pop(p, 1);
    return SBS_OK;
#endif

    rid = content_put(&tktchng, 0, 0);
    if( rid==0 ){
      fossil_panic("trouble committing ticket: %s", g.zErrMsg);
    }
    manifest_crosslink(rid, &tktchng);
    return SBS_RETURN;
  }
  SbS_Pop(p, 1);
  return SBS_OK;  
}

/*
** WEBPAGE: tktedit
*/
void tktedit_page(void){
  char *zScript;
  int nScript;
  int chnged = 0;
  int nName;
  const char *zName;
  int nRec;


  login_check_credentials();
  if( !g.okApndTkt && !g.okWrTkt ){ login_needed(); return; }
  style_header("Edit Ticket");
  zName = P("name");
  if( zName==0 || (nName = strlen(zName))<4 || nName>UUID_SIZE
          || !validate16(zName,nName) ){







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


















>
>
|











<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<





<



>







371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
























































































































































508
509
510
511
512

513
514
515
516
517
518
519
520
521
522
523
    }
    manifest_crosslink(rid, &tktchng);
    return SBS_RETURN;
  }
  SbS_Pop(p, 1);
  return SBS_OK;  
}


/*
** Subscript command:   STRING FIELD append_field
**
** FIELD is the name of a database column to which we might want
** to append text.  STRING is the text to be appended to that
** column.  The append does not actually occur until the
** submit_ticket_change verb is run.
*/
static int appendRemarkCmd(struct Subscript *p, void *notUsed){
  int idx;
  const char *zField, *zValue;
  int nField, nValue;

  if( SbS_RequireStack(p, 2, "append_field") ) return 1;
  zField = SbS_StackValue(p, 0, &nField);
  for(idx=0; idx<nField; idx++){
    if( strncmp(azField[idx], zField, nField)==0 && azField[idx][nField]==0 ){
      break;
    }
  }
  if( idx>=nField ){
    SbS_SetErrorMessage(p, "no such TICKET column: %.*s", nField, zField);
    return SBS_ERROR;
  }
  zValue = SbS_StackValue(p, 1, &nValue);
  azAppend[idx] = mprintf("%.*s", nValue, zValue);
  SbS_Pop(p, 2);
  return SBS_OK;
}

/*
** Subscript command:   submit_ticket
**
** Construct and submit a new ticket artifact.
*/
static int submitTicketCmd(struct Subscript *p, void *pUuid){
  char *zDate;
  const char *zUuid;
  int i;
  int rid;
  Blob tktchng, cksum;

  zUuid = (const char *)pUuid;
  blob_zero(&tktchng);
  zDate = db_text(0, "SELECT datetime('now')");
  zDate[10] = 'T';
  blob_appendf(&tktchng, "D %s\n", zDate);
  free(zDate);
  for(i=0; i<nField; i++){
    const char *zValue;
    int nValue;
    if( azAppend[i] ){
      blob_appendf(&tktchng, "J +%s %z\n", azField[i],
                   fossilize(azAppend[i], -1));
    }else{
      zValue = SbS_Fetch(p, azField[i], -1, &nValue);
      if( zValue ){
        while( nValue>0 && isspace(zValue[nValue-1]) ){ nValue--; }
        if( strncmp(zValue, azValue[i], nValue)
                || strlen(azValue[i])!=nValue ){
          blob_appendf(&tktchng, "J %s %z\n",
             azField[i], fossilize(zValue,nValue));
        }
      }
    }
  }
  if( *(char**)pUuid==0 ){
    zUuid = db_text(0, 
       "SELECT tkt_uuid FROM ticket WHERE tkt_uuid GLOB '%s*'", P("name")
    );
  }else{
    zUuid = db_text(0, "SELECT lower(hex(randomblob(20)))");
  }
  *(const char**)pUuid = zUuid;
  blob_appendf(&tktchng, "K %s\n", zUuid);
  blob_appendf(&tktchng, "U %F\n", g.zLogin ? g.zLogin : "");
  md5sum_blob(&tktchng, &cksum);
  blob_appendf(&tktchng, "Z %b\n", &cksum);

#if 1
  @ <hr><pre>
  @ %h(blob_str(&tktchng))
  @ </pre><hr>
  blob_zero(&tktchng);
  SbS_Pop(p, 1);
  return SBS_OK;
#endif

  rid = content_put(&tktchng, 0, 0);
  if( rid==0 ){
    fossil_panic("trouble committing ticket: %s", g.zErrMsg);
  }
  manifest_crosslink(rid, &tktchng);
  return SBS_RETURN;
}


/*
** WEBPAGE: tktnew
*/
void tktnew_page(void){
  char *zScript;
  int nScript;
  char *zNewUuid = 0;

  login_check_credentials();
  if( !g.okNewTkt ){ login_needed(); return; }
  style_header("New Ticket");
  ticket_init();
  getAllTicketFields();
  initializeVariablesFromCGI();
  @ <form method="POST" action="%s(g.zBaseURL)/tktnew">
  zScript = (char*)SbS_Fetch(pInterp, "tktnew_template", -1, &nScript);
  zScript = mprintf("%.*s", nScript, zScript);
  SbS_Store(pInterp, "login", g.zLogin, 0);
  SbS_Store(pInterp, "date", db_text(0, "SELECT datetime('now')"), 2);
  SbS_AddVerb(pInterp, "submit_ticket", submitNewCmd, (void*)&zNewUuid);
  if( SbS_Render(pInterp, zScript)==SBS_RETURN && zNewUuid ){
    cgi_redirect(mprintf("%s/tktview/%s", g.zBaseURL, zNewUuid));
    return;
  }
  @ </form>
  style_footer();
}



/*
























































































































































** WEBPAGE: tktedit
*/
void tktedit_page(void){
  char *zScript;
  int nScript;

  int nName;
  const char *zName;
  int nRec;
  char *zUuid = 0;

  login_check_credentials();
  if( !g.okApndTkt && !g.okWrTkt ){ login_needed(); return; }
  style_header("Edit Ticket");
  zName = P("name");
  if( zName==0 || (nName = strlen(zName))<4 || nName>UUID_SIZE
          || !validate16(zName,nName) ){
589
590
591
592
593
594
595


596
597
598
599
600
601
602
603
604
  getAllTicketFields();
  initializeVariablesFromCGI();
  initializeVariablesFromDb();
  @ <form method="POST" action="%s(g.zBaseURL)/tktedit">
  @ <input type="hidden" name="name" value="%s(zName)">
  zScript = (char*)SbS_Fetch(pInterp, "tktedit_template", -1, &nScript);
  zScript = mprintf("%.*s", nScript, zScript);


  SbS_AddVerb(pInterp, "append_remark", appendRemarkCmd, 0);
  SbS_AddVerb(pInterp, "submit_ticket_change", submitEditCmd, (void*)&chnged);
  if( SbS_Render(pInterp, zScript)==SBS_RETURN && chnged ){
    cgi_redirect(mprintf("%s/tktview/%s", g.zBaseURL, zName));
    return;
  }
  @ </form>
  style_footer();
}







>
>
|
|
|
|





541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
  getAllTicketFields();
  initializeVariablesFromCGI();
  initializeVariablesFromDb();
  @ <form method="POST" action="%s(g.zBaseURL)/tktedit">
  @ <input type="hidden" name="name" value="%s(zName)">
  zScript = (char*)SbS_Fetch(pInterp, "tktedit_template", -1, &nScript);
  zScript = mprintf("%.*s", nScript, zScript);
  SbS_Store(pInterp, "login", g.zLogin, 0);
  SbS_Store(pInterp, "date", db_text(0, "SELECT datetime('now')"), 2);
  SbS_AddVerb(pInterp, "append_field", appendRemarkCmd, 0);
  SbS_AddVerb(pInterp, "submit_ticket", submitTicketCmd, (void*)&zUuid);
  if( SbS_Render(pInterp, zScript)==SBS_RETURN && zUuid ){
    cgi_redirect(mprintf("%s/tktview/%s", g.zBaseURL, zUuid));
    return;
  }
  @ </form>
  style_footer();
}