Artifact
0e526e76713a0463dbf098ef12d10ff4d56ec2d7:
- Executable file
extroot/trees
— part of check-in
[cb3512ec8a]
at
2021-10-19 17:52:10
on branch trunk
— Add GitHub trees query
(user:
mario
size: 1465)
0000: 23 21 2f 75 73 72 2f 62 69 6e 2f 70 68 70 2d 63 #!/usr/bin/php-c
0010: 67 69 20 2d 64 63 67 69 2e 66 6f 72 63 65 5f 72 gi -dcgi.force_r
0020: 65 64 69 72 65 63 74 3d 30 0a 3c 3f 70 68 70 0a edirect=0.<?php.
0030: 23 20 65 6e 63 6f 64 69 6e 67 3a 20 75 74 66 2d # encoding: utf-
0040: 38 0a 23 20 61 70 69 3a 20 63 67 69 0a 23 20 74 8.# api: cgi.# t
0050: 79 70 65 3a 20 6f 75 74 70 75 74 0a 23 20 63 61 ype: output.# ca
0060: 74 65 67 6f 72 79 3a 20 61 70 69 0a 23 20 74 69 tegory: api.# ti
0070: 74 6c 65 3a 20 74 72 65 65 73 20 41 50 49 0a 23 tle: trees API.#
0080: 20 64 65 73 63 72 69 70 74 69 6f 6e 3a 20 53 69 description: Si
0090: 6d 75 6c 61 74 65 20 47 69 74 48 75 62 20 64 69 mulate GitHub di
00a0: 72 65 63 74 6f 72 79 20 6c 69 73 74 69 6e 67 20 rectory listing
00b0: 72 65 73 70 6f 6e 73 65 0a 23 20 76 65 72 73 69 response.# versi
00c0: 6f 6e 3a 20 30 2e 31 0a 23 20 73 74 61 74 65 3a on: 0.1.# state:
00d0: 20 62 65 74 61 0a 23 20 64 65 70 65 6e 64 73 3a beta.# depends:
00e0: 20 70 68 70 3a 73 71 6c 69 74 65 0a 23 20 63 6f php:sqlite.# co
00f0: 6e 66 69 67 3a 20 2d 0a 23 0a 23 20 41 6c 77 61 nfig: -.#.# Alwa
0100: 79 73 20 6f 75 74 70 75 74 73 20 63 6f 6d 70 6c ys outputs compl
0110: 65 74 65 20 66 69 6c 65 20 6c 69 73 74 2c 20 74 ete file list, t
0120: 72 75 6e 6b 20 6f 6e 6c 79 2e 0a 23 0a 0a 0a 23 runk only..#...#
0130: 2d 2d 20 69 6e 69 74 0a 69 6e 63 6c 75 64 65 28 -- init.include(
0140: 22 2e 2f 66 6f 73 73 69 6c 5f 63 6f 6d 6d 6f 6e "./fossil_common
0150: 2e 70 68 70 22 29 3b 0a 23 24 66 6e 20 3d 20 6c .php");.#$fn = l
0160: 74 72 69 6d 28 24 5f 53 45 52 56 45 52 5b 22 50 trim($_SERVER["P
0170: 41 54 48 5f 49 4e 46 4f 22 5d 2c 20 22 2f 22 29 ATH_INFO"], "/")
0180: 3b 0a 23 70 72 69 6e 74 5f 72 28 24 5f 53 45 52 ;.#print_r($_SER
0190: 56 45 52 29 3b 0a 0a 0a 23 2d 2d 20 73 65 61 72 VER);...#-- sear
01a0: 63 68 0a 66 75 6e 63 74 69 6f 6e 20 66 69 6c 65 ch.function file
01b0: 5f 6c 69 73 74 28 29 20 7b 0a 20 20 20 20 72 65 _list() {. re
01c0: 74 75 72 6e 20 64 62 28 22 0a 20 20 20 20 20 20 turn db(".
01d0: 20 53 45 4c 45 43 54 20 75 75 69 64 2c 20 6e 61 SELECT uuid, na
01e0: 6d 65 2c 20 6d 70 65 72 6d 2c 20 73 69 7a 65 2c me, mperm, size,
01f0: 20 4d 41 58 28 72 69 64 29 0a 20 20 20 20 20 20 MAX(rid).
0200: 20 46 52 4f 4d 20 62 6c 6f 62 20 4c 45 46 54 20 FROM blob LEFT
0210: 4a 4f 49 4e 20 6d 6c 69 6e 6b 20 4f 4e 20 62 6c JOIN mlink ON bl
0220: 6f 62 2e 72 69 64 3d 6d 6c 69 6e 6b 2e 66 69 64 ob.rid=mlink.fid
0230: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
0240: 20 20 4c 45 46 54 20 4a 4f 49 4e 20 66 69 6c 65 LEFT JOIN file
0250: 6e 61 6d 65 20 4f 4e 20 6d 6c 69 6e 6b 2e 66 6e name ON mlink.fn
0260: 69 64 3d 66 69 6c 65 6e 61 6d 65 2e 66 6e 69 64 id=filename.fnid
0270: 0a 20 20 20 20 20 20 20 47 52 4f 55 50 20 42 59 . GROUP BY
0280: 20 6e 61 6d 65 0a 20 20 20 20 20 20 20 4f 52 44 name. ORD
0290: 45 52 20 42 59 20 6e 61 6d 65 0a 20 20 20 20 20 ER BY name.
02a0: 20 20 2d 2d 4f 52 44 45 52 20 42 59 20 72 69 64 --ORDER BY rid
02b0: 20 44 45 53 43 0a 20 20 20 20 22 29 3b 0a 7d 0a DESC. ");.}.
02c0: 66 75 6e 63 74 69 6f 6e 20 74 72 65 65 73 28 29 function trees()
02d0: 20 7b 0a 20 20 20 20 24 72 20 3d 20 5b 5d 3b 0a {. $r = [];.
02e0: 20 20 20 20 66 6f 72 65 61 63 68 28 66 69 6c 65 foreach(file
02f0: 5f 6c 69 73 74 28 29 20 61 73 20 24 66 29 20 7b _list() as $f) {
0300: 0a 20 20 20 20 20 20 20 20 69 66 20 28 65 6d 70 . if (emp
0310: 74 79 28 24 66 5b 22 6e 61 6d 65 22 5d 29 29 20 ty($f["name"]))
0320: 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 6f {. co
0330: 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 ntinue;.
0340: 7d 0a 20 20 20 20 20 20 20 20 24 72 5b 5d 20 3d }. $r[] =
0350: 20 5b 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 [. "
0360: 70 61 74 68 22 20 3d 3e 20 24 66 5b 22 6e 61 6d path" => $f["nam
0370: 65 22 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 e"],.
0380: 20 22 6d 6f 64 65 22 20 3d 3e 20 64 65 63 6f 63 "mode" => decoc
0390: 74 28 24 66 5b 22 6d 70 65 72 6d 22 5d 29 2c 0a t($f["mperm"]),.
03a0: 20 20 20 20 20 20 20 20 20 20 20 20 22 74 79 70 "typ
03b0: 65 22 20 3d 3e 20 22 62 6c 6f 62 22 2c 0a 20 20 e" => "blob",.
03c0: 20 20 20 20 20 20 20 20 20 20 22 73 68 61 22 20 "sha"
03d0: 3d 3e 20 24 66 5b 22 75 75 69 64 22 5d 2c 0a 20 => $f["uuid"],.
03e0: 20 20 20 20 20 20 20 20 20 20 20 22 73 69 7a 65 "size
03f0: 22 20 3d 3e 20 24 66 5b 22 73 69 7a 65 22 5d 2c " => $f["size"],
0400: 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 75 72 . "ur
0410: 6c 22 20 3d 3e 20 22 24 5f 53 45 52 56 45 52 5b l" => "$_SERVER[
0420: 46 4f 53 53 49 4c 5f 53 45 4c 46 5d 72 61 77 2f FOSSIL_SELF]raw/
0430: 24 66 5b 75 75 69 64 5d 3f 61 74 3d 24 66 5b 6e $f[uuid]?at=$f[n
0440: 61 6d 65 5d 22 0a 20 20 20 20 20 20 20 20 5d 3b ame]". ];
0450: 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 . }. retur
0460: 6e 20 24 72 3b 0a 7d 0a 66 75 6e 63 74 69 6f 6e n $r;.}.function
0470: 20 6c 61 73 74 5f 75 75 69 64 28 29 20 7b 0a 20 last_uuid() {.
0480: 20 20 20 72 65 74 75 72 6e 20 64 62 28 22 53 45 return db("SE
0490: 4c 45 43 54 20 75 75 69 64 20 46 52 4f 4d 20 62 LECT uuid FROM b
04a0: 6c 6f 62 20 4f 52 44 45 52 20 42 59 20 72 69 64 lob ORDER BY rid
04b0: 20 44 45 53 43 20 4c 49 4d 49 54 20 31 22 29 5b DESC LIMIT 1")[
04c0: 30 5d 5b 22 75 75 69 64 22 5d 3b 0a 7d 0a 0a 23 0]["uuid"];.}..#
04d0: 2d 2d 20 6f 75 74 70 75 74 0a 68 65 61 64 65 72 -- output.header
04e0: 28 22 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 ("Content-Type:
04f0: 61 70 70 6c 69 63 61 74 69 6f 6e 2f 6a 73 6f 6e application/json
0500: 22 29 3b 0a 70 72 69 6e 74 28 6a 73 6f 6e 5f 65 ");.print(json_e
0510: 6e 63 6f 64 65 28 5b 0a 20 20 22 73 68 61 22 20 ncode([. "sha"
0520: 3d 3e 20 6c 61 73 74 5f 75 75 69 64 28 29 2c 0a => last_uuid(),.
0530: 20 20 22 75 72 6c 22 20 3d 3e 20 22 24 5f 53 45 "url" => "$_SE
0540: 52 56 45 52 5b 46 4f 53 53 49 4c 5f 53 45 4c 46 RVER[FOSSIL_SELF
0550: 5d 65 78 74 2f 74 72 65 65 73 22 2c 0a 20 20 22 ]ext/trees",. "
0560: 74 72 65 65 22 20 3d 3e 20 74 72 65 65 73 28 29 tree" => trees()
0570: 2c 0a 20 20 22 74 72 75 6e 63 61 74 65 64 22 20 ,. "truncated"
0580: 3d 3e 20 66 61 6c 73 65 0a 5d 2c 20 4a 53 4f 4e => false.], JSON
0590: 5f 50 52 45 54 54 59 5f 50 52 49 4e 54 7c 4a 53 _PRETTY_PRINT|JS
05a0: 4f 4e 5f 55 4e 45 53 43 41 50 45 44 5f 53 4c 41 ON_UNESCAPED_SLA
05b0: 53 48 45 53 29 29 3b 0a 0a SHES));..