美国主机Lunarpages

Mediawiki中文技术论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 4332|回复: 0

MediaWiki扩展:随机展示

[复制链接]
发表于 2010-12-25 16:24:04 | 显示全部楼层 |阅读模式
RAKsmart美国服务器
This extension allows for randomly-generated content inside your wiki; e.g. rotating images, random greetings, etc.

Example

The code in this page will only work in MediaWiki 1.5 and above. (Alternate version for 1.4.x)

Algorithm 05:57, 8 September 2005 (UTC)

  1. <?php
  2. # Wiki random selection extension
  3. # Randomly chooses one of the given options to display.
  4. # Usage: <choose><option>A</option><option>B</option></choose>
  5. # Optional parameter: <option weight="3"> == 3x weight given

  6. $wgExtensionFunctions[] = "wfChooseExt";

  7. function wfChooseExt()
  8. {
  9.      global $wgParser;
  10.      $wgParser->setHook( "choose", "renderChosen" );
  11. }

  12. function renderChosen( $input )
  13. {
  14.      # Prevent caching
  15.      global $wgParser;
  16.      $wgParser->disableCache();

  17.      # Choose an option at random
  18.      $len = preg_match_all("/<option(?:(?:\\s[^>]*?)?\\sweight=["']?([^\\s>]+))?"
  19.          . "(?:\\s[^>]*)?>([\\s\\S]*?)<\\/option>/", $input, $out);
  20.      $r = 0;
  21.      for($i=0; $i<$len; $i++)
  22.      {
  23.          if(strlen($out[1][$i])==0) $out[1][$i] = 1;
  24.          else $out[1][$i] = intval($out[1][$i]);
  25.          $r += $out[1][$i];
  26.      }
  27.      if($r <= 0) return "";
  28.      $r = mt_rand(1,$r);
  29.      for($i=0; $i<$len; $i++)
  30.      {
  31.          $r -= $out[1][$i];
  32.          if($r <= 0)
  33.          {
  34.              $input = $out[2][$i];
  35.              break;
  36.          }
  37.      }

  38.      # Render chosen text
  39.      global $wgUser, $wgTitle;
  40.      $parserOptions = ParserOptions::newFromUser( $wgUser );
  41.      $localParser = new Parser();
  42.      $input = $localParser->parse($input, $wgTitle, $parserOptions, false);
  43.      return $input->mText;
  44. }
  45. ?>
复制代码

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

美国HostEase虚拟主机一年198元

Archiver|手机版|小黑屋|Mediawiki中文技术论坛

GMT+8, 2024-3-29 14:13 , Processed in 0.040468 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2024, Tencent Cloud.

快速回复 返回顶部 返回列表