-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikibox.class.php
63 lines (51 loc) · 1.78 KB
/
wikibox.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
require_once 'wikirip.inc.php';
class WikiBox extends WikiRip {
var $test = null;
var $testText = null;
function __construct( $url, $cache = null, $cachedir = null ) {
parent::__construct( $url, $cache, $cachedir );
}
function pick_page( $listPage, $purge = false, $testPage = false ) {
$list = $this->fetchList( $listPage, $purge, $testPage );
if ( $testPage ) {
$page = WikiRip::normalizeTitle( "Web:{$testPage}" );
$this->cache_duration = 0; //disable cache for testing
$html = $this->rip_page( $page, 'render', true );
if ( !$html ) {
$html = '<i class="error">page not found: ' . htmlspecialchars( $page ) . '</i>';
} else if ( !in_array( $page, $list ) && !preg_match( '/wikibox-test/i', $html ) ) {
$html = '<i class="error">inactive feature page not marked with "wikibox-test": ' . htmlspecialchars( $page ) . '</i>';
}
} else {
$i = mt_rand( 0, count( $list ) - 1 );
$page = $list[$i];
$html = $this->rip_page( $page, 'render', $purge );
if ( !$html ) {
return false;
}
}
if ( $html ) {
$html = preg_replace( '!(<(span|div)[^<>]*>)?\s*BEGIN_NORIP.*?END_NORIP\s*(</\2 *>)?!', '', $html );
$html = preg_replace( '!(<(span|div)[^<>]*wikibox-test[^<>]*>).*(</\2 *>)!', '', $html );
}
return $html;
}
private function fetchList( $listPage, $purge = false, $testPage = false ) {
$list = $this->rip_page( $listPage, 'raw', $purge );
if ( !$list && !$testPage ) {
return false;
}
$list = preg_replace( '@<!--.*?-->@s', '', $list );
$items = array();
preg_match_all( '/^\*+.*\[\[(.+?)( *\|.*?)?\]\]/m', $list, $items, PREG_SET_ORDER );
if ( !$items && !$testPage ) {
return false;
}
$list = array();
foreach ( $items as $item ) {
$list[] = WikiRip::normalizeTitle( $item[1] );
}
return $list;
}
}