-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwiki2api.php
315 lines (256 loc) · 9.26 KB
/
wiki2api.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php /* 311 Lines */
/* PHP-Wiki-API: This is a simple class to get short Wikipedia info boxes from a given Keyword.
*
* @package PHP-Wiki-API
* @copyright Copyright (c) 2019 Igor Gaffling <pro@gaffling.com>
* @license https://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL License
* @version Release: @1.1@
* @link https://github.com/gaffling/PHP-Wiki-API
* @since Class available since Release 1.0
*
* @example <php>
* require_once __DIR__.'/wiki2api.php'; // Include the Wikipedia API Class
* $wiki = new wiki(); // Start the Wikipedia API Class
* echo $wiki->api($_GET['q']); // Output the API Response
* </php>
*/
class wiki
{
// Read and set Parameters
public function __construct($params=array())
{
// Default Values
$defaults = array(
'language' => 'de',
'userAgent' => 'WikiBot/1.0 (+http://'.$_SERVER['SERVER_NAME'].'/)',
'betterResults' => true,
'proxy' => '',
'imageProxy' => true,
'DEBUG' => '',
);
// Merge Parameters and Defaults
$this->params = array_merge($defaults, $params);
}
// Helper Function to get the Content from the API URL
private function getContent($url, $user_agent, $proxy='')
{
// Hopfully we run PHP 4 >= 4.3.0
if (function_exists('file_get_contents'))
{
// Set User-Agent and Proxy
$context = array (
'http' => array (
'user_agent' => $user_agent,
'proxy' => $proxy,
'request_fulluri' => true,
),
);
// Build Stream Context
$context = stream_context_create ($context);
// Use file_get_contents() Function and hide Error with @
$content = @file_get_contents($url, NULL, $context);
}
else // We run PHP < 4.3.0 - OMG :-o
{
// Ini Var
$content = '';
// Open URL and hide Error with @
if($handle = @fopen($url, 'r'))
{
// While there is Data
while (!feof($handle))
{
// Read the Data-Line
$line = fgets($handle, 4096);
// Add the Data-Line to the Content Var
$content .= $line;
}
// Better Close the FileHandle after the fgets()
fclose($handle);
}
}
// The Function returns the Content
return $content;
}
// Call the API Main Function
public function api($query)
{
// Ini Vars
$text = $image = $description = '';
// Convert Query to Lowercase for Headline
$strtolower = mb_strtolower($query);
// Convert Headlie to UTF-8 Uppercase Words
$headline = mb_convert_case($strtolower, MB_CASE_TITLE, 'UTF-8');
// If Query is complete Uppercase make also complete Uppercase Headline
if ($query === strtoupper($query))
{
$headline = mb_strtoupper($query);
}
// Replace spaces in Query to Underscore and use Uppercase Words from Headline
$query = str_replace(' ', '_', $headline);
// In DEBUG Mode print Query
if ($this->params['DEBUG']=='KEY' || $this->params['DEBUG']=='ALL')
{
echo '<tt><b>Search-Keyword </b><xmp>#'.$query.'#</xmp></tt>';
}
// First search the API if betterResults==true
if ($this->params['betterResults'] == true)
{
// Wikipedia API URL 1 - https://en.wikipedia.org/w/api.php
$url = 'https://'.$this->params['language'].'.wikipedia.org/w/api.php'.
'?action=query&format=json&list=search&srsearch=intitle:'.$query.
'&maxlag=1'; /* stop if wiki server is busy */
// If API Call 1 could be reached
if ($api = $this->getContent($url, $this->params['userAgent'], $this->params['proxy']))
{
// Decode the 1 Response
$data = json_decode($api, TRUE);
// In DEBUG Mode print 1 Response
if ($this->params['DEBUG']=='API1' || $this->params['DEBUG']=='ALL')
{
echo '<pre><b>Search API-Call (1) Response</b> ';
echo var_dump($data);
echo '</pre>';
}
// If there is a search Result
if (isset($data['query']['search'][0]['title']))
{
// Set Headline
$headline = $data['query']['search'][0]['title'];
// Set the Query to the first Search Result (and replace Spaces with Underscores)
$query = str_replace(' ', '_', $data['query']['search'][0]['title']);
// In DEBUG Mode print Found Keyword
if ($this->params['DEBUG']=='KEY' || $this->params['DEBUG']=='ALL')
{
echo '<tt><b>Found Search-Keyword </b><xmp>#'.$query.'#</xmp></tt>';
}
}
// If Search Result is a 'Did you mean:' Hint
if ( isset($data['query']['searchinfo']['suggestion']))
{
// Set Text Hints depending on selected Language
if ($this->params['language'] == 'de')
{
$suggestionText = 'Meinten Sie: ';
}
else
{
$suggestionText = 'Did you mean: ';
}
// Remove 'q=' Variable=Value Pair from Querystring
$QUERY_STRING = preg_replace('/'.('q'?'(\&|)q(\=(.*?)((?=&(?!amp\;))|$)|(.*?)\b)':'(\?.*)').'/i','',$_SERVER['QUERY_STRING']);
// Delete 'intitle:' from Suggestion Keyword
$suggestion = str_replace('intitle:','',$data['query']['searchinfo']['suggestion']);
// Make Suggestion UTF-8 Uppercase Words
$suggestion = mb_convert_case($suggestion, MB_CASE_TITLE, 'UTF-8');
// Make HTML Link for Suggestion
$description = $suggestionText.'<a href="?q='.
str_replace(' ', '_', $suggestion).$QUERY_STRING.'">'.$suggestion.'</a>';
}
}
}
// Wikipedia API URL 2 - https://en.wikipedia.org/w/api.php
$url = 'https://'.$this->params['language'].
'.wikipedia.org/api/rest_v1/page/summary/'.$query.
'?maxlag=1'; /* stop if wiki server is busy */
// If API Call 2 could be reached
if ($api = $this->getContent($url, $this->params['userAgent'], $this->params['proxy']))
{
// Decode the 2 Response
$data = json_decode($api, TRUE);
// In DEBUG Mode print 2 Response
if ($this->params['DEBUG']=='API2' || $this->params['DEBUG']=='ALL')
{
echo '<pre><b>Main API-Call (2) Response</b> ';
echo var_dump($data);
echo '</pre>';
}
// If there is an Image in the Search Result
if (isset($data['originalimage']['source']))
{
// If the DSGVO imageProxy should be use define it
$proxy = '';
if ($this->params['imageProxy']==TRUE)
{
$proxy = 'wiki-image-proxy.php?url=';
}
// Build HTML for Image
$image = '<img src="'.$proxy.$data['thumbnail']['source'].'" />';
}
// Correct the Text
$text = str_replace('#', ': ', $data['extract_html']);
// If there is a Description
if (isset($data['description']))
{
// Correct the Description depending on selected Language
$description = str_replace(
array(
'Wikimedia-Begriffsklärungsseite',
'Disambiguation page providing links to topics that could be referred to by the same search term'
),
array(
'kann sich auf Folgendes beziehen',
'may refer to the following'
),
$data['description']
);
// Set Keyword to UTF-8 Uppercase Words of Query
$keyword = mb_convert_case($strtolower, MB_CASE_TITLE, 'UTF-8');
// Highlight the Query in the Text and Delete some Text
$text = str_replace(
array($keyword, ' may refer to', ' steht für:'),
array('<b class="hint">'.$keyword.'</b>', '', ''),
$text
);
}
// If there is no Article Text set a Default depending on selected Language
// e.g. q=Leonardo%20di%20caprio&language=de OR q=100&language=de
if ($text == '')
{
$description = $image = '';
if ($this->params['language'] == 'de')
{
$text = 'Zu diesem Stichwort ist kein Artikel vorhanden.';
}
else if($text == '')
{
$text = 'There is no article available for this keyword.';
}
return; // ONLY IF YOU WHANT NO OUTPUT !!
}
}
// Build the HTML Output
if ($this->params['language']=='de')
{
$moreAbout = 'Mehr über';
$from = 'bei';
}
else
{
$moreAbout = 'More about';
$from = 'from';
}
// Without any Search Result return nothing
if ($text == '' && $description == '')
{
return '';
}
// With a Search Resuld build a Footer Link
if ($text != '')
{
$footer = $moreAbout.' »'.$headline.'« '.$from;
$url = 'https://'.$this->params['language'].'.wikipedia.org/wiki/'.$query;
}
else if ($description != '')
{
// Footer Link for Suggestion-Link
$footer = '';
$url = 'https://'.$this->params['language'].'.wikipedia.org/';
}
// Use the Template
ob_start();
include 'wiki2tpl.phtm';
// Return the HTML
return ob_get_clean();
}
}