Fossil SCM

fossil-scm / src / tkt.c
Source Blame History 1940 lines
d0305b3… aku 1 /*
c19f34c… drh 2 ** Copyright (c) 2007 D. Richard Hipp
d0305b3… aku 3 **
d0305b3… aku 4 ** This program is free software; you can redistribute it and/or
c06edd2… drh 5 ** modify it under the terms of the Simplified BSD License (also
c06edd2… drh 6 ** known as the "2-Clause License" or "FreeBSD License".)
c06edd2… drh 7
d0305b3… aku 8 ** This program is distributed in the hope that it will be useful,
c06edd2… drh 9 ** but without any warranty; without even the implied warranty of
c06edd2… drh 10 ** merchantability or fitness for a particular purpose.
d0305b3… aku 11 **
d0305b3… aku 12 ** Author contact information:
d0305b3… aku 13 ** [email protected]
d0305b3… aku 14 ** http://www.hwaci.com/drh/
d0305b3… aku 15 **
d0305b3… aku 16 *******************************************************************************
d0305b3… aku 17 **
d0305b3… aku 18 ** This file contains code used render and control ticket entry
d0305b3… aku 19 ** and display pages.
d0305b3… aku 20 */
d0305b3… aku 21 #include "config.h"
d0305b3… aku 22 #include "tkt.h"
d0305b3… aku 23 #include <assert.h>
d0305b3… aku 24
d0305b3… aku 25 /*
d0305b3… aku 26 ** The list of database user-defined fields in the TICKET table.
d0305b3… aku 27 ** The real table also contains some addition fields for internal
cf00d07… george 28 ** use. The internal-use fields begin with "tkt_".
d0305b3… aku 29 */
d0305b3… aku 30 static int nField = 0;
4f8c897… drh 31 static struct tktFieldInfo {
4f8c897… drh 32 char *zName; /* Name of the database field */
4f8c897… drh 33 char *zValue; /* Value to store */
4f8c897… drh 34 char *zAppend; /* Value to append */
872a3b2… george 35 char *zBsln; /* "baseline for $zName" if that field exists*/
4f8c897… drh 36 unsigned mUsed; /* 01: TICKET 02: TICKETCHNG */
4f8c897… drh 37 } *aField;
4f8c897… drh 38 #define USEDBY_TICKET 01
4f8c897… drh 39 #define USEDBY_TICKETCHNG 02
a18a49c… drh 40 #define USEDBY_BOTH 03
872a3b2… george 41 #define JCARD_ASSIGN ('=')
872a3b2… george 42 #define JCARD_APPEND ('+')
872a3b2… george 43 #define JCARD_PRIVATE ('p')
8554d3e… drh 44 static u8 haveTicket = 0; /* True if the TICKET table exists */
8554d3e… drh 45 static u8 haveTicketCTime = 0; /* True if TICKET.TKT_CTIME exists */
8554d3e… drh 46 static u8 haveTicketChng = 0; /* True if the TICKETCHNG table exists */
8554d3e… drh 47 static u8 haveTicketChngRid = 0; /* True if TICKETCHNG.TKT_RID exists */
cf00d07… george 48 static u8 haveTicketChngUser = 0;/* True if TICKETCHNG.TKT_USER exists */
6fb642c… george 49 static u8 useTicketGenMt = 0; /* use generated TICKET.MIMETYPE */
6fb642c… george 50 static u8 useTicketChngGenMt = 0;/* use generated TICKETCHNG.MIMETYPE */
872a3b2… george 51 static int nTicketBslns = 0; /* number of valid "baseline for ..." */
6fb642c… george 52
d0305b3… aku 53
d0305b3… aku 54 /*
4f8c897… drh 55 ** Compare two entries in aField[] for sorting purposes
d0305b3… aku 56 */
d0305b3… aku 57 static int nameCmpr(const void *a, const void *b){
4f8c897… drh 58 return fossil_strcmp(((const struct tktFieldInfo*)a)->zName,
4f8c897… drh 59 ((const struct tktFieldInfo*)b)->zName);
4f8c897… drh 60 }
4f8c897… drh 61
4f8c897… drh 62 /*
4f8c897… drh 63 ** Return the index into aField[] of the given field name.
4f8c897… drh 64 ** Return -1 if zFieldName is not in aField[].
4f8c897… drh 65 */
4f8c897… drh 66 static int fieldId(const char *zFieldName){
4f8c897… drh 67 int i;
4f8c897… drh 68 for(i=0; i<nField; i++){
4f8c897… drh 69 if( fossil_strcmp(aField[i].zName, zFieldName)==0 ) return i;
4f8c897… drh 70 }
4f8c897… drh 71 return -1;
d0305b3… aku 72 }
d0305b3… aku 73
d0305b3… aku 74 /*
3df526c… jan.nijtmans 75 ** Obtain a list of all fields of the TICKET and TICKETCHNG tables. Put them
4f8c897… drh 76 ** in sorted order in aField[].
d0305b3… aku 77 **
4f8c897… drh 78 ** The haveTicket and haveTicketChng variables are set to 1 if the TICKET and
4f8c897… drh 79 ** TICKETCHANGE tables exist, respectively.
d0305b3… aku 80 */
d0305b3… aku 81 static void getAllTicketFields(void){
d0305b3… aku 82 Stmt q;
872a3b2… george 83 int i, noRegularMimetype, nBaselines;
4f8c897… drh 84 static int once = 0;
4f8c897… drh 85 if( once ) return;
4f8c897… drh 86 once = 1;
872a3b2… george 87 nBaselines = 0;
d0305b3… aku 88 db_prepare(&q, "PRAGMA table_info(ticket)");
d0305b3… aku 89 while( db_step(&q)==SQLITE_ROW ){
4f8c897… drh 90 const char *zFieldName = db_column_text(&q, 1);
4f8c897… drh 91 haveTicket = 1;
8554d3e… drh 92 if( memcmp(zFieldName,"tkt_",4)==0 ){
8554d3e… drh 93 if( strcmp(zFieldName, "tkt_ctime")==0 ) haveTicketCTime = 1;
8554d3e… drh 94 continue;
8554d3e… drh 95 }
872a3b2… george 96 if( memcmp(zFieldName,"baseline for ",13)==0 ){
872a3b2… george 97 if( strcmp(db_column_text(&q,2),"INTEGER")==0 ){
872a3b2… george 98 nBaselines++;
872a3b2… george 99 }
872a3b2… george 100 continue;
872a3b2… george 101 }
dbcb446… george 102 if( strchr(zFieldName,' ')!=0 ) continue;
4f8c897… drh 103 if( nField%10==0 ){
4f8c897… drh 104 aField = fossil_realloc(aField, sizeof(aField[0])*(nField+10) );
4f8c897… drh 105 }
872a3b2… george 106 aField[nField].zBsln = 0;
4c3e172… danield 107 aField[nField].zName = fossil_strdup(zFieldName);
4f8c897… drh 108 aField[nField].mUsed = USEDBY_TICKET;
4f8c897… drh 109 nField++;
4f8c897… drh 110 }
4f8c897… drh 111 db_finalize(&q);
872a3b2… george 112 if( nBaselines ){
872a3b2… george 113 db_prepare(&q, "SELECT 1 FROM pragma_table_info('ticket') "
872a3b2… george 114 "WHERE type = 'INTEGER' AND name = :n");
872a3b2… george 115 for(i=0; i<nField && nBaselines!=0; i++){
872a3b2… george 116 char *zBsln = mprintf("baseline for %s",aField[i].zName);
872a3b2… george 117 db_bind_text(&q, ":n", zBsln);
872a3b2… george 118 if( db_step(&q)==SQLITE_ROW ){
872a3b2… george 119 aField[i].zBsln = zBsln;
872a3b2… george 120 nTicketBslns++;
872a3b2… george 121 nBaselines--;
872a3b2… george 122 }else{
872a3b2… george 123 free(zBsln);
872a3b2… george 124 }
872a3b2… george 125 db_reset(&q);
872a3b2… george 126 }
872a3b2… george 127 db_finalize(&q);
872a3b2… george 128 }
4f8c897… drh 129 db_prepare(&q, "PRAGMA table_info(ticketchng)");
4f8c897… drh 130 while( db_step(&q)==SQLITE_ROW ){
4f8c897… drh 131 const char *zFieldName = db_column_text(&q, 1);
4f8c897… drh 132 haveTicketChng = 1;
8554d3e… drh 133 if( memcmp(zFieldName,"tkt_",4)==0 ){
cf00d07… george 134 if( strcmp(zFieldName+4,"rid")==0 ){
cf00d07… george 135 haveTicketChngRid = 1; /* tkt_rid */
cf00d07… george 136 }else if( strcmp(zFieldName+4,"user")==0 ){
cf00d07… george 137 haveTicketChngUser = 1; /* tkt_user */
cf00d07… george 138 }
8554d3e… drh 139 continue;
8554d3e… drh 140 }
dbcb446… george 141 if( strchr(zFieldName,' ')!=0 ) continue;
4f8c897… drh 142 if( (i = fieldId(zFieldName))>=0 ){
4f8c897… drh 143 aField[i].mUsed |= USEDBY_TICKETCHNG;
4f8c897… drh 144 continue;
4f8c897… drh 145 }
d0305b3… aku 146 if( nField%10==0 ){
4f8c897… drh 147 aField = fossil_realloc(aField, sizeof(aField[0])*(nField+10) );
d0305b3… aku 148 }
872a3b2… george 149 aField[nField].zBsln = 0;
4c3e172… danield 150 aField[nField].zName = fossil_strdup(zFieldName);
4f8c897… drh 151 aField[nField].mUsed = USEDBY_TICKETCHNG;
d0305b3… aku 152 nField++;
d0305b3… aku 153 }
d0305b3… aku 154 db_finalize(&q);
4f8c897… drh 155 qsort(aField, nField, sizeof(aField[0]), nameCmpr);
6fb642c… george 156 noRegularMimetype = 1;
4f8c897… drh 157 for(i=0; i<nField; i++){
4f8c897… drh 158 aField[i].zValue = "";
4f8c897… drh 159 aField[i].zAppend = 0;
6fb642c… george 160 if( strcmp(aField[i].zName,"mimetype")==0 ){
6fb642c… george 161 noRegularMimetype = 0;
6fb642c… george 162 }
6fb642c… george 163 }
6fb642c… george 164 if( noRegularMimetype ){ /* check for generated "mimetype" columns */
6fb642c… george 165 useTicketGenMt = db_exists(
6fb642c… george 166 "SELECT 1 FROM pragma_table_xinfo('ticket') "
6fb642c… george 167 "WHERE name = 'mimetype'");
6fb642c… george 168 useTicketChngGenMt = db_exists(
6fb642c… george 169 "SELECT 1 FROM pragma_table_xinfo('ticketchng') "
6fb642c… george 170 "WHERE name = 'mimetype'");
4f8c897… drh 171 }
d0305b3… aku 172 }
d0305b3… aku 173
d0305b3… aku 174 /*
d0305b3… aku 175 ** Query the database for all TICKET fields for the specific
d0305b3… aku 176 ** ticket whose name is given by the "name" CGI parameter.
d0305b3… aku 177 ** Load the values for all fields into the interpreter.
d0305b3… aku 178 **
d0305b3… aku 179 ** Only load those fields which do not already exist as
d0305b3… aku 180 ** variables.
f46fe42… drh 181 **
f46fe42… drh 182 ** Fields of the TICKET table that begin with "private_" are
b344d3c… drh 183 ** expanded using the db_reveal() function. If g.perm.RdAddr is
31e94c0… drh 184 ** true, then the db_reveal() function will decode the content
db0c512… drh 185 ** using the CONCEALED table so that the content legible.
f46fe42… drh 186 ** Otherwise, db_reveal() is a no-op and the content remains
f46fe42… drh 187 ** obscured.
d0305b3… aku 188 */
d0305b3… aku 189 static void initializeVariablesFromDb(void){
d0305b3… aku 190 const char *zName;
d0305b3… aku 191 Stmt q;
d0305b3… aku 192 int i, n, size, j;
6476f28… drh 193 const char *zCTimeColumn = haveTicketCTime ? "tkt_ctime" : "tkt_mtime";
d0305b3… aku 194
49380d5… drh 195 zName = PD("name","-none-");
fab9f00… jkosche 196 db_prepare(&q, "SELECT datetime(tkt_mtime,toLocal()) AS tkt_datetime, "
6476f28… drh 197 "datetime(%s,toLocal()) AS tkt_datetime_creation, "
fab9f00… jkosche 198 "julianday('now') - tkt_mtime, "
6476f28… drh 199 "julianday('now') - %s, *"
bd11511… jan.nijtmans 200 " FROM ticket WHERE tkt_uuid GLOB '%q*'",
6476f28… drh 201 zCTimeColumn/*safe-for-%s*/, zCTimeColumn/*safe-for-%s*/,
ea63a2d… drh 202 zName);
d0305b3… aku 203 if( db_step(&q)==SQLITE_ROW ){
d0305b3… aku 204 n = db_column_count(&q);
d0305b3… aku 205 for(i=0; i<n; i++){
d0305b3… aku 206 const char *zVal = db_column_text(&q, i);
d0305b3… aku 207 const char *zName = db_column_name(&q, i);
f46fe42… drh 208 char *zRevealed = 0;
f46fe42… drh 209 if( zVal==0 ){
f46fe42… drh 210 zVal = "";
f46fe42… drh 211 }else if( strncmp(zName, "private_", 8)==0 ){
f46fe42… drh 212 zVal = zRevealed = db_reveal(zVal);
f46fe42… drh 213 }
4f8c897… drh 214 if( (j = fieldId(zName))>=0 ){
4c3e172… danield 215 aField[j].zValue = fossil_strdup(zVal);
4f8c897… drh 216 }else if( memcmp(zName, "tkt_", 4)==0 && Th_Fetch(zName, &size)==0 ){
9e035ee… drh 217 /* TICKET table columns that begin with "tkt_" are always safe */
9e035ee… drh 218 Th_Store(zName, zVal);
f46fe42… drh 219 }
f46fe42… drh 220 free(zRevealed);
f46fe42… drh 221 }
fab9f00… jkosche 222 Th_Store("tkt_mage", human_readable_age(db_column_double(&q, 2)));
fab9f00… jkosche 223 Th_Store("tkt_cage", human_readable_age(db_column_double(&q, 3)));
4f8c897… drh 224 }
4f8c897… drh 225 db_finalize(&q);
4f8c897… drh 226 for(i=0; i<nField; i++){
4f8c897… drh 227 if( Th_Fetch(aField[i].zName, &size)==0 ){
2116238… drh 228 Th_StoreUnsafe(aField[i].zName, aField[i].zValue);
4f8c897… drh 229 }
4f8c897… drh 230 }
d0305b3… aku 231 }
d0305b3… aku 232
d0305b3… aku 233 /*
d0305b3… aku 234 ** Transfer all CGI parameters to variables in the interpreter.
d0305b3… aku 235 */
d0305b3… aku 236 static void initializeVariablesFromCGI(void){
d0305b3… aku 237 int i;
d0305b3… aku 238 const char *z;
d0305b3… aku 239
d0305b3… aku 240 for(i=0; (z = cgi_parameter_name(i))!=0; i++){
2116238… drh 241 Th_StoreUnsafe(z, P(z));
7c2577b… drh 242 }
7c2577b… drh 243 }
872a3b2… george 244
872a3b2… george 245 /*
872a3b2… george 246 ** Information about a single J-card
872a3b2… george 247 */
872a3b2… george 248 struct jCardInfo {
872a3b2… george 249 char *zValue;
872a3b2… george 250 int mimetype;
872a3b2… george 251 int rid;
872a3b2… george 252 double mtime;
872a3b2… george 253 };
4f8c897… drh 254
4f8c897… drh 255 /*
4f8c897… drh 256 ** Update an entry of the TICKET and TICKETCHNG tables according to the
4f8c897… drh 257 ** information in the ticket artifact given in p. Attempt to create
4f8c897… drh 258 ** the appropriate TICKET table entry if tktid is zero. If tktid is nonzero
4f8c897… drh 259 ** then it will be the ROWID of an existing TICKET entry.
4f8c897… drh 260 **
4f8c897… drh 261 ** Parameter rid is the recordID for the ticket artifact in the BLOB table.
872a3b2… george 262 ** Upon assignment of a field this rid is stored into a corresponding
872a3b2… george 263 ** zBsln integer column (provided that it is defined within TICKET table).
872a3b2… george 264 **
872a3b2… george 265 ** If a field is USEDBY_TICKETCHNG table then back-references within it
872a3b2… george 266 ** are extracted and inserted into the BACKLINK table; otherwise
872a3b2… george 267 ** a corresponding blob in the `fields` array is updated so that the
872a3b2… george 268 ** caller could extract backlinks from the most recent field's values.
7c2577b… drh 269 **
4f8c897… drh 270 ** Return the new rowid of the TICKET table entry.
7c2577b… drh 271 */
872a3b2… george 272 static int ticket_insert(const Manifest *p, const int rid, int tktid,
872a3b2… george 273 Blob *fields){
cf00d07… george 274 Blob sql1; /* update or replace TICKET ... */
cf00d07… george 275 Blob sql2; /* list of TICKETCHNG's fields that are in the manifest */
cf00d07… george 276 Blob sql3; /* list of values which correspond to the previous list */
7c2577b… drh 277 Stmt q;
4f8c897… drh 278 int i, j;
a18a49c… drh 279 char *aUsed;
6fb642c… george 280 int mimetype_tkt = MT_NONE, mimetype_tktchng = MT_NONE;
7c2577b… drh 281
4f8c897… drh 282 if( tktid==0 ){
4f8c897… drh 283 db_multi_exec("INSERT INTO ticket(tkt_uuid, tkt_mtime) "
7c2577b… drh 284 "VALUES(%Q, 0)", p->zTicketUuid);
4f8c897… drh 285 tktid = db_last_insert_rowid();
7c2577b… drh 286 }
4f8c897… drh 287 blob_zero(&sql1);
4f8c897… drh 288 blob_zero(&sql2);
4f8c897… drh 289 blob_zero(&sql3);
49b0ff1… drh 290 blob_append_sql(&sql1, "UPDATE OR REPLACE ticket SET tkt_mtime=:mtime");
8554d3e… drh 291 if( haveTicketCTime ){
49b0ff1… drh 292 blob_append_sql(&sql1, ", tkt_ctime=coalesce(tkt_ctime,:mtime)");
8554d3e… drh 293 }
872a3b2… george 294 aUsed = fossil_malloc_zero( nField );
7c2577b… drh 295 for(i=0; i<p->nField; i++){
cf00d07… george 296 const char * const zName = p->aField[i].zName;
cf00d07… george 297 const char * const zBaseName = zName[0]=='+' ? zName+1 : zName;
67f787d… drh 298 j = fieldId(zBaseName);
67f787d… drh 299 if( j<0 ) continue;
a18a49c… drh 300 aUsed[j] = 1;
a18a49c… drh 301 if( aField[j].mUsed & USEDBY_TICKET ){
cf00d07… george 302 if( zName[0]=='+' ){
49b0ff1… drh 303 blob_append_sql(&sql1,", \"%w\"=coalesce(\"%w\",'') || %Q",
cf00d07… george 304 zBaseName, zBaseName, p->aField[i].zValue);
872a3b2… george 305 /* when appending keep "baseline for ..." unchanged */
a18a49c… drh 306 }else{
cf00d07… george 307 blob_append_sql(&sql1,", \"%w\"=%Q", zBaseName, p->aField[i].zValue);
872a3b2… george 308 if( aField[j].zBsln ){
872a3b2… george 309 blob_append_sql(&sql1,", \"%w\"=%d", aField[j].zBsln, rid);
872a3b2… george 310 }
4f8c897… drh 311 }
4f8c897… drh 312 }
4f8c897… drh 313 if( aField[j].mUsed & USEDBY_TICKETCHNG ){
cf00d07… george 314 blob_append_sql(&sql2, ",\"%w\"", zBaseName);
49b0ff1… drh 315 blob_append_sql(&sql3, ",%Q", p->aField[i].zValue);
a18a49c… drh 316 }
ae1dac8… drh 317 if( strcmp(zBaseName,"mimetype")==0 ){
6fb642c… george 318 const char *zMimetype = p->aField[i].zValue;
6fb642c… george 319 /* "mimetype" is a regular column => these two flags must be 0 */
6fb642c… george 320 assert(!useTicketGenMt);
6fb642c… george 321 assert(!useTicketChngGenMt);
6fb642c… george 322 mimetype_tkt = mimetype_tktchng = parse_mimetype( zMimetype );
a18a49c… drh 323 }
a18a49c… drh 324 }
49b0ff1… drh 325 blob_append_sql(&sql1, " WHERE tkt_id=%d", tktid);
6fb642c… george 326 if( useTicketGenMt ){
6fb642c… george 327 blob_append_literal(&sql1, " RETURNING mimetype");
6fb642c… george 328 }
49b0ff1… drh 329 db_prepare(&q, "%s", blob_sql_text(&sql1));
a18a49c… drh 330 db_bind_double(&q, ":mtime", p->rDate);
a18a49c… drh 331 db_step(&q);
6fb642c… george 332 if( useTicketGenMt ){
6fb642c… george 333 mimetype_tkt = parse_mimetype( db_column_text(&q,0) );
6fb642c… george 334 if( !useTicketChngGenMt ){
6fb642c… george 335 mimetype_tktchng = mimetype_tkt;
6fb642c… george 336 }
6fb642c… george 337 }
a18a49c… drh 338 db_finalize(&q);
a18a49c… drh 339 blob_reset(&sql1);
cf00d07… george 340 if( blob_size(&sql2)>0 || haveTicketChngRid || haveTicketChngUser ){
a18a49c… drh 341 int fromTkt = 0;
a18a49c… drh 342 if( haveTicketChngRid ){
cf00d07… george 343 blob_append_literal(&sql2, ",tkt_rid");
49b0ff1… drh 344 blob_append_sql(&sql3, ",%d", rid);
cf00d07… george 345 }
cf00d07… george 346 if( haveTicketChngUser && p->zUser ){
cf00d07… george 347 blob_append_literal(&sql2, ",tkt_user");
cf00d07… george 348 blob_append_sql(&sql3, ",%Q", p->zUser);
a18a49c… drh 349 }
a18a49c… drh 350 for(i=0; i<nField; i++){
a18a49c… drh 351 if( aUsed[i]==0
a18a49c… drh 352 && (aField[i].mUsed & USEDBY_BOTH)==USEDBY_BOTH
a18a49c… drh 353 ){
878f700… drh 354 const char *z = aField[i].zName;
878f700… drh 355 if( z[0]=='+' ) z++;
a18a49c… drh 356 fromTkt = 1;
49b0ff1… drh 357 blob_append_sql(&sql2, ",\"%w\"", z);
49b0ff1… drh 358 blob_append_sql(&sql3, ",\"%w\"", z);
a18a49c… drh 359 }
a18a49c… drh 360 }
a18a49c… drh 361 if( fromTkt ){
a18a49c… drh 362 db_prepare(&q, "INSERT INTO ticketchng(tkt_id,tkt_mtime%s)"
6fb642c… george 363 "SELECT %d,:mtime%s FROM ticket WHERE tkt_id=%d%s",
49b0ff1… drh 364 blob_sql_text(&sql2), tktid,
6fb642c… george 365 blob_sql_text(&sql3), tktid,
6fb642c… george 366 useTicketChngGenMt ? " RETURNING mimetype" : "");
a18a49c… drh 367 }else{
a18a49c… drh 368 db_prepare(&q, "INSERT INTO ticketchng(tkt_id,tkt_mtime%s)"
6fb642c… george 369 "VALUES(%d,:mtime%s)%s",
6fb642c… george 370 blob_sql_text(&sql2), tktid, blob_sql_text(&sql3),
6fb642c… george 371 useTicketChngGenMt ? " RETURNING mimetype" : "");
a18a49c… drh 372 }
a18a49c… drh 373 db_bind_double(&q, ":mtime", p->rDate);
a18a49c… drh 374 db_step(&q);
6fb642c… george 375 if( useTicketChngGenMt ){
6fb642c… george 376 mimetype_tktchng = parse_mimetype( db_column_text(&q, 0) );
6fb642c… george 377 /* substitute NULL with a value generated within another table */
6fb642c… george 378 if( !useTicketGenMt ){
6fb642c… george 379 mimetype_tkt = mimetype_tktchng;
6fb642c… george 380 }else if( mimetype_tktchng==MT_NONE ){
6fb642c… george 381 mimetype_tktchng = mimetype_tkt;
6fb642c… george 382 }else if( mimetype_tkt==MT_NONE ){
6fb642c… george 383 mimetype_tkt = mimetype_tktchng;
6fb642c… george 384 }
6fb642c… george 385 }
4f8c897… drh 386 db_finalize(&q);
4f8c897… drh 387 }
4f8c897… drh 388 blob_reset(&sql2);
4f8c897… drh 389 blob_reset(&sql3);
a18a49c… drh 390 fossil_free(aUsed);
6fb642c… george 391 if( rid>0 ){ /* extract backlinks */
6fb642c… george 392 for(i=0; i<p->nField; i++){
6fb642c… george 393 const char *zName = p->aField[i].zName;
6fb642c… george 394 const char *zBaseName = zName[0]=='+' ? zName+1 : zName;
6fb642c… george 395 j = fieldId(zBaseName);
6fb642c… george 396 if( j<0 ) continue;
6fb642c… george 397 if( aField[j].mUsed & USEDBY_TICKETCHNG ){
872a3b2… george 398 backlink_extract(p->aField[i].zValue, mimetype_tktchng,
872a3b2… george 399 rid, BKLNK_TICKET, p->rDate,
872a3b2… george 400 /* existing backlinks must have been
872a3b2… george 401 * already deleted by the caller */ 0 );
6fb642c… george 402 }else{
872a3b2… george 403 /* update field's data with the most recent values */
872a3b2… george 404 Blob *cards = fields + j;
872a3b2… george 405 struct jCardInfo card = {
872a3b2… george 406 fossil_strdup(p->aField[i].zValue),
872a3b2… george 407 mimetype_tkt, rid, p->rDate
872a3b2… george 408 };
872a3b2… george 409 if( blob_size(cards) && zName[0]!='+' ){
872a3b2… george 410 struct jCardInfo *x = (struct jCardInfo *)blob_buffer(cards);
872a3b2… george 411 struct jCardInfo *end = x + blob_count(cards,struct jCardInfo);
872a3b2… george 412 for(; x!=end; x++){
872a3b2… george 413 fossil_free( x->zValue );
872a3b2… george 414 }
872a3b2… george 415 blob_truncate(cards,0);
872a3b2… george 416 }
872a3b2… george 417 blob_append(cards, (const char*)(&card), sizeof(card));
6fb642c… george 418 }
6fb642c… george 419 }
6fb642c… george 420 }
4f8c897… drh 421 return tktid;
636c334… mistachkin 422 }
636c334… mistachkin 423
636c334… mistachkin 424 /*
636c334… mistachkin 425 ** Returns non-zero if moderation is required for ticket changes and ticket
636c334… mistachkin 426 ** attachments.
636c334… mistachkin 427 */
636c334… mistachkin 428 int ticket_need_moderation(
636c334… mistachkin 429 int localUser /* Are we being called for a local interactive user? */
636c334… mistachkin 430 ){
636c334… mistachkin 431 /*
636c334… mistachkin 432 ** If the FOSSIL_FORCE_TICKET_MODERATION variable is set, *ALL* changes for
636c334… mistachkin 433 ** tickets will be required to go through moderation (even those performed
636c334… mistachkin 434 ** by the local interactive user via the command line). This can be useful
636c334… mistachkin 435 ** for local (or remote) testing of the moderation subsystem and its impact
636c334… mistachkin 436 ** on the contents and status of tickets.
636c334… mistachkin 437 */
636c334… mistachkin 438 if( fossil_getenv("FOSSIL_FORCE_TICKET_MODERATION")!=0 ){
636c334… mistachkin 439 return 1;
636c334… mistachkin 440 }
636c334… mistachkin 441 if( localUser ){
636c334… mistachkin 442 return 0;
636c334… mistachkin 443 }
636c334… mistachkin 444 return g.perm.ModTkt==0 && db_get_boolean("modreq-tkt",0)==1;
d0305b3… aku 445 }
d0305b3… aku 446
d0305b3… aku 447 /*
d0305b3… aku 448 ** Rebuild an entire entry in the TICKET table
d0305b3… aku 449 */
d0305b3… aku 450 void ticket_rebuild_entry(const char *zTktUuid){
d0305b3… aku 451 char *zTag = mprintf("tkt-%s", zTktUuid);
d0305b3… aku 452 int tagid = tag_findid(zTag, 1);
d0305b3… aku 453 Stmt q;
d13054c… drh 454 Manifest *pTicket;
872a3b2… george 455 int tktid, i;
d0305b3… aku 456 int createFlag = 1;
872a3b2… george 457 Blob *fields; /* array of blobs; each blob holds array of jCardInfo */
d8ec765… drh 458
4f8c897… drh 459 fossil_free(zTag);
4f8c897… drh 460 getAllTicketFields();
4f8c897… drh 461 if( haveTicket==0 ) return;
4f8c897… drh 462 tktid = db_int(0, "SELECT tkt_id FROM ticket WHERE tkt_uuid=%Q", zTktUuid);
c0d402f… preben 463 if( tktid!=0 ) search_doc_touch('t', tktid, 0);
4f8c897… drh 464 if( haveTicketChng ){
4f8c897… drh 465 db_multi_exec("DELETE FROM ticketchng WHERE tkt_id=%d;", tktid);
4f8c897… drh 466 }
4f8c897… drh 467 db_multi_exec("DELETE FROM ticket WHERE tkt_id=%d", tktid);
4f8c897… drh 468 tktid = 0;
872a3b2… george 469 fields = blobarray_new( nField );
872a3b2… george 470 db_multi_exec("DELETE FROM backlink WHERE srctype=%d AND srcid IN "
872a3b2… george 471 "(SELECT rid FROM tagxref WHERE tagid=%d)",BKLNK_TICKET, tagid);
d0305b3… aku 472 db_prepare(&q, "SELECT rid FROM tagxref WHERE tagid=%d ORDER BY mtime",tagid);
d0305b3… aku 473 while( db_step(&q)==SQLITE_ROW ){
d0305b3… aku 474 int rid = db_column_int(&q, 0);
ec81aee… jan.nijtmans 475 pTicket = manifest_get(rid, CFTYPE_TICKET, 0);
d13054c… drh 476 if( pTicket ){
872a3b2… george 477 tktid = ticket_insert(pTicket, rid, tktid, fields);
d13054c… drh 478 manifest_ticket_event(rid, pTicket, createFlag, tagid);
d13054c… drh 479 manifest_destroy(pTicket);
d13054c… drh 480 }
d0305b3… aku 481 createFlag = 0;
d0305b3… aku 482 }
d0305b3… aku 483 db_finalize(&q);
c0d402f… preben 484 search_doc_touch('t', tktid, 0);
872a3b2… george 485 /* Extract backlinks from the most recent values of TICKET fields */
872a3b2… george 486 for(i=0; i<nField; i++){
872a3b2… george 487 Blob *cards = fields + i;
872a3b2… george 488 if( blob_size(cards) ){
872a3b2… george 489 struct jCardInfo *x = (struct jCardInfo *)blob_buffer(cards);
872a3b2… george 490 struct jCardInfo *end = x + blob_count(cards,struct jCardInfo);
872a3b2… george 491 for(; x!=end; x++){
872a3b2… george 492 assert( x->zValue );
872a3b2… george 493 backlink_extract(x->zValue,x->mimetype,
872a3b2… george 494 x->rid,BKLNK_TICKET,x->mtime,0);
872a3b2… george 495 fossil_free( x->zValue );
872a3b2… george 496 }
872a3b2… george 497 }
872a3b2… george 498 blob_truncate(cards,0);
872a3b2… george 499 }
872a3b2… george 500 blobarray_delete(fields,nField);
d0305b3… aku 501 }
a18a49c… drh 502
4f8c897… drh 503
d0305b3… aku 504 /*
4f8c897… drh 505 ** Create the TH1 interpreter and load the "common" code.
d0305b3… aku 506 */
d0305b3… aku 507 void ticket_init(void){
86db6fa… drh 508 const char *zConfig;
0e68620… mistachkin 509 Th_FossilInit(TH_INIT_DEFAULT);
86db6fa… drh 510 zConfig = ticket_common_code();
0c99a15… drh 511 Th_Eval(g.interp, 0, zConfig, -1);
0c99a15… drh 512 }
0c99a15… drh 513
0c99a15… drh 514 /*
4f8c897… drh 515 ** Create the TH1 interpreter and load the "change" code.
a905596… drh 516 */
fc32fae… mistachkin 517 int ticket_change(const char *zUuid){
a905596… drh 518 const char *zConfig;
0e68620… mistachkin 519 Th_FossilInit(TH_INIT_DEFAULT);
fc32fae… mistachkin 520 Th_Store("uuid", zUuid);
a905596… drh 521 zConfig = ticket_change_code();
a905596… drh 522 return Th_Eval(g.interp, 0, zConfig, -1);
a905596… drh 523 }
a905596… drh 524
a905596… drh 525 /*
f741baa… drh 526 ** An authorizer function for the SQL used to initialize the
93c45cd… drh 527 ** schema for the ticketing system. Only allow
93c45cd… drh 528 **
93c45cd… drh 529 ** CREATE TABLE
93c45cd… drh 530 ** CREATE INDEX
93c45cd… drh 531 ** CREATE VIEW
c717f1e… drh 532 ** DROP INDEX
c717f1e… drh 533 ** DROP VIEW
93c45cd… drh 534 **
93c45cd… drh 535 ** And for objects in "main" or "repository" whose names
93c45cd… drh 536 ** begin with "ticket" or "fx_". Also allow
93c45cd… drh 537 **
93c45cd… drh 538 ** INSERT
93c45cd… drh 539 ** UPDATE
93c45cd… drh 540 ** DELETE
93c45cd… drh 541 **
93c45cd… drh 542 ** But only for tables in "main" or "repository" whose names
93c45cd… drh 543 ** begin with "ticket", "sqlite_", or "fx_".
93c45cd… drh 544 **
93c45cd… drh 545 ** Of particular importance for security is that this routine
93c45cd… drh 546 ** disallows data changes on the "config" table, as that could
93c45cd… drh 547 ** allow a malicious server to modify settings in such a way as
93c45cd… drh 548 ** to cause a remote code execution.
c717f1e… drh 549 **
c717f1e… drh 550 ** Use the "fossil test-db-prepare --auth-ticket SQL" command to perform
c717f1e… drh 551 ** manual testing of this authorizer.
f741baa… drh 552 */
f741baa… drh 553 static int ticket_schema_auth(
f741baa… drh 554 void *pNErr,
f741baa… drh 555 int eCode,
f741baa… drh 556 const char *z0,
f741baa… drh 557 const char *z1,
f741baa… drh 558 const char *z2,
f741baa… drh 559 const char *z3
f741baa… drh 560 ){
f741baa… drh 561 switch( eCode ){
c717f1e… drh 562 case SQLITE_DROP_VIEW:
93c45cd… drh 563 case SQLITE_CREATE_VIEW:
f741baa… drh 564 case SQLITE_CREATE_TABLE: {
f741baa… drh 565 if( sqlite3_stricmp(z2,"main")!=0
f741baa… drh 566 && sqlite3_stricmp(z2,"repository")!=0
f741baa… drh 567 ){
f741baa… drh 568 goto ticket_schema_error;
f741baa… drh 569 }
275da70… danield 570 if( sqlite3_strnicmp(z0,"ticket",6)!=0
93c45cd… drh 571 && sqlite3_strnicmp(z0,"fx_",3)!=0
93c45cd… drh 572 ){
f741baa… drh 573 goto ticket_schema_error;
f741baa… drh 574 }
f741baa… drh 575 break;
f741baa… drh 576 }
c717f1e… drh 577 case SQLITE_DROP_INDEX:
f741baa… drh 578 case SQLITE_CREATE_INDEX: {
f741baa… drh 579 if( sqlite3_stricmp(z2,"main")!=0
f741baa… drh 580 && sqlite3_stricmp(z2,"repository")!=0
f741baa… drh 581 ){
f741baa… drh 582 goto ticket_schema_error;
f741baa… drh 583 }
93c45cd… drh 584 if( sqlite3_strnicmp(z1,"ticket",6)!=0
93c45cd… drh 585 && sqlite3_strnicmp(z0,"fx_",3)!=0
93c45cd… drh 586 ){
f741baa… drh 587 goto ticket_schema_error;
f741baa… drh 588 }
f741baa… drh 589 break;
f741baa… drh 590 }
f741baa… drh 591 case SQLITE_INSERT:
f741baa… drh 592 case SQLITE_UPDATE:
f741baa… drh 593 case SQLITE_DELETE: {
f741baa… drh 594 if( sqlite3_stricmp(z2,"main")!=0
f741baa… drh 595 && sqlite3_stricmp(z2,"repository")!=0
f741baa… drh 596 ){
f741baa… drh 597 goto ticket_schema_error;
f741baa… drh 598 }
f741baa… drh 599 if( sqlite3_strnicmp(z0,"ticket",6)!=0
f741baa… drh 600 && sqlite3_strnicmp(z0,"sqlite_",7)!=0
93c45cd… drh 601 && sqlite3_strnicmp(z0,"fx_",3)!=0
f741baa… drh 602 ){
f741baa… drh 603 goto ticket_schema_error;
f741baa… drh 604 }
f741baa… drh 605 break;
f741baa… drh 606 }
3fe8a8c… drh 607 case SQLITE_SELECT:
4740cf7… drh 608 case SQLITE_FUNCTION:
f741baa… drh 609 case SQLITE_REINDEX:
f741baa… drh 610 case SQLITE_TRANSACTION:
f741baa… drh 611 case SQLITE_READ: {
f741baa… drh 612 break;
f741baa… drh 613 }
f741baa… drh 614 default: {
f741baa… drh 615 goto ticket_schema_error;
f741baa… drh 616 }
f741baa… drh 617 }
f741baa… drh 618 return SQLITE_OK;
f741baa… drh 619
f741baa… drh 620 ticket_schema_error:
f741baa… drh 621 if( pNErr ) *(int*)pNErr = 1;
f741baa… drh 622 return SQLITE_DENY;
f741baa… drh 623 }
f741baa… drh 624
c717f1e… drh 625 /*
c717f1e… drh 626 ** Activate the ticket schema authorizer. Must be followed by
c717f1e… drh 627 ** an eventual call to ticket_unrestrict_sql().
c717f1e… drh 628 */
c717f1e… drh 629 void ticket_restrict_sql(int * pNErr){
c717f1e… drh 630 db_set_authorizer(ticket_schema_auth,(void*)pNErr,"Ticket-Schema");
c717f1e… drh 631 }
c717f1e… drh 632 /*
c717f1e… drh 633 ** Deactivate the ticket schema authorizer.
c717f1e… drh 634 */
c717f1e… drh 635 void ticket_unrestrict_sql(void){
c717f1e… drh 636 db_clear_authorizer();
c717f1e… drh 637 }
c717f1e… drh 638
f741baa… drh 639
f741baa… drh 640 /*
4f8c897… drh 641 ** Recreate the TICKET and TICKETCHNG tables.
d0305b3… aku 642 */
d0305b3… aku 643 void ticket_create_table(int separateConnection){
4cf8dbe… stephan 644 char *zSql;
86db6fa… drh 645
4f8c897… drh 646 db_multi_exec(
4f8c897… drh 647 "DROP TABLE IF EXISTS ticket;"
4f8c897… drh 648 "DROP TABLE IF EXISTS ticketchng;"
4f8c897… drh 649 );
86db6fa… drh 650 zSql = ticket_table_schema();
c717f1e… drh 651 ticket_restrict_sql(0);
d0305b3… aku 652 if( separateConnection ){
f5eb03f… drh 653 if( db_transaction_nesting_depth() ) db_end_transaction(0);
d0305b3… aku 654 db_init_database(g.zRepositoryName, zSql, 0);
d0305b3… aku 655 }else{
49b0ff1… drh 656 db_multi_exec("%s", zSql/*safe-for-%s*/);
d0305b3… aku 657 }
c717f1e… drh 658 ticket_unrestrict_sql();
4cf8dbe… stephan 659 fossil_free(zSql);
d0305b3… aku 660 }
d0305b3… aku 661
d0305b3… aku 662 /*
4f8c897… drh 663 ** Repopulate the TICKET and TICKETCHNG tables from scratch using all
4f8c897… drh 664 ** available ticket artifacts.
d0305b3… aku 665 */
d0305b3… aku 666 void ticket_rebuild(void){
d0305b3… aku 667 Stmt q;
f815d48… stephan 668 if( db_database_slot("repository")==0 ){
f815d48… stephan 669 /* forum:4f3f572abff6b8c1: to avoid losing all of the tickets if the
f815d48… stephan 670 ** current operation fails at some point after this call, we need
f815d48… stephan 671 ** to start the transaction before dropping the ticket tables.
f815d48… stephan 672 **
f815d48… stephan 673 ** Claude was able to reproduce that report reliably using a
f815d48… stephan 674 ** fossil server running with --max-latency 2 and 40k tickets. */
f815d48… stephan 675 db_begin_transaction();
f815d48… stephan 676 ticket_create_table(0);
f815d48… stephan 677 }else{
f815d48… stephan 678 /* A checkout: the tickets go into the associated repo db via
f815d48… stephan 679 ** ticket_create_table(). Delay transaction start until that
f815d48… stephan 680 ** succeeds. */
f815d48… stephan 681 ticket_create_table(1);
f815d48… stephan 682 db_begin_transaction();
f815d48… stephan 683 }
d0305b3… aku 684 db_prepare(&q,"SELECT tagname FROM tag WHERE tagname GLOB 'tkt-*'");
d0305b3… aku 685 while( db_step(&q)==SQLITE_ROW ){
d0305b3… aku 686 const char *zName = db_column_text(&q, 0);
d0305b3… aku 687 int len;
d0305b3… aku 688 zName += 4;
d0305b3… aku 689 len = strlen(zName);
d0305b3… aku 690 if( len<20 || !validate16(zName, len) ) continue;
d0305b3… aku 691 ticket_rebuild_entry(zName);
d0305b3… aku 692 }
d0305b3… aku 693 db_finalize(&q);
d0305b3… aku 694 db_end_transaction(0);
d0305b3… aku 695 }
d0305b3… aku 696
d0305b3… aku 697 /*
a18a49c… drh 698 ** COMMAND: test-ticket-rebuild
a18a49c… drh 699 **
a18a49c… drh 700 ** Usage: %fossil test-ticket-rebuild TICKETID|all
a18a49c… drh 701 **
a18a49c… drh 702 ** Rebuild the TICKET and TICKETCHNG tables for the given ticket ID
a18a49c… drh 703 ** or for ALL.
a18a49c… drh 704 */
a18a49c… drh 705 void test_ticket_rebuild(void){
a18a49c… drh 706 db_find_and_open_repository(0, 0);
a18a49c… drh 707 if( g.argc!=3 ) usage("TICKETID|all");
a18a49c… drh 708 if( fossil_strcmp(g.argv[2], "all")==0 ){
a18a49c… drh 709 ticket_rebuild();
a18a49c… drh 710 }else{
a18a49c… drh 711 const char *zUuid;
a18a49c… drh 712 zUuid = db_text(0, "SELECT substr(tagname,5) FROM tag"
a18a49c… drh 713 " WHERE tagname GLOB 'tkt-%q*'", g.argv[2]);
a18a49c… drh 714 if( zUuid==0 ) fossil_fatal("no such ticket: %s", g.argv[2]);
a18a49c… drh 715 ticket_rebuild_entry(zUuid);
a18a49c… drh 716 }
a18a49c… drh 717 }
a18a49c… drh 718
a18a49c… drh 719 /*
4f8c897… drh 720 ** For trouble-shooting purposes, render a dump of the aField[] table to
4f8c897… drh 721 ** the webpage currently under construction.
4f8c897… drh 722 */
4f8c897… drh 723 static void showAllFields(void){
4f8c897… drh 724 int i;
d1f60ca… jan.nijtmans 725 @ <div style="color:blue">
4f8c897… drh 726 @ <p>Database fields:</p><ul>
4f8c897… drh 727 for(i=0; i<nField; i++){
4f8c897… drh 728 @ <li>aField[%d(i)].zName = "%h(aField[i].zName)";
4f8c897… drh 729 @ originally = "%h(aField[i].zValue)";
8fdb682… drh 730 @ currently = "%h(PD(aField[i].zName,""))";
4f8c897… drh 731 if( aField[i].zAppend ){
4f8c897… drh 732 @ zAppend = "%h(aField[i].zAppend)";
4f8c897… drh 733 }
4f8c897… drh 734 @ mUsed = %d(aField[i].mUsed);
4f8c897… drh 735 }
3b3310a… jan.nijtmans 736 @ </ul></div>
4f8c897… drh 737 }
4f8c897… drh 738
4f8c897… drh 739 /*
d0305b3… aku 740 ** WEBPAGE: tktview
5f22b96… drh 741 ** URL: tktview/HASH
7ab0328… drh 742 **
7ab0328… drh 743 ** View a ticket identified by the name= query parameter.
3d13152… drh 744 ** Other query parameters:
3d13152… drh 745 **
3d13152… drh 746 ** tl Show a timeline of the ticket above the status
d0305b3… aku 747 */
d0305b3… aku 748 void tktview_page(void){
86db6fa… drh 749 const char *zScript;
7c2577b… drh 750 char *zFullName;
7c2577b… drh 751 const char *zUuid = PD("name","");
3d13152… drh 752 int showTimeline = P("tl")!=0;
7c2577b… drh 753
d0305b3… aku 754 login_check_credentials();
653dd40… drh 755 if( !g.perm.RdTkt ){ login_needed(g.anon.RdTkt); return; }
653dd40… drh 756 if( g.anon.WrTkt || g.anon.ApndTkt ){
5f22b96… drh 757 style_submenu_element("Edit", "%R/tktedit/%T", PD("name",""));
433cde1… drh 758 }
433cde1… drh 759 if( g.perm.Hyperlink ){
a40e8a0… drh 760 style_submenu_element("History", "%R/tkthistory/%T", zUuid);
c04c814… george 761 if( g.perm.Read ){
c04c814… george 762 style_submenu_element("Check-ins", "%R/tkttimeline/%T?y=ci", zUuid);
c04c814… george 763 }
653dd40… drh 764 }
653dd40… drh 765 if( g.anon.NewTkt ){
a40e8a0… drh 766 style_submenu_element("New Ticket", "%R/tktnew");
653dd40… drh 767 }
ca90659… stephan 768 zFullName = db_text(0,
ca90659… stephan 769 "SELECT tkt_uuid FROM ticket"
ca90659… stephan 770 " WHERE tkt_uuid GLOB '%q*'", zUuid);
653dd40… drh 771 if( g.anon.ApndTkt && g.anon.Attach ){
ca90659… stephan 772 style_submenu_element("Attach", "%R/attachadd?target=%T&from=%R/tktview/%t",
ca90659… stephan 773 zFullName, zUuid);
4f8c897… drh 774 }
4f8c897… drh 775 if( P("plaintext") ){
187424e… andygoth 776 style_submenu_element("Formatted", "%R/tktview/%s", zUuid);
4f8c897… drh 777 }else{
187424e… andygoth 778 style_submenu_element("Plaintext", "%R/tktview/%s?plaintext", zUuid);
4f8c897… drh 779 }
112c713… drh 780 style_set_current_feature("tkt");
ba418ee… drh 781 style_header("View Ticket");
3d13152… drh 782 if( showTimeline ){
3d13152… drh 783 int tagid = db_int(0,"SELECT tagid FROM tag WHERE tagname GLOB 'tkt-%q*'",
3d13152… drh 784 zUuid);
3d13152… drh 785 if( tagid ){
3d13152… drh 786 tkt_draw_timeline(tagid, "a");
3d13152… drh 787 @ <hr>
3d13152… drh 788 }else{
3d13152… drh 789 showTimeline = 0;
3d13152… drh 790 }
3d13152… drh 791 }
3d13152… drh 792 if( !showTimeline && g.perm.Hyperlink ){
a40e8a0… drh 793 style_submenu_element("Timeline", "%R/info/%T", zUuid);
3d13152… drh 794 }
f5482a0… wyoung 795 if( g.thTrace ) Th_Trace("BEGIN_TKTVIEW<br>\n", -1);
ba418ee… drh 796 ticket_init();
4f8c897… drh 797 initializeVariablesFromCGI();
4f8c897… drh 798 getAllTicketFields();
ba418ee… drh 799 initializeVariablesFromDb();
ba418ee… drh 800 zScript = ticket_viewpage_code();
4f8c897… drh 801 if( P("showfields")!=0 ) showAllFields();
f5482a0… wyoung 802 if( g.thTrace ) Th_Trace("BEGIN_TKTVIEW_SCRIPT<br>\n", -1);
89b6dda… drh 803 safe_html_context(DOCSRC_TICKET);
ba418ee… drh 804 Th_Render(zScript);
f5482a0… wyoung 805 if( g.thTrace ) Th_Trace("END_TKTVIEW<br>\n", -1);
ba418ee… drh 806
7c2577b… drh 807 if( zFullName ){
ca90659… stephan 808 char * z = mprintf(
ca90659… stephan 809 "<h2><a href='%R/attachlist?tkt=%t'>Attachments</a>:</h2>",
ca90659… stephan 810 zFullName
ca90659… stephan 811 );
ca90659… stephan 812 attachment_list(zFullName, z, 1);
ca90659… stephan 813 fossil_free(z);
ba418ee… drh 814 }
cc09064… stephan 815
cc09064… stephan 816 builtin_fossil_js_bundle_or("dom", "storage", NULL);
cc09064… stephan 817 builtin_request_js("fossil.page.ticket.js");
cc09064… stephan 818 builtin_fulfill_js_requests();
112c713… drh 819
112c713… drh 820 style_finish_page();
d0305b3… aku 821 }
d0305b3… aku 822
d0305b3… aku 823 /*
109d8f5… mistachkin 824 ** TH1 command: append_field FIELD STRING
d0305b3… aku 825 **
d0305b3… aku 826 ** FIELD is the name of a database column to which we might want
d0305b3… aku 827 ** to append text. STRING is the text to be appended to that
d0305b3… aku 828 ** column. The append does not actually occur until the
588bb7c… aku 829 ** submit_ticket command is run.
d0305b3… aku 830 */
588bb7c… aku 831 static int appendRemarkCmd(
3df526c… jan.nijtmans 832 Th_Interp *interp,
3df526c… jan.nijtmans 833 void *p,
3df526c… jan.nijtmans 834 int argc,
3df526c… jan.nijtmans 835 const char **argv,
588bb7c… aku 836 int *argl
588bb7c… aku 837 ){
d0305b3… aku 838 int idx;
d0305b3… aku 839
588bb7c… aku 840 if( argc!=3 ){
588bb7c… aku 841 return Th_WrongNumArgs(interp, "append_field FIELD STRING");
588bb7c… aku 842 }
f55c6a1… drh 843 if( g.thTrace ){
f5482a0… wyoung 844 Th_Trace("append_field %#h {%#h}<br>\n",
2116238… drh 845 TH1_LEN(argl[1]), argv[1], TH1_LEN(argl[2]), argv[2]);
f55c6a1… drh 846 }
d0305b3… aku 847 for(idx=0; idx<nField; idx++){
2116238… drh 848 if( memcmp(aField[idx].zName, argv[1], TH1_LEN(argl[1]))==0
2116238… drh 849 && aField[idx].zName[TH1_LEN(argl[1])]==0 ){
d0305b3… aku 850 break;
d0305b3… aku 851 }
d0305b3… aku 852 }
d0305b3… aku 853 if( idx>=nField ){
588bb7c… aku 854 Th_ErrorMessage(g.interp, "no such TICKET column: ", argv[1], argl[1]);
588bb7c… aku 855 return TH_ERROR;
588bb7c… aku 856 }
4f8c897… drh 857 aField[idx].zAppend = mprintf("%.*s", argl[2], argv[2]);
588bb7c… aku 858 return TH_OK;
ba418ee… drh 859 }
ba418ee… drh 860
ba418ee… drh 861 /*
ba418ee… drh 862 ** Write a ticket into the repository.
872a3b2… george 863 ** Upon reassignment of fields try to delta-compress an artifact against
872a3b2… george 864 ** all artifacts that are referenced in the corresponding zBsln fields.
ba418ee… drh 865 */
1311841… jan.nijtmans 866 static int ticket_put(
ba418ee… drh 867 Blob *pTicket, /* The text of the ticket change record */
ba418ee… drh 868 const char *zTktId, /* The ticket to which this change is applied */
872a3b2… george 869 const char *aUsed, /* Indicators for fields' modifications */
ba418ee… drh 870 int needMod /* True if moderation is needed */
ba418ee… drh 871 ){
1311841… jan.nijtmans 872 int result;
65e69b8… drh 873 int rid;
1b6635a… andybradford 874 manifest_crosslink_begin();
65e69b8… drh 875 rid = content_put_ex(pTicket, 0, 0, 0, needMod);
ba418ee… drh 876 if( rid==0 ){
320f143… drh 877 fossil_fatal("trouble committing ticket: %s", g.zErrMsg);
65e69b8… drh 878 }
872a3b2… george 879 if( nTicketBslns ){
872a3b2… george 880 int i, s, buf[8], nSrc=0, *aSrc=&(buf[0]);
872a3b2… george 881 if( nTicketBslns > count(buf) ){
872a3b2… george 882 aSrc = (int*)fossil_malloc(sizeof(int)*nTicketBslns);
872a3b2… george 883 }
872a3b2… george 884 for(i=0; i<nField; i++){
872a3b2… george 885 if( aField[i].zBsln && aUsed[i]==JCARD_ASSIGN ){
872a3b2… george 886 s = db_int(0,"SELECT \"%w\" FROM ticket WHERE tkt_uuid = '%q'",
872a3b2… george 887 aField[i].zBsln, zTktId );
872a3b2… george 888 if( s > 0 ) aSrc[nSrc++] = s;
872a3b2… george 889 }
872a3b2… george 890 }
872a3b2… george 891 if( nSrc ) content_deltify(rid, aSrc, nSrc, 0);
872a3b2… george 892 if( aSrc!=&(buf[0]) ) fossil_free( aSrc );
872a3b2… george 893 }
ba418ee… drh 894 if( needMod ){
ba418ee… drh 895 moderation_table_create();
ba418ee… drh 896 db_multi_exec(
49b0ff1… drh 897 "INSERT INTO modreq(objid, tktid) VALUES(%d,%Q)",
ba418ee… drh 898 rid, zTktId
ba418ee… drh 899 );
ba418ee… drh 900 }else{
673dc38… stephan 901 db_add_unsent(rid);
ba418ee… drh 902 db_multi_exec("INSERT OR IGNORE INTO unclustered VALUES(%d);", rid);
ba418ee… drh 903 }
42316a1… mistachkin 904 result = (manifest_crosslink(rid, pTicket, MC_NONE)==0);
ba418ee… drh 905 assert( blob_is_reset(pTicket) );
42316a1… mistachkin 906 if( !result ){
42316a1… mistachkin 907 result = manifest_crosslink_end(MC_PERMIT_HOOKS);
42316a1… mistachkin 908 }else{
42316a1… mistachkin 909 manifest_crosslink_end(MC_NONE);
42316a1… mistachkin 910 }
1311841… jan.nijtmans 911 return result;
d0305b3… aku 912 }
d0305b3… aku 913
d0305b3… aku 914 /*
d0305b3… aku 915 ** Subscript command: submit_ticket
d0305b3… aku 916 **
f46fe42… drh 917 ** Construct and submit a new ticket artifact. The fields of the artifact
f46fe42… drh 918 ** are the names of the columns in the TICKET table. The content is
f46fe42… drh 919 ** taken from TH variables. If the content is unchanged, the field is
f46fe42… drh 920 ** omitted from the artifact. Fields whose names begin with "private_"
f46fe42… drh 921 ** are concealed using the db_conceal() function.
d0305b3… aku 922 */
588bb7c… aku 923 static int submitTicketCmd(
3df526c… jan.nijtmans 924 Th_Interp *interp,
3df526c… jan.nijtmans 925 void *pUuid,
3df526c… jan.nijtmans 926 int argc,
3df526c… jan.nijtmans 927 const char **argv,
588bb7c… aku 928 int *argl
588bb7c… aku 929 ){
872a3b2… george 930 char *zDate, *aUsed;
d0305b3… aku 931 const char *zUuid;
d0305b3… aku 932 int i;
872a3b2… george 933 int nJ = 0, rc = TH_OK;
d0305b3… aku 934 Blob tktchng, cksum;
636c334… mistachkin 935 int needMod;
d0305b3… aku 936
920ace1… drh 937 if( !cgi_csrf_safe(2) ){
920ace1… drh 938 @ <p class="generalError">Error: Invalid CSRF token.</p>
920ace1… drh 939 return TH_OK;
920ace1… drh 940 }
b77f1aa… drh 941 if( !captcha_is_correct(0) ){
82b8587… drh 942 @ <p class="generalError">Error: Incorrect security code.</p>
82b8587… drh 943 return TH_OK;
82b8587… drh 944 }
d0305b3… aku 945 zUuid = (const char *)pUuid;
d0305b3… aku 946 blob_zero(&tktchng);
722d7ca… drh 947 zDate = date_in_standard_format("now");
d0305b3… aku 948 blob_appendf(&tktchng, "D %s\n", zDate);
d0305b3… aku 949 free(zDate);
872a3b2… george 950 aUsed = fossil_malloc_zero( nField );
e1dac0e… drh 951 for(i=0; i<nField; i++){
4f8c897… drh 952 if( aField[i].zAppend ){
4f8c897… drh 953 blob_appendf(&tktchng, "J +%s %z\n", aField[i].zName,
4f8c897… drh 954 fossilize(aField[i].zAppend, -1));
f02946a… viriketo 955 ++nJ;
872a3b2… george 956 aUsed[i] = JCARD_APPEND;
e1dac0e… drh 957 }
e1dac0e… drh 958 }
d0305b3… aku 959 for(i=0; i<nField; i++){
d0305b3… aku 960 const char *zValue;
d0305b3… aku 961 int nValue;
4f8c897… drh 962 if( aField[i].zAppend ) continue;
4f8c897… drh 963 zValue = Th_Fetch(aField[i].zName, &nValue);
e1dac0e… drh 964 if( zValue ){
2116238… drh 965 nValue = TH1_LEN(nValue);
e1dac0e… drh 966 while( nValue>0 && fossil_isspace(zValue[nValue-1]) ){ nValue--; }
4f8c897… drh 967 if( ((aField[i].mUsed & USEDBY_TICKETCHNG)!=0 && nValue>0)
4f8c897… drh 968 || memcmp(zValue, aField[i].zValue, nValue)!=0
53db40e… drh 969 ||(int)strlen(aField[i].zValue)!=nValue
4f8c897… drh 970 ){
4f8c897… drh 971 if( memcmp(aField[i].zName, "private_", 8)==0 ){
e1dac0e… drh 972 zValue = db_conceal(zValue, nValue);
4f8c897… drh 973 blob_appendf(&tktchng, "J %s %s\n", aField[i].zName, zValue);
872a3b2… george 974 aUsed[i] = JCARD_PRIVATE;
e1dac0e… drh 975 }else{
4f8c897… drh 976 blob_appendf(&tktchng, "J %s %#F\n", aField[i].zName, nValue, zValue);
872a3b2… george 977 aUsed[i] = JCARD_ASSIGN;
e1dac0e… drh 978 }
7097562… drh 979 nJ++;
d0305b3… aku 980 }
d0305b3… aku 981 }
d0305b3… aku 982 }
d0305b3… aku 983 if( *(char**)pUuid ){
3df526c… jan.nijtmans 984 zUuid = db_text(0,
e766df0… drh 985 "SELECT tkt_uuid FROM ticket WHERE tkt_uuid GLOB '%q*'", P("name")
d0305b3… aku 986 );
d0305b3… aku 987 }else{
d0305b3… aku 988 zUuid = db_text(0, "SELECT lower(hex(randomblob(20)))");
d0305b3… aku 989 }
d0305b3… aku 990 *(const char**)pUuid = zUuid;
d0305b3… aku 991 blob_appendf(&tktchng, "K %s\n", zUuid);
840b762… drh 992 blob_appendf(&tktchng, "U %F\n", login_name());
d0305b3… aku 993 md5sum_blob(&tktchng, &cksum);
d0305b3… aku 994 blob_appendf(&tktchng, "Z %b\n", &cksum);
7097562… drh 995 if( nJ==0 ){
7097562… drh 996 blob_reset(&tktchng);
872a3b2… george 997 goto finish;
7097562… drh 998 }
636c334… mistachkin 999 needMod = ticket_need_moderation(0);
06c5b3e… drh 1000 if( g.zPath[0]=='d' ){
636c334… mistachkin 1001 const char *zNeedMod = needMod ? "required" : "skipped";
06c5b3e… drh 1002 /* If called from /debug_tktnew or /debug_tktedit... */
d1f60ca… jan.nijtmans 1003 @ <div style="color:blue">
06c5b3e… drh 1004 @ <p>Ticket artifact that would have been submitted:</p>
06c5b3e… drh 1005 @ <blockquote><pre>%h(blob_str(&tktchng))</pre></blockquote>
636c334… mistachkin 1006 @ <blockquote><pre>Moderation would be %h(zNeedMod).</pre></blockquote>
d1f60ca… jan.nijtmans 1007 @ </div>
f5482a0… wyoung 1008 @ <hr>
ba418ee… drh 1009 }else{
9e940f2… jan.nijtmans 1010 if( g.thTrace ){
9e940f2… jan.nijtmans 1011 Th_Trace("submit_ticket {\n<blockquote><pre>\n%h\n</pre></blockquote>\n"
f5482a0… wyoung 1012 "}<br>\n",
9e940f2… jan.nijtmans 1013 blob_str(&tktchng));
9e940f2… jan.nijtmans 1014 }
872a3b2… george 1015 ticket_put(&tktchng, zUuid, aUsed, needMod);
872a3b2… george 1016 rc = ticket_change(zUuid);
ba418ee… drh 1017 }
872a3b2… george 1018 finish:
872a3b2… george 1019 fossil_free( aUsed );
872a3b2… george 1020 return rc;
d0305b3… aku 1021 }
d0305b3… aku 1022
d0305b3… aku 1023
d0305b3… aku 1024 /*
d0305b3… aku 1025 ** WEBPAGE: tktnew
d0305b3… aku 1026 ** WEBPAGE: debug_tktnew
d0305b3… aku 1027 **
db0c512… drh 1028 ** Enter a new ticket. The tktnew_template script in the ticket
d0305b3… aku 1029 ** configuration is used. The /tktnew page is the official ticket
d0305b3… aku 1030 ** entry page. The /debug_tktnew page is used for debugging the
d0305b3… aku 1031 ** tktnew_template in the ticket configuration. /debug_tktnew works
d0305b3… aku 1032 ** just like /tktnew except that it does not really save the new ticket
d0305b3… aku 1033 ** when you press submit - it just prints the ticket artifact at the
d0305b3… aku 1034 ** top of the screen.
d0305b3… aku 1035 */
d0305b3… aku 1036 void tktnew_page(void){
86db6fa… drh 1037 const char *zScript;
d6a4ab2… stephan 1038 char *zNewUuid = 0;
f974583… mark 1039 int uid;
d0305b3… aku 1040
d0305b3… aku 1041 login_check_credentials();
653dd40… drh 1042 if( !g.perm.NewTkt ){ login_needed(g.anon.NewTkt); return; }
27d2e6e… drh 1043 if( P("cancel") ){
27d2e6e… drh 1044 cgi_redirect("home");
27d2e6e… drh 1045 }
112c713… drh 1046 style_set_current_feature("tkt");
d0305b3… aku 1047 style_header("New Ticket");
6714c94… drh 1048 ticket_standard_submenu(T_ALL_BUT(T_NEW));
f5482a0… wyoung 1049 if( g.thTrace ) Th_Trace("BEGIN_TKTNEW<br>\n", -1);
d0305b3… aku 1050 ticket_init();
4f8c897… drh 1051 initializeVariablesFromCGI();
588bb7c… aku 1052 getAllTicketFields();
588bb7c… aku 1053 initializeVariablesFromDb();
4f8c897… drh 1054 if( g.zPath[0]=='d' ) showAllFields();
dfa3579… drh 1055 form_begin(0, "%R/%s", g.zPath);
44f028c… drh 1056 if( P("date_override") && g.perm.Setup ){
44f028c… drh 1057 @ <input type="hidden" name="date_override" value="%h(P("date_override"))">
44f028c… drh 1058 }
86db6fa… drh 1059 zScript = ticket_newpage_code();
271a6bd… jkosche 1060 Th_Store("private_contact", "");
f974583… mark 1061 if( g.zLogin && g.zLogin[0] ){
271a6bd… jkosche 1062 uid = db_int(0, "SELECT uid FROM user WHERE login=%Q", g.zLogin);
f974583… mark 1063 if( uid ){
d6a4ab2… stephan 1064 char * zEmail =
d6a4ab2… stephan 1065 db_text(0, "SELECT find_emailaddr(info) FROM user WHERE uid=%d",
d6a4ab2… stephan 1066 uid);
f974583… mark 1067 if( zEmail ){
2116238… drh 1068 Th_StoreUnsafe("private_contact", zEmail);
d6a4ab2… stephan 1069 fossil_free(zEmail);
f974583… mark 1070 }
f974583… mark 1071 }
f974583… mark 1072 }
2116238… drh 1073 Th_StoreUnsafe("login", login_name());
588bb7c… aku 1074 Th_Store("date", db_text(0, "SELECT datetime('now')"));
588bb7c… aku 1075 Th_CreateCommand(g.interp, "submit_ticket", submitTicketCmd,
588bb7c… aku 1076 (void*)&zNewUuid, 0);
f5482a0… wyoung 1077 if( g.thTrace ) Th_Trace("BEGIN_TKTNEW_SCRIPT<br>\n", -1);
f55c6a1… drh 1078 if( Th_Render(zScript)==TH_RETURN && !g.thTrace && zNewUuid ){
271a6bd… jkosche 1079 if( P("submitandnew") ){
271a6bd… jkosche 1080 cgi_redirect(mprintf("%R/tktnew/%s", zNewUuid));
271a6bd… jkosche 1081 }else{
271a6bd… jkosche 1082 cgi_redirect(mprintf("%R/tktview/%s", zNewUuid));
271a6bd… jkosche 1083 }
d0305b3… aku 1084 return;
d0305b3… aku 1085 }
f8a2aa0… drh 1086 captcha_generate(0);
d0305b3… aku 1087 @ </form>
f5482a0… wyoung 1088 if( g.thTrace ) Th_Trace("END_TKTVIEW<br>\n", -1);
112c713… drh 1089 style_finish_page();
d0305b3… aku 1090 }
d0305b3… aku 1091
d0305b3… aku 1092 /*
d0305b3… aku 1093 ** WEBPAGE: tktedit
d0305b3… aku 1094 ** WEBPAGE: debug_tktedit
d0305b3… aku 1095 **
d0305b3… aku 1096 ** Edit a ticket. The ticket is identified by the name CGI parameter.
d0305b3… aku 1097 ** /tktedit is the official page. The /debug_tktedit page does the same
d0305b3… aku 1098 ** thing except that it does not save the ticket change record when you
d0305b3… aku 1099 ** press submit - it instead prints the ticket change record at the top
d0305b3… aku 1100 ** of the page. The /debug_tktedit page is intended to be used when
d0305b3… aku 1101 ** debugging ticket configurations.
d0305b3… aku 1102 */
d0305b3… aku 1103 void tktedit_page(void){
86db6fa… drh 1104 const char *zScript;
d0305b3… aku 1105 int nName;
d0305b3… aku 1106 const char *zName;
d0305b3… aku 1107 int nRec;
d0305b3… aku 1108
d0305b3… aku 1109 login_check_credentials();
653dd40… drh 1110 if( !g.perm.ApndTkt && !g.perm.WrTkt ){
653dd40… drh 1111 login_needed(g.anon.ApndTkt || g.anon.WrTkt);
653dd40… drh 1112 return;
653dd40… drh 1113 }
d0305b3… aku 1114 zName = P("name");
27d2e6e… drh 1115 if( P("cancel") ){
5f22b96… drh 1116 cgi_redirectf("tktview/%T", zName);
27d2e6e… drh 1117 }
112c713… drh 1118 style_set_current_feature("tkt");
27d2e6e… drh 1119 style_header("Edit Ticket");
323299c… drh 1120 if( zName==0 || (nName = strlen(zName))<4 || nName>HNAME_LEN_SHA1
d0305b3… aku 1121 || !validate16(zName,nName) ){
b772b36… drh 1122 @ <span class="tktError">Not a valid ticket id: "%h(zName)"</span>
112c713… drh 1123 style_finish_page();
d0305b3… aku 1124 return;
d0305b3… aku 1125 }
d0305b3… aku 1126 nRec = db_int(0, "SELECT count(*) FROM ticket WHERE tkt_uuid GLOB '%q*'",
d0305b3… aku 1127 zName);
d0305b3… aku 1128 if( nRec==0 ){
b772b36… drh 1129 @ <span class="tktError">No such ticket: "%h(zName)"</span>
112c713… drh 1130 style_finish_page();
d0305b3… aku 1131 return;
d0305b3… aku 1132 }
d0305b3… aku 1133 if( nRec>1 ){
5a48a9b… drh 1134 @ <span class="tktError">%d(nRec) tickets begin with:
699fca1… mistachkin 1135 @ "%h(zName)"</span>
112c713… drh 1136 style_finish_page();
d0305b3… aku 1137 return;
d0305b3… aku 1138 }
f5482a0… wyoung 1139 if( g.thTrace ) Th_Trace("BEGIN_TKTEDIT<br>\n", -1);
d0305b3… aku 1140 ticket_init();
d0305b3… aku 1141 getAllTicketFields();
d0305b3… aku 1142 initializeVariablesFromCGI();
d0305b3… aku 1143 initializeVariablesFromDb();
4f8c897… drh 1144 if( g.zPath[0]=='d' ) showAllFields();
dfa3579… drh 1145 form_begin(0, "%R/%s", g.zPath);
f5482a0… wyoung 1146 @ <input type="hidden" name="name" value="%s(zName)">
86db6fa… drh 1147 zScript = ticket_editpage_code();
2116238… drh 1148 Th_StoreUnsafe("login", login_name());
588bb7c… aku 1149 Th_Store("date", db_text(0, "SELECT datetime('now')"));
588bb7c… aku 1150 Th_CreateCommand(g.interp, "append_field", appendRemarkCmd, 0, 0);
588bb7c… aku 1151 Th_CreateCommand(g.interp, "submit_ticket", submitTicketCmd, (void*)&zName,0);
f5482a0… wyoung 1152 if( g.thTrace ) Th_Trace("BEGIN_TKTEDIT_SCRIPT<br>\n", -1);
f55c6a1… drh 1153 if( Th_Render(zScript)==TH_RETURN && !g.thTrace && zName ){
a40e8a0… drh 1154 cgi_redirect(mprintf("%R/tktview/%s", zName));
d0305b3… aku 1155 return;
d0305b3… aku 1156 }
f8a2aa0… drh 1157 captcha_generate(0);
d0305b3… aku 1158 @ </form>
f5482a0… wyoung 1159 if( g.thTrace ) Th_Trace("BEGIN_TKTEDIT<br>\n", -1);
112c713… drh 1160 style_finish_page();
d0305b3… aku 1161 }
d0305b3… aku 1162
d0305b3… aku 1163 /*
86db6fa… drh 1164 ** Check the ticket table schema in zSchema to see if it appears to
d0305b3… aku 1165 ** be well-formed. If everything is OK, return NULL. If something is
d0305b3… aku 1166 ** amiss, then return a pointer to a string (obtained from malloc) that
d0305b3… aku 1167 ** describes the problem.
d0305b3… aku 1168 */
86db6fa… drh 1169 char *ticket_schema_check(const char *zSchema){
588bb7c… aku 1170 char *zErr = 0;
588bb7c… aku 1171 int rc;
588bb7c… aku 1172 sqlite3 *db;
588bb7c… aku 1173 rc = sqlite3_open(":memory:", &db);
588bb7c… aku 1174 if( rc==SQLITE_OK ){
86db6fa… drh 1175 rc = sqlite3_exec(db, zSchema, 0, 0, &zErr);
588bb7c… aku 1176 if( rc!=SQLITE_OK ){
588bb7c… aku 1177 sqlite3_close(db);
588bb7c… aku 1178 return zErr;
588bb7c… aku 1179 }
86db6fa… drh 1180 rc = sqlite3_exec(db, "SELECT tkt_id, tkt_uuid, tkt_mtime FROM ticket",
86db6fa… drh 1181 0, 0, 0);
86db6fa… drh 1182 if( rc!=SQLITE_OK ){
4f8c897… drh 1183 zErr = mprintf("schema fails to define valid a TICKET "
4f8c897… drh 1184 "table containing all required fields");
4f8c897… drh 1185 }else{
4f8c897… drh 1186 rc = sqlite3_exec(db, "SELECT tkt_id, tkt_mtime FROM ticketchng", 0,0,0);
4f8c897… drh 1187 if( rc!=SQLITE_OK ){
4f8c897… drh 1188 zErr = mprintf("schema fails to define valid a TICKETCHNG "
4f8c897… drh 1189 "table containing all required fields");
4f8c897… drh 1190 }
c8a7800… drh 1191 }
4f8c897… drh 1192 sqlite3_close(db);
7c2577b… drh 1193 }
4f8c897… drh 1194 return zErr;
7c2577b… drh 1195 }
7c2577b… drh 1196
7c2577b… drh 1197 /*
3d13152… drh 1198 ** Draw a timeline for a ticket with tag.tagid given by the tagid
3d13152… drh 1199 ** parameter.
ae1dac8… drh 1200 **
ae1dac8… drh 1201 ** If zType[0]=='c' then only show check-ins associated with the
ae1dac8… drh 1202 ** ticket. For any other value of zType, show all events associated
ae1dac8… drh 1203 ** with the ticket.
3d13152… drh 1204 */
3d13152… drh 1205 void tkt_draw_timeline(int tagid, const char *zType){
3d13152… drh 1206 Stmt q;
3d13152… drh 1207 char *zFullUuid;
3d13152… drh 1208 char *zSQL;
3d13152… drh 1209 zFullUuid = db_text(0, "SELECT substr(tagname, 5) FROM tag WHERE tagid=%d",
3d13152… drh 1210 tagid);
3d13152… drh 1211 if( zType[0]=='c' ){
3d13152… drh 1212 zSQL = mprintf(
3d13152… drh 1213 "%s AND event.objid IN "
ae1dac8… drh 1214 " (SELECT srcid FROM backlink WHERE target GLOB '%.4s*' "
ae1dac8… drh 1215 "AND srctype=0 "
3d13152… drh 1216 "AND '%s' GLOB (target||'*')) "
3d13152… drh 1217 "ORDER BY mtime DESC",
3d13152… drh 1218 timeline_query_for_www(), zFullUuid, zFullUuid
3d13152… drh 1219 );
3d13152… drh 1220 }else{
3d13152… drh 1221 zSQL = mprintf(
3d13152… drh 1222 "%s AND event.objid IN "
3d13152… drh 1223 " (SELECT rid FROM tagxref WHERE tagid=%d"
ae1dac8… drh 1224 " UNION"
ae1dac8… drh 1225 " SELECT CASE srctype WHEN 2 THEN"
ae1dac8… drh 1226 " (SELECT rid FROM tagxref WHERE tagid=backlink.srcid"
ae1dac8… drh 1227 " ORDER BY mtime DESC LIMIT 1)"
ae1dac8… drh 1228 " ELSE srcid END"
ae1dac8… drh 1229 " FROM backlink"
3d13152… drh 1230 " WHERE target GLOB '%.4s*'"
3d13152… drh 1231 " AND '%s' GLOB (target||'*')"
3d13152… drh 1232 " UNION SELECT attachid FROM attachment"
3d13152… drh 1233 " WHERE target=%Q) "
3d13152… drh 1234 "ORDER BY mtime DESC",
3d13152… drh 1235 timeline_query_for_www(), tagid, zFullUuid, zFullUuid, zFullUuid
3d13152… drh 1236 );
3d13152… drh 1237 }
3d13152… drh 1238 db_prepare(&q, "%z", zSQL/*safe-for-%s*/);
d80adfe… drh 1239 www_print_timeline(&q,
ae1dac8… drh 1240 TIMELINE_ARTID | TIMELINE_DISJOINT | TIMELINE_GRAPH | TIMELINE_NOTKT |
ae1dac8… drh 1241 TIMELINE_REFS,
e454028… drh 1242 0);
3d13152… drh 1243 db_finalize(&q);
3d13152… drh 1244 fossil_free(zFullUuid);
3d13152… drh 1245 }
3d13152… drh 1246
3d13152… drh 1247 /*
653dd40… drh 1248 ** WEBPAGE: tkttimeline
ae1dac8… drh 1249 ** URL: /tkttimeline/TICKETUUID
653dd40… drh 1250 **
653dd40… drh 1251 ** Show the change history for a single ticket in timeline format.
275da70… danield 1252 **
ae1dac8… drh 1253 ** Query parameters:
ae1dac8… drh 1254 **
ae1dac8… drh 1255 ** y=ci Show only check-ins associated with the ticket
653dd40… drh 1256 */
653dd40… drh 1257 void tkttimeline_page(void){
653dd40… drh 1258 char *zTitle;
653dd40… drh 1259 const char *zUuid;
653dd40… drh 1260 int tagid;
653dd40… drh 1261 char zGlobPattern[50];
653dd40… drh 1262 const char *zType;
653dd40… drh 1263
653dd40… drh 1264 login_check_credentials();
653dd40… drh 1265 if( !g.perm.Hyperlink || !g.perm.RdTkt ){
653dd40… drh 1266 login_needed(g.anon.Hyperlink && g.anon.RdTkt);
653dd40… drh 1267 return;
653dd40… drh 1268 }
653dd40… drh 1269 zUuid = PD("name","");
653dd40… drh 1270 zType = PD("y","a");
653dd40… drh 1271 if( zType[0]!='c' ){
c04c814… george 1272 if( g.perm.Read ){
c04c814… george 1273 style_submenu_element("Check-ins", "%R/tkttimeline/%T?y=ci", zUuid);
c04c814… george 1274 }
653dd40… drh 1275 }else{
5f22b96… drh 1276 style_submenu_element("Timeline", "%R/tkttimeline/%T", zUuid);
653dd40… drh 1277 }
a40e8a0… drh 1278 style_submenu_element("History", "%R/tkthistory/%s", zUuid);
a40e8a0… drh 1279 style_submenu_element("Status", "%R/info/%s", zUuid);
653dd40… drh 1280 if( zType[0]=='c' ){
30c107e… mistachkin 1281 zTitle = mprintf("Check-ins Associated With Ticket %h", zUuid);
653dd40… drh 1282 }else{
653dd40… drh 1283 zTitle = mprintf("Timeline Of Ticket %h", zUuid);
653dd40… drh 1284 }
112c713… drh 1285 style_set_current_feature("tkt");
653dd40… drh 1286 style_header("%z", zTitle);
653dd40… drh 1287
653dd40… drh 1288 sqlite3_snprintf(6, zGlobPattern, "%s", zUuid);
653dd40… drh 1289 canonical16(zGlobPattern, strlen(zGlobPattern));
653dd40… drh 1290 tagid = db_int(0, "SELECT tagid FROM tag WHERE tagname GLOB 'tkt-%q*'",zUuid);
653dd40… drh 1291 if( tagid==0 ){
653dd40… drh 1292 @ No such ticket: %h(zUuid)
112c713… drh 1293 style_finish_page();
653dd40… drh 1294 return;
653dd40… drh 1295 }
3d13152… drh 1296 tkt_draw_timeline(tagid, zType);
112c713… drh 1297 style_finish_page();
c8a7800… drh 1298 }
c8a7800… drh 1299
c8a7800… drh 1300 /*
c8a7800… drh 1301 ** WEBPAGE: tkthistory
5f22b96… drh 1302 ** URL: /tkthistory/TICKETUUID
4c9e24f… drh 1303 **
4c9e24f… drh 1304 ** Show the complete change history for a single ticket. Or (to put it
4c9e24f… drh 1305 ** another way) show a list of artifacts associated with a single ticket.
4c9e24f… drh 1306 **
f1e8cd6… drh 1307 ** By default, the artifacts are decoded and formatted. Text fields
f1e8cd6… drh 1308 ** are formatted as text/plain, since in the general case Fossil does
f1e8cd6… drh 1309 ** not have knowledge of the encoding. If the "raw" query parameter
2f78b2c… danield 1310 ** is present, then the undecoded and unformatted text of each artifact
f1e8cd6… drh 1311 ** is displayed.
872a3b2… george 1312 **
872a3b2… george 1313 ** Reassignments of a field of the TICKET table that has a corresponding
872a3b2… george 1314 ** "baseline for ..." companion are rendered as unified diffs.
c8a7800… drh 1315 */
c8a7800… drh 1316 void tkthistory_page(void){
c8a7800… drh 1317 Stmt q;
c8a7800… drh 1318 char *zTitle;
c8a7800… drh 1319 const char *zUuid;
c8a7800… drh 1320 int tagid;
4f8c897… drh 1321 int nChng = 0;
872a3b2… george 1322 Blob *aLastVal = 0; /* holds the last rendered value for each field */
c8a7800… drh 1323
c8a7800… drh 1324 login_check_credentials();
653dd40… drh 1325 if( !g.perm.Hyperlink || !g.perm.RdTkt ){
653dd40… drh 1326 login_needed(g.anon.Hyperlink && g.anon.RdTkt);
653dd40… drh 1327 return;
653dd40… drh 1328 }
c8a7800… drh 1329 zUuid = PD("name","");
c8a7800… drh 1330 zTitle = mprintf("History Of Ticket %h", zUuid);
a40e8a0… drh 1331 style_submenu_element("Status", "%R/info/%s", zUuid);
c04c814… george 1332 if( g.perm.Read ){
c04c814… george 1333 style_submenu_element("Check-ins", "%R/tkttimeline/%s?y=ci", zUuid);
c04c814… george 1334 }
5f22b96… drh 1335 style_submenu_element("Timeline", "%R/tkttimeline/%s", zUuid);
4c9e24f… drh 1336 if( P("raw")!=0 ){
f1e8cd6… drh 1337 style_submenu_element("Decoded", "%R/tkthistory/%s", zUuid);
f1e8cd6… drh 1338 }else if( g.perm.Admin ){
f1e8cd6… drh 1339 style_submenu_element("Raw", "%R/tkthistory/%s?raw", zUuid);
f1e8cd6… drh 1340 }
112c713… drh 1341 style_set_current_feature("tkt");
49b0ff1… drh 1342 style_header("%z", zTitle);
c8a7800… drh 1343
c8a7800… drh 1344 tagid = db_int(0, "SELECT tagid FROM tag WHERE tagname GLOB 'tkt-%q*'",zUuid);
c8a7800… drh 1345 if( tagid==0 ){
c8a7800… drh 1346 @ No such ticket: %h(zUuid)
112c713… drh 1347 style_finish_page();
c8a7800… drh 1348 return;
4c9e24f… drh 1349 }
4c9e24f… drh 1350 if( P("raw")!=0 ){
4c9e24f… drh 1351 @ <h2>Raw Artifacts Associated With Ticket %h(zUuid)</h2>
f1e8cd6… drh 1352 }else{
f1e8cd6… drh 1353 @ <h2>Artifacts Associated With Ticket %h(zUuid)</h2>
872a3b2… george 1354 getAllTicketFields();
872a3b2… george 1355 if( nTicketBslns ){
872a3b2… george 1356 aLastVal = blobarray_new(nField);
872a3b2… george 1357 }
ea63a2d… drh 1358 }
c8a7800… drh 1359 db_prepare(&q,
ea63a2d… drh 1360 "SELECT datetime(mtime,toLocal()), objid, uuid, NULL, NULL, NULL"
7c2577b… drh 1361 " FROM event, blob"
c8a7800… drh 1362 " WHERE objid IN (SELECT rid FROM tagxref WHERE tagid=%d)"
c8a7800… drh 1363 " AND blob.rid=event.objid"
7c2577b… drh 1364 " UNION "
ea63a2d… drh 1365 "SELECT datetime(mtime,toLocal()), attachid, uuid, src, filename, user"
7c2577b… drh 1366 " FROM attachment, blob"
7c2577b… drh 1367 " WHERE target=(SELECT substr(tagname,5) FROM tag WHERE tagid=%d)"
7c2577b… drh 1368 " AND blob.rid=attachid"
4f8c897… drh 1369 " ORDER BY 1",
ea63a2d… drh 1370 tagid, tagid
7c2577b… drh 1371 );
fc70ec0… drh 1372 for(nChng=0; db_step(&q)==SQLITE_ROW; nChng++){
d13054c… drh 1373 Manifest *pTicket;
7c2577b… drh 1374 const char *zDate = db_column_text(&q, 0);
7c2577b… drh 1375 int rid = db_column_int(&q, 1);
7c2577b… drh 1376 const char *zChngUuid = db_column_text(&q, 2);
7c2577b… drh 1377 const char *zFile = db_column_text(&q, 4);
4f8c897… drh 1378 if( nChng==0 ){
872a3b2… george 1379 @ <ol class="tkt-changes">
4f8c897… drh 1380 }
7c2577b… drh 1381 if( zFile!=0 ){
7c2577b… drh 1382 const char *zSrc = db_column_text(&q, 3);
7c2577b… drh 1383 const char *zUser = db_column_text(&q, 5);
872a3b2… george 1384 @
872a3b2… george 1385 @ <li id="%S(zChngUuid)"><p><span>
7c2577b… drh 1386 if( zSrc==0 || zSrc[0]==0 ){
872a3b2… george 1387 @ Delete attachment "%h(zFile)"
7c2577b… drh 1388 }else{
872a3b2… george 1389 @ Add attachment
1fee037… drh 1390 @ "%z(href("%R/artifact/%!S",zSrc))%s(zFile)</a>"
7c2577b… drh 1391 }
872a3b2… george 1392 @ [%z(href("%R/artifact/%!S",zChngUuid))%S(zChngUuid)</a>]</span>
7c2577b… drh 1393 @ (rid %d(rid)) by
7c2577b… drh 1394 hyperlink_to_user(zUser,zDate," on");
7c2577b… drh 1395 hyperlink_to_date(zDate, ".</p>");
7c2577b… drh 1396 }else{
ec81aee… jan.nijtmans 1397 pTicket = manifest_get(rid, CFTYPE_TICKET, 0);
d13054c… drh 1398 if( pTicket ){
7c2577b… drh 1399 @
872a3b2… george 1400 @ <li id="%S(zChngUuid)"><p><span>Ticket change
872a3b2… george 1401 @ [%z(href("%R/artifact/%!S",zChngUuid))%S(zChngUuid)</a>]</span>
7c2577b… drh 1402 @ (rid %d(rid)) by
d13054c… drh 1403 hyperlink_to_user(pTicket->zUser,zDate," on");
7c2577b… drh 1404 hyperlink_to_date(zDate, ":");
7c2577b… drh 1405 @ </p>
4c9e24f… drh 1406 if( P("raw")!=0 ){
4c9e24f… drh 1407 Blob c;
4c9e24f… drh 1408 content_get(rid, &c);
4c9e24f… drh 1409 @ <blockquote><pre>
4c9e24f… drh 1410 @ %h(blob_str(&c))
4c9e24f… drh 1411 @ </pre></blockquote>
4c9e24f… drh 1412 blob_reset(&c);
4c9e24f… drh 1413 }else{
872a3b2… george 1414 ticket_output_change_artifact(pTicket, "a", nChng, aLastVal);
4c9e24f… drh 1415 }
7c2577b… drh 1416 }
d13054c… drh 1417 manifest_destroy(pTicket);
7c2577b… drh 1418 }
872a3b2… george 1419 @ </li>
c8a7800… drh 1420 }
c8a7800… drh 1421 db_finalize(&q);
4f8c897… drh 1422 if( nChng ){
4f8c897… drh 1423 @ </ol>
4f8c897… drh 1424 }
112c713… drh 1425 style_finish_page();
872a3b2… george 1426 if( aLastVal ) blobarray_delete(aLastVal, nField);
c8a7800… drh 1427 }
c8a7800… drh 1428
c8a7800… drh 1429 /*
c8a7800… drh 1430 ** Return TRUE if the given BLOB contains a newline character.
c8a7800… drh 1431 */
c8a7800… drh 1432 static int contains_newline(Blob *p){
c8a7800… drh 1433 const char *z = blob_str(p);
c8a7800… drh 1434 while( *z ){
c8a7800… drh 1435 if( *z=='\n' ) return 1;
c8a7800… drh 1436 z++;
86db6fa… drh 1437 }
588bb7c… aku 1438 return 0;
b3ee50c… drh 1439 }
b3ee50c… drh 1440
b3ee50c… drh 1441 /*
c8a7800… drh 1442 ** The pTkt object is a ticket change artifact. Output a detailed
c8a7800… drh 1443 ** description of this object.
872a3b2… george 1444 **
872a3b2… george 1445 ** If `aLastVal` is not NULL then render selected fields as unified diffs
872a3b2… george 1446 ** and update corresponding elements of that array with values from `pTkt`.
c8a7800… drh 1447 */
fc70ec0… drh 1448 void ticket_output_change_artifact(
fc70ec0… drh 1449 Manifest *pTkt, /* Parsed artifact for the ticket change */
fc70ec0… drh 1450 const char *zListType, /* Which type of list */
872a3b2… george 1451 int n, /* Which ticket change is this */
872a3b2… george 1452 Blob *aLastVal /* Array of the latest values for the diffs */
fc70ec0… drh 1453 ){
c8a7800… drh 1454 int i;
4f8c897… drh 1455 if( zListType==0 ) zListType = "1";
fc70ec0… drh 1456 getAllTicketFields();
4f8c897… drh 1457 @ <ol type="%s(zListType)">
c8a7800… drh 1458 for(i=0; i<pTkt->nField; i++){
872a3b2… george 1459 const char *z = pTkt->aField[i].zName;
872a3b2… george 1460 const char *zX = z[0]=='+' ? z+1 : z;
872a3b2… george 1461 const int id = fieldId(zX);
872a3b2… george 1462 const char *zValue = pTkt->aField[i].zValue;
872a3b2… george 1463 const size_t nValue = strlen(zValue);
872a3b2… george 1464 const int bLong = nValue>50 || memchr(zValue,'\n',nValue)!=NULL;
872a3b2… george 1465 /* zValue is long enough to justify a <blockquote> */
872a3b2… george 1466 const int bCanDiff = aLastVal && id>=0 && aField[id].zBsln;
872a3b2… george 1467 /* preliminary flag for rendering via unified diff */
872a3b2… george 1468 int bAppend = 0; /* zValue is being appended to a TICKET's field */
872a3b2… george 1469 int bRegular = 0; /* prev value of a TICKET's field is being superseded*/
fc70ec0… drh 1470 @ <li>\
fc70ec0… drh 1471 if( id<0 ){
fc70ec0… drh 1472 @ Untracked field %h(zX):
fc70ec0… drh 1473 }else if( aField[id].mUsed==USEDBY_TICKETCHNG ){
fc70ec0… drh 1474 @ %h(zX):
fc70ec0… drh 1475 }else if( n==0 ){
fc70ec0… drh 1476 @ %h(zX) initialized to:
fc70ec0… drh 1477 }else if( z[0]=='+' && (aField[id].mUsed&USEDBY_TICKET)!=0 ){
fc70ec0… drh 1478 @ Appended to %h(zX):
872a3b2… george 1479 bAppend = 1;
872a3b2… george 1480 }else{
872a3b2… george 1481 if( !bCanDiff ){
872a3b2… george 1482 @ %h(zX) changed to: \
872a3b2… george 1483 }
872a3b2… george 1484 bRegular = 1;
872a3b2… george 1485 }
872a3b2… george 1486 if( bCanDiff ){
872a3b2… george 1487 Blob *prev = aLastVal+id;
872a3b2… george 1488 Blob val = BLOB_INITIALIZER;
872a3b2… george 1489 if( nValue ){
872a3b2… george 1490 blob_init(&val, zValue, nValue+1);
872a3b2… george 1491 val.nUsed--; /* makes blob_str() faster */
872a3b2… george 1492 }
872a3b2… george 1493 if( bRegular && nValue && blob_buffer(prev) && blob_size(prev) ){
872a3b2… george 1494 Blob d = BLOB_INITIALIZER;
872a3b2… george 1495 DiffConfig DCfg;
872a3b2… george 1496 construct_diff_flags(1, &DCfg);
872a3b2… george 1497 DCfg.diffFlags |= DIFF_HTML | DIFF_LINENO;
872a3b2… george 1498 text_diff(prev, &val, &d, &DCfg);
872a3b2… george 1499 @ %h(zX) changed as:
872a3b2… george 1500 @ %s(blob_str(&d))
872a3b2… george 1501 @ </li>
872a3b2… george 1502 blob_reset(&d);
872a3b2… george 1503 }else{
872a3b2… george 1504 if( bRegular ){
872a3b2… george 1505 @ %h(zX) changed to:
872a3b2… george 1506 }
872a3b2… george 1507 if( bLong ){
872a3b2… george 1508 @ <blockquote><pre class='verbatim'>
872a3b2… george 1509 @ %h(zValue)
872a3b2… george 1510 @ </pre></blockquote></li>
872a3b2… george 1511 }else{
872a3b2… george 1512 @ "%h(zValue)"</li>
872a3b2… george 1513 }
872a3b2… george 1514 }
872a3b2… george 1515 if( blob_buffer(prev) && blob_size(prev) && !bAppend ){
872a3b2… george 1516 blob_truncate(prev,0);
872a3b2… george 1517 }
872a3b2… george 1518 if( nValue ) blob_appendb(prev, &val);
872a3b2… george 1519 blob_reset(&val);
872a3b2… george 1520 }else{
872a3b2… george 1521 if( bLong ){
872a3b2… george 1522 @ <blockquote><pre class='verbatim'>
872a3b2… george 1523 @ %h(zValue)
872a3b2… george 1524 @ </pre></blockquote></li>
872a3b2… george 1525 }else{
872a3b2… george 1526 @ "%h(zValue)"</li>
872a3b2… george 1527 }
872a3b2… george 1528 }
c8a7800… drh 1529 }
c8a7800… drh 1530 @ </ol>
2f5d45e… drh 1531 }
2f5d45e… drh 1532
2f5d45e… drh 1533 /*
841772c… drh 1534 ** COMMAND: ticket*
8bfd995… rberteig 1535 **
2f5d45e… drh 1536 ** Usage: %fossil ticket SUBCOMMAND ...
2f5d45e… drh 1537 **
2f5d45e… drh 1538 ** Run various subcommands to control tickets
2f5d45e… drh 1539 **
e58c76a… drh 1540 ** > fossil ticket show (REPORTTITLE|REPORTNR) ?TICKETFILTER? ?OPTIONS?
ea4acb5… drh 1541 **
26eef7f… rberteig 1542 ** Options:
26eef7f… rberteig 1543 ** -l|--limit LIMITCHAR
74d5ce3… florian 1544 ** --quote
2f7c93f… stephan 1545 ** -R|--repository REPO
ea4acb5… drh 1546 **
ea4acb5… drh 1547 ** Run the ticket report, identified by the report format title
26eef7f… rberteig 1548 ** used in the GUI. The data is written as flat file on stdout,
f6ad697… stephan 1549 ** using TAB as separator. The separator can be changed using
ea4acb5… drh 1550 ** the -l or --limit option.
ea4acb5… drh 1551 **
e2bdc10… danield 1552 ** If TICKETFILTER is given on the command line, the query is
ea4acb5… drh 1553 ** limited with a new WHERE-condition.
ea4acb5… drh 1554 ** example: Report lists a column # with the uuid
ea4acb5… drh 1555 ** TICKETFILTER may be [#]='uuuuuuuuu'
ea4acb5… drh 1556 ** example: Report only lists rows with status not open
ea4acb5… drh 1557 ** TICKETFILTER: status != 'open'
63220d9… jan.nijtmans 1558 **
26eef7f… rberteig 1559 ** If --quote is used, the tickets are encoded by quoting special
26eef7f… rberteig 1560 ** chars (space -> \\s, tab -> \\t, newline -> \\n, cr -> \\r,
26eef7f… rberteig 1561 ** formfeed -> \\f, vtab -> \\v, nul -> \\0, \\ -> \\\\).
26eef7f… rberteig 1562 ** Otherwise, the simplified encoding as on the show report raw page
26eef7f… rberteig 1563 ** in the GUI is used. This has no effect in JSON mode.
26eef7f… rberteig 1564 **
26eef7f… rberteig 1565 ** Instead of the report title it's possible to use the report
26eef7f… rberteig 1566 ** number; the special report number 0 lists all columns defined in
26eef7f… rberteig 1567 ** the ticket table.
26eef7f… rberteig 1568 **
e58c76a… drh 1569 ** > fossil ticket list fields
e58c76a… drh 1570 ** > fossil ticket ls fields
26eef7f… rberteig 1571 **
26eef7f… rberteig 1572 ** List all fields defined for ticket in the fossil repository.
26eef7f… rberteig 1573 **
e58c76a… drh 1574 ** > fossil ticket list reports
e58c76a… drh 1575 ** > fossil ticket ls reports
26eef7f… rberteig 1576 **
26eef7f… rberteig 1577 ** List all ticket reports defined in the fossil repository.
26eef7f… rberteig 1578 **
74d5ce3… florian 1579 ** > fossil ticket set TICKETUUID (FIELD VALUE)+ ?--quote?
74d5ce3… florian 1580 ** > fossil ticket change TICKETUUID (FIELD VALUE)+ ?--quote?
26eef7f… rberteig 1581 **
26eef7f… rberteig 1582 ** Change ticket identified by TICKETUUID to set the values of
26eef7f… rberteig 1583 ** each field FIELD to VALUE.
ea4acb5… drh 1584 **
ea4acb5… drh 1585 ** Field names as defined in the TICKET table. By default, these
ea4acb5… drh 1586 ** names include: type, status, subsystem, priority, severity, foundin,
ea4acb5… drh 1587 ** resolution, title, and comment, but other field names can be added
ea4acb5… drh 1588 ** or substituted in customized installations.
ea4acb5… drh 1589 **
26eef7f… rberteig 1590 ** If you use +FIELD, the VALUE is appended to the field FIELD. You
e2bdc10… danield 1591 ** can use more than one field/value pair on the command line. Using
26eef7f… rberteig 1592 ** --quote enables the special character decoding as in "ticket
26eef7f… rberteig 1593 ** show", which allows setting multiline text or text with special
26eef7f… rberteig 1594 ** characters.
26eef7f… rberteig 1595 **
74d5ce3… florian 1596 ** > fossil ticket add FIELD VALUE ?FIELD VALUE .. ? ?--quote?
26eef7f… rberteig 1597 **
26eef7f… rberteig 1598 ** Like set, but create a new ticket with the given values.
26eef7f… rberteig 1599 **
e58c76a… drh 1600 ** > fossil ticket history TICKETUUID
ea4acb5… drh 1601 **
ea4acb5… drh 1602 ** Show the complete change history for the ticket
ea4acb5… drh 1603 **
26eef7f… rberteig 1604 ** Note that the values in set|add are not validated against the
26eef7f… rberteig 1605 ** definitions given in "Ticket Common Script".
2f5d45e… drh 1606 */
2f5d45e… drh 1607 void ticket_cmd(void){
2f5d45e… drh 1608 int n;
ea4acb5… drh 1609 const char *zUser;
ea4acb5… drh 1610 const char *zDate;
294cddf… drh 1611 const char *zTktUuid;
2f5d45e… drh 1612
bc36fdc… danield 1613 /* do some ints, we want to be inside a check-out */
c0c3d92… drh 1614 db_find_and_open_repository(0, 0);
2f5d45e… drh 1615 user_select();
ea4acb5… drh 1616
ea4acb5… drh 1617 zUser = find_option("user-override",0,1);
840b762… drh 1618 if( zUser==0 ) zUser = login_name();
ea4acb5… drh 1619 zDate = find_option("date-override",0,1);
ea4acb5… drh 1620 if( zDate==0 ) zDate = "now";
ea4acb5… drh 1621 zDate = date_in_standard_format(zDate);
294cddf… drh 1622 zTktUuid = find_option("uuid-override",0,1);
294cddf… drh 1623 if( zTktUuid && (strlen(zTktUuid)!=40 || !validate16(zTktUuid,40)) ){
294cddf… drh 1624 fossil_fatal("invalid --uuid-override: must be 40 characters of hex");
294cddf… drh 1625 }
ea4acb5… drh 1626
2f5d45e… drh 1627 /*
2f5d45e… drh 1628 ** Check that the user exists.
2f5d45e… drh 1629 */
ea4acb5… drh 1630 if( !db_exists("SELECT 1 FROM user WHERE login=%Q", zUser) ){
ea4acb5… drh 1631 fossil_fatal("no such user: %s", zUser);
2f5d45e… drh 1632 }
2f5d45e… drh 1633
2f5d45e… drh 1634 if( g.argc<3 ){
f6ad697… stephan 1635 usage("add|change|list|set|show|history");
ea4acb5… drh 1636 }
ea4acb5… drh 1637 n = strlen(g.argv[2]);
ea4acb5… drh 1638 if( n==1 && g.argv[2][0]=='s' ){
ea4acb5… drh 1639 /* set/show cannot be distinguished, so show the usage */
f6ad697… stephan 1640 usage("add|change|list|set|show|history");
ea4acb5… drh 1641 }
68c11db… drh 1642 if(( strncmp(g.argv[2],"list",n)==0 ) || ( strncmp(g.argv[2],"ls",n)==0 )){
ea4acb5… drh 1643 if( g.argc==3 ){
ea4acb5… drh 1644 usage("list fields|reports");
ea4acb5… drh 1645 }else{
ea4acb5… drh 1646 n = strlen(g.argv[3]);
ea4acb5… drh 1647 if( !strncmp(g.argv[3],"fields",n) ){
ea4acb5… drh 1648 /* simply show all field names */
ea4acb5… drh 1649 int i;
ea4acb5… drh 1650
ea4acb5… drh 1651 /* read all available ticket fields */
ea4acb5… drh 1652 getAllTicketFields();
ea4acb5… drh 1653 for(i=0; i<nField; i++){
4f8c897… drh 1654 printf("%s\n",aField[i].zName);
ea4acb5… drh 1655 }
ea4acb5… drh 1656 }else if( !strncmp(g.argv[3],"reports",n) ){
ea4acb5… drh 1657 rpt_list_reports();
ea4acb5… drh 1658 }else{
ea4acb5… drh 1659 fossil_fatal("unknown ticket list option '%s'!",g.argv[3]);
ea4acb5… drh 1660 }
ea4acb5… drh 1661 }
ea4acb5… drh 1662 }else{
ea4acb5… drh 1663 /* add a new ticket or set fields on existing tickets */
ea4acb5… drh 1664 tTktShowEncoding tktEncoding;
ea4acb5… drh 1665
74d5ce3… florian 1666 tktEncoding = find_option("quote",0,0) ? tktFossilize : tktNoTab;
3df526c… jan.nijtmans 1667
ea4acb5… drh 1668 if( strncmp(g.argv[2],"show",n)==0 ){
ea4acb5… drh 1669 if( g.argc==3 ){
ea4acb5… drh 1670 usage("show REPORTNR");
ea4acb5… drh 1671 }else{
ea4acb5… drh 1672 const char *zRep = 0;
ea4acb5… drh 1673 const char *zSep = 0;
ea4acb5… drh 1674 const char *zFilterUuid = 0;
ea4acb5… drh 1675 zSep = find_option("limit","l",1);
ea4acb5… drh 1676 zRep = g.argv[3];
ea4acb5… drh 1677 if( !strcmp(zRep,"0") ){
ea4acb5… drh 1678 zRep = 0;
ea4acb5… drh 1679 }
ea4acb5… drh 1680 if( g.argc>4 ){
ea4acb5… drh 1681 zFilterUuid = g.argv[4];
ea4acb5… drh 1682 }
ea4acb5… drh 1683 rptshow( zRep, zSep, zFilterUuid, tktEncoding );
ea4acb5… drh 1684 }
ea4acb5… drh 1685 }else{
ea4acb5… drh 1686 /* add a new ticket or update an existing ticket */
ea4acb5… drh 1687 enum { set,add,history,err } eCmd = err;
ea4acb5… drh 1688 int i = 0;
ea4acb5… drh 1689 Blob tktchng, cksum;
872a3b2… george 1690 char *aUsed;
ea4acb5… drh 1691
ea4acb5… drh 1692 /* get command type (set/add) and get uuid, if needed for set */
ea4acb5… drh 1693 if( strncmp(g.argv[2],"set",n)==0 || strncmp(g.argv[2],"change",n)==0 ||
ea4acb5… drh 1694 strncmp(g.argv[2],"history",n)==0 ){
ea4acb5… drh 1695 if( strncmp(g.argv[2],"history",n)==0 ){
ea4acb5… drh 1696 eCmd = history;
ea4acb5… drh 1697 }else{
ea4acb5… drh 1698 eCmd = set;
ea4acb5… drh 1699 }
ea4acb5… drh 1700 if( g.argc==3 ){
fc5a217… mistachkin 1701 usage("set|change|history TICKETUUID");
ea4acb5… drh 1702 }
3df526c… jan.nijtmans 1703 zTktUuid = db_text(0,
49b0ff1… drh 1704 "SELECT tkt_uuid FROM ticket WHERE tkt_uuid GLOB '%q*'", g.argv[3]
ea4acb5… drh 1705 );
ea4acb5… drh 1706 if( !zTktUuid ){
ea4acb5… drh 1707 fossil_fatal("unknown ticket: '%s'!",g.argv[3]);
ea4acb5… drh 1708 }
ea4acb5… drh 1709 i=4;
ea4acb5… drh 1710 }else if( strncmp(g.argv[2],"add",n)==0 ){
ea4acb5… drh 1711 eCmd = add;
ea4acb5… drh 1712 i = 3;
294cddf… drh 1713 if( zTktUuid==0 ){
294cddf… drh 1714 zTktUuid = db_text(0, "SELECT lower(hex(randomblob(20)))");
294cddf… drh 1715 }
ea4acb5… drh 1716 }
ea4acb5… drh 1717 /* none of set/add, so show the usage! */
ea4acb5… drh 1718 if( eCmd==err ){
ea4acb5… drh 1719 usage("add|fieldlist|set|show|history");
ea4acb5… drh 1720 }
ea4acb5… drh 1721
ea4acb5… drh 1722 /* we just handle history separately here, does not get out */
ea4acb5… drh 1723 if( eCmd==history ){
ea4acb5… drh 1724 Stmt q;
ea4acb5… drh 1725 int tagid;
ea4acb5… drh 1726
b9b3ce3… jan.nijtmans 1727 if( i != g.argc ){
ea4acb5… drh 1728 fossil_fatal("no other parameters expected to %s!",g.argv[2]);
ea4acb5… drh 1729 }
4f8c897… drh 1730 tagid = db_int(0, "SELECT tagid FROM tag WHERE tagname GLOB 'tkt-%q*'",
4f8c897… drh 1731 zTktUuid);
ea4acb5… drh 1732 if( tagid==0 ){
ea4acb5… drh 1733 fossil_fatal("no such ticket %h", zTktUuid);
3df526c… jan.nijtmans 1734 }
ea4acb5… drh 1735 db_prepare(&q,
ea63a2d… drh 1736 "SELECT datetime(mtime,toLocal()), objid, NULL, NULL, NULL"
ea4acb5… drh 1737 " FROM event, blob"
ea4acb5… drh 1738 " WHERE objid IN (SELECT rid FROM tagxref WHERE tagid=%d)"
ea4acb5… drh 1739 " AND blob.rid=event.objid"
ea4acb5… drh 1740 " UNION "
ea63a2d… drh 1741 "SELECT datetime(mtime,toLocal()), attachid, filename, "
b18f3a5… jan.nijtmans 1742 " src, user"
ea4acb5… drh 1743 " FROM attachment, blob"
ea4acb5… drh 1744 " WHERE target=(SELECT substr(tagname,5) FROM tag WHERE tagid=%d)"
ea4acb5… drh 1745 " AND blob.rid=attachid"
ea4acb5… drh 1746 " ORDER BY 1 DESC",
ea63a2d… drh 1747 tagid, tagid
ea4acb5… drh 1748 );
ea4acb5… drh 1749 while( db_step(&q)==SQLITE_ROW ){
ea4acb5… drh 1750 Manifest *pTicket;
ea4acb5… drh 1751 const char *zDate = db_column_text(&q, 0);
ea4acb5… drh 1752 int rid = db_column_int(&q, 1);
b18f3a5… jan.nijtmans 1753 const char *zFile = db_column_text(&q, 2);
ea4acb5… drh 1754 if( zFile!=0 ){
ea4acb5… drh 1755 const char *zSrc = db_column_text(&q, 3);
b18f3a5… jan.nijtmans 1756 const char *zUser = db_column_text(&q, 4);
4f8c897… drh 1757 if( zSrc==0 || zSrc[0]==0 ){
4f8c897… drh 1758 fossil_print("Delete attachment %s\n", zFile);
4f8c897… drh 1759 }else{
4f8c897… drh 1760 fossil_print("Add attachment %s\n", zFile);
4f8c897… drh 1761 }
4f8c897… drh 1762 fossil_print(" by %s on %s\n", zUser, zDate);
4f8c897… drh 1763 }else{
ec81aee… jan.nijtmans 1764 pTicket = manifest_get(rid, CFTYPE_TICKET, 0);
ea4acb5… drh 1765 if( pTicket ){
ea4acb5… drh 1766 int i;
ea4acb5… drh 1767
4f8c897… drh 1768 fossil_print("Ticket Change by %s on %s:\n",
4f8c897… drh 1769 pTicket->zUser, zDate);
ea4acb5… drh 1770 for(i=0; i<pTicket->nField; i++){
ea4acb5… drh 1771 Blob val;
ea4acb5… drh 1772 const char *z;
ea4acb5… drh 1773 z = pTicket->aField[i].zName;
ea4acb5… drh 1774 blob_set(&val, pTicket->aField[i].zValue);
ea4acb5… drh 1775 if( z[0]=='+' ){
ea4acb5… drh 1776 fossil_print(" Append to ");
b3e32c8… jan.nijtmans 1777 z++;
b3e32c8… jan.nijtmans 1778 }else{
b3e32c8… jan.nijtmans 1779 fossil_print(" Change ");
b3e32c8… jan.nijtmans 1780 }
b3e32c8… jan.nijtmans 1781 fossil_print("%h: ",z);
b3e32c8… jan.nijtmans 1782 if( blob_size(&val)>50 || contains_newline(&val)) {
49b0ff1… drh 1783 fossil_print("\n ");
2476b81… drh 1784 comment_print(blob_str(&val),0,4,-1,get_comment_format());
ea4acb5… drh 1785 }else{
ea4acb5… drh 1786 fossil_print("%s\n",blob_str(&val));
ea4acb5… drh 1787 }
ea4acb5… drh 1788 blob_reset(&val);
ea4acb5… drh 1789 }
ea4acb5… drh 1790 }
ea4acb5… drh 1791 manifest_destroy(pTicket);
ea4acb5… drh 1792 }
ea4acb5… drh 1793 }
ea4acb5… drh 1794 db_finalize(&q);
ea4acb5… drh 1795 return;
ea4acb5… drh 1796 }
ea4acb5… drh 1797 /* read all given ticket field/value pairs from command line */
ea4acb5… drh 1798 if( i==g.argc ){
ea4acb5… drh 1799 fossil_fatal("empty %s command aborted!",g.argv[2]);
ea4acb5… drh 1800 }
ea4acb5… drh 1801 getAllTicketFields();
2f78b2c… danield 1802 /* read command-line and assign fields in the aField[].zValue array */
ea4acb5… drh 1803 while( i<g.argc ){
ea4acb5… drh 1804 char *zFName;
ea4acb5… drh 1805 char *zFValue;
ea4acb5… drh 1806 int j;
ea4acb5… drh 1807 int append = 0;
ea4acb5… drh 1808
ea4acb5… drh 1809 zFName = g.argv[i++];
ea4acb5… drh 1810 if( i==g.argc ){
ea4acb5… drh 1811 fossil_fatal("missing value for '%s'!",zFName);
ea4acb5… drh 1812 }
ea4acb5… drh 1813 zFValue = g.argv[i++];
ea4acb5… drh 1814 if( tktEncoding == tktFossilize ){
4c3e172… danield 1815 zFValue=fossil_strdup(zFValue);
ea4acb5… drh 1816 defossilize(zFValue);
ea4acb5… drh 1817 }
ea4acb5… drh 1818 append = (zFName[0] == '+');
b9b3ce3… jan.nijtmans 1819 if( append ){
ea4acb5… drh 1820 zFName++;
ea4acb5… drh 1821 }
ea4acb5… drh 1822 j = fieldId(zFName);
ea4acb5… drh 1823 if( j == -1 ){
ea4acb5… drh 1824 fossil_fatal("unknown field name '%s'!",zFName);
ea4acb5… drh 1825 }else{
b9b3ce3… jan.nijtmans 1826 if( append ){
4f8c897… drh 1827 aField[j].zAppend = zFValue;
b9b3ce3… jan.nijtmans 1828 }else{
4f8c897… drh 1829 aField[j].zValue = zFValue;
ea4acb5… drh 1830 }
ea4acb5… drh 1831 }
ea4acb5… drh 1832 }
872a3b2… george 1833 aUsed = fossil_malloc_zero( nField );
ea4acb5… drh 1834
ea4acb5… drh 1835 /* now add the needed artifacts to the repository */
ea4acb5… drh 1836 blob_zero(&tktchng);
ea4acb5… drh 1837 /* add the time to the ticket manifest */
ea4acb5… drh 1838 blob_appendf(&tktchng, "D %s\n", zDate);
ea4acb5… drh 1839 /* append defined elements */
ea4acb5… drh 1840 for(i=0; i<nField; i++){
ea4acb5… drh 1841 char *zValue = 0;
ea4acb5… drh 1842 char *zPfx;
ea4acb5… drh 1843
b9b3ce3… jan.nijtmans 1844 if( aField[i].zAppend && aField[i].zAppend[0] ){
ea4acb5… drh 1845 zPfx = " +";
4f8c897… drh 1846 zValue = aField[i].zAppend;
872a3b2… george 1847 aUsed[i] = JCARD_APPEND;
b9b3ce3… jan.nijtmans 1848 }else if( aField[i].zValue && aField[i].zValue[0] ){
ea4acb5… drh 1849 zPfx = " ";
4f8c897… drh 1850 zValue = aField[i].zValue;
872a3b2… george 1851 aUsed[i] = JCARD_ASSIGN;
b9b3ce3… jan.nijtmans 1852 }else{
ea4acb5… drh 1853 continue;
ea4acb5… drh 1854 }
4f8c897… drh 1855 if( memcmp(aField[i].zName, "private_", 8)==0 ){
ea4acb5… drh 1856 zValue = db_conceal(zValue, strlen(zValue));
4f8c897… drh 1857 blob_appendf(&tktchng, "J%s%s %s\n", zPfx, aField[i].zName, zValue);
872a3b2… george 1858 aUsed[i] = JCARD_PRIVATE;
ea4acb5… drh 1859 }else{
ea4acb5… drh 1860 blob_appendf(&tktchng, "J%s%s %#F\n", zPfx,
4f8c897… drh 1861 aField[i].zName, strlen(zValue), zValue);
ea4acb5… drh 1862 }
ea4acb5… drh 1863 }
ea4acb5… drh 1864 blob_appendf(&tktchng, "K %s\n", zTktUuid);
ea4acb5… drh 1865 blob_appendf(&tktchng, "U %F\n", zUser);
ea4acb5… drh 1866 md5sum_blob(&tktchng, &cksum);
ea4acb5… drh 1867 blob_appendf(&tktchng, "Z %b\n", &cksum);
872a3b2… george 1868 if( ticket_put(&tktchng, zTktUuid, aUsed,
872a3b2… george 1869 ticket_need_moderation(1) )==0 ){
2c42b21… andygoth 1870 fossil_fatal("%s", g.zErrMsg);
1311841… jan.nijtmans 1871 }else{
1311841… jan.nijtmans 1872 fossil_print("ticket %s succeeded for %s\n",
ba418ee… drh 1873 (eCmd==set?"set":"add"),zTktUuid);
1311841… jan.nijtmans 1874 }
872a3b2… george 1875 fossil_free( aUsed );
6714c94… drh 1876 }
6714c94… drh 1877 }
6714c94… drh 1878 }
6714c94… drh 1879
6714c94… drh 1880
6714c94… drh 1881 #if INTERFACE
6714c94… drh 1882 /* Standard submenu items for wiki pages */
6714c94… drh 1883 #define T_SRCH 0x00001
6714c94… drh 1884 #define T_REPLIST 0x00002
6714c94… drh 1885 #define T_NEW 0x00004
6714c94… drh 1886 #define T_ALL 0x00007
6714c94… drh 1887 #define T_ALL_BUT(x) (T_ALL&~(x))
6714c94… drh 1888 #endif
6714c94… drh 1889
6714c94… drh 1890 /*
6714c94… drh 1891 ** Add some standard submenu elements for ticket screens.
6714c94… drh 1892 */
6714c94… drh 1893 void ticket_standard_submenu(unsigned int ok){
2e9e369… drh 1894 if( (ok & T_SRCH)!=0 && search_restrict(SRCH_TKT)!=0 ){
187424e… andygoth 1895 style_submenu_element("Search", "%R/tktsrch");
6714c94… drh 1896 }
6714c94… drh 1897 if( (ok & T_REPLIST)!=0 ){
187424e… andygoth 1898 style_submenu_element("Reports", "%R/reportlist");
b9daba5… drh 1899 }
653dd40… drh 1900 if( (ok & T_NEW)!=0 && g.anon.NewTkt ){
187424e… andygoth 1901 style_submenu_element("New", "%R/tktnew");
6714c94… drh 1902 }
b9daba5… drh 1903 }
b9daba5… drh 1904
b9daba5… drh 1905 /*
b9daba5… drh 1906 ** WEBPAGE: ticket
b9daba5… drh 1907 **
b9daba5… drh 1908 ** This is intended to be the primary "Ticket" page. Render as
5260fbf… jan.nijtmans 1909 ** either ticket-search (if search is enabled) or as the
b9daba5… drh 1910 ** /reportlist page (if ticket search is disabled).
b9daba5… drh 1911 */
b9daba5… drh 1912 void tkt_home_page(void){
b9daba5… drh 1913 login_check_credentials();
2e9e369… drh 1914 if( search_restrict(SRCH_TKT)!=0 ){
b9daba5… drh 1915 tkt_srchpage();
b9daba5… drh 1916 }else{
b9daba5… drh 1917 view_list();
6714c94… drh 1918 }
6714c94… drh 1919 }
6714c94… drh 1920
6714c94… drh 1921 /*
6714c94… drh 1922 ** WEBPAGE: tktsrch
6714c94… drh 1923 ** Usage: /tktsrch?s=PATTERN
6714c94… drh 1924 **
6714c94… drh 1925 ** Full-text search of all current tickets
6714c94… drh 1926 */
6714c94… drh 1927 void tkt_srchpage(void){
c51ace6… drh 1928 char *defaultReport;
6714c94… drh 1929 login_check_credentials();
112c713… drh 1930 style_set_current_feature("tkt");
6714c94… drh 1931 style_header("Ticket Search");
6714c94… drh 1932 ticket_standard_submenu(T_ALL_BUT(T_SRCH));
c51ace6… drh 1933 if( !search_screen(SRCH_TKT, 0) ){
c51ace6… drh 1934 defaultReport = db_get("ticket-default-report", 0);
c51ace6… drh 1935 if( defaultReport ){
c51ace6… drh 1936 rptview_page_content(defaultReport, 0, 0);
c51ace6… drh 1937 }
c51ace6… drh 1938 }
112c713… drh 1939 style_finish_page();
d0305b3… aku 1940 }

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button