使用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:
Recommended Approach: Use DokuWiki's Event System with a Custom Local Plugin
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
- In your DokuWiki installation directory, make a new folder:
lib/plugins/pagecreatelog - Inside this folder, create a
plugin.info.txtfile 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 - Create a
script.phpfile—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_logtable. - 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

