跳到正文

变更历史 ​

机器翻译

本页由英文原文机器翻译而来,尚未经过母语审校,欢迎在 GitHub 上提出修改。内容如有出入,以英文原文为准。

任务: 知道谁在什么时候修改了维基的某个页面,以及改了什么。

数据源上的 history: true 就是全部配置:

yaml
# Recipe: who changed what, and when — recorded by every action's writes.
paths:
  components: ./components
  migrations: ./migrations

datasources:
  db:
    driver: sqlite
    database: ./data/wiki.db
    history: true
sql
CREATE TABLE pages (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    title TEXT NOT NULL,
    body TEXT NOT NULL DEFAULT ''
);

INSERT INTO pages (title, body) VALUES ('Welcome', 'First draft.');

动作的每一次写入都会在同一个事务中,记录到同一个数据库的 quantum_history 数据表里: 时间、会话的用户、动作、这一行,以及修改前后的这一行。ui:history 按从新到旧列出 一行的变更,每个被修改的列显示为 old → new。

xml
<q:component name="Wiki">
  <!-- history: true on the datasource: each write below is recorded with
       the session's user, the action, and the row before and after. -->
  <q:action name="edit" method="POST">
    <q:param name="title" required="true" minlength="3" />
    <q:param name="body" default="" />
    <q:query name="saved" datasource="db">
      UPDATE pages SET title = :title, body = :body WHERE id = 1
      <q:param name="title" value="{title}" type="string" />
      <q:param name="body" value="{body}" type="string" />
    </q:query>
    <q:redirect url="/" flash="Saved." />
  </q:action>

  <q:query name="page" datasource="db">SELECT id, title, body FROM pages WHERE id = 1</q:query>

  <ui:window title="{page.title}">
    <ui:text>{page.body}</ui:text>
    <ui:form on-submit="edit" values="{page}" submit="Save" />
    <!-- Newest first: when, who, which action, and each changed column. -->
    <ui:history table="pages" key="{page.id}" datasource="db" />
  </ui:window>
</q:component>

test:as 让测试登录;history= 检查记录了什么。被拒绝或回滚的写入不会留下历史:

xml
<q:test name="an edit is recorded with who made it" page="/">
  <test:as user="ana" />
  <test:submit action="edit" title="Welcome!" body="Second draft." />
  <test:expect redirect="/" flash="Saved." />
  <test:expect history="pages" action="edit" op="update" user="ana" count="1" />
  <test:expect text="title: Welcome → Welcome!" />
  <test:expect text="body: First draft. → Second draft." />
</q:test>

<q:test name="a refused edit leaves no history" page="/">
  <test:as user="ana" />
  <test:submit action="edit" title="W" />
  <test:expect error="title" />
  <test:expect history="pages" count="0" />
</q:test>
text
tests/history.test.q
  PASS  an edit is recorded with who made it
  PASS  a refused edit leaves no history
2 passed, 0 failed

参见 DB-11。

MIT 许可证 · 使用 VitePress 构建