It's a multifunctional Wiki.js, but I didn't know how to batch import existing data, so I tried to force the database to be updated. This is not a formal method, so please try it at your own risk.
--Preparation --Launch Wiki.js with docker-compose --Build SQLAlchemy environment locally --Create a class from the database --Create an article with SQLAlchemy
Please also refer to GIGAZINE article.
The work is confirmed with bash of macOS. Please have docker-compose and Python 3.8 installed.
There are various startup methods in "Wiki.js --Docs", but docker-compose is used. First, create docker-compose.yml.
bash
mkdir wikijs
cd wikijs
cat << EOF > docker-compose.yaml
version: "3"
services:
db:
image: postgres:11-alpine
environment:
POSTGRES_DB: wiki
POSTGRES_PASSWORD: wikijsrocks
POSTGRES_USER: wikijs
ports:
- "5432:5432"
logging:
driver: "none"
restart: unless-stopped
volumes:
- db-data:/var/lib/postgresql/data
wiki:
image: requarks/wiki:2
depends_on:
- db
environment:
DB_TYPE: postgres
DB_HOST: db
DB_PORT: 5432
DB_USER: wikijs
DB_PASS: wikijsrocks
DB_NAME: wiki
restart: unless-stopped
ports:
- "80:3000"
volumes:
db-data:
EOF
If you change the port 5432 on the local side, change "5432: 5432" to "15432: 5432", and change the subsequent 5432 as well.
Wiki.js starts with docker-compose up -d
. The stop is docker-compose down
.
Wiki.js is available at http: // localhost /
.
Create ADMINISTRATOR ACCOUNT appropriately. Then log in with the account you created.
Immediately after logging in, there are no pages. First, create the top page.
The path of the top page is / <language> / home
. Initially, you can only use English (ʻen`) as the language.
Here, we will use Japanese (ja
) as the language.
First, after creating the English top page, install the Japanese language, delete the English top page, and create the Japanese top page.
Press " + CREATE HOME PAGE
".
Press "Markdown", press " ✓ OK
", and then press" ✓ CREATE
".
The top page (http: // localhost / en / home
) opens. Since you are logged in as Administrator, open the management screen (http: // localhost / a / dashboard
) with " ⚙
"in the upper right.
Select " Locale
"from the left side. Select "Japanese" from " Download Locale
"and press" Download "in the" cloud with down arrow mark ". Then go back up and set " Locale Settings
"to" Japanese "and press" ✓ APPLY
" in the upper right.
Since the active namespace has been changed to Japanese, the top page needs to be recreated in Japanese.
Select " Page
"from the left side. Select the item whose Path is "en / home" and delete it with " Delete
"of" ʻACTIONS v` ".
Click "Close
" on the upper right to return to the first screen, so please create a Japanese top page.
Wiki.js allows you to create an article in a browser, but here we will try to create an article by editing the database directly from Python. First, create a Python virtual environment.
bash
python3.8 -m venv venv
source venv/bin/activate
pip install psycopg2-binary SQLAlchemy
Create a class from the database for use with SQLAlchemy. Execute the following to create db_class.py
.
bash
cat << EOF | python
import re
from sqlalchemy import MetaData, create_engine
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine("postgresql+psycopg2://wikijs:wikijsrocks@localhost:5432/wiki")
metadata = MetaData()
metadata.reflect(engine)
Base = declarative_base(metadata=metadata)
def _subfunc(m):
s = ".".join(m.group(1).split(".")[-2:])
return rf"ForeignKey('{s}')"
def make_class(cls):
lst = [f"class {str(cls).split('.')[-1][:-2]}(Base):"]
lst.append(f' __tablename__ = "{cls.__tablename__}"\n')
for column in cls.__table__.columns:
s = repr(column)
nam = s.split("'")[1]
s = re.sub(r", table=[^>]+>", "", s)
s = re.sub(r", server_default=[^)]+\)", "", s)
s = re.sub(r"ForeignKey\('([^']+)'\)", _subfunc, s)
lst.append(f" {nam} = {s}")
res = "\n".join(lst) + "\n"
res = res.replace("metadata = Column", "metadata_ = Column")
return res
def make_classes():
lst = [
"""\
# made by make_classes
from typing import Any
from sqlalchemy import Column, ForeignKey, Text
from sqlalchemy.dialects.postgresql import (
BOOLEAN, BYTEA, INTEGER, JSON, TEXT, TIMESTAMP, VARCHAR
)
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base() # type: Any
"""
]
dc = {"__table_args__": {"autoload": True}}
for tbl in sorted(metadata.tables):
if tbl in {"brute"}:
continue
typ = type(tbl.title(), (Base,), dict(__tablename__=tbl, **dc))
lst.append(make_class(typ))
with open("db_class.py", "w") as fp:
fp.write("\n".join(lst))
make_classes()
EOF
First, create a file.
bash
cat << EOF > main.py
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from db_class import *
engine = create_engine("postgresql+psycopg2://wikijs:wikijsrocks@localhost:5432/wiki")
Session = sessionmaker(bind=engine)
session = Session()
pgid = max(i.id for i in session.query(Pages).all()) + 1
ptid = max(i.id for i in session.query(Pagetree).all()) + 1
pg = Pages(
id=pgid,
path=f"test{pgid}",
hash="0123456789001234567890abcdefghijklmnopqr",
title=f"Test page{pgid}",
isPrivate=False,
isPublished=True,
content="",
contentType="markdown",
createdAt="2020-10-04T09:00:00.000Z",
updatedAt="2020-10-04T09:00:00.000Z",
editorKey="markdown",
localeCode="ja",
description="",
authorId=1,
creatorId=1,
render="<div></div>",
toc=[{"title": "", "anchor": "", "children": []}],
extra={"js": "", "css": ""},
publishStartDate="",
publishEndDate="",
)
pt = Pagetree(
id=ptid,
path=pg.path,
localeCode="ja",
pageId=pgid,
title=pg.title,
depth=1,
isPrivate=False,
isFolder=False,
ancestors=[],
)
session.add(pg)
session.add(pt)
session.commit()
session.close()
EOF
Above, create main.py
.
python main.py
creates an empty page calledTest Page ...
.
The content is described in Pages.content
and Pages.render
. It will not be displayed if you just described it in content, but it will be displayed if you edit and save it.
You can dump the contents of the database with pg_dump -h localhost -p 5432 -U wikijs wiki -f data.sql
.
You can use psql -h host -p 5432 -U user database -f data.sql
to return data to an empty PostgreSQL.
Recommended Posts