# RIS Mirror — Consumer Specification

This document describes the on-disk / on-the-web layout of a `ris-mirror` tree so
that **humans** and **automated agents** can navigate it without prior knowledge.
It is the contract for consumers; the producer is the `ris-mirror` tool.

- **Source:** Austrian Rechtsinformationssystem (RIS), via the official OGD REST
  API `https://data.bka.gv.at/ris/api` (version `v2.6`).
- **Nature:** a static file tree. No database, no server-side logic. Any static
  web server (the reference deployment uses nginx) serves it; it is fully
  cacheable / CDN-able.
- **Not legal advice.** The RIS documents are a documentation of Austrian law;
  this mirror reproduces them as published. Authoritative texts are the
  *authentic* RIS applications (e.g. `BgblAuth`).

---

## 1. Encoding & compression — read this first

| Thing | How it is stored | How to read it |
|-------|------------------|----------------|
| `index.html` | **raw** UTF-8 HTML | open directly |
| `*.json`, `*.xml` (indices, sitemaps) | **brotli only**, file ends `.br` | brotli-decompress |
| Text representations (`*.html`, `*.xml`, `*.rtf`) | **brotli only**, file ends `.br` | brotli-decompress |
| Binary representations (`*.pdf`, `*.png`, …) | **raw** | use directly |

- Compressed files have **no** `.gz` or uncompressed sibling — brotli only.
- **Over HTTP and on disk:** always use the `.br` suffix (e.g. `index.json.br`).
- **Indices, sitemaps, `metadata.json`:** the server returns raw brotli bytes
  (`application/octet-stream`) with no `Content-Encoding` header.
  Brotli-decompress the response body before parsing.
- **Text representations** (`*.xml.br`, `*.html.br`, `*.rtf.br`, `*.json.br`,
  `*.txt.br` in a document directory): the server sends the same brotli bytes with
  `Content-Encoding: br` and the media type of the inner file (e.g.
  `application/xml`), so a browser shows the document. An HTTP client that decodes
  `Content-Encoding` automatically already hands you the plain document — do **not**
  brotli-decode it a second time. Decode only if the client gave you the raw bytes.

Throughout this spec, a path written without `.br` (e.g. `index.json`) names the
logical resource; the actual stored and served file always carries the `.br` suffix.

---

## 2. Directory tree

```
<root>/
├── index.html                  Human entry: list of Applikationen
├── index.json                  Machine entry (see §3.1)
├── sitemapindex.xml            Sitemap index → per-Application sitemaps (§4)
├── SPEC.md                     This document
├── .watermark.json             Producer state (incremental updates); ignore as a consumer
│
└── <Applikation>/              One per RIS Applikation id, e.g. Judikatur, Justiz, BrKons, Spg
    ├── index.html
    ├── index.json
    ├── sitemap-00000.xml …     This Applikation's sitemap chunks
    │
    └── … documents, in one of two layouts (§2.1)
```

`<Applikation>` is the RIS **Applikation id** (the `Applikation` query value),
e.g. `BrKons`, `LrKons`, `Justiz`, `Vfgh`, `BgblAuth`, `Spg`, … — not the broader
*Bereich* (`Bundesrecht`, `Judikatur`, …) used by the API endpoint.

### 2.1 Two document layouts

**(a) Flat** — for non-versioned applications (case law, gazettes, most apps):

```
<Applikation>/<bucket>/<Dokumentnummer>/
    ├── index.html              Human document viewer
    ├── metadata.json           Machine record (§3.4)
    └── <representation files>   e.g. NORxx….xml.br, …pdf, image001.png
```

`<bucket>` is a 2-hex-digit shard (`00`–`ff`) derived from a stable hash of the
`Dokumentnummer`. It exists only to keep directories small; it carries no
meaning. **Do not** compute it — discover paths via the indices (§3) or sitemaps.

