Dokuwiki 二次开发记录

DokuWiki 是一种在 GPLv2 下获得许可并以 PHP 编程语言编写的 wiki 应用程序。 它适用于纯文本文件,因此不需要数据库。它的语法与 MediaWiki 使用的语法类似。

DokuWiki 具有丰富的插件,如Markdown、sidebar、流程图、时序图、svg、数学公式、ToDo等等这些实用的插件。

但由于直接安装 DokuWiki 和使用她的插件,对有些地方和样式不是很满意。如 Markdown 插件存在 bug,样式风格过于传统,登录方式需要自定义等。由于这些问题的存在,也就需要在源码的基础上做些配置和二次开发。

下载安装Dokuwiki

https://www.dokuwiki.org/

安装插件

  1. SVG-Edit plugin
  2. DokuWiki Upgrade
  3. Wrap Plugin
  4. Sequencediagram plugin
  5. Plugin: Numbered Headings
  6. Note Plugin
  7. Move plugin
  8. MathJax plugin
  9. PHP Markdown Extra plugin
  10. Indexmenu Plugin
  11. imgpaste plugin
  12. Gallery Plugin
  13. Flowchartjs Plugin
  14. S5 Slideshow Plugin
  15. edittable plugin
  16. color syntax plugin
  17. ToDo
  18. Copy Page Plugin
  19. Page Redirect
  20. Remove irrelevant entries from the change history
  21. Searchindex Manager
  22. CSV Plugin
  23. ShortURL Plugin

data目录名支持中文

inc/pageutils.php

# utf8_encodeFN()
// $file = urlencode($file);
// $file = str_replace('%2F','/',$file);
return $file;
# utf8_decodeFN()
// return urldecode($file);
return $file;

配置管理器

DokuWiki 设置

  1. title: 聚美wiki
  2. 语言:zh
  3. 高级设置:自动检查更新关闭

显示设置

toptoclevel -> 1
tocminheads -> 1
maxtoclevel -> 3
maxseclevel -> 3

Mathjax配置

plugin»mathjax»url

// 先从网上下载MathJax到指定目录.
/lib/plugins/mathjax/2.7.1/MathJax.js?config=TeX-AMS_CHTML.js

plugin»mathjax»config

MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [ ["\\$","\\$"] ],
        displayMath: [ ["\\$\\$","\\$\\$"] ],
        processEscapes: true
    }
});

Indexmenu

生成管理左侧目录树。

添加sidebar页面,内容为:

{{indexmenu>..#1|js navbar}}

Markdownextra

lib/plugins/markdownextra/action.php

if (preg_match('/^---\s*\n(.*?\n?)^---\s*$\n?(.+)/sm',$event->data, $match)){
   $event->data = preg_replace('/^---\s*\n(.*?\n?)^---\s*$\n?/sm', "</markdown>\n\\1<markdown>\n" ,$event->data, -1);
   $event->data = "<markdown>\n".$event->data."\n</markdown>";
   $event->data = preg_replace('/^<markdown>\s*\n*<\/markdown>\s*$\n?/sm', "" ,$event->data);
}else{
   $event->data = "<markdown>\n".$event->data."\n</markdown>";
}

lib/plugins/markdownextra/syntax.php

Warning: DOMDocument::loadHTML(),加上 @ 屏蔽报错

解决解析md时概率性出错问题

1)inc/parserutils.php -> p_cached_instructions():

$isMD = strtolower(substr($file, -7)) === '.md.txt';
if (!$isMD && ($cacheonly || $cache->useCache() || (isset($run[$file]) && !defined('DOKU_UNITTEST')))) {
    return $cache->retrieveCache();
} else {
    if (file_exists($file)) {
        // no cache - do some work
        $ins = p_get_instructions(io_readWikiPage($file, $id));
        if (!$isMD) {
            if ($cache->storeCache($ins)) {
                $run[$file] = true; // we won't rebuild these instructions in the same run again
            } else {
                msg('Unable to save cache file. Hint: disk full; file permissions; safe_mode setting.', -1);
            }
        }
        return $ins;
    }
}

2)inc/parserutils.php -> p_cached_output():

