blob: 0742fe847687e8e32750375fbba8e2726bfa0506 [file] [log] [blame]
<?php
/*******************************************************************************
* Copyright (c) 2011 Eclipse Foundation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Wayne Beaton (Eclipse Foundation)- initial API and implementation
*******************************************************************************/
function getRandomEdpFaqHtml() {
return getRandomEdpFaq()->toHtml();
}
function parseEdpPreamble($line, &$entries) {
if (preg_match('/^=/', $line)) return 'parseEdpContent';
return 'parseEdpPreamble';
}
function parseEdpContent($line, &$entries) {
if (trim($line)) $entries[] = $line;
return 'parseEdpContent';
}
function getrandomEdpFaq() {
$all = split("\n", file_get_contents("http://wiki.eclipse.org/index.php?title=EDP%20Wisdom&action=raw"));
$entries = array();
$parse = 'parseEdpPreamble';
foreach ($all as $line) {
$parse = $parse($line, $entries);
}
return new FAQ($entries[array_rand($entries)]);
}
class FAQ {
var $text;
function __construct($text) {
$this->text = $text;
}
function toHtml() {
$content = $this->getContent();
return "<div>$content</div>";
}
/**
* @internal
* @return string
*/
function getRawContent() {
return $this->text;
}
function getContent() {
$content = $this->getRawContent();
// Double \n is replaced with paragraph tags.
$content = preg_replace('/(\n+)(\n|$)/', '<p>$1</p>', $content);
$content = preg_replace('/\[\[Category:([^\]]+)\]\]/', '', $content);
// Fix image tags
$content = preg_replace_callback('/\[\[Image:([^\]]+)\]\]/', 'fixMediaWikiImageUrl', $content);
// Fix link tags
$content = preg_replace('/\[\[\s*([^\|]+)\s*\|\s*([^\]]+)\]\]/', '<a href="http://wiki.eclipse.org/$1" target=\"_blank\">$2</a>', $content);
$content = preg_replace('/\[\[([^\]\|]+)\]\]/', '<a href="http://wiki.eclipse.org/$1" target=\"_blank\">$1</a>', $content);
$content = preg_replace('/\[(https?:\/\/[^\s\]]+)\]/', '<a href="$1" target=\"_blank\">$1</a>', $content);
$content = preg_replace('/\[(https?:\/\/[^\s\]]+)\s+([^\]]+)\]/', '<a href="$1" target=\"_blank\">$2</a>', $content);
// Get rid of everything else
$content = preg_replace('/\[\[[^\]]*\]\]/', '', $content);
return trim($content);
}
}
function fixMediaWikiImageUrl($matches) {
$name = ucfirst($matches[1]);
$md5 = md5($name);
$part1 = substr($md5, 0, 1);
$part2 = substr($md5, 0, 2);
return "<img src=\"http://wiki.eclipse.org/images/$part1/$part2/$name\"/>";
}
?>