**(b) Law-grouped** — for consolidated law (`BrKons`, `LrKons`), where each
document is a time-sliced *Fassung* (version) of a paragraph:

```
<Applikation>/<bucket>/<Gesetzesnummer>/<Paragraph>/<Dokumentnummer>/
    ├── index.html
    ├── metadata.json
    └── <representation files>
```

- `<Gesetzesnummer>` = stable id of the whole law (groups all its paragraphs and
  Fassungen). `<bucket>` here is the shard of the **Gesetzesnummer**.
- `<Paragraph>` = the article/§/annex, e.g. `§_297`, `Art._9`, `Anl._1`.
- `<Dokumentnummer>` = one **Fassung** (version) of that paragraph.
- The directory at `<Gesetzesnummer>/` has its own `index.html` / `index.json`
  (§3.3) listing the law's paragraphs and their Fassungen.

A consumer detects the layout from the metadata: a document is law-grouped iff
its `metadata.json` has a non-empty `gesetzesnummer` (§3.4, §5).

---

## 3. Machine indices (`index.json`)

All `index.json` are brotli (`index.json.br`). Schemas by level:

### 3.1 Root — `/index.json`
```json
{
  "applications": [ { "application": "BrKons", "count": 12345 }, … ],
  "documents": 678901
}
```
`count` / `documents` = number of stored documents (Fassungen counted
individually for consolidated law).

### 3.2 Application — `/<Applikation>/index.json`

*Flat* applications:
```json
{
  "application": "Justiz",
  "documentCount": 12345,
  "count": 12345,
  "buckets": [ { "bucket": "a3", "count": 87 }, … ]
}
```

*Law-grouped* applications:
```json
{
  "application": "BrKons",
  "documentCount": 125,
  "lawCount": 35,
  "laws": [
    { "gesetzesnummer": "10001622", "title": "Allgemeines bürgerliches Gesetzbuch",
      "paragraphCount": 1453, "fassungCount": 3201,
      "path": "BrKons/fc/10001622/" }, …
  ]
}
```
`path` is **root-relative**, with a trailing slash. Use it directly.
`laws[].title` is a display label derived from the RIS title (§3.5).

### 3.3 Bucket / Law

*Flat — bucket* `/<Applikation>/<bucket>/index.json`:
```json
{
  "bucket": "a3",
  "count": 87,
  "documents": [
    { "dokumentnummer": "JJT_…", "title": "…",
      "path": "Justiz/a3/JJT_…/", "contents": ["Html","Pdf"] }, …
  ]
}
```
`documents[].title` is the raw title, identical to `metadata.json` `title` (§3.5).

*Law-grouped — law* `/<Applikation>/<bucket>/<Gesetzesnummer>/index.json`:
```json
{
  "gesetzesnummer": "10001622",
  "title": "Allgemeines bürgerliches Gesetzbuch",
  "paragraphCount": 1453,
  "fassungCount": 3201,
  "paragraphs": [
    { "paragraph": "§ 297",
      "fassungen": [
        { "dokumentnummer": "NOR12018023", "inkrafttreten": "1917-01-01",
          "ausserkrafttreten": "2010-07-31", "inForce": false,
          "path": "BrKons/fc/10001622/§_297/NOR12018023/" },
        { "dokumentnummer": "NOR40120163", "inkrafttreten": "2010-08-01",
          "ausserkrafttreten": "", "inForce": true,
          "path": "BrKons/fc/10001622/§_297/NOR40120163/" }
      ] }, …
  ]
}
```
`fassungen` are sorted ascending by `inkrafttreten`. Exactly the entries with
`inForce: true` (empty `ausserkrafttreten`) are currently valid. `title` is a
display label derived from the RIS title (§3.5).

