-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheoeapi.php.my
79 lines (65 loc) · 1.61 KB
/
eoeapi.php.my
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
$eoe = new eoecache($_GET['page']);
echo $eoe->show();
class eoecache
{
var $page;
var $filehash;
var $cachefile;
function __construct($page)
{
$this->page = $page;
$this->filehash = md5($page);
$this->cachefile = "eoeapi/{$this->filehash}.html";
}
function show(){
if(empty($this->page))
return false;
if($this->status())
return $this->get();
else
return $this->set();
}
function status()
{
if(!file_exists($this->cachefile))
return false;
//@TODO 添加过期时间
return true;
}
function set()
{
$url = "http://wiki.eoeandroid.com/api.php?action=parse&format=json&page=".$this->page;
$data = file_get_contents( $url );
$data = json_decode($data);
$text = (array)$data->parse->text;
$text = $this->format($text['*']);
$title = $data->parse->title;
$data = array("title"=>$title, "text"=>trim($text));
$data = json_encode($data);
file_put_contents($this->cachefile, $data);
return $this->get();
}
function get()
{
$data = file_get_contents($this->cachefile);
if(!empty($_GET['callback']))
{
return sprintf("%s(%s)", $_GET['callback'], $data);
}else{
return $data;
}
}
function format($data){
$filter = array(
'<!\-\-[\w\W]*?\-\->' =>'',
'<span class="editsection">.*?</span>' =>'',
'href="/' =>'href="http://wiki.eoeandroid.com/',
'src="/' =>'src="http://wiki.eoeandroid.com/',
'http://developer.android.com' =>'http://docs.eoeandroid.com',
);
foreach ($filter as $match=>$replace)
$data = preg_replace("@{$match}@is", $replace, $data);
return $data;
}
}