- Add Docker Compose setup for Wiki.js and PostgreSQL. - Include setup and backup scripts for easy management. - Create command-line scripts to create and edit wiki pages via the GraphQL API. - Add comprehensive README and .gitignore.
102 lines
3.8 KiB
Python
102 lines
3.8 KiB
Python
import os
|
|
import requests
|
|
import argparse
|
|
from dotenv import load_dotenv
|
|
|
|
# Load environment variables from .env file
|
|
# This will search for a .env file in the current directory or parent directories
|
|
load_dotenv()
|
|
|
|
# --- Configuration ---
|
|
WIKI_URL = os.getenv("WIKI_URL")
|
|
API_KEY = os.getenv("WIKI_API_KEY")
|
|
|
|
# --- GraphQL Mutation ---
|
|
GRAPHQL_CREATE_MUTATION = """
|
|
mutation CreatePage($content: String!, $description: String!, $editor: String!, $isPrivate: Boolean!, $isPublished: Boolean!, $locale: String!, $path: String!, $tags: [String]!, $title: String!) {
|
|
pages {
|
|
create(content: $content, description: $description, editor: $editor, isPrivate: $isPrivate, isPublished: $isPublished, locale: $locale, path: $path, tags: $tags, title: $title) {
|
|
responseResult { succeeded, errorCode, slug, message }
|
|
page { id, path, title }
|
|
}
|
|
}
|
|
}
|
|
"""
|
|
|
|
def create_wiki_page(path, title, content_file, tags, locale):
|
|
"""Sends a GraphQL request to create a new wiki page."""
|
|
if not all([API_KEY, WIKI_URL]):
|
|
print("❌ Error: WIKI_API_KEY and/or WIKI_URL not found in environment variables.")
|
|
return
|
|
|
|
try:
|
|
with open(content_file, 'r') as f:
|
|
content = f.read()
|
|
except FileNotFoundError:
|
|
print(f"❌ Error: Content file not found at '{content_file}'")
|
|
return
|
|
|
|
headers = {
|
|
"Authorization": f"Bearer {API_KEY}",
|
|
"Content-Type": "application/json"
|
|
}
|
|
|
|
variables = {
|
|
"content": content,
|
|
"description": f"Page for {title}",
|
|
"editor": "markdown",
|
|
"isPrivate": False,
|
|
"isPublished": True,
|
|
"locale": locale,
|
|
"path": path,
|
|
"tags": tags or [],
|
|
"title": title
|
|
}
|
|
|
|
graphql_query = {
|
|
"query": GRAPHQL_CREATE_MUTATION,
|
|
"variables": variables
|
|
}
|
|
|
|
print(f"🚀 Sending request to {WIKI_URL}/graphql to create page at path '{path}'...")
|
|
|
|
try:
|
|
response = requests.post(f"{WIKI_URL}/graphql", headers=headers, json=graphql_query, timeout=15)
|
|
response.raise_for_status()
|
|
result = response.json()
|
|
|
|
if 'errors' in result:
|
|
print("❌ GraphQL API returned errors:")
|
|
for error in result['errors']:
|
|
print(f" - {error.get('message')}")
|
|
return
|
|
|
|
creation_result = result.get('data', {}).get('pages', {}).get('create', {})
|
|
response_result = creation_result.get('responseResult', {})
|
|
|
|
if response_result.get('succeeded'):
|
|
page_info = creation_result.get('page', {})
|
|
print(f"✅ Success! Page '{page_info.get('title')}' created.")
|
|
print(f" - ID: {page_info.get('id')}")
|
|
print(f" - Path: {page_info.get('path')}")
|
|
print(f"🌐 View it at: {WIKI_URL}/{page_info.get('path')}")
|
|
else:
|
|
print(f"❌ API call failed: {response_result.get('message')}")
|
|
print(f" - Error Code: {response_result.get('errorCode')}")
|
|
|
|
except requests.exceptions.RequestException as e:
|
|
print(f"❌ An error occurred while connecting to the wiki: {e}")
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(description="Create a new page in Wiki.js.")
|
|
parser.add_argument('--path', required=True, help='The path for the new page (e.g., \"my-folder/my-page\").')
|
|
parser.add_argument('--title', required=True, help='The title of the new page.')
|
|
parser.add_argument('--content-file', required=True, help='Path to the markdown file containing the page content.')
|
|
parser.add_argument('--tags', nargs='*', help='A list of tags to add to the page.')
|
|
parser.add_argument('--locale', default='en', help='The locale for the page (default: en).')
|
|
|
|
args = parser.parse_args()
|
|
|
|
create_wiki_page(args.path, args.title, args.content_file, args.tags, args.locale)
|
|
|