-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauto_node.php
More file actions
executable file
·59 lines (44 loc) · 1.27 KB
/
auto_node.php
File metadata and controls
executable file
·59 lines (44 loc) · 1.27 KB
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
<?php
$limit = isset($_POST['limit']) ? (int) $_POST['limit'] : 5;
$text = isset($_POST['text']) ? $_POST['text'] : '';
$url = 'http://%s.wikipedia.org/w/index.php?title=%s&action=edit';
// crawl wikipedia data
$html = mindmap__curlHelper(sprintf($url, 'de', urlencode($text)));
// if there is this string we are redirected to another page...
$pattern = '!#REDIRECT\[\[(.*)\]\]!U';
if(preg_match($pattern, $html, $matches))
{
$text = $matches[1];
$html = mindmap__curlHelper(sprintf($url, 'de', urlencode($text)));
}
preg_match_all('#\[\[([a-zA-Z0-9 _-]*)\]]#u', $html, $matches);
$nodes = array();
foreach($matches[1] as $m)
{
if(!empty($m) && !in_array($m, $nodes))
{
$nodes[] = $m;
}
if(count($nodes)>=$limit)
{
break;
}
}
/**
* Helper function to crawl a given url and return the content.
* @param String $url The url to open
* @return String The content of $url
*/
function mindmap__curlHelper($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
return curl_exec($ch);
}
header('Content-Type: application/xml');?><xml>
<?php foreach($nodes as $node):?>
<node><?php echo $node;?></node>
<?php endforeach;?>
</xml>