-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateMenu.php
50 lines (44 loc) · 1.18 KB
/
generateMenu.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
<?php
/**
* This script looks at pages to extract
* subpages and will return an Appropedia-style
* menu in the form of wikitext.
*/
if ( !array_key_exists( 'title', $_GET ) ) {
exit( 'Title required' );
}
$title = $_GET['title'];
$title = preg_replace("/\s+/", "_", $title);
// Request subpages to the API.
$metadata = file_get_contents( "https://www.appropedia.org/w/api.php?action=query&list=allpages&aplimit=100&apprefix=$title/&format=json" );
$metadata = json_decode($metadata, true);
$metadata = $metadata["query"]["allpages"];
// Using the number of forward slashes as
// a parameter for the page's depth.
$pages = [];
foreach ($metadata as $m){
$pages[$m["title"]] = substr_count($m["title"], '/');
}
// Create the menu string for each page.
// It will generate asterisks according to
// the depth level.
$text_array = [];
foreach ($pages as $k => $p){
array_push($text_array,
str_repeat("*", $p)
. " [["
. $k
. "|"
. basename($k)
. "]]<br>"
);
}
$text = implode("", $text_array);
// Resulting text
echo(
"<code>{{Menu<br>
| class = sequence<br>
| title = [[" . preg_replace("/_/", " ", $title) . "]]<br>
| content =<br><br>"
. $text . "}}</code>"
);