# 130
+ $isMD = strtolower(substr($file, -7)) === '.md.txt';
* if (!$isMD && $cache->useCache()) {

其他修改文件

  1. lib/plugins/markdownextra/lib/meltdown/js/jquery.meltdown.js
  2. lib/plugins/markdownextra/lib/meltdown/css/meltdown.css
  3. lib/plugins/markdownextra/lib/meltdown-tweaks/meltdown-tweaks.js
  4. lib/plugins/markdownextra/lib/meltdown-tweaks/meltdown-tweaks.css
  5. lib/scripts/editor.js

svgedit

lib/plugins/svgedit/script.js

* var svgeditor_path = DOKU_BASE+'lib/plugins/svgedit/svg-edit/'; //offline

网上下载embedapi.js文件放到 lib/plugins/svgedit/svg-edit/ 目录下。

外部链接新tab打开

在conf/local.php加上:

$conf['target']['extern'] = '_black';

copypage

修改图片顺序

修改右侧“页面复制”按钮的顺序由默认的倒数第一变为倒数第二。

lib/plugins/copypage/action.php

public function add_tool_button(Doku_Event &$event, $param) {
    $newitem = '<li>' .
        '<a href="#" class="action copypage copypageplugin__copy" rel="nofollow">' .
        '<span>' .
        $this->getLang('copypage') .
        '</span>' .
        '</a>' .
        '</li>';
    $offset = count($event->data['items']) - 1;
    $event->data['items'] =
        array_slice($event->data['items'], 0, $offset, true) +
        array('copypage' => $newitem) +
        array_slice($event->data['items'], $offset, null, true);
}

添加中文翻译

进入 lib/plugins/copypage/lang

cp -r en zh

修改lang.php

<?php
$lang['copypage'] = '页面复制';
$lang['js']['enter_page_id'] = "输入新文档地址";
$lang['js']['different_id_required'] = '不能和当前文档重名.';

S5

可添加一个自定义的模板,自定义样式。

imgpaste

图片粘贴后默认是wiki语法,如果是Markdown类型的文档则需要做兼容性支持:

lib/plugins/imgpaste/script.js

// insert syntax and close dialog
success: function (data) {
    $box.find('.content').addClass('success').text(data.message);
    var _id = getUrlParam('id');
    if (_id == null || _id.substr(-3, 3).toLowerCase() != '.md') {
        insertAtCarret('wiki__text', '{{ :' + data.id + ' |}}');
    } else {
       insertAtCarret('wiki__text', '![Title](/lib/exe/fetch.php?media=' + data.id + ')');
    }
    $box.delay(500).fadeOut(500, function () {
        $box.dialog('destroy').remove()
    });
},

配置设置

管理 -> 模板样式设置

  1. 全站的宽度 :100em
  2. 侧边栏的宽度 :18em

修改样式

1、lib/scripts/editor.js

300px -> 550px;

2、conf目录下添加userstyle.css文件:

.dokuwiki div.page, .dokuwiki .pageId span, pre.code, pre, code {
    box-shadow: none;
}

.dokuwiki .group p, .dokuwiki li, .dokuwiki dd {
    line-height: 1.7;
}

.dokuwiki div.page {
    border: 1px solid #a7d7f9;
    font-size: 15px;
    padding: 1.556em 1.5em 1.5em;
}

#dokuwiki__pagetools {
    top: 2.5em;
}

div.mode_edit #dokuwiki__header, div.mode_preview #dokuwiki__header, div.mode_draft #dokuwiki__header, div.mode_recover #dokuwiki__header {
    display: none;
}

div.mode_edit #dokuwiki__content div.page, div.mode_preview #dokuwiki__content div.page, div.mode_draft #dokuwiki__content div.page, div.mode_recover #dokuwiki__content div.page {
    padding: 10px 0px 0px 0px;
    background: inherit;
    border: 0px;
}

div.mode_edit #dokuwiki__footer, div.mode_preview #dokuwiki__footer, div.mode_draft #dokuwiki__footer, div.mode_recover #dokuwiki__footer {
    display: none;
}

div.mode_edit .dokuwiki.hasSidebar div.preview, div.mode_preview div.preview, div.mode_draft div.preview, div.mode_recover div.preview {
    border-right: 0px;
    background: #fff;
}

