You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用DokuWiki时,如何在用户创建新页面时向数据库记录信息

Hey there! Since you're already using authpdo with MySQL for DokuWiki authentication, you're in a great position to add page creation logging to your database without hunting for a pre-made plugin. Here are some practical, actionable approaches tailored to your setup:

DokuWiki has a built-in event system that triggers hooks at key points in its workflow—perfect for capturing when a new page is created. A lightweight local plugin is the cleanest way to implement this, as it won’t interfere with core updates or your existing authpdo setup.

Step 1: Create the Local Plugin Structure

  1. In your DokuWiki installation directory, make a new folder: lib/plugins/pagecreatelog
  2. Inside this folder, create a plugin.info.txt file with basic plugin metadata:
    base   pagecreatelog
    name   Page Creation Logger
    desc   Logs new page creations to MySQL database
    author Your Name
    email  your@email.com
    date   2024-05-20
    url    (leave blank or add your own)
    type   action
    
  3. Create a script.php file—this is where we’ll handle the event and database insertion.

Step 2: Add the Event Hook and Logging Logic

Paste this code into script.php (adjust the database table/columns to match your schema):

<?php
if(!defined('DOKU_INC')) die();

class plugin_pagecreatelog extends DokuWiki_Plugin {
    // Register the event hook
    function register(Doku_Event_Handler $controller) {
        // Hook into PAGE_SAVE_FINISH to ensure the page was successfully created
        $controller->register_hook('PAGE_SAVE_FINISH', 'AFTER', $this, 'log_new_page');
    }

    // Handle the page creation event
    function log_new_page(Doku_Event $event, $param) {
        $page_id = $event->data['id'];
        
        // Skip edits—only log brand new pages
        if(page_exists($page_id)) return;

        // Grab current user details (uses authpdo's user data)
        global $USERINFO;
        $username = $USERINFO['name'] ?? $USERINFO['user'];
        $user_id = $USERINFO['id'] ?? $username; // Match your authpdo user table's ID field

        // Get page metadata
        $page_title = p_get_title($page_id);
        $namespace = getNS($page_id);
        $creation_timestamp = time();

        // Reuse authpdo's existing MySQL connection (no need to set up a new one!)
        global $auth;
        if($auth instanceof auth_plugin_authpdo) {
            $db_connection = $auth->_getDB();
            
            try {
                // Prepare and execute the INSERT statement
                $stmt = $db_connection->prepare("
                    INSERT INTO page_creation_log 
                    (page_id, page_title, namespace, user_id, username, created_at)
                    VALUES (?, ?, ?, ?, ?, ?)
                ");
                $stmt->execute([$page_id, $page_title, $namespace, $user_id, $username, $creation_timestamp]);
            } catch(PDOException $e) {
                // Log errors to DokuWiki's internal message system (optional)
                msg("Failed to log page creation: " . $e->getMessage(), -1);
            }
        }
    }
}

Step 3: Create the Log Table in MySQL

Run this SQL query in your database to create the logging table (adjust columns if needed):

CREATE TABLE page_creation_log (
    id INT AUTO_INCREMENT PRIMARY KEY,
    page_id VARCHAR(255) NOT NULL UNIQUE, -- Prevent duplicate logs for the same page
    page_title VARCHAR(255),
    namespace VARCHAR(255),
    user_id VARCHAR(100) NOT NULL,
    username VARCHAR(100) NOT NULL,
    created_at INT NOT NULL -- Stores Unix timestamp
);

Step 4: Test It Out

Enable the plugin in your DokuWiki admin panel, create a new page, and check your page_creation_log table—you should see a new entry with all the details!

Alternative: Quick-and-Dirty Action Script (Less Reliable)

If writing a plugin feels too involved, you can add a snippet to conf/local.protected.php to trigger logging. Note that this might miss edge cases (like pages created via API or other plugins):

if($ACT === 'save' && !page_exists($ID)) {
    // Establish your MySQL connection (use the same credentials as authpdo)
    $dsn = 'mysql:host=YOUR_DB_HOST;dbname=YOUR_DB_NAME;charset=utf8';
    $db_user = 'YOUR_DB_USER';
    $db_pass = 'YOUR_DB_PASS';

    try {
        $db = new PDO($dsn, $db_user, $db_pass);
        $stmt = $db->prepare("
            INSERT INTO page_creation_log 
            (page_id, page_title, user_id, username, created_at)
            VALUES (?, ?, ?, ?, ?)
        ");
        global $USERINFO;
        $stmt->execute([$ID, p_get_title($ID), $USERINFO['id'], $USERINFO['name'], time()]);
    } catch(PDOException $e) {
        msg("Logging failed: " . $e->getMessage(), -1);
    }
}

Key Notes

  • Make sure your database user has INSERT permissions on the page_creation_log table.
  • The plugin approach is preferred because it’s maintainable—updates to DokuWiki or authpdo won’t overwrite your code.
  • Adjust the column names and table structure to fit your existing database schema.

内容的提问来源于stack exchange,提问作者Ho.Chun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:41:33