# Wiki API

## Endpoints

### List Wiki Pages

```
GET /api/v1/repos/{owner}/{repo}/wiki/pages
```

```bash
curl -s "${FORGEJO_URL}/api/v1/repos/{owner}/{repo}/wiki/pages" \
  -H "Authorization: token ${FORGEJO_PAT}"
```

**Response:**

```json
[
  {
    "title": "Home",
    "sub_url": "Home",
    "last_commit": {
      "id": "abc123...",
      "message": "Update Home page",
      "author": { "name": "{author_name}", "email": "{email}" },
      "committer": { "name": "{author_name}", "email": "{email}" },
      "timestamp": "2024-03-15T10:00:00Z"
    }
  }
]
```

---

### Create Wiki Page

```
POST /api/v1/repos/{owner}/{repo}/wiki/new
```

**Request body:**

| Field | Type | Description |
|-------|------|-------------|
| `title` | string | Page title (used as the page name/slug) |
| `content_base64` | string | Page content, base64 encoded |
| `message` | string | Commit message |

```bash
# Create a wiki page
CONTENT=$(echo -n "# Welcome\n\nThis is the home page of our wiki.\n\n## Getting Started\n\nSee the [Setup Guide](Setup-Guide) for instructions." | base64 -w0)

curl -s -X POST "${FORGEJO_URL}/api/v1/repos/{owner}/{repo}/wiki/new" \
  -H "Authorization: token ${FORGEJO_PAT}" \
  -H "Content-Type: application/json" \
  -d "{
    \"title\": \"Home\",
    \"content_base64\": \"${CONTENT}\",
    \"message\": \"Create Home page\"
  }"

# Create another page
CONTENT=$(echo -n "# Setup Guide\n\n## Prerequisites\n\n- Git\n- Docker\n\n## Installation\n\n1. Clone the repo\n2. Run docker compose up" | base64 -w0)

curl -s -X POST "${FORGEJO_URL}/api/v1/repos/{owner}/{repo}/wiki/new" \
  -H "Authorization: token ${FORGEJO_PAT}" \
  -H "Content-Type: application/json" \
  -d "{
    \"title\": \"Setup Guide\",
    \"content_base64\": \"${CONTENT}\",
    \"message\": \"Add setup guide\"
  }"
```

---

### Get Wiki Page

```
GET /api/v1/repos/{owner}/{repo}/wiki/page/{pageName}
```

The `pageName` is the URL-safe page name (spaces replaced with hyphens).

```bash
# Get the Home page
curl -s "${FORGEJO_URL}/api/v1/repos/{owner}/{repo}/wiki/page/{pageName}" \
  -H "Authorization: token ${FORGEJO_PAT}"

# Get a page with spaces in the name
curl -s "${FORGEJO_URL}/api/v1/repos/{owner}/{repo}/wiki/page/{pageName}" \
  -H "Authorization: token ${FORGEJO_PAT}"
```

**Response:**

```json
{
  "title": "Home",
  "sub_url": "Home",
  "content_base64": "IyBXZWxjb21lCg==",
  "commit_count": 3,
  "last_commit": {
    "id": "abc123...",
    "message": "Update Home page",
    "author": { "name": "{author_name}", "email": "{email}" },
    "timestamp": "2024-03-15T10:00:00Z"
  },
  "sidebar": "",
  "footer": ""
}
```

To decode the content:

```bash
# Get and decode wiki page content
curl -s "${FORGEJO_URL}/api/v1/repos/{owner}/{repo}/wiki/page/{pageName}" \
  -H "Authorization: token ${FORGEJO_PAT}" \
  | jq -r '.content_base64' | base64 -d
```

---

### Edit Wiki Page

```
PATCH /api/v1/repos/{owner}/{repo}/wiki/page/{pageName}
```

**Request body:**

| Field | Type | Description |
|-------|------|-------------|
| `title` | string | New page title (can rename the page) |
| `content_base64` | string | Updated content, base64 encoded |
| `message` | string | Commit message |

```bash
# Update the Home page content
CONTENT=$(echo -n "# Welcome to MyProject\n\nUpdated home page with new content.\n\n## Quick Links\n\n- [Setup Guide](Setup-Guide)\n- [API Docs](API-Docs)\n- [FAQ](FAQ)" | base64 -w0)

curl -s -X PATCH "${FORGEJO_URL}/api/v1/repos/{owner}/{repo}/wiki/page/{pageName}" \
  -H "Authorization: token ${FORGEJO_PAT}" \
  -H "Content-Type: application/json" \
  -d "{
    \"title\": \"Home\",
    \"content_base64\": \"${CONTENT}\",
    \"message\": \"Update Home page with quick links\"
  }"

# Rename a page (change title)
CONTENT=$(echo -n "# Installation Guide\n\nRenamed from Setup Guide." | base64 -w0)

curl -s -X PATCH "${FORGEJO_URL}/api/v1/repos/{owner}/{repo}/wiki/page/{pageName}" \
  -H "Authorization: token ${FORGEJO_PAT}" \
  -H "Content-Type: application/json" \
  -d "{
    \"title\": \"Installation Guide\",
    \"content_base64\": \"${CONTENT}\",
    \"message\": \"Rename Setup Guide to Installation Guide\"
  }"
```

---

### Delete Wiki Page

```
DELETE /api/v1/repos/{owner}/{repo}/wiki/page/{pageName}
```

```bash
curl -s -X DELETE "${FORGEJO_URL}/api/v1/repos/{owner}/{repo}/wiki/page/{pageName}" \
  -H "Authorization: token ${FORGEJO_PAT}"
```

---

### Get Page Revision History

```
GET /api/v1/repos/{owner}/{repo}/wiki/revisions/{pageName}
```

```bash
curl -s "${FORGEJO_URL}/api/v1/repos/{owner}/{repo}/wiki/revisions/{pageName}" \
  -H "Authorization: token ${FORGEJO_PAT}"
```

**Response:**

```json
{
  "count": 3,
  "revisions": [
    {
      "id": "abc123...",
      "message": "Update Home page with quick links",
      "author": { "name": "{author_name}", "email": "{email}" },
      "committer": { "name": "{author_name}", "email": "{email}" },
      "timestamp": "2024-03-15T12:00:00Z"
    },
    {
      "id": "def456...",
      "message": "Add table of contents",
      "author": { "name": "{author_name}", "email": "{email}" },
      "committer": { "name": "{author_name}", "email": "{email}" },
      "timestamp": "2024-03-14T09:00:00Z"
    }
  ]
}
```

---

## Notes

- Wiki page names use hyphens (`-`) instead of spaces in URLs
- Content is always base64 encoded in both request and response
- The `sub_url` field is the URL-safe version of the title
- Wiki pages are stored in a separate git repository (`{repo}.wiki.git`)
- The `sidebar` and `footer` fields in the response contain the content of `_Sidebar` and `_Footer` pages if they exist
