1. 程式人生 > >dokuwiki安裝使用教程(支援中文、editor.md、貼上上傳圖片)

dokuwiki安裝使用教程(支援中文、editor.md、貼上上傳圖片)

一、安裝

  1. 在網站一欄輸入:http://你的ip地址/Dokuwiki/install.php 在右上角選好語言,簡體中文zh
  2. 然後會彈出這樣的安裝示意圖,啟用ACL(推薦)
  3. 根據上示意圖,填寫資訊。分別填寫超級使用者,全名,E-mail,密碼等資訊。
  4. 安裝結束後,為了安全,要刪除install.php檔案,然後再用剛才設定的使用者名稱密碼登陸。
  5. 關於安全: http://你的ip地址/data/pages/wiki/dokuwiki.txt
  6. 訪問http://你的ip地址/dokuwiki/doku.php

二、配置

  1. 點選admin處的管理 按鈕,然後出現頁面
  2. 可分別根據使用者管理、訪問控制列表等來進行設定管理。

三、中文檔名的亂碼問題

  1. 在伺服器機子上 D:\phpStudy\WWW\dokuwiki\conf\local.php
    在最後一行加上:
$conf['fnencode']='gb2312'; #注意分號不能少。 
  1. 在伺服器機子上 D:\phpStudy\WWW\dokuwiki\Inc\pageutils.php
    修改兩個函式:utf8_encodeFN函式
function utf8_encodeFN($file,$safe=true){
    global $conf;
    if($conf['fnencode'] == 'utf-8') return
$file; if($safe && preg_match('#^[a-zA-Z0-9/_\-\.%]+$#',$file)){ return $file; } if($conf['fnencode'] == 'safe'){ return SafeFN::encode($file); } // 中文支援 if ($conf['fnencode']=='gb2312'){ return iconv('UTF-8','GB2312',$file); } $file = urlencode($file
); $file = str_replace('%2F','/',$file); return $file; }

和utf8_decodeFN函式:

function utf8_decodeFN($file){
    global $conf;
    if($conf['fnencode'] == 'utf-8') return $file;

    if($conf['fnencode'] == 'safe'){
        return SafeFN::decode($file);
    }
    // 中文支援
    if ($conf['fnencode']=='gb2312'){
        return iconv('GB2312','UTF-8',$file);
    }
    return urldecode($file);
}

四、安裝外掛

  1. 新增頁面Add New Page外掛
  2. Markdown外掛PHP Markdown Extra plugin
  3. 側邊欄
    1)安裝simplenavi外掛
    2) 新建data\pages\sidebar.txt,內容如下:
===== 導航目錄 =====
{{simplenavi>}}
===== 新增新頁面 =====
{{NEWPAGE}}

五、支援editer.md

2.解壓到dokuwiki\lib\editor.md\

3.替換/inc/form.php裡的函式form_wikitext($attrs),修改return結果

function form_wikitext($attrs) {
    // mandatory attributes
    unset($attrs['name']);
    unset($attrs['id']);
    $text = str_replace("<markdown>\n",'',$attrs['_text']);
    $text = str_replace("\n</markdown>",'',$text);
    /*
    return '<textarea name="wikitext" id="wiki__text" dir="auto" '
                 .buildAttributes($attrs,true).'>'.DOKU_LF
                 .formText($attrs['_text'])
                 .'</textarea>';
    */
    return '<div id="editormd" contenteditable="true"><textarea name="wikitext">'.DOKU_LF.formText($text)
    .'</textarea></div>';
}

4.在/inc/parser/xhtml.php裡更改cdata函式

    function cdata($text) {
        //$this->doc .= $this->_xmlEntities($text);
        return $this->doc.=$text;
    }

替換原因是:因為以前是純字元編輯器,會將一些特殊符號進行過濾,比如:<>等等.而替換之後的xheditor本身已經做了一次過濾了,再次過濾就會導致字元<變成&lt,因此去掉這段之後,就只過濾一次

5.inc/actions.php的act_save函式

