Diff
Not logged in

Differences From Artifact [f8ab2686b7]:

To Artifact [73fae28608]:


55
56
57
58
59
60
61

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

80



81
82
83
84
85
86
87
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

80
81
82
83
84
85
86
87
88
89
90
91







+

















-
+

+
+
+







** 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;
  int i;
  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);
    if( strncmp(zField,"tkt_",4)==0 ) continue;
    if( nField%10==0 ){
      azField = realloc(azField, sizeof(azField)*3*(nField+10) );
      if( azField==0 ){
        fossil_fatal("out of memory");
      }
    }
    azField[nField] = mprintf("%s", zField);
    nField++;
  }
  db_finalize(&q);
  qsort(azField, nField, sizeof(azField[0]), nameCmpr);
  azAppend = &azField[nField];
  memset(azAppend, 0, sizeof(azAppend[0])*nField*2);
  memset(azAppend, 0, sizeof(azAppend[0])*nField);
  azValue = &azAppend[nField];
  for(i=0; i<nField; i++){
    azValue[i] = "";
  }
}

/*
** Return the index into azField[] of the given field name.
** Return -1 if zField is not in azField[].
*/
static int fieldId(const char *zField){
211
212
213
214
215
216
217
218

219
220
221
222
223
224
225
215
216
217
218
219
220
221

222
223
224
225
226
227
228
229







-
+







  if( checkTime && db_changes()==0 ){
    static int isInit = 0;
    if( !isInit ){
      db_multi_exec("CREATE TEMP TABLE _pending_ticket(uuid TEXT UNIQUE)");
      db_commit_hook(ticket_rebuild_at_commit, 1);
      isInit = 1;
    }
    db_multi_exec("INSERT OR IGNORE INTO _pending_ticket"
    db_multi_exec("INSERT OR IGNORE INTO _pending_ticket "
                  "VALUES(%Q)", p->zTicketUuid);
  }
  blob_reset(&sql);
}

/*
** Rebuild an entire entry in the TICKET table
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
317
318
319
320
321
322
323


























































324
325
326
327
328
329
330







-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-







  initializeVariablesFromDb();
  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;
  if( SbS_RequireStack(p, 1, "submit_new_ticket") ) 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;

    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;
      zValue = SbS_Fetch(p, azField[i], -1, &nValue);
      if( zValue ){
        while( nValue>0 && isspace(zValue[nValue-1]) ){ nValue--; }
        blob_appendf(&tktchng, "J %s %z\n",
           azField[i], fossilize(zValue,nValue));
      }
    }
    zUuid = db_text(0, "SELECT lower(hex(randomblob(20)))");
    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 0
    @ <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;  
}


/*
** 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
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
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







-
+












-
-
-
-
-
-
-
+
+
+
+
+
+
+
-
+












+












-
+




-
+












+







-







                || strlen(azValue[i])!=nValue ){
          blob_appendf(&tktchng, "J %s %z\n",
             azField[i], fossilize(zValue,nValue));
        }
      }
    }
  }
  if( *(char**)pUuid==0 ){
  if( *(char**)pUuid ){
    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;
  if( strncmp(g.zPath,"debug_",6)==0 ){
    @ <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
** WEBPAGE: debug_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">
  @ <form method="POST" action="%s(g.zBaseURL)/%s(g.zPath)">
  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);
  SbS_AddVerb(pInterp, "submit_ticket", submitTicketCmd, (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
** WEBPAGE: debug_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) ){
537
538
539
540
541
542
543
544

545
546
547
548
549
550
551
552
553



554
555
556
557
558
484
485
486
487
488
489
490

491
492
493
494
495
496
497



498
499
500
501
502
503
504
505







-
+






-
-
-
+
+
+





    style_footer();
    return;
  }
  ticket_init();
  getAllTicketFields();
  initializeVariablesFromCGI();
  initializeVariablesFromDb();
  @ <form method="POST" action="%s(g.zBaseURL)/tktedit">
  @ <form method="POST" action="%s(g.zBaseURL)/%s(g.zPath)">
  @ <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));
  SbS_AddVerb(pInterp, "submit_ticket", submitTicketCmd, (void*)&zName);
  if( SbS_Render(pInterp, zScript)==SBS_RETURN && zName ){
    cgi_redirect(mprintf("%s/tktview/%s", g.zBaseURL, zName));
    return;
  }
  @ </form>
  style_footer();
}