div.mode_edit .dokuwiki.hasSidebar div.preview, div.mode_preview div.preview .pad, div.mode_draft div.preview .pad, div.mode_recover div.preview .pad {
    padding-left: 5px;
    padding-right: 5px;
}

div.mode_edit textarea, div.mode_preview textarea, div.mode_draft textarea, div.mode_recover textarea {
    padding-left: 5px;
    padding-right: 5px;
}

div.mode_edit #edbtn__save, div.mode_preview #edbtn__save, div.mode_draft #edbtn__save, div.mode_recover #edbtn__save {
    background: #4285f4;
    border: 1px solid #4285f4;
    color: #fff;
}

div.mode_edit div.docInfo, div.mode_preview div.docInfo, div.mode_draft div.docInfo, div.mode_recover div.docInfo {
    display: none;
}

div.mode_edit button, div.mode_preview button, div.mode_draft button, div.mode_recover button {
    padding: .1em 1.5em;
}

div.mode_edit #dokuwiki__pagetools, div.mode_preview #dokuwiki__pagetools, div.mode_draft #dokuwiki__pagetools, div.mode_recover #dokuwiki__pagetools {
    top: 6em;
}

html, body {
    background: #fbfaf9;
    -webkit-font-smoothing: antialiased;
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}

a:link, a:visited, a:hover, a:active {
    color: #4183c4
}

form.search {
    display: inline-block; /*float: left;*/
}

#dokuwiki__header .tools li#site_home_l {
    float: left;
    margin-left: 0px;
    font-size: 20px;
    font-weight: bold;
    margin-top: -6px;
}

div.mode_show #dokuwiki__content div.page pre.code, div.mode_edit #dokuwiki__content div.page pre.code {
    background: #f6f8fa;
    border: 0px;
    font-size: 14px;
}

div.mode_show #dokuwiki__content div.page code {
    font-size: 14px;
}

div.mode_show #dokuwiki__content div.page pre, div.mode_edit #dokuwiki__content div.page pre {
    background: #f6f8fa;
    border: 0px;
    box-shadow: none;
}

.dokuwiki dl.code dt, .dokuwiki dl.file dt {
    background: #f6f8fa;
    border: 0px;
}

div.mode_show p code {
    padding: 3px;
}

div.mode_show p a {
    padding: 0 3px;
}

.dokuwiki .page ul li, .dokuwiki .aside ul li, .dokuwiki .page ol li, .dokuwiki .aside ol li {
    color: #333;
}

.dokuwiki .fc_flowchartjs {
    overflow: auto;
    background: #f6f8fa;
}

#dokuwiki__site .wrapper {
    margin-top: -10px;
}

form.search input {
    padding: .35em 22px .35em 0.5em;
    border: 1px solid #a7d7f9;
    box-shadow: none;
}

code {
    background: #f6f8fa;
}

.code .re0 {
    color: #008;
}

strong, b {
    color: #555;
}

.mode_preview #dokuwiki__content div.preview {
    border: 1px solid #a7d7f9;
    padding: 1.556em 1.5em 1.5em;
    margin: 0 0 0 0;
}

.mode_preview #dokuwiki__content div.preview div.pad {
    margin: 0px;
    padding: 0px;
}

#dw__toc {
    margin: -1.556em -1em .5em 1.4em;
}

pre.code, pre, code {
    border: 0px;
    background: #f6f8fa;
    font-size: 14px;
}

#dokuwiki__usertools {
    position: inherit;
}

#dokuwiki__header .headings, #dokuwiki__header .tools {
    width: 100%;
    margin-bottom: 5px;
    margin-top: 0px;
}

#dokuwiki__header {
    padding: 1em 0 1.5em;
}

#size__ctl img {
    margin-left: 10px;
}

fieldset {
    border-radius: 5px;
}

fieldset input {
    padding: 3px;
}

#wiki__text {
    height: 500px;
}

.dokuwiki .editBar .summary input.missing {
    background-color: #fff;
}

#TZT {
    display: none;
}

.dokuwiki div.toolbar {
    margin-top: .5em;
}

