LoginSignup
0
1

More than 5 years have passed since last update.

pukiwikiにランダムページ機能を実装する

Last updated at Posted at 2017-12-06

pukiwiki内のページにランダムにアクセスできたら面白いよねって感じで、作ってみました。

プラグインの作成

ページ一覧を取得し、その中からランダムに1つ選択
取得したページへのリンクを張る。

randomlink.inc.php
<?php

function plugin_randomlink_convert()
{
    $pages = array();
    $i = 0;

    //すべてのページ一覧を取得
    $pages = get_existpages();

    //ページ数をカウント
    $size = count($pages);
    //ランダムで一つのページをとる
    $rand_page = array_rand($pages, 1);
    //.txt拡張子名削除
    $rand_page = substr($rand_page, 0, -4);
    //ページ名の整形(分割、2文字ごとに%を入れる)
    $randpage_array = str_split($rand_page, 2);

    while($i < count($randpage_array)){
        $randpage_array[$i] = "%".$randpage_array[$i]; 
        $i++;
    } 
    $rand_page = join('', $randpage_array);

    //htmlの形にする
    $href   = get_page_uri($rand_page);

    //%記号がなぜかエンコーディングされるので、とりあえず25の部分を消去
    $href = str_replace("25", "", $href);

    //終了
    $res = '<a href='.$href.'>ランダムページ</a>';
    return $res;
}

//ページのファイル名からURLに変更する関数
function get_page_uri($page, $query = '')
{
    if (function_exists('get_script_uri')) { // from pukiwiki 1.4
        $url = get_script_uri() . '?' . rawurlencode($page);
    } else {
        global $script;
        $url = $script . '?' . rawurlencode($page);
    }
    if ($query != '') {
        $url .= '&amp;' . $query;
    }
    return $url;
}

?>

これをpluginフォルダに保存。
Wiki内のランダムページへのリンクを張りたい部分に

#randomlink

を記述

課題

ランダムページへのリンクはリンクを張るページが生成された時点で作成される。
そんなわけで、「ランダムページに飛ぶ」→「戻る」→「ランダムページに飛ぶ」だと同じページに飛ぶ。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1