public
Description: Allows site developers to easily leverage the awesome power of Wikipedia’s articles.
Homepage: http://thecodetrain.co.uk/code/wikislurp/
Clone URL: git://github.com/NeilCrosby/wikislurp.git
Click here to lend your support to: wikislurp and make a donation at www.pledgie.com !
wikislurp / index.php
100644 105 lines (89 sloc) 2.987 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
 
/**
* Controller page for the WikiSlurp application.
*
* @param secret A key used to allow access to the WikiSlurper.
* @param query The base query to search MediaWiki for.
* @param context Optional. Extra context to use to clarify the query.
* @param section Optional. The section number you would like data for. If
* not given, the entire article is returned.
* @param xpath Optional. XPath describing the data to be returned. If
* Not given, all data will be returned.
* @param output Optional. The output type for this page. Options are
* php, json. Default is php.
* @param keepjunk Optional. If any value is given then footnote, needs
* citation etc links will be kept if they exist. If no
* value is given then they will be stripped.
*
* @return An array containing the following keys: 'url', 'title', 'html'.
*
* @author Neil Crosby <neil@neilcrosby.com>
* @license Creative Commons Attribution-Share Alike 3.0 Unported
* http://creativecommons.org/licenses/by-sa/3.0/
**/
 
require_once('config/config.php');
require_once('classes/MediaWiki.php');
 
function removeMagicQuotes (&$postArray, $trim = false) {
if (!get_magic_quotes_gpc()) {
return;
}
 
foreach ($postArray as $key => $val){
if (is_array($val)) {
removeMagicQuotes ($postArray[$key], $trim);
} else {
if ($trim == true) {
$val = trim($val);
}
$postArray[$key] = stripslashes($val);
}
}
}
 
removeMagicQuotes($_GET);
 
$error = false;
 
// secret must be set in the config
if ( 0 == strlen($WIKI_SLURP_CONFIG['SECRET']) ) {
    $error = array(
        "error" => "Forbidden - Secret not set in config",
        "status" => 403,
    );
}
 
// Check that required parameters are given
 
if (!$error && ( !isset($_GET['secret']) || !$_GET['secret'] )) {
    $error = array(
        "error" => "Bad request - Secret not given",
        "status" => 400,
    );
}
 
if (!$error && ( !isset($_GET['query']) || !$_GET['query'] )) {
    $error = array(
        "error" => "Bad request - Query not given",
        "status" => 400,
    );
}
 
// check that secret matches config secret
 
if (!$error && ( $WIKI_SLURP_CONFIG['SECRET'] != $_GET['secret'] )) {
    $error = array(
        "error" => "Unauthorised - Secret is not valid",
        "status" => 401,
    );
}
 
if ( $error ) {
    $obj = $error;
} else {
    $wiki = new MediaWiki($WIKI_SLURP_CONFIG);
    $obj = $wiki->getArticle( $_GET );
}
 
if ( isset($obj['status']) && 200 != $obj['status'] ) {
    header("HTTP/1.0 {$obj['status']} {$obj['error']}");
}
 
$output = isset($_GET['output']) ? $_GET['output'] : '';
switch ( $output ) {
    case 'json':
        header("Content-type: application/json");
        echo json_encode($obj);
        break;
    default:
        // is this a suitable mimetype?
        header("Content-type: text/x-php");
        echo serialize($obj);
}