### 3.4 Document — `/<…>/<Dokumentnummer>/metadata.json`
```json
{
  "applikation": "BrKons",
  "dokumentnummer": "NOR40120163",
  "title": "Allgemeines bürgerliches Gesetzbuch<br/>StF: JGS Nr. 946/1811",
  "contents": [
    { "dataType": "Xml", "url": "https://www.ris.bka.gv.at/…/NOR40120163.xml" },
    { "dataType": "Html", "url": "https://…" }
  ],

  "gesetzesnummer": "10001622",   // ↓ versioned docs only
  "paragraph": "§ 297",
  "inkrafttreten": "2010-08-01",
  "ausserkrafttreten": "",        // empty = in force
  "inForce": true
}
```

- `contents` lists the representations **actually mirrored** (a run may restrict
  these, e.g. XML only). The on-disk file name for each is the **last path
  segment of its `url`** (e.g. `NOR40120163.xml` → stored `NOR40120163.xml.br`);
  request it relative to the document directory.
- `title` is the raw RIS title. It can contain HTML (§3.5).
- The five version fields are present only for consolidated-law documents.
- `metadata.json` is an excerpt of the RIS metadata. Other RIS fields (for
  example `Kurztitel` when a `Titel` exists, `Beachte`, `Aenderung`) are not
  stored here; they are in the content XML (§3.6) when an `Xml` representation
  is mirrored.

### 3.5 Titles and markup

`metadata.json` `title` comes from the RIS metadata branch of the document
(`Bundesrecht`, `Landesrecht`, `Judikatur`, …): the long title `Titel` if present,
else the short title `Kurztitel`. The value is stored unchanged.

- The RIS schema declares `Titel` and `Kurztitel` as plain strings
  (`xs:string`, `OGD_*_Response.xsd`). In practice `Titel` contains **HTML
  fragments**: `<br/>` before the "StF:" (Stammfassung) part and between lines,
  and in state law links such as
  `<A HREF="https://alex.onb.ac.at/…?aid=lgs&datum=1977&page=155" Target="_blank">`.
- These fragments are **not always well-formed XML** (unescaped `&` in `HREF`,
  uppercase tags). Treat `title` as an HTML fragment, not as XML and not as
  plain text.
- `Kurztitel` had no markup in the observed data. It can start with `*`
  (e.g. `* Salzburger Jagdgesetz 1977`); RIS does not document the meaning.
- Labels (`laws[].title` in §3.2 and the law `title` in §3.3, and the HTML pages)
  are the text of `title` before the first `<br>`. A `<br/>` inside the long title
  itself (rare) cuts the label early. For the full, structured title use the
  content XML (§3.6).

### 3.6 Content XML (`<Dokumentnummer>.xml.br`)

The `Xml` representation is the RIS document format `risdok`, copied unchanged
from RIS (brotli-compressed, §1).

- **Schema:** `RISDok.xsd` with `RISJudikaturNutzdaten.xsd`, namespace
  `http://www.bka.gv.at`, published at
  <https://data.bka.gv.at/ris/ogd/v2.6/XSD/RIS_Nutzdaten_Schema.zip>. The RIS
  metadata schemas are at the same location (`OGD_Response_Schema.zip`).
- **Structure:** `risdok/nutzdaten/abschnitt` holds a sequence of
  `<ueberschrift typ="titel">` (field caption) and
  `<absatz typ="erltext" ct="…">` (field value). One field can span several
  `absatz` elements with the same `ct`.
- **Field names (`ct`)** observed in consolidated state law (LrKons):
  `bundesland`, `kurztitel`, `kundmachungsorgan`, `typ`, `artikel_anlage`,
  `ikra`, `akra`, `index`, `beachte`, `langtitel`, `aenderung`, `geaendert`,
  `gesnr`, `doknr`, `adoknr`. Other applications can use other names.
- **Title:** `absatz[@ct="kurztitel"]` is the short title. The first
  `absatz[@ct="langtitel"]` is the long title without "StF:"; further
  `langtitel` paragraphs hold the "StF:" reference.