saveWikiText($ID,con($PRE,$TEXT,$SUF,true),$SUM,$INPUT->bool('minor')); //use pretty mode for con
替換成
saveWikiText($ID,con($PRE,"<markdown>\n".$TEXT."\n</markdown>",$SUF,true),$SUM,$INPUT->bool('minor')); //use pretty mode for con

6.在/lib/tpl/dokuwiki/main.php新增editor.md包
head節中新增

<link rel="stylesheet" href="<?php echo DOKU_BASE;?>lib/editor.md/css/editormd.min.css" />

body節中新增

    <script src="<?php echo DOKU_BASE;?>lib/editor.md/examples/js/jquery.min.js"></script>
    <script src="<?php echo DOKU_BASE;?>lib/editor.md/editormd.js"></script>
    <script type="text/javascript">
        var testEditor;
        $(function(md) {
            testEditor = editormd("editormd", {
                width: "100%",
                height: 740,

                path: '<?php echo DOKU_BASE;?>lib/editor.md/lib/',
                theme: "dark",
                /*
                previewTheme : "dark",
                editorTheme : "pastel-on-dark",
                markdown : md,
                */
                codeFold: true,
                syncScrolling: "single",

                saveHTMLToTextarea: true, // 儲存 HTML 到 Textarea
                searchReplace: true,
                //watch : false,                                // 關閉實時預覽
                htmlDecode: "style,script,iframe|on*", // 開啟 HTML 標籤解析,為了安全性,預設不開啟
                //toolbar    : false,                         //關閉工具欄
                //previewCodeHighlight : false, // 關閉預覽 HTML 的程式碼塊高亮,預設開啟
                emoji: true,
                taskList: true,
                tocm: true, // Using [TOCM]
                tex: true, // 開啟科學公式TeX語言支援,預設關閉
                flowChart: true, // 開啟流程圖支援,預設關閉
                sequenceDiagram: true, // 開啟時序/序列圖支援,預設關閉,
                //dialogLockScreen : false,     // 設定彈出層對話方塊不鎖屏,全域性通用,預設為true
                //dialogShowMask : false,         // 設定彈出層對話方塊顯示透明遮罩層,全域性通用,預設為true
                //dialogDraggable : false,        // 設定彈出層對話方塊不可拖動,全域性通用,預設為true
                //dialogMaskOpacity : 0.4,        // 設定透明遮罩層的透明度,全域性通用,預設值為0.1
                //dialogMaskBgColor : "#000", // 設定透明遮罩層的背景顏色,全域性通用,預設為#fff
                imageUpload: true,
                imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
                imageUploadURL: "<?php echo DOKU_BASE;?>uploadimg.php",
                onload: function() {}
            });
        });

        window.onload = function() {
            //document.getElementById("editormd").addEventListener('paste', function (event) {
            $("#editormd").on('paste', function(event) {
                //console.log(event);
                var items = (event.clipboardData || event.originalEvent.clipboardData).items;
                for (var index in items) {
                    var item = items[index];
                    //console.log(item);
                    if (item.kind === 'file') {
                        var blob = item.getAsFile();                        
                        var reader = new FileReader();
                        reader.onload = function(event) {                            
                            var base64 = event.target.result;
                            console.log(base64);
                            //ajax上傳圖片
                            $.post("<?php echo DOKU_BASE;?>uploadimg.php", {
                                screenshots: base64
                            }, function(rets) {
                                ret = JSON.parse(rets);
                                //layer.msg(ret.msg);
                                console.log(ret);
                                if (ret.success === 1) {
                                    //新一行的圖片顯示
                                    testEditor.insertValue("\n![](" + ret.url + ")");
                                } else {
                                    alert("截圖上傳失敗:" + ret.message);
                                }
                            });
                        };
                        reader.readAsDataURL(blob);
                    }
                }
            });
        }
    </script>

7.貼上圖片自動上傳支援
修改檔案data/.htaccess中的Deny from all改為allow from all
上傳圖片儲存程式碼:

<?php
if(!defined('DOKU_INC')) define('DOKU_INC', dirname(__FILE__).'/');
require_once(DOKU_INC.'inc/init.php');

