Fossil

Diff
Login

Differences From Artifact [0d0a9751aa]:

To Artifact [cee508f4de]:


25
26
27
28
29
30
31
















































































































32
33
34
35
















36
37
38
39
40
41
42








43
44
45


46
47
48


49

50

51
52




53
54
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
92



93
94
95
96




97
98
99

100
101


102
103
104
105
106
107



108
109
110
111
112
113
114
115
116
117
118
119





120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135



136
137
138

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154

155
156
157
158






159
160
161
162
163
164
165

166
167


168
169



170
171
172

173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188

189
190
191
192




193
194


195
196
197
198
199

200
201


202
203
204
205
206
207
208
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143




144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159







160
161
162
163
164
165
166
167



168
169



170
171
172
173

174
175
176
177
178
179
180
181
182

183
184
185
186
187
188
189
190
191
192
193
194
195







196
197
198
199
200
201
202
203
204








205
206
207




208
209
210
211



212


213
214






215
216
217












218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243

244
245
246
247
248
249
250










251




252
253
254
255
256
257







258


259
260
261
262
263
264
265
266
267

268
269
270
271
272
273
274










275




276
277
278
279


280
281





282


283
284
285
286
287
288
289
290
291







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

+
-
+


+
+
+
+


-













-
-
-
-
-
-
-









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
















+
+
+


-
+






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


+
+
+


-
+






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







**
** Implementation of the Tag View page
*/
#include <assert.h>
#include "config.h"
#include "tagview.h"

/**
tagview_strxform_f is a typedef for funcs with
the following policy:

The accept a string which they then transform into
some other form. They return a transformed copy,
which the caller is responsible for freeing.

The intention of this is to provide a way for
a generic query routine to format specific column
data (e.g. transform an object ID into a link to
that object).
*/
typedef char * (*tagview_strxform_f)( char const * );

#if 0
/** A no-op transformer which can be used as a placeholder. */
static char * tagview_xf_copy( char const * uuid )
{
  int len = strlen(uuid) + 1;
  char * ret = (char *) malloc( len );
  ret[len] = '\0';
  strncpy( ret, uuid, len-1 );
  return ret;
}
#endif

/** Returns a hyperlink to uuid. */
static char * tagview_xf_link_to_uuid( char const * uuid )
{
    const int offset = 10;
    char shortname[offset+1];
    shortname[offset] = '\0';
    memcpy( shortname, uuid, offset );
    return mprintf( "<tt><a href='%s/vinfo/%s'><strong>%s</strong>%s</a></tt>",
      g.zBaseURL, uuid, shortname, uuid+offset );
}

/** Returns a hyperlink to the given tag. */
static char * tagview_xf_link_to_tagid( char const * tagid )
{
  return mprintf( "<a href='%s/tagview?tagid=%s'>%s</a>",
        g.zBaseURL, tagid, tagid );
}

/** Returns a hyperlink to the named tag. */
static char * tagview_xf_link_to_tagname( char const * tagid )
{
  return mprintf( "<a href='%s/tagview/%s'>%s</a>",
        g.zBaseURL, tagid, tagid );
}