- **Markup is XML, not HTML:** links are `<link href="…">`, line breaks `<br/>`,
  and special characters are **empty elements**, not entities (e.g. `<amp/>` for
  `&`, `<nbsp/>` for a no-break space, `<bdquo/>` for „). The schema documents the
  character of each element.
- **Dates** in the content use `DD.MM.YYYY`; the metadata uses `YYYY-MM-DD`.
- The `stylesheet` attribute names the RIS XSL that makes the `Html`
  representation. The XSL is not published; use the mirrored `Html`
  representation for the rendered form.

---

## 4. Sitemaps

- `/sitemapindex.xml` (brotli) — a `<sitemapindex>` referencing every
  per-Application `sitemap-NNNNN.xml.br`.
- Each `/<Applikation>/sitemap-NNNNN.xml` (brotli) is a `<urlset>` of document
  **directory** URLs (≤ 50 000 per chunk), each the human viewer page.
- Advertised in `/robots.txt`.

---

## 5. How an agent should traverse

**Goal: enumerate everything.**
1. GET `/index.json` → `applications[]`.
2. For each application GET `/<app>/index.json`.
   - If it has `buckets` → for each bucket GET its `index.json` → `documents[].path`.
   - If it has `laws` → for each law GET `<law.path>index.json` →
     `paragraphs[].fassungen[].path`.
3. Each `path` is a document directory; GET `<path>metadata.json` for its
   record, then fetch representation files by URL-basename relative to `<path>`.

*(Or skip the indices entirely and consume `/sitemapindex.xml` → sitemaps →
document directories.)*

**Goal: current text of a consolidated law.** Open the law's `index.json`; for
each paragraph take the single `fassung` with `inForce: true`.

**Goal: a paragraph's history.** That paragraph's `fassungen[]`, already ordered
by `inkrafttreten`; each entry's in-force window is `[inkrafttreten,
ausserkrafttreten)` (`ausserkrafttreten` empty = open / still valid).

**Field origins.** `dokumentnummer`, `gesetzesnummer`, `inkrafttreten`,
`ausserkrafttreten`, `paragraph` come verbatim from RIS OGD metadata
(`Technisch.ID`, `…BrKons.Gesetzesnummer`, `Inkrafttretensdatum`,
`Ausserkrafttretensdatum`, `ArtikelParagraphAnlage`).

---

## 6. Coverage & freshness

> Full statement of what is included / omitted: [`COMPLETENESS.md`](COMPLETENESS.md).


- **Consolidated law (`BrKons`/`LrKons`):** the mirror keeps **all Fassungen of
  laws that are still in force**, and **omits fully repealed laws** (laws with no
  in-force Fassung). So a present law carries its full version history; a law
  absent from the tree was repealed in its entirety.
- **Other applications:** every document the API returns is mirrored.
- **Updates** are incremental (RIS *Historyabfrage*); a document's bytes may be
  refreshed in place when RIS changes it. There is no per-document change log in
  the tree.
- **Stability:** `path` values and document directories are stable across runs
  (keyed by Dokumentnummer); the `<bucket>` shard is deterministic but treated as
  opaque — always follow `path`/sitemap links, never reconstruct paths.

---

## 7. Quick reference

| You have | You want | Do |
|----------|----------|----|
| nothing | the whole catalog | `/index.json` → app → bucket/law → `metadata.json` |
| a `path` from an index | the document | `GET <path>metadata.json`, then its `contents[].url` basenames |
| a consolidated law | current text | law `index.json` → each paragraph's `inForce:true` fassung |
| a paragraph | its versions | that paragraph's `fassungen[]` (sorted by `inkrafttreten`) |
| an index, sitemap or `metadata.json` | its bytes | fetch the `.br` URL, then brotli-decode the body (§1) |
| a text representation | its bytes | fetch the `.br` URL; decode only if your client did not apply `Content-Encoding: br` (§1) |
