1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
| /*
** Copyright (c) 2007 D. Richard Hipp
** Copyright (c) 2008 Stephan Beal
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the Simplified BSD License (also
** known as the "2-Clause License" or "FreeBSD License".)
**
** This program is distributed in the hope that it will be useful,
** but without any warranty; without even the implied warranty of
** merchantability or fitness for a particular purpose.
**
** Author contact information:
** drh@hwaci.com
** http://www.hwaci.com/drh/
**
*******************************************************************************
**
** This file contains code to do formatting of wiki text.
*/
#include "config.h"
#include <assert.h>
#include <ctype.h>
#include "wiki.h"
/*
** Return true if the input string is a well-formed wiki page name.
**
** Well-formed wiki page names do not begin or end with whitespace,
** and do not contain tabs or other control characters and do not
** contain more than a single space character in a row. Well-formed
** names must be between 1 and 100 characters in length, inclusive.
*/
int wiki_name_is_wellformed(const unsigned char *z){
int i;
if( z[0]<=0x20 ){
return 0;
}
for(i=1; z[i]; i++){
if( z[i]<0x20 ) return 0;
if( z[i]==0x20 && z[i-1]==0x20 ) return 0;
}
if( z[i-1]==' ' ) return 0;
if( i<1 || i>100 ) return 0;
return 1;
}
/*
** Output rules for well-formed wiki pages
*/
static void well_formed_wiki_name_rules(void){
@ <ul>
@ <li> Must not begin or end with a space.</li>
@ <li> Must not contain any control characters, including tab or
@ newline.</li>
@ <li> Must not have two or more spaces in a row internally.</li>
@ <li> Must be between 1 and 100 characters in length.</li>
@ </ul>
}
/*
** Check a wiki name. If it is not well-formed, then issue an error
** and return true. If it is well-formed, return false.
*/
static int check_name(const char *z){
if( !wiki_name_is_wellformed((const unsigned char *)z) ){
style_header("Wiki Page Name Error");
@ The wiki name "<span class="wikiError">%h(z)</span>" is not well-formed.
@ Rules for wiki page names:
well_formed_wiki_name_rules();
style_footer();
return 1;
}
return 0;
}
/*
** WEBPAGE: home
** WEBPAGE: index
** WEBPAGE: not_found
**
** The /home, /index, and /not_found pages all redirect to the homepage
** configured by the administrator.
*/
void home_page(void){
char *zPageName = db_get("project-name",0);
char *zIndexPage = db_get("index-page",0);
login_check_credentials();
if( zIndexPage ){
const char *zPathInfo = P("PATH_INFO");
while( zIndexPage[0]=='/' ) zIndexPage++;
while( zPathInfo[0]=='/' ) zPathInfo++;
if( fossil_strcmp(zIndexPage, zPathInfo)==0 ) zIndexPage = 0;
}
if( zIndexPage ){
cgi_redirectf("%s/%s", g.zTop, zIndexPage);
}
if( !g.perm.RdWiki ){
cgi_redirectf("%s/login?g=%s/home", g.zTop, g.zTop);
}
if( zPageName ){
login_check_credentials();
g.zExtra = zPageName;
cgi_set_parameter_nocopy("name", g.zExtra, 1);
g.isHome = 1;
wiki_page();
return;
}
style_header("Home");
@ <p>This is a stub home-page for the project.
@ To fill in this page, first go to
@ %z(href("%R/setup_config"))setup/config</a>
@ and establish a "Project Name". Then create a
@ wiki page with that name. The content of that wiki page
@ will be displayed in place of this message.</p>
style_footer();
}
/*
** Return true if the given pagename is the name of the sandbox
*/
static int is_sandbox(const char *zPagename){
return fossil_stricmp(zPagename,"sandbox")==0 ||
fossil_stricmp(zPagename,"sand box")==0;
}
/*
** Formal, common and short names for the various wiki styles.
*/
static const char *const azStyles[] = {
"text/x-fossil-wiki", "Fossil Wiki", "wiki",
"text/x-markdown", "Markdown", "markdown",
"text/plain", "Plain Text", "plain"
};
/*
** Only allow certain mimetypes through.
** All others become "text/x-fossil-wiki"
*/
const char *wiki_filter_mimetypes(const char *zMimetype){
if( zMimetype!=0 ){
int i;
for(i=0; i<count(azStyles); i+=3){
if( fossil_strcmp(zMimetype,azStyles[i+2])==0 ){
return azStyles[i];
}
}
if( fossil_strcmp(zMimetype, "text/x-markdown")==0
|| fossil_strcmp(zMimetype, "text/plain")==0 ){
return zMimetype;
}
}
return "text/x-fossil-wiki";
}
/*
** Render wiki text according to its mimetype.
**
** text/x-fossil-wiki Fossil wiki
** text/x-markdown Markdown
** anything else... Plain text
*/
void wiki_render_by_mimetype(Blob *pWiki, const char *zMimetype){
if( zMimetype==0 || fossil_strcmp(zMimetype, "text/x-fossil-wiki")==0 ){
wiki_convert(pWiki, 0, 0);
}else if( fossil_strcmp(zMimetype, "text/x-markdown")==0 ){
Blob tail = BLOB_INITIALIZER;
markdown_to_html(pWiki, 0, &tail);
@ %s(blob_str(&tail))
blob_reset(&tail);
}else{
@ <pre class='textPlain'>
@ %h(blob_str(pWiki))
@ </pre>
}
}
/*
** WEBPAGE: md_rules
**
** Show a summary of the Markdown wiki formatting rules.
*/
void markdown_rules_page(void){
Blob x;
int fTxt = P("txt")!=0;
style_header("Markdown Formatting Rules");
if( fTxt ){
style_submenu_element("Formatted", "%R/md_rules");
}else{
style_submenu_element("Plain-Text", "%R/md_rules?txt=1");
}
blob_init(&x, builtin_text("markdown.md"), -1);
wiki_render_by_mimetype(&x, fTxt ? "text/plain" : "text/x-markdown");
blob_reset(&x);
style_footer();
}
/*
** WEBPAGE: wiki_rules
**
** Show a summary of the wiki formatting rules.
*/
void wiki_rules_page(void){
Blob x;
int fTxt = P("txt")!=0;
style_header("Wiki Formatting Rules");
if( fTxt ){
style_submenu_element("Formatted", "%R/wiki_rules");
}else{
style_submenu_element("Plain-Text", "%R/wiki_rules?txt=1");
}
blob_init(&x, builtin_text("wiki.wiki"), -1);
wiki_render_by_mimetype(&x, fTxt ? "text/plain" : "text/x-fossil-wiki");
blob_reset(&x);
style_footer();
}
/*
** Returns non-zero if moderation is required for wiki changes and wiki
** attachments.
*/
int wiki_need_moderation(
int localUser /* Are we being called for a local interactive user? */
){
/*
** If the FOSSIL_FORCE_WIKI_MODERATION variable is set, *ALL* changes for
** wiki pages will be required to go through moderation (even those performed
** by the local interactive user via the command line). This can be useful
** for local (or remote) testing of the moderation subsystem and its impact
** on the contents and status of wiki pages.
*/
if( fossil_getenv("FOSSIL_FORCE_WIKI_MODERATION")!=0 ){
return 1;
}
if( localUser ){
return 0;
}
return g.perm.ModWiki==0 && db_get_boolean("modreq-wiki",0)==1;
}
/* Standard submenu items for wiki pages */
#define W_SRCH 0x00001
#define W_LIST 0x00002
#define W_HELP 0x00004
#define W_NEW 0x00008
#define W_BLOG 0x00010
#define W_SANDBOX 0x00020
#define W_ALL 0x0001f
#define W_ALL_BUT(x) (W_ALL&~(x))
/*
** Add some standard submenu elements for wiki screens.
*/
static void wiki_standard_submenu(unsigned int ok){
if( (ok & W_SRCH)!=0 && search_restrict(SRCH_WIKI)!=0 ){
style_submenu_element("Search", "%R/wikisrch");
}
if( (ok & W_LIST)!=0 ){
style_submenu_element("List", "%R/wcontent");
}
if( (ok & W_HELP)!=0 ){
style_submenu_element("Help", "%R/wikihelp");
}
if( (ok & W_NEW)!=0 && g.anon.NewWiki ){
style_submenu_element("New", "%R/wikinew");
}
#if 0
if( (ok & W_BLOG)!=0
#endif
if( (ok & W_SANDBOX)!=0 ){
style_submenu_element("Sandbox", "%R/wiki?name=Sandbox");
}
}
/*
** WEBPAGE: wikihelp
** A generic landing page for wiki.
*/
void wiki_helppage(void){
login_check_credentials();
if( !g.perm.RdWiki ){ login_needed(g.anon.RdWiki); return; }
style_header("Wiki Help");
wiki_standard_submenu(W_ALL_BUT(W_HELP));
@ <h2>Wiki Links</h2>
@ <ul>
{ char *zWikiHomePageName = db_get("index-page",0);
if( zWikiHomePageName ){
@ <li> %z(href("%R%s",zWikiHomePageName))
@ %h(zWikiHomePageName)</a> wiki home page.</li>
}
}
{ char *zHomePageName = db_get("project-name",0);
if( zHomePageName ){
@ <li> %z(href("%R/wiki?name=%t",zHomePageName))
@ %h(zHomePageName)</a> project home page.</li>
}
}
@ <li> %z(href("%R/timeline?y=w"))Recent changes</a> to wiki pages.</li>
@ <li> Formatting rules for %z(href("%R/wiki_rules"))Fossil Wiki</a> and for
@ %z(href("%R/md_rules"))Markdown Wiki</a>.</li>
@ <li> Use the %z(href("%R/wiki?name=Sandbox"))Sandbox</a>
@ to experiment.</li>
if( g.anon.NewWiki ){
@ <li> Create a %z(href("%R/wikinew"))new wiki page</a>.</li>
if( g.anon.Write ){
@ <li> Create a %z(href("%R/technoteedit"))new tech-note</a>.</li>
}
}
@ <li> %z(href("%R/wcontent"))List of All Wiki Pages</a>
@ available on this server.</li>
if( g.anon.ModWiki ){
@ <li> %z(href("%R/modreq"))Tend to pending moderation requests</a></li>
}
if( search_restrict(SRCH_WIKI)!=0 ){
@ <li> %z(href("%R/wikisrch"))Search</a> for wiki pages containing key
@ words</li>
}
@ </ul>
style_footer();
return;
}
/*
** WEBPAGE: wikisrch
** Usage: /wikisrch?s=PATTERN
**
** Full-text search of all current wiki text
*/
void wiki_srchpage(void){
login_check_credentials();
style_header("Wiki Search");
wiki_standard_submenu(W_HELP|W_LIST|W_SANDBOX);
search_screen(SRCH_WIKI, 0);
style_footer();
}
/*
** WEBPAGE: wiki
** URL: /wiki?name=PAGENAME
*/
void wiki_page(void){
char *zTag;
int rid = 0;
int isSandbox;
char *zUuid;
unsigned submenuFlags = W_ALL;
Blob wiki;
Manifest *pWiki = 0;
const char *zPageName;
const char *zMimetype = 0;
char *zBody = mprintf("%s","<i>Empty Page</i>");
login_check_credentials();
if( !g.perm.RdWiki ){ login_needed(g.anon.RdWiki); return; }
zPageName = P("name");
if( zPageName==0 ){
if( search_restrict(SRCH_WIKI)!=0 ){
wiki_srchpage();
}else{
wiki_helppage();
}
return;
}
if( check_name(zPageName) ) return;
isSandbox = is_sandbox(zPageName);
if( isSandbox ){
submenuFlags &= ~W_SANDBOX;
zBody = db_get("sandbox",zBody);
zMimetype = db_get("sandbox-mimetype","text/x-fossil-wiki");
rid = 0;
}else{
const char *zUuid = P("id");
if( zUuid==0 || (rid = symbolic_name_to_rid(zUuid,"w"))==0 ){
zTag = mprintf("wiki-%s", zPageName);
rid = db_int(0,
"SELECT rid FROM tagxref"
" WHERE tagid=(SELECT tagid FROM tag WHERE tagname=%Q)"
" ORDER BY mtime DESC", zTag
);
free(zTag);
}
pWiki = manifest_get(rid, CFTYPE_WIKI, 0);
if( pWiki ){
zBody = pWiki->zWiki;
zMimetype = pWiki->zMimetype;
}
}
zMimetype = wiki_filter_mimetypes(zMimetype);
if( !g.isHome ){
if( rid ){
style_submenu_element("Diff", "%R/wdiff?name=%T&a=%d", zPageName, rid);
zUuid = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", rid);
style_submenu_element("Details", "%R/info/%s", zUuid);
}
if( (rid && g.anon.WrWiki) || (!rid && g.anon.NewWiki) ){
if( db_get_boolean("wysiwyg-wiki", 0) ){
style_submenu_element("Edit", "%s/wikiedit?name=%T&wysiwyg=1",
g.zTop, zPageName);
}else{
style_submenu_element("Edit", "%s/wikiedit?name=%T", g.zTop, zPageName);
}
}
if( rid && g.anon.ApndWiki && g.anon.Attach ){
style_submenu_element("Attach",
"%s/attachadd?page=%T&from=%s/wiki%%3fname=%T",
g.zTop, zPageName, g.zTop, zPageName);
}
if( rid && g.anon.ApndWiki ){
style_submenu_element("Append", "%s/wikiappend?name=%T&mimetype=%s",
g.zTop, zPageName, zMimetype);
}
if( g.perm.Hyperlink ){
style_submenu_element("History", "%s/whistory?name=%T",
g.zTop, zPageName);
}
}
style_set_current_page("%T?name=%T", g.zPath, zPageName);
style_header("%s", zPageName);
wiki_standard_submenu(submenuFlags);
blob_init(&wiki, zBody, -1);
wiki_render_by_mimetype(&wiki, zMimetype);
blob_reset(&wiki);
attachment_list(zPageName, "<hr /><h2>Attachments:</h2><ul>");
manifest_destroy(pWiki);
style_footer();
}
/*
** Write a wiki artifact into the repository
*/
int wiki_put(Blob *pWiki, int parent, int needMod){
int nrid;
if( !needMod ){
nrid = content_put_ex(pWiki, 0, 0, 0, 0);
if( parent) content_deltify(parent, &nrid, 1, 0);
}else{
nrid = content_put_ex(pWiki, 0, 0, 0, 1);
moderation_table_create();
db_multi_exec("INSERT INTO modreq(objid) VALUES(%d)", nrid);
}
db_multi_exec("INSERT OR IGNORE INTO unsent VALUES(%d)", nrid);
db_multi_exec("INSERT OR IGNORE INTO unclustered VALUES(%d);", nrid);
manifest_crosslink(nrid, pWiki, MC_NONE);
return nrid;
}
/*
** Output a selection box from which the user can select the
** wiki mimetype.
*/
void mimetype_option_menu(const char *zMimetype){
unsigned i;
@ <select name="mimetype" size="1">
for(i=0; i<count(azStyles); i+=3){
if( fossil_strcmp(zMimetype,azStyles[i])==0 ){
@ <option value="%s(azStyles[i])" selected>%s(azStyles[i+1])</option>
}else{
@ <option value="%s(azStyles[i])">%s(azStyles[i+1])</option>
}
}
@ </select>
}
/*
** Given a mimetype, return its common name.
*/
static const char *mimetype_common_name(const char *zMimetype){
int i;
for(i=4; i>=2; i-=2){
if( zMimetype && fossil_strcmp(zMimetype, azStyles[i])==0 ){
return azStyles[i+1];
}
}
return azStyles[1];
}
/*
** WEBPAGE: wikiedit
** URL: /wikiedit?name=PAGENAME
**
** Edit a wiki page.
*/
void wikiedit_page(void){
char *zTag;
int rid = 0;
int isSandbox;
Blob wiki;
Manifest *pWiki = 0;
const char *zPageName;
int n;
const char *z;
char *zBody = (char*)P("w");
const char *zMimetype = wiki_filter_mimetypes(P("mimetype"));
int isWysiwyg = P("wysiwyg")!=0;
int goodCaptcha = 1;
if( P("edit-wysiwyg")!=0 ){ isWysiwyg = 1; zBody = 0; }
if( P("edit-markup")!=0 ){ isWysiwyg = 0; zBody = 0; }
if( zBody ){
if( isWysiwyg ){
Blob body;
blob_zero(&body);
htmlTidy(zBody, &body);
zBody = blob_str(&body);
}else{
zBody = mprintf("%s", zBody);
}
}
login_check_credentials();
zPageName = PD("name","");
if( check_name(zPageName) ) return;
isSandbox = is_sandbox(zPageName);
if( isSandbox ){
if( !g.perm.WrWiki ){
login_needed(g.anon.WrWiki);
return;
}
if( zBody==0 ){
zBody = db_get("sandbox","");
zMimetype = db_get("sandbox-mimetype","text/x-fossil-wiki");
}
}else{
zTag = mprintf("wiki-%s", zPageName);
rid = db_int(0,
"SELECT rid FROM tagxref"
" WHERE tagid=(SELECT tagid FROM tag WHERE tagname=%Q)"
" ORDER BY mtime DESC", zTag
);
free(zTag);
if( (rid && !g.perm.WrWiki) || (!rid && !g.perm.NewWiki) ){
login_needed(rid ? g.anon.WrWiki : g.anon.NewWiki);
return;
}
if( zBody==0 && (pWiki = manifest_get(rid, CFTYPE_WIKI, 0))!=0 ){
zBody = pWiki->zWiki;
zMimetype = pWiki->zMimetype;
}
}
if( P("submit")!=0 && zBody!=0
&& (goodCaptcha = captcha_is_correct(0))
){
char *zDate;
Blob cksum;
blob_zero(&wiki);
db_begin_transaction();
if( isSandbox ){
db_set("sandbox",zBody,0);
db_set("sandbox-mimetype",zMimetype,0);
}else{
login_verify_csrf_secret();
zDate = date_in_standard_format("now");
blob_appendf(&wiki, "D %s\n", zDate);
free(zDate);
blob_appendf(&wiki, "L %F\n", zPageName);
if( fossil_strcmp(zMimetype,"text/x-fossil-wiki")!=0 ){
blob_appendf(&wiki, "N %s\n", zMimetype);
}
if( rid ){
char *zUuid = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", rid);
blob_appendf(&wiki, "P %s\n", zUuid);
free(zUuid);
}
if( !login_is_nobody() ){
blob_appendf(&wiki, "U %F\n", login_name());
}
blob_appendf(&wiki, "W %d\n%s\n", strlen(zBody), zBody);
md5sum_blob(&wiki, &cksum);
blob_appendf(&wiki, "Z %b\n", &cksum);
blob_reset(&cksum);
wiki_put(&wiki, 0, wiki_need_moderation(0));
}
db_end_transaction(0);
cgi_redirectf("wiki?name=%T", zPageName);
}
if( P("cancel")!=0 ){
cgi_redirectf("wiki?name=%T", zPageName);
return;
}
if( zBody==0 ){
zBody = mprintf("<i>Empty Page</i>");
}
style_set_current_page("%T?name=%T", g.zPath, zPageName);
style_header("Edit: %s", zPageName);
if( !goodCaptcha ){
@ <p class="generalError">Error: Incorrect security code.</p>
}
blob_zero(&wiki);
blob_append(&wiki, zBody, -1);
if( P("preview")!=0 ){
@ Preview:<hr />
wiki_render_by_mimetype(&wiki, zMimetype);
@ <hr />
blob_reset(&wiki);
}
for(n=2, z=zBody; z[0]; z++){
if( z[0]=='\n' ) n++;
}
if( n<20 ) n = 20;
if( n>30 ) n = 30;
if( !isWysiwyg ){
/* Traditional markup-only editing */
form_begin(0, "%R/wikiedit");
@ <div>Markup style:
mimetype_option_menu(zMimetype);
@ <br /><textarea name="w" class="wikiedit" cols="80"
@ rows="%d(n)" wrap="virtual">%h(zBody)</textarea>
@ <br />
if( db_get_boolean("wysiwyg-wiki", 0) ){
@ <input type="submit" name="edit-wysiwyg" value="Wysiwyg Editor"
@ onclick='return confirm("Switching to WYSIWYG-mode\nwill erase your markup\nedits. Continue?")' />
}
@ <input type="submit" name="preview" value="Preview Your Changes" />
}else{
/* Wysiwyg editing */
Blob html, temp;
form_begin("onsubmit='wysiwygSubmit()'", "%R/wikiedit");
@ <div>
@ <input type="hidden" name="wysiwyg" value="1" />
blob_zero(&temp);
wiki_convert(&wiki, &temp, 0);
blob_zero(&html);
htmlTidy(blob_str(&temp), &html);
blob_reset(&temp);
wysiwygEditor("w", blob_str(&html), 60, n);
blob_reset(&html);
@ <br />
@ <input type="submit" name="edit-markup" value="Markup Editor"
@ onclick='return confirm("Switching to markup-mode\nwill erase your WYSIWYG\nedits. Continue?")' />
}
login_insert_csrf_secret();
@ <input type="submit" name="submit" value="Apply These Changes" />
@ <input type="hidden" name="name" value="%h(zPageName)" />
@ <input type="submit" name="cancel" value="Cancel"
@ onclick='confirm("Abandon your changes?")' />
@ </div>
captcha_generate(0);
@ </form>
manifest_destroy(pWiki);
blob_reset(&wiki);
style_footer();
}
/*
** WEBPAGE: wikinew
** URL /wikinew
**
** Prompt the user to enter the name of a new wiki page. Then redirect
** to the wikiedit screen for that new page.
*/
void wikinew_page(void){
const char *zName;
const char *zMimetype;
login_check_credentials();
if( !g.perm.NewWiki ){
login_needed(g.anon.NewWiki);
return;
}
zName = PD("name","");
zMimetype = wiki_filter_mimetypes(P("mimetype"));
if( zName[0] && wiki_name_is_wellformed((const unsigned char *)zName) ){
if( fossil_strcmp(zMimetype,"text/x-fossil-wiki")==0
&& db_get_boolean("wysiwyg-wiki", 0)
){
cgi_redirectf("wikiedit?name=%T&wysiwyg=1", zName);
}else{
cgi_redirectf("wikiedit?name=%T&mimetype=%s", zName, zMimetype);
}
}
style_header("Create A New Wiki Page");
wiki_standard_submenu(W_ALL_BUT(W_NEW));
@ <p>Rules for wiki page names:</p>
well_formed_wiki_name_rules();
form_begin(0, "%R/wikinew");
@ <p>Name of new wiki page:
@ <input style="width: 35;" type="text" name="name" value="%h(zName)" /><br />
@ Markup style:
mimetype_option_menu("text/x-fossil-wiki");
@ <br /><input type="submit" value="Create" />
@ </p></form>
if( zName[0] ){
@ <p><span class="wikiError">
@ "%h(zName)" is not a valid wiki page name!</span></p>
}
style_footer();
}
/*
** Append the wiki text for an remark to the end of the given BLOB.
*/
static void appendRemark(Blob *p, const char *zMimetype){
char *zDate;
const char *zUser;
const char *zRemark;
char *zId;
zDate = db_text(0, "SELECT datetime('now')");
zRemark = PD("r","");
zUser = PD("u",g.zLogin);
if( fossil_strcmp(zMimetype, "text/x-fossil-wiki")==0 ){
zId = db_text(0, "SELECT lower(hex(randomblob(8)))");
blob_appendf(p, "\n\n<hr /><div id=\"%s\"><i>On %s UTC %h",
zId, zDate, login_name());
if( zUser[0] && fossil_strcmp(zUser,login_name()) ){
blob_appendf(p, " (claiming to be %h)", zUser);
}
blob_appendf(p, " added:</i><br />\n%s</div id=\"%s\">", zRemark, zId);
}else if( fossil_strcmp(zMimetype, "text/x-markdown")==0 ){
blob_appendf(p, "\n\n------\n*On %s UTC %h", zDate, login_name());
if( zUser[0] && fossil_strcmp(zUser,login_name()) ){
blob_appendf(p, " (claiming to be %h)", zUser);
}
blob_appendf(p, " added:*\n\n%s\n", zRemark);
}else{
blob_appendf(p, "\n\n------------------------------------------------\n"
"On %s UTC %s", zDate, login_name());
if( zUser[0] && fossil_strcmp(zUser,login_name()) ){
blob_appendf(p, " (claiming to be %s)", zUser);
}
blob_appendf(p, " added:\n\n%s\n", zRemark);
}
fossil_free(zDate);
}
/*
** WEBPAGE: wikiappend
** URL: /wikiappend?name=PAGENAME&mimetype=MIMETYPE
**
** Append text to the end of a wiki page.
*/
void wikiappend_page(void){
char *zTag;
int rid = 0;
int isSandbox;
const char *zPageName;
const char *zUser;
const char *zMimetype;
int goodCaptcha = 1;
const char *zFormat;
login_check_credentials();
zPageName = PD("name","");
zMimetype = wiki_filter_mimetypes(P("mimetype"));
if( check_name(zPageName) ) return;
isSandbox = is_sandbox(zPageName);
if( !isSandbox ){
zTag = mprintf("wiki-%s", zPageName);
rid = db_int(0,
"SELECT rid FROM tagxref"
" WHERE tagid=(SELECT tagid FROM tag WHERE tagname=%Q)"
" ORDER BY mtime DESC", zTag
);
free(zTag);
if( !rid ){
fossil_redirect_home();
return;
}
}
if( !g.perm.ApndWiki ){
login_needed(g.anon.ApndWiki);
return;
}
if( P("submit")!=0 && P("r")!=0 && P("u")!=0
&& (goodCaptcha = captcha_is_correct(0))
){
char *zDate;
Blob cksum;
Blob body;
Blob wiki;
Manifest *pWiki = 0;
blob_zero(&body);
if( isSandbox ){
blob_append(&body, db_get("sandbox",""), -1);
appendRemark(&body, zMimetype);
db_set("sandbox", blob_str(&body), 0);
}else{
login_verify_csrf_secret();
pWiki = manifest_get(rid, CFTYPE_WIKI, 0);
if( pWiki ){
blob_append(&body, pWiki->zWiki, -1);
manifest_destroy(pWiki);
}
blob_zero(&wiki);
db_begin_transaction();
zDate = date_in_standard_format("now");
blob_appendf(&wiki, "D %s\n", zDate);
blob_appendf(&wiki, "L %F\n", zPageName);
if( fossil_strcmp(zMimetype, "text/x-fossil-wiki")!=0 ){
blob_appendf(&wiki, "N %s\n", zMimetype);
}
if( rid ){
char *zUuid = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", rid);
blob_appendf(&wiki, "P %s\n", zUuid);
free(zUuid);
}
if( !login_is_nobody() ){
blob_appendf(&wiki, "U %F\n", login_name());
}
appendRemark(&body, zMimetype);
blob_appendf(&wiki, "W %d\n%s\n", blob_size(&body), blob_str(&body));
md5sum_blob(&wiki, &cksum);
blob_appendf(&wiki, "Z %b\n", &cksum);
blob_reset(&cksum);
wiki_put(&wiki, rid, wiki_need_moderation(0));
db_end_transaction(0);
}
cgi_redirectf("wiki?name=%T", zPageName);
}
if( P("cancel")!=0 ){
cgi_redirectf("wiki?name=%T", zPageName);
return;
}
style_set_current_page("%T?name=%T", g.zPath, zPageName);
style_header("Append Comment To: %s", zPageName);
if( !goodCaptcha ){
@ <p class="generalError">Error: Incorrect security code.</p>
}
if( P("preview")!=0 ){
Blob preview;
blob_zero(&preview);
appendRemark(&preview, zMimetype);
@ Preview:<hr />
wiki_render_by_mimetype(&preview, zMimetype);
@ <hr />
blob_reset(&preview);
}
zUser = PD("u", g.zLogin);
form_begin(0, "%R/wikiappend");
login_insert_csrf_secret();
@ <input type="hidden" name="name" value="%h(zPageName)" />
@ <input type="hidden" name="mimetype" value="%h(zMimetype)" />
@ Your Name:
@ <input type="text" name="u" size="20" value="%h(zUser)" /><br />
zFormat = mimetype_common_name(zMimetype);
@ Comment to append (formatted as %s(zFormat)):<br />
@ <textarea name="r" class="wikiedit" cols="80"
@ rows="10" wrap="virtual">%h(PD("r",""))</textarea>
@ <br />
@ <input type="submit" name="preview" value="Preview Your Comment" />
@ <input type="submit" name="submit" value="Append Your Changes" />
@ <input type="submit" name="cancel" value="Cancel" />
captcha_generate(0);
@ </form>
style_footer();
}
/*
** Name of the wiki history page being generated
*/
static const char *zWikiPageName;
/*
** Function called to output extra text at the end of each line in
** a wiki history listing.
*/
static void wiki_history_extra(int rid){
if( db_exists("SELECT 1 FROM tagxref WHERE rid=%d", rid) ){
@ %z(href("%R/wdiff?name=%t&a=%d",zWikiPageName,rid))[diff]</a>
}
}
/*
** WEBPAGE: whistory
** URL: /whistory?name=PAGENAME
**
** Show the complete change history for a single wiki page.
*/
void whistory_page(void){
Stmt q;
const char *zPageName;
login_check_credentials();
if( !g.perm.Hyperlink ){ login_needed(g.anon.Hyperlink); return; }
zPageName = PD("name","");
style_header("History Of %s", zPageName);
db_prepare(&q, "%s AND event.objid IN "
" (SELECT rid FROM tagxref WHERE tagid="
"(SELECT tagid FROM tag WHERE tagname='wiki-%q')"
" UNION SELECT attachid FROM attachment"
" WHERE target=%Q)"
"ORDER BY mtime DESC",
timeline_query_for_www(), zPageName, zPageName);
zWikiPageName = zPageName;
www_print_timeline(&q, TIMELINE_ARTID, 0, 0, 0, wiki_history_extra);
db_finalize(&q);
style_footer();
}
/*
** WEBPAGE: wdiff
** URL: /whistory?name=PAGENAME&a=RID1&b=RID2
**
** Show the difference between two wiki pages.
*/
void wdiff_page(void){
int rid1, rid2;
const char *zPageName;
Manifest *pW1, *pW2 = 0;
Blob w1, w2, d;
u64 diffFlags;
login_check_credentials();
rid1 = atoi(PD("a","0"));
if( !g.perm.Hyperlink ){ login_needed(g.anon.Hyperlink); return; }
if( rid1==0 ) fossil_redirect_home();
rid2 = atoi(PD("b","0"));
zPageName = PD("name","");
style_header("Changes To %s", zPageName);
if( rid2==0 ){
rid2 = db_int(0,
"SELECT objid FROM event JOIN tagxref ON objid=rid AND tagxref.tagid="
"(SELECT tagid FROM tag WHERE tagname='wiki-%q')"
" WHERE event.mtime<(SELECT mtime FROM event WHERE objid=%d)"
" ORDER BY event.mtime DESC LIMIT 1",
zPageName, rid1
);
}
pW1 = manifest_get(rid1, CFTYPE_WIKI, 0);
if( pW1==0 ) fossil_redirect_home();
blob_init(&w1, pW1->zWiki, -1);
blob_zero(&w2);
if( rid2 && (pW2 = manifest_get(rid2, CFTYPE_WIKI, 0))!=0 ){
blob_init(&w2, pW2->zWiki, -1);
}
blob_zero(&d);
diffFlags = construct_diff_flags(1);
text_diff(&w2, &w1, &d, 0, diffFlags | DIFF_HTML | DIFF_LINENO);
@ <pre class="udiff">
@ %s(blob_str(&d))
@ <pre>
manifest_destroy(pW1);
manifest_destroy(pW2);
style_footer();
}
/*
** prepare()s pStmt with a query requesting:
**
** - wiki page name
** - tagxref (whatever that really is!)
**
** Used by wcontent_page() and the JSON wiki code.
*/
void wiki_prepare_page_list( Stmt * pStmt ){
db_prepare(pStmt,
"SELECT"
" substr(tagname, 6) as name,"
" (SELECT value FROM tagxref WHERE tagid=tag.tagid"
" ORDER BY mtime DESC) as tagXref"
" FROM tag WHERE tagname GLOB 'wiki-*'"
" ORDER BY lower(tagname) /*sort*/"
);
}
/*
** WEBPAGE: wcontent
**
** all=1 Show deleted pages
**
** List all available wiki pages with date created and last modified.
*/
void wcontent_page(void){
Stmt q;
int showAll = P("all")!=0;
login_check_credentials();
if( !g.perm.RdWiki ){ login_needed(g.anon.RdWiki); return; }
style_header("Available Wiki Pages");
if( showAll ){
style_submenu_element("Active", "%s/wcontent", g.zTop);
}else{
style_submenu_element("All", "%s/wcontent?all=1", g.zTop);
}
wiki_standard_submenu(W_ALL_BUT(W_LIST));
@ <ul>
wiki_prepare_page_list(&q);
while( db_step(&q)==SQLITE_ROW ){
const char *zName = db_column_text(&q, 0);
int size = db_column_int(&q, 1);
if( size>0 ){
@ <li>%z(href("%R/wiki?name=%T",zName))%h(zName)</a></li>
}else if( showAll ){
@ <li>%z(href("%R/wiki?name=%T",zName))<s>%h(zName)</s></a></li>
}
}
db_finalize(&q);
@ </ul>
style_footer();
}
/*
** WEBPAGE: wfind
**
** URL: /wfind?title=TITLE
** List all wiki pages whose titles contain the search text
*/
void wfind_page(void){
Stmt q;
const char *zTitle;
login_check_credentials();
if( !g.perm.RdWiki ){ login_needed(g.anon.RdWiki); return; }
zTitle = PD("title","*");
style_header("Wiki Pages Found");
@ <ul>
db_prepare(&q,
"SELECT substr(tagname, 6, 1000) FROM tag WHERE tagname like 'wiki-%%%q%%'"
" ORDER BY lower(tagname) /*sort*/" ,
zTitle);
while( db_step(&q)==SQLITE_ROW ){
const char *zName = db_column_text(&q, 0);
@ <li>%z(href("%R/wiki?name=%T",zName))%h(zName)</a></li>
}
db_finalize(&q);
@ </ul>
style_footer();
}
/*
** Add a new wiki page to the repository. The page name is
** given by the zPageName parameter. rid must be zero to create
** a new page otherwise the page identified by rid is updated.
**
** The content of the new page is given by the blob pContent.
**
** zMimeType specifies the N-card for the wiki page. If it is 0,
** empty, or "text/x-fossil-wiki" (the default format) then it is
** ignored.
*/
int wiki_cmd_commit(const char *zPageName, int rid, Blob *pContent,
const char *zMimeType, int localUser){
Blob wiki; /* Wiki page content */
Blob cksum; /* wiki checksum */
char *zDate; /* timestamp */
char *zUuid; /* uuid for rid */
blob_zero(&wiki);
zDate = date_in_standard_format("now");
blob_appendf(&wiki, "D %s\n", zDate);
free(zDate);
blob_appendf(&wiki, "L %F\n", zPageName );
if( zMimeType && *zMimeType
&& 0!=fossil_strcmp(zMimeType,"text/x-fossil-wiki") ){
blob_appendf(&wiki, "N %F\n", zMimeType);
}
if( rid ){
zUuid = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", rid);
blob_appendf(&wiki, "P %s\n", zUuid);
free(zUuid);
}
user_select();
if( !login_is_nobody() ){
blob_appendf(&wiki, "U %F\n", login_name());
}
blob_appendf( &wiki, "W %d\n%s\n", blob_size(pContent),
blob_str(pContent) );
md5sum_blob(&wiki, &cksum);
blob_appendf(&wiki, "Z %b\n", &cksum);
blob_reset(&cksum);
db_begin_transaction();
wiki_put(&wiki, 0, wiki_need_moderation(localUser));
db_end_transaction(0);
return 1;
}
/*
** Determine the rid for a tech note given either its id or its
** timestamp. Returns 0 if there is no such item and -1 if the details
** are ambiguous and could refer to multiple items.
*/
int wiki_technote_to_rid(const char *zETime) {
int rid=0; /* Artifact ID of the tech note */
int nETime = strlen(zETime);
Stmt q;
if( nETime>=4 && nETime<=HNAME_MAX && validate16(zETime, nETime) ){
char zUuid[HNAME_MAX+1];
memcpy(zUuid, zETime, nETime+1);
canonical16(zUuid, nETime);
db_prepare(&q,
"SELECT e.objid"
" FROM event e, tag t"
" WHERE e.type='e' AND e.tagid IS NOT NULL AND t.tagid=e.tagid"
" AND t.tagname GLOB 'event-%q*'",
zUuid
);
if( db_step(&q)==SQLITE_ROW ){
rid = db_column_int(&q, 0);
if( db_step(&q)==SQLITE_ROW ) rid = -1;
}
db_finalize(&q);
}
if (!rid) {
if (strlen(zETime)>4) {
rid = db_int(0, "SELECT objid"
" FROM event"
" WHERE datetime(mtime)=datetime('%q')"
" AND type='e'"
" AND tagid IS NOT NULL"
" ORDER BY objid DESC LIMIT 1",
zETime);
}
}
return rid;
}
/*
** COMMAND: wiki*
**
** Usage: %fossil wiki (export|create|commit|list) WikiName
**
** Run various subcommands to work with wiki entries or tech notes.
**
** %fossil wiki export PAGENAME ?FILE?
** %fossil wiki export ?FILE? -t|--technote DATETIME|TECHNOTE-ID
**
** Sends the latest version of either a wiki page or of a tech note
** to the given file or standard output.
** If PAGENAME is provided, the wiki page will be output. For
** a tech note either DATETIME or TECHNOTE-ID must be specified. If
** DATETIME is used, the most recently modified tech note with that
** DATETIME will be sent.
**
** %fossil wiki (create|commit) PAGENAME ?FILE? ?OPTIONS?
**
** Create a new or commit changes to an existing wiki page or
** technote from FILE or from standard input. PAGENAME is the
** name of the wiki entry or the timeline comment of the
** technote.
**
** Options:
** -M|--mimetype TEXT-FORMAT The mime type of the update.
** Defaults to the type used by
** the previous version of the
** page, or text/x-fossil-wiki.
** Valid values are: text/x-fossil-wiki,
** text/markdown and text/plain. fossil,
** markdown or plain can be specified as
** synonyms of these values.
** -t|--technote DATETIME Specifies the timestamp of
** the technote to be created or
** updated. When updating a tech note
** the most recently modified tech note
** with the specified timestamp will be
** updated.
** -t|--technote TECHNOTE-ID Specifies the technote to be
** updated by its technote id.
** --technote-tags TAGS The set of tags for a technote.
** --technote-bgcolor COLOR The color used for the technote
** on the timeline.
**
** %fossil wiki list ?OPTIONS?
** %fossil wiki ls ?OPTIONS?
**
** Lists all wiki entries, one per line, ordered
** case-insensitively by name.
**
** Options:
** -t|--technote Technotes will be listed instead of
** pages. The technotes will be in order
** of timestamp with the most recent
** first.
** -s|--show-technote-ids The id of the tech note will be listed
** along side the timestamp. The tech note
** id will be the first word on each line.
** This option only applies if the
** --technote option is also specified.
**
** DATETIME may be "now" or "YYYY-MM-DDTHH:MM:SS.SSS". If in
** year-month-day form, it may be truncated, the "T" may be replaced by
** a space, and it may also name a timezone offset from UTC as "-HH:MM"
** (westward) or "+HH:MM" (eastward). Either no timezone suffix or "Z"
** means UTC.
**
*/
void wiki_cmd(void){
int n;
db_find_and_open_repository(0, 0);
if( g.argc<3 ){
goto wiki_cmd_usage;
}
n = strlen(g.argv[2]);
if( n==0 ){
goto wiki_cmd_usage;
}
if( strncmp(g.argv[2],"export",n)==0 ){
const char *zPageName; /* Name of the wiki page to export */
const char *zFile; /* Name of the output file (0=stdout) */
const char *zETime; /* The name of the technote to export */
int rid; /* Artifact ID of the wiki page */
int i; /* Loop counter */
char *zBody = 0; /* Wiki page content */
Blob body; /* Wiki page content */
Manifest *pWiki = 0; /* Parsed wiki page content */
zETime = find_option("technote","t",1);
if( !zETime ){
if( (g.argc!=4) && (g.argc!=5) ){
usage("export PAGENAME ?FILE?");
}
zPageName = g.argv[3];
rid = db_int(0, "SELECT x.rid FROM tag t, tagxref x"
" WHERE x.tagid=t.tagid AND t.tagname='wiki-%q'"
" ORDER BY x.mtime DESC LIMIT 1",
zPageName
);
if( (pWiki = manifest_get(rid, CFTYPE_WIKI, 0))!=0 ){
zBody = pWiki->zWiki;
}
if( zBody==0 ){
fossil_fatal("wiki page [%s] not found",zPageName);
}
zFile = (g.argc==4) ? "-" : g.argv[4];
}else{
if( (g.argc!=3) && (g.argc!=4) ){
usage("export ?FILE? --technote DATETIME|TECHNOTE-ID");
}
rid = wiki_technote_to_rid(zETime);
if ( rid==-1 ){
fossil_fatal("ambiguous tech note id: %s", zETime);
}
if( (pWiki = manifest_get(rid, CFTYPE_EVENT, 0))!=0 ){
zBody = pWiki->zWiki;
}
if( zBody==0 ){
fossil_fatal("technote [%s] not found",zETime);
}
zFile = (g.argc==3) ? "-" : g.argv[3];
}
for(i=strlen(zBody); i>0 && fossil_isspace(zBody[i-1]); i--){}
zBody[i] = 0;
blob_init(&body, zBody, -1);
blob_append(&body, "\n", 1);
blob_write_to_file(&body, zFile);
blob_reset(&body);
manifest_destroy(pWiki);
return;
}else if( strncmp(g.argv[2],"commit",n)==0
|| strncmp(g.argv[2],"create",n)==0 ){
const char *zPageName; /* page name */
Blob content; /* Input content */
int rid = 0;
Manifest *pWiki = 0; /* Parsed wiki page content */
const char *zMimeType = find_option("mimetype", "M", 1);
const char *zETime = find_option("technote", "t", 1);
const char *zTags = find_option("technote-tags", NULL, 1);
const char *zClr = find_option("technote-bgcolor", NULL, 1);
if( g.argc!=4 && g.argc!=5 ){
usage("commit|create PAGENAME ?FILE? [--mimetype TEXT-FORMAT]"
" [--technote DATETIME] [--technote-tags TAGS]"
" [--technote-bgcolor COLOR]");
}
zPageName = g.argv[3];
if( g.argc==4 ){
blob_read_from_channel(&content, stdin, -1);
}else{
blob_read_from_file(&content, g.argv[4], ExtFILE);
}
if( !zMimeType || !*zMimeType ){
/* Try to deduce the mime type based on the prior version. */
if ( !zETime ){
rid = db_int(0, "SELECT x.rid FROM tag t, tagxref x"
" WHERE x.tagid=t.tagid AND t.tagname='wiki-%q'"
" ORDER BY x.mtime DESC LIMIT 1",
zPageName
);
if( rid>0 && (pWiki = manifest_get(rid, CFTYPE_WIKI, 0))!=0
&& (pWiki->zMimetype && *pWiki->zMimetype) ){
zMimeType = pWiki->zMimetype;
}
}else{
rid = wiki_technote_to_rid(zETime);
if( rid>0 && (pWiki = manifest_get(rid, CFTYPE_EVENT, 0))!=0
&& (pWiki->zMimetype && *pWiki->zMimetype) ){
zMimeType = pWiki->zMimetype;
}
}
}else{
zMimeType = wiki_filter_mimetypes(zMimeType);
}
if( g.argv[2][1]=='r' && rid>0 ){
if ( !zETime ){
fossil_fatal("wiki page %s already exists", zPageName);
}else{
/* Creating a tech note with same timestamp is permitted
and should create a new tech note */
rid = 0;
}
}else if( g.argv[2][1]=='o' && rid == 0 ){
if ( !zETime ){
fossil_fatal("no such wiki page: %s", zPageName);
}else{
fossil_fatal("no such tech note: %s", zETime);
}
}
if( !zETime ){
wiki_cmd_commit(zPageName, rid, &content, zMimeType, 1);
if( g.argv[2][1]=='r' ){
fossil_print("Created new wiki page %s.\n", zPageName);
}else{
fossil_print("Updated wiki page %s.\n", zPageName);
}
}else{
if( rid != -1 ){
char *zMETime; /* Normalized, mutable version of zETime */
zMETime = db_text(0, "SELECT coalesce(datetime(%Q),datetime('now'))",
zETime);
event_cmd_commit(zMETime, rid, &content, zMimeType, zPageName,
zTags, zClr);
if( g.argv[2][1]=='r' ){
fossil_print("Created new tech note %s.\n", zMETime);
}else{
fossil_print("Updated tech note %s.\n", zMETime);
}
free(zMETime);
}else{
fossil_fatal("ambiguous tech note id: %s", zETime);
}
}
manifest_destroy(pWiki);
blob_reset(&content);
}else if( strncmp(g.argv[2],"delete",n)==0 ){
if( g.argc!=5 ){
usage("delete PAGENAME");
}
fossil_fatal("delete not yet implemented.");
}else if(( strncmp(g.argv[2],"list",n)==0 )
|| ( strncmp(g.argv[2],"ls",n)==0 )){
Stmt q;
int showIds = 0;
if ( !find_option("technote","t",0) ){
db_prepare(&q,
"SELECT substr(tagname, 6) FROM tag WHERE tagname GLOB 'wiki-*'"
" ORDER BY lower(tagname) /*sort*/"
);
}else{
showIds = find_option("show-technote-ids","s",0)!=0;
db_prepare(&q,
"SELECT datetime(e.mtime), substr(t.tagname,7)"
" FROM event e, tag t"
" WHERE e.type='e'"
" AND e.tagid IS NOT NULL"
" AND t.tagid=e.tagid"
" ORDER BY e.mtime DESC /*sort*/"
);
}
while( db_step(&q)==SQLITE_ROW ){
const char *zName = db_column_text(&q, 0);
if( showIds ){
const char *zUuid = db_column_text(&q, 1);
fossil_print("%s ",zUuid);
}
fossil_print( "%s\n",zName);
}
db_finalize(&q);
}else{
goto wiki_cmd_usage;
}
return;
wiki_cmd_usage:
usage("export|create|commit|list ...");
}
/*
** COMMAND: test-markdown-render
**
** Usage: %fossil test-markdown-render FILE
**
** Render markdown wiki from FILE to stdout.
**
*/
void test_markdown_render(void){
Blob in, out;
verify_all_options();
if( g.argc!=3 ) usage("FILE");
blob_zero(&out);
blob_read_from_file(&in, g.argv[2], ExtFILE);
markdown_to_html(&in, 0, &out);
blob_write_to_file(&out, "-");
}
|