/**
* tagview_run_query():
*
* A very primitive helper to run an SQL query and table-ize the
* results.
*
* The sql parameter should be a single, complete SQL statement.
*
* The coln parameter is optional (it may be 0). If it is 0 then the
* column names using in the output will be taken directly from the
* SQL. If it is not null then it must have as many entries as the SQL
* result has columns. Each entry is a column name for the SQL result
* column of the same index. Any given entry may be 0, in which case
* the column name from the SQL is used.
*
* The xform argument is an array of transformation functions (type
* tagview_strxform_f). The array, or any single entry, may be 0, but
* if the array is non-0 then it must have at least as many entries as
* colnames does. Each index corresponds directly to an entry in
* colnames and the SQL results.  Any given entry may be 0 If it has
* fewer, undefined behaviour results.  If a column has an entry in
* xform, then the xform function will be called to transform the
* column data before rendering it. This function takes care of freeing
* the strings created by the xform functions.
*
* Example:
*
*  char const * const colnames[] = {
*   "Tag ID", "Tag Name", "Something Else", "UUID"
*  };
*  tagview_strxform_f xf[] = {
*    tagview_xf_link_to_tagid,
*    tagview_xf_link_to_tagname,
*    0,
*    tagview_xf_link_to_uuid
*  };
*  tagview_run_query( "select a,b,c,d from foo", colnames, xf );
*
*/
static void tagview_run_query(
  char const * sql,
  char const * const * coln,
  tagview_strxform_f * xform )
{
  
  Stmt st;
  @ <table cellpadding='4px' border='1'><tbody>
  int i = 0;
  int rc = db_prepare( &st, sql );
  /**
    Achtung: makeheaders apparently can't pull the function
    name from this:
   if( SQLITE_OK != db_prepare( &st, sql ) )
  */
  if( SQLITE_OK != rc )
  {
    @ tagview_run_query(): Error processing SQL: [%s(sql)]
    return;

/*
** Output a single entry for a menu generated using an HTML table.
** If zLink is not NULL or an empty string, then it is the page that
  }
  int colc = db_column_count(&st);
  @ <tr>
  for( i = 0; i < colc; ++i ) {
    if( coln )
    {
      @ <th>%s(coln[i] ? coln[i] : db_column_name(&st,i))</th>
    }
    else
    {
      @ <td>%s(db_column_name(&st,i))</td>
    }
  }
  @ </tr>


** the menu entry will hyperlink to.  If zLink is NULL or "", then
** the menu entry has no hyperlink - it is disabled.
*/
void tagview_menu_entry(
  const char *zTitle,
  const char *zLink,
  const char *zDesc
  while( SQLITE_ROW == db_step(&st) ){
    @ <tr>
      for( i = 0; i < colc; ++i ) {
        char * xf = 0;
        char const * xcf = 0;
        xcf = (xform && xform[i])
          ? (xf=(xform[i])(db_column_text(&st,i)))
          : db_column_text(&st,i);
){
  @ <tr><td valign="top" align="right">
  if( zLink && zLink[0] ){
        @ <td>%s(xcf)</td>
        if( xf ) free( xf );
    @ <a href="%s(g.zBaseURL)/%s(zLink)">%h(zTitle)</a>
  }else{
    @ %h(zTitle)
      }
    @ </tr>
  }
  db_finalize( &st );
  @ </td><td valign="top">%h(zDesc)</td></tr>
  @ </tbody></table>
}

/**
  Lists all tags matching the given LIKE clause (which
may be 0).
*/
static void tagview_page_list_tags( char const * like )
{
  Stmt st;
  char * likeclause = 0;
  const int limit = 10;
  char * limitstr = 0;
  if( like && strlen(like) )
  {
    likeclause = mprintf( "AND t.tagname LIKE '%%%%%q%%%%'", like );
    @ <h2>Tags matching [%s(likeclause)]:</h2>
  }
  else
  {
    limitstr = mprintf( "LIMIT %d", limit );
    @ <h2>%d(limit) most recent tags:</h2>
  }
  @ <table cellpadding='4px' border='1'><tbody>
  @ <tr>
  @ <th>Tag ID</th>
  @ <th>Tag name</th>
  @ <th>Timestamp</th>
  @ <th>Version</th>
  @ </tr>
  char * sql = mprintf( 
    "SELECT t.tagid, t.tagname, DATETIME(tx.mtime), b.uuid "
    "FROM tag t, tagxref tx, blob b "
    "WHERE (t.tagid=tx.tagid) and (tx.srcid=b.rid) "
    "AND (tx.tagtype != 0) %s "
    "ORDER BY tx.mtime DESC %s",
    likeclause ? likeclause : " ",
    limitstr ? limitstr : " "
    );
  db_prepare( &st, sql );
  if( likeclause ) free( likeclause );
  free( sql );
  while( SQLITE_ROW == db_step(&st) ){
    int tagid = db_column_int( &st, 0 );
    char const * tagname = db_column_text( &st, 1 );
    char const * tagtime = db_column_text( &st, 2 );
    char const * uuid = db_column_text( &st, 3 );
   /* "   AND t.tagname NOT GLOB 'wiki-*'" // Do we want this?? */

  char const * const colnames[] = {
    const int offset = 10;
    char shortname[offset+1];
    shortname[offset] = '\0';
    memcpy( shortname, uuid, offset );
    "Tag ID", "Name", "Timestamp", "Version"
  };
  tagview_strxform_f xf[] = {
    tagview_xf_link_to_tagid,
    @ <tr>
    @ <td><tt>
    @ <a href='%s(g.zBaseURL)/tagview?tagid=%d(tagid)'>%d(tagid)</a>
    tagview_xf_link_to_tagname,
    @ </tt></td>
    @ <td><tt><a href='%s(g.zBaseURL)/tagview/%q(tagname)'>%s(tagname)</a></tt></td>
    0,
    tagview_xf_link_to_uuid
    @ <td align='center'><tt>%s(tagtime)</tt></td>
    @ <td><tt>
    @ <a href='%s(g.zBaseURL)/vinfo/%s(uuid)'><strong>%s(shortname)</strong>%s(uuid+offset)</a>
    @ </tt></td></tr>
  }
  db_finalize( &st );
  };
  tagview_run_query( sql, colnames, xf );
  free( sql );
  @ </tbody></table>
  @ <hr/>TODOs include:
  @ <ul>
  @  <li>Page through long tags lists.</li>
  @  <li>Refactor the internal report routines to be reusable.</li>
  @  <li>Allow different sorting.</li>
  @  <li>Selectively filter out wiki/ticket/baseline</li>
  @  <li>?</li>
  @ </ul>

}

}

/**
A small search form which forwards to ?like=SEARCH_STRING
*/
static void tagview_page_search_miniform(void){
  char const * like = P("like");
  @ <div style='font-size:smaller'>
  @ <form action='/tagview' method='post'>
  @ Search for tags: 
  @ <input type='text' name='like' value='%s((like?like:""))' size='10'/>
  @ <input type='submit'/>
  @ </form>
  @ </div>
}


static void tagview_page_default(void){
  tagview_page_list_tags( 0 );
}

/**
  Lists all tags matching the given tagid.
*/
static void tagview_page_tag_by_id( int tagid )
{
  Stmt st;
  @ <h2>Tag #%d(tagid):</h2>
  char * sql = mprintf( 
    "SELECT DISTINCT (t.tagname), DATETIME(tx.mtime), b.uuid "
    "FROM tag t, tagxref tx, blob b "
    "WHERE (t.tagid=%d) AND (t.tagid=tx.tagid) AND (tx.srcid=b.rid) "
    "ORDER BY tx.mtime DESC",
  tagid);
  db_prepare( &st, sql );
  free( sql );
  @ <h2>Tag ID %d(tagid):</h2>
  @ <table cellpadding='4px' border='1'><tbody>
  @ <tr><th>Tag name</th><th>Timestamp</th><th>Version</th></tr>
  while( SQLITE_ROW == db_step(&st) )
  {
    char const * tagname = db_column_text( &st, 0 );
    char const * tagtime = db_column_text( &st, 1 );
    char const * uuid = db_column_text( &st, 2 );
  char const * const colnames[] = {
    const int offset = 10;
    char shortname[offset+1];
    shortname[offset] = '\0';
    memcpy( shortname, uuid, offset );
      "Tag Name", "Timestamp", "Version"
    };
  tagview_strxform_f xf[] = {
      tagview_xf_link_to_tagname,
      0,
      tagview_xf_link_to_uuid
    @ <tr>
    @ <td><tt><a href='%s(g.zBaseURL)/tagview/%q(tagname)'>%s(tagname)</a></tt></td>
    @ <td align='center'><tt>%s(tagtime)</tt></td>
    @ <td><tt>
    @ <a href='%s(g.zBaseURL)/vinfo/%s(uuid)'><strong>%s(shortname)</strong>%s(uuid+offset)</a>
    @ </tt></td></tr>
  }
    };
  @ </tbody></table>
  db_finalize( &st );
  tagview_run_query( sql, colnames, xf );
  free(sql);
}

/**
  Lists all tags matching the given tag name.
*/
static void tagview_page_tag_by_name( char const * tagname )
{
  Stmt st;
  @ <h2>Tag '%s(tagname)':</h2>
  char * sql = mprintf( 
    "SELECT DISTINCT t.tagid, DATETIME(tx.mtime), b.uuid "
    "FROM tag t, tagxref tx, blob b "
    "WHERE (t.tagname='%q') AND (t.tagid=tx.tagid) AND (tx.srcid=b.rid) "
    "ORDER BY tx.mtime DESC",
    tagname);
  db_prepare( &st, sql );
  free( sql );
  @ <h2>Tag '%s(tagname)':</h2>
  @ <table cellpadding='4px' border='1'><tbody>
  @ <tr><th>Tag ID</th><th>Timestamp</th><th>Version</th></tr>
  while( SQLITE_ROW == db_step(&st) )
  {
    int tagid = db_column_int( &st, 0 );
    char const * tagtime = db_column_text( &st, 1 );
    char const * uuid = db_column_text( &st, 2 );
  char const * const colnames[] = {
    const int offset = 10;
    char shortname[offset+1];
    shortname[offset] = '\0';
    memcpy( shortname, uuid, offset );
      "Tag ID", "Timestamp", "Version"
  };
  tagview_strxform_f xf[] = {
      tagview_xf_link_to_tagid,
    @ <tr>
    @ <td><tt><a href='%s(g.zBaseURL)/tagview?tagid=%d(tagid)'>%d(tagid)</a></tt></td>
      0,
      tagview_xf_link_to_uuid
    @ <td align='center'><tt>%s(tagtime)</tt></td>
    @ <td><tt>
    @ <a href='%s(g.zBaseURL)/vinfo/%s(uuid)'><strong>%s(shortname)</strong>%s(uuid+offset)</a>
    @ </tt></td></tr>
  }
  };
  @ </tbody></table>
  db_finalize( &st );
  tagview_run_query( sql, colnames, xf );
  free( sql );
}


/*
** WEBPAGE: /tagview
*/
void tagview_page(void){