.dokuwiki .wrapper {
    margin-bottom: 0px;
}

#wiki__editbar .summary span, #draft__status {
    color: #555;
    font-size: 13px;
}

table tbody tr:hover {
    background: #f6f8fa
}

div.export {
    background: #fff;
    padding: 10px;
}

div.export #dw__toc {
    margin-top: 0px;
}

#dokuwiki__content ul li {
    list-style: disc;
}

#dokuwiki__content ul li ul li {
    list-style: circle;
}

#dokuwiki__content #dw__toc ul li {
    list-style: circle;
}

#dokuwiki__content div.meltdown_bar ul li {
    list-style: none;
}

#dokuwiki__content div.diagram {
    margin-bottom: 1.5em;
}

#dokuwiki__content div.flowchartjs {
    overflow: auto;
    background: #f6f8fa;
}

.noteclassic, .noteimportant, .notewarning, .notetip {
    border-radius: 0px;
}

#dokuwiki__content h1,#dokuwiki__content h2{
    border-bottom: 1px solid #eaecef;
    padding-bottom: 0.3em;
}

.dokuwiki .plugin_wrap{
    overflow: auto;
}

ul, ol{
    padding: 0 0 0 0;
}
.dtree{
    font-size:13px;
}
.dokuwiki a.wikilink1{
    color:#08c;
}
del{
    color:#808080;
}
span.todohlght:hover{
    background: #eee;
    border-radius: 3px;
}
div.mode_show p a{
    display: inline-block;
}
.mode_search dl.search_results dt{
    color: #999;
    font-size:small;
}
dl.search_results dt a.wikilink1{
    color: #609;
    font-size: 18px;
}
.mode_search dl.search_results dd{
    color: #000;
    font-size:small;
    margin: 0 0 26px 0;
}
.mode_search .search_results strong.search_hit{
    color: #dd4b39;
    background: none;
}
.mode_search .page h1.sectionedit1,.mode_search .page div.level1 p{
    display: none;
}
.dokuwiki div.search_quickresult ul li{
    width: 24em;
}
.dokuwiki div.editbutton_table form div.no button, .dokuwiki div.editbutton_table form div.no input.button{
    background: #fefefe;
    border: 1px solid #eee;
    margin-left:0px;
}
.noteclassic, .noteimportant, .notewarning, .notetip{
    min-height: auto;
}
blockquote{
    color: #6a6a6a;
}

.dokuwiki .pageId {
    float: left;
    margin-top: 0px;
    padding-left: 0px;
    position: absolute;
    width: 5em;
    padding: 5px 0;
}

.dokuwiki .pageId #short-url{
    margin-top: -10px;
    width: 10px;
    height: 12px;
    border:#fff 0px solid;
    background: #fff;
    color: #fff;
    margin-left: 2px;
}

.dokuwiki .pageId #copy-url-btn {
    width: 5em;
    height: 20px;
    line-height: 20px;
    position: absolute;
    left: 1px;
    top: 1px;
    background: #eee;
    color: #08c;
    border: 0;
    border-radius: 0 0 5px 0;
    font-size: 13px;
    padding: 0px;
}

3、lib/tpl/dokuwiki/tpl_header.php 改为:

<?php
/**
 * Template header, included in the main and detail files
 */

// must be run from within DokuWiki
if (!defined('DOKU_INC')) {
    die();
}
?>

