Fossil

Diff
Login

Differences From Artifact [2bdc0de1c6]:

To Artifact [7252277a58]:


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







-
-
+
+

-
-
-
-
+
+
+
+
+

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



+

+
-
+



-
+





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

+

-
+
-
-
+







+
-
-
+
+

-
+



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



-
-
-
-
-
+
-
-
-


-
+


-
-
+
+

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

-
-
+
+
+


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


-
-
+
-
+
+

-
-
-
+









-
+







  for(i=2; i<g.argc; i++){
    fossil_print("%-20s -> %s\n", g.argv[i], mimetype_from_name(g.argv[i]));
  }
}

/*
** WEBPAGE: doc
** URL: /doc?name=BASELINE/PATH
** URL: /doc/BASELINE/PATH
** URL: /doc?name=CHECKIN/FILE
** URL: /doc/CHECKIN/FILE
**
** BASELINE can be either a baseline uuid prefix or magic words "tip"
** to mean the most recently checked in baseline or "ckout" to mean the
** content of the local checkout, if any.  PATH is the relative pathname
** of some file.  This method returns the file content.
** CHECKIN can be either tag or SHA1 hash or timestamp identifying a
** particular check, or the name of a branch (meaning the most recent
** check-in on that branch) or one of various magic words:
**
**     "tip"      means the most recent check-in
**
** If PATH matches the patterns *.wiki or *.txt then formatting content
** is added before returning the file.  For all other names, the content
** is returned straight without any interpretation or processing.
**     "ckout"    means the current check-out, if the server is run from
**                within a check-out, otherwise it is the same as "tip"
**
** FILE is the name of a file to delivered up as a webpage.  FILE is relative
** to the root of the source tree of the repository. The FILE must
** be a part of CHECKIN, except when CHECKIN=="ckout" when FILE is read
** directly from disk and need not be a managed file.
**
** The "ckout" CHECKIN is intended for development - to provide a mechanism
** for looking at what a file will look like using the /doc webpage after
** it gets checked in.
**
** The file extension is used to decide how to render the file.
*/
void doc_page(void){
  const char *zName;                /* Argument to the /doc page */
  const char *zOrigName;            /* Original document name */
  const char *zMime;                /* Document MIME type */
  char *zCheckin;                   /* The checkin holding the document */
  int vid = 0;                      /* Artifact of baseline */
  int vid = 0;                      /* Artifact of checkin */
  int rid = 0;                      /* Artifact of file */
  int i;                            /* Loop counter */
  Blob filebody;                    /* Content of the documentation file */
  char zBaseline[UUID_SIZE+1];      /* Baseline UUID */
  int nMiss = 0;                    /* Failed attempts to find the document */

  login_check_credentials();
  if( !g.perm.Read ){ login_needed(); return; }
  zName = PD("name", "tip/index.wiki");
  for(i=0; zName[i] && zName[i]!='/'; i++){}
  zCheckin = mprintf("%.*s", i, zName);
  if( zName[i]==0 || i>UUID_SIZE ){
    zName = "index.html";
  if( zName[i]==0 ){
    zName = "index.wiki";
    goto doc_not_found;
  }
  }else{
  g.zPath = mprintf("%s/%s", g.zPath, zName);
  memcpy(zBaseline, zName, i);
  zBaseline[i] = 0;
  zName += i;
    zName += i;
  }
  while( zName[0]=='/' ){ zName++; }
  g.zPath = mprintf("%s/%s/%s", g.zPath, zCheckin, zName);
  if( !file_is_simple_pathname(zName, 1) ){
    int n = strlen(zName);
    if( sqlite3_strglob("*/", zName)==0 ){
    if( n>0 && zName[n-1]=='/' ){
      zName = mprintf("%sindex.html", zName);
      zName = mprintf("%sindex.wiki", zName);
      if( !file_is_simple_pathname(zName, 1) ){
        goto doc_not_found;
      }
    }else{
      goto doc_not_found;
    }
  }
  zOrigName = zName;
  if( fossil_strcmp(zBaseline,"ckout")==0 && db_open_local(0)==0 ){
    sqlite3_snprintf(sizeof(zBaseline), zBaseline, "tip");
  if( fossil_strcmp(zCheckin,"ckout")==0 && db_open_local(0)==0 ){
    sqlite3_snprintf(sizeof(zCheckin), zCheckin, "tip");
  }
  if( fossil_strcmp(zBaseline,"ckout")==0 ){
  if( fossil_strcmp(zCheckin,"ckout")==0 ){
    /* Read from the local checkout */
    char *zFullpath;
    db_must_be_within_tree();
    while( rid==0 && nMiss<2 ){
    zFullpath = mprintf("%s/%s", g.zLocalRoot, zName);
    if( !file_isfile(zFullpath) ){
      zFullpath = mprintf("%s/%s", g.zLocalRoot, zName);
      if( file_isfile(zFullpath)
      goto doc_not_found;
    }
    if( blob_read_from_file(&filebody, zFullpath)<0 ){
       && blob_read_from_file(&filebody, zFullpath)<0 ){
        rid = 1;  /* Fake RID just to get the loop to end */
      }
      fossil_free(zFullpath);
      goto doc_not_found;
      if( rid ) break;
      nMiss++;
      zName = "404.md";
    }
  }else{
    db_begin_transaction();
    if( fossil_strcmp(zBaseline,"tip")==0 ){
      vid = db_int(0, "SELECT objid FROM event WHERE type='ci'"
                      " ORDER BY mtime DESC LIMIT 1");
    }else{
      vid = name_to_typed_rid(zBaseline, "ci");
    vid = name_to_typed_rid(zCheckin, "ci");
    }

    /* Create the baseline cache if it does not already exist */
    db_multi_exec(
      "CREATE TABLE IF NOT EXISTS vcache(\n"
      "  vid INTEGER,         -- baseline ID\n"
      "  vid INTEGER,         -- checkin ID\n"
      "  fname TEXT,          -- filename\n"
      "  rid INTEGER,         -- artifact ID\n"
      "  UNIQUE(vid,fname,rid)\n"
      ")"
      "  PRIMARY KEY(vid,fname)\n"
      ") WITHOUT ROWID"
    );



    /* Check to see if the documentation file artifact ID is contained
    ** in the baseline cache */
    rid = db_int(0, "SELECT rid FROM vcache"
                    " WHERE vid=%d AND fname=%Q", vid, zName);
    if( rid==0 && db_exists("SELECT 1 FROM vcache WHERE vid=%d", vid) ){
    if( !db_exists("SELECT 1 FROM vcache WHERE vid=%d", vid) ){
      goto doc_not_found;
    }

      db_multi_exec(
    if( rid==0 ){
      Stmt s;
      Manifest *pM;
      ManifestFile *pFile;

      /* Add the vid baseline to the cache */
      if( db_int(0, "SELECT count(*) FROM vcache")>10000 ){
        db_multi_exec("DELETE FROM vcache");
        "DELETE FROM vcache;\n"
      }
      pM = manifest_get(vid, CFTYPE_MANIFEST, 0);
      if( pM==0 ){
        goto doc_not_found;
      }
      db_prepare(&s,
        "CREATE VIRTUAL TABLE temp.foci USING files_of_checkin;\n"
        "INSERT INTO vcache(vid,fname,rid)"
        " SELECT %d, :fname, rid FROM blob"
        "  WHERE uuid=:uuid",
        "  SELECT checkinID, filename, blob.rid FROM foci, blob"
        "   WHERE blob.uuid=foci.uuid"
        "     AND foci.checkinID=%d;",
        vid
      );
      manifest_file_rewind(pM);
      while( (pFile = manifest_file_next(pM,0))!=0 ){
        db_bind_text(&s, ":fname", pFile->zName);
        db_bind_text(&s, ":uuid", pFile->zUuid);
        db_step(&s);
        db_reset(&s);
      }
    }
      db_finalize(&s);
      manifest_destroy(pM);

    while( rid==0 && nMiss<2 ){
      /* Try again to find the file */
      rid = db_int(0, "SELECT rid FROM vcache"
                      " WHERE vid=%d AND fname=%Q", vid, zName);
    }
    if( rid==0 ){
      if( rid ) break;
      goto doc_not_found;
      nMiss++;
      zName = "404.md";
    }

    /* Get the file content */
    if( content_get(rid, &filebody)==0 ){
    if( rid==0 || content_get(rid, &filebody)==0 ){
      goto doc_not_found;
    }
    db_end_transaction(0);
  }
  blob_to_utf8_no_bom(&filebody, 0);

  /* The file is now contained in the filebody blob.  Deliver the
  ** file to the user
  */
  zMime = P("mimetype");
  zMime = nMiss==0 ? P("mimetype") : 0;
  if( zMime==0 ){
    zMime = mimetype_from_name(zName);
  }
  Th_Store("doc_name", zName);
  Th_Store("doc_version", db_text(0, "SELECT '[' || substr(uuid,1,10) || ']'"
                                     "  FROM blob WHERE rid=%d", vid));
  Th_Store("doc_date", db_text(0, "SELECT datetime(mtime) FROM event"
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
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







-
+




















+


-

+

+
-
-
+
+
+
+
+







  }else if( fossil_strcmp(zMime, "text/x-markdown")==0 ){
    Blob title = BLOB_INITIALIZER;
    Blob tail = BLOB_INITIALIZER;
    markdown_to_html(&filebody, &title, &tail);
    if( blob_size(&title)>0 ){
      style_header("%s", blob_str(&title));
    }else{
      style_header("Documentation");
      style_header("%s", nMiss?"Not Found":"Documentation");
    }
    blob_append(cgi_output_blob(), blob_buffer(&tail), blob_size(&tail));
    style_footer();
  }else if( fossil_strcmp(zMime, "text/plain")==0 ){
    style_header("Documentation");
    @ <blockquote><pre>
    @ %h(blob_str(&filebody))
    @ </pre></blockquote>
    style_footer();
#ifdef FOSSIL_ENABLE_TH1_DOCS
  }else if( db_get_boolean("th1-docs", 0) &&
            fossil_strcmp(zMime, "application/x-th1")==0 ){
    style_header("%h", zName);
    Th_Render(blob_str(&filebody));
    style_footer();
#endif
  }else{
    cgi_set_content_type(zMime);
    cgi_set_content(&filebody);
  }
  if( nMiss ) cgi_set_status(404, "Not Found");
  return;

doc_not_found:
  /* Jump here when unable to locate the document */
doc_not_found:
  db_end_transaction(0);
  cgi_set_status(404, "Not Found");
  style_header("Document Not Found");
  @ <p>No such document: %h(zName)</p>
  style_header("Not Found");
  @ <p>Document %h(zOrigName) not found
  if( fossil_strcmp(zCheckin,"ckout")!=0 ){
    @ in %z(href("%R/tree?ci=%T",zCheckin))%h(zCheckin)</a>
  }
  style_footer();
  return;
}

/*
** The default logo.
*/