$hostpath=getBaseURL(false);
$attachDir='/data/media/editor/';//上傳檔案儲存路徑,結尾不要帶/
$maxAttachSize = 2*1024*1024;  //最大上傳大小,預設是2M

function upEditorImg(){
    global $hostpath, $attachDir, $maxAttachSize;
    //獲取檔案的大小
    $file_size=$_FILES['editormd-image-file']['size'];
    //echo "$file_size $maxAttachSize";
    if($file_size > $maxAttachSize) {
        echo '{"success":0,"message":"不能上傳大於2M的檔案"}';
        return false;
    }

    //獲取檔案型別
    $file_type=$_FILES['editormd-image-file']['type'];
    if($file_type!="image/jpeg" && $file_type!='image/pjpeg' && $file_type!="image/png") {
        echo '{"success":0,"message":"圖片格式異常"}';
        return false;
    }

    //判斷是否上傳成功(是否使用post方式上傳)
    if(is_uploaded_file($_FILES['editormd-image-file']['tmp_name'])) {
        //把檔案轉存到你希望的目錄(不要使用copy函式)
        $uploaded_file=$_FILES['editormd-image-file']['tmp_name'];

        //我們給每個使用者動態的建立一個資料夾
        $save_path=$_SERVER['DOCUMENT_ROOT'].$hostpath.$attachDir;
        //判斷該使用者資料夾是否已經有這個資料夾
        if(!file_exists($save_path)) {
            mkdir($save_path);
        }

        //$move_to_file=$save_path."/".$_FILES['editormd-image-file']['name'];
        $file_true_name=$_FILES['editormd-image-file']['name'];
        $move_file_name=time().rand(1,1000).substr($file_true_name,strrpos($file_true_name,"."));
        $move_to_file=$save_path.$move_file_name;
        //echo "$uploaded_file   $move_to_file";
        if(move_uploaded_file($uploaded_file,iconv("utf-8","gb2312",$move_to_file))) {
            //echo $_FILES['editormd-image-file']['name']."上傳成功";
            //echo '{"success":1,"message":"上傳成功", "url":"'.$hostpath.$attachDir.$move_file_name.'"}';
            $result=array(
              'success'=> 1,
              'message'=>'上傳成功',
              'url'=>$hostpath.$attachDir.$move_file_name
            );
            echo json_encode($result);
        } else {
            //echo "上傳失敗";
            echo '{"success":0,"message":"伺服器儲存檔案失敗"}';
        }
    } else {
        //echo "上傳失敗";
        echo '{"success":0,"message":"上傳失敗"}';
        return false;
    }
}

//$_POST= [screenshots] => data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAI0AAACcCAYAAABC1CibAAAL6UlEQVR4Ae2dbUiU6RrH...
function upEditorScreenshots(){
    global $hostpath, $attachDir, $maxAttachSize;

    $content = $_POST['screenshots'];

    if (preg_match('/^data:image\/(\w+);base64,(\S+)/', $content, $result)) {
        $file_type = $result[1];
        $base64data = $result[2];

        //echo "$file_type $base64data";
        $save_path = $_SERVER['DOCUMENT_ROOT'].$hostpath.$attachDir;
        if (!is_dir($save_path)) {
            mkdir($save_path, 0777);
        }

        $filedata = base64_decode($base64data);
        $filename = time().rand(1,1000).".".$file_type;
        if (!file_put_contents($save_path . $filename, $filedata)) {
            echo '{"success":0,"message":"伺服器儲存檔案失敗"}';
            return false;
        }
        unset($filedata);

        echo '{"success":1,"message":"上傳成功", "url":"'.$hostpath.$attachDir.$filename.'"}';
        return true;
    } else {
        echo '{"success":0,"message":"圖片格式異常"}';
        return false;
    }
}

//print_r($_POST);
//print_r($_FILES);

if(isset($_FILES['editormd-image-file'])){
    upEditorImg();
    exit();
}

if(isset($_POST['screenshots'])){
    upEditorScreenshots();
    exit();
}
?>