<!-- ********** HEADER ********** -->
<div id="dokuwiki__header">
    <div class="pad group">
        <?php tpl_includeFile('header.html') ?>
        <div class="tools group">
            <!-- USER TOOLS -->
            <?php if ($conf['useacl']): ?>
                <div id="dokuwiki__usertools">

                    <h3 class="a11y"><?php echo $lang['user_tools']; ?></h3>
                    <ul>
                        <li id="site_home_l">
                            <?php
                            tpl_link(
                                wl(),
                                $conf["title"]
                            );
                            ?>
                        </li>
                        <li><?php tpl_searchform(); ?></li>
                        <?php
                        if (!empty($_SERVER['REMOTE_USER'])) {
                            echo '<li class="user">';
                            tpl_userinfo(); /* 'Logged in as ...' */
                            echo '</li>';
                        }
                        tpl_toolsevent('usertools', array(
                            tpl_action('recent', true, 'li', true),
                            tpl_action('media', true, 'li', true),
                            tpl_action('index', true, 'li', true),
                            tpl_action('admin', true, 'li', true),
                            tpl_action('profile', true, 'li', true),
                            tpl_action('register', true, 'li', true),
                            //tpl_action('login', true, 'li', true)
                        ));
            if ($_SERVER['REMOTE_USER']) {
                tpl_toolsevent('usertools', array(
                                 tpl_action('login', true, 'li', true)
                            ));
            }
                        ?>
            <?php
            function get_current_url(){ 
                            $current_url='http://'; 
                            if(isset($_SERVER['HTTPS'])&&$_SERVER['HTTPS']=='on'){ 
                                $current_url='https://'; 
                            }           
                            if($_SERVER['SERVER_PORT']!='80'){ 
                                $current_url.=$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI']; 
                            }else{ 
                                $current_url.=$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; 
                            } 
                           return $current_url; 
                      }
                      if (empty($_SERVER['REMOTE_USER'])) {
                          echo '<li><a href="https://auth.xxx.com/api/login/?camefrom=dokuwiki&login_url='.rawurlencode(get_current_url()).'" class="action login" rel="nofollow" title="登录">登录</a></li>';
                      }
                      ?>
                    </ul>
                </div>
            <?php endif ?>
        </div>

        <!-- BREADCRUMBS -->
        <?php if ($conf['breadcrumbs'] || $conf['youarehere']): ?>
            <div class="breadcrumbs">
                <?php if ($conf['youarehere']): ?>
                    <div class="youarehere"><?php tpl_youarehere() ?></div>
                <?php endif ?>
                <?php if ($conf['breadcrumbs']): ?>
                    <div class="trace"><?php tpl_breadcrumbs() ?></div>
                <?php endif ?>
            </div>
        <?php else: ?>
            <div class="breadcrumbs">
            </div>
        <?php endif ?>

        <hr class="a11y"/>
    </div>
</div><!-- /header -->

4、lib/tpl/dokuwiki/css/content.less

.dokuwiki.hasSidebar div.preview -> border-right:0px;
[dir=rtl] .dokuwiki.hasSidebar div.preview -> border-left:0px;

.dokuwiki table.inline tr:hover td {
*    //background-color: @ini_background_alt;
}

.dokuwiki table.inline tr:hover th {
  //background-color: @ini_border;
}

5、lib/tpl/dokuwiki/main.php

<!--<div class="pageId"><span><?php echo hsc($ID) ?></span></div>-->

6、lib/tpl/dokuwiki/css/mobile.less

#dokuwiki__aside > .pad, [dir=rtl] #dokuwiki__aside > .pad
border: 1px solid #a7d7f9;
/* box-shadow: 0 0 .5em @ini_text_alt;*/

7、inc/html.php

if(false && $wr && $conf['license']){...}

8、lib/tpl/dokuwiki/css/basic.less

address {
    margin: 0 0 0.85em 0; /* bottom margin = line-height */
    padding: 0;
}

短链

安装 ShortURL Plugin 插件 修改 lib/tpl/dokuwiki/main.php

# 61
function copyShortUrl() {
    var obj = document.getElementById("short-url");
    obj.select();
    document.execCommand("Copy");
    document.getElementById("copy-url-btn").innerText = "已复制";
}
</script>
<?php
    $shorturl =& plugin_load('helper', 'shorturl');
    if ($shorturl) {
        $shortID = $shorturl->autoGenerateShortUrl($ID);
        $schema = $_SERVER['HTTPS'] ? 'https://' : 'http://';
        $shortUrl = $schema . $_SERVER['HTTP_HOST'] . '/' . $shortID;
        echo "<div class=\"pageId\">";
        echo "<input type=\"text\" id=\"short-url\" value=\"{$shortUrl}\">";
        echo "<button id=\"copy-url-btn\" onclick=\"copyShortUrl()\">复制短链</button>";
        echo "</div>";
    }
?>

修改index.php

# 开头加上
$shortId = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
if(preg_match('/\/[a-z0-5]{6}/', $shortId)) {
    header("Location: /x.php?id=" . $shortId);
    exit;
}

自定义注册 & 登录支持

案例:在根目录下新加 login.php 文件,内容为:

<?php
define('AUTH_APP_KEY', 'xxx');
define('DEFAULT_USER_PASS', '123456');
define('LOGIN_TOKEN_SALT', 'xxx');

if (isset($_GET['_ltoken']) && isset($_GET['username']) && isset($_GET['fullname']) && isset($_GET['mail'])) {
    $token = md5($_GET['username'] . $_GET['fullname'] . $_GET['mail'] . LOGIN_TOKEN_SALT);
    if ($token != $_GET['_ltoken']) {
        exit('Login token verify failed!');
    }
    $userInfo = array(
        'username' => $_GET['username'],
        'fullname' => rawurldecode($_GET['fullname']),
        'mail' => $_GET['mail'],
    );
} else {
    if (!isset($_REQUEST['token']) || !isset($_REQUEST['username'])) {
        exit('Paramater invalid.');
    }
    $sessionId = md5($_REQUEST['token'] . AUTH_APP_KEY . $_REQUEST['username']);
    $resp = file_get_contents('https://auth.xxx.com/api/info/?session_id=' . $sessionId);
    $userInfo = @json_decode($resp, true);
    if (!isset($userInfo['username']) || !isset($userInfo['fullname']) || !isset($userInfo['mail'])) {
        exit($resp);
    }
}
$username = $userInfo['username'];
$fullname = $userInfo['fullname'];
$mail = $userInfo['mail'];
$_POST['u'] = $username;
$_POST['p'] = DEFAULT_USER_PASS;
$_SERVER['REQUEST_METHOD'] = 'post';
//========Doku========

if (!defined('DOKU_INC')) {
    define('DOKU_INC', dirname(__FILE__) . '/');
}

// define all DokuWiki globals here (needed within test requests but also helps to keep track)
global $ACT, $INPUT, $QUERY, $ID, $REV, $DATE_AT, $IDX, $DATE, $RANGE, $HIGH, $TEXT, $PRE, $SUF, $SUM, $INFO, $JSINFO;

$ACT = 'login';
// load and initialize the core system
require_once(DOKU_INC . 'inc/init.php');
// check user exists.
$authplain = new auth_plugin_authplain();
// user not exists & create a new user.
if ($authplain->getUserData($username) == false) {
    if ($authplain->createUser($username, DEFAULT_USER_PASS, $fullname, $mail, 'user') == null) {
        exit('Login failed(MESSAGE:write user infomation to file failed;CODE:1024)...');
    }
    $query = array(
        'username' => $username,
        'fullname' => rawurlencode($fullname),
        'mail' => $mail,
        '_ltoken' => md5($username . rawurlencode($fullname) . $mail . LOGIN_TOKEN_SALT)
    );
    $location = '/login.php?' . http_build_query($query, null, '&');;
    header('Location: ' . $location);
}

//import variables.
$id = 'sitemap';
if (!empty($_REQUEST['login_url'])) {
    $urlArr = parse_url(rawurldecode($_REQUEST['login_url']));
    if (!empty($urlArr['query'])) {
        $tArr = explode('&', $urlArr['query']);
        foreach ($tArr as $param) {
            $tItem = explode('=', $param);
            if (count($tItem) == 2 && $tItem[0] == 'id') {
        $id = $tItem[1];
        break;
            }
        }
    }
}
//$INPUT->set('id', str_replace("\xC2\xAD", '', $INPUT->str('id')));
$INPUT->set('id', str_replace("\xC2\xAD", '', $id));
$QUERY = trim($INPUT->str('id'));
$ID = getID();
$INFO = pageinfo();

// No event data
$tmp = array();
trigger_event('DOKUWIKI_STARTED', $tmp);

//close session
session_write_close();

//do the work (picks up what to do from global env)
act_dispatch();

$tmp = array(); // No event data
trigger_event('DOKUWIKI_DONE', $tmp);

其他

  1. inc/lang/{zh,en}/edit.txt \ 内容清空.
  • 上一篇: http2
  • 下一篇: 没有了