📜 ⬆️ ⬇️

We connect Wikipedia to the site

Now almost every site has buttons to go to the tweeter site or add a page to the bookmarks. But for some reason very few Wikipedia icons can be seen near the names of great people or terms.

It may be that someone will seem to overload the site with such functionality, but it is better to ask users about it. So, in one new project it was proposed to give links to Wikipedia for authors of books.

If we take into account that only active users click on the “Leave your feedback” button and vote, then probably 33 votes can be multiplied by 10 - 100.

Having dealt with api Wikipedia I want to offer a solution how to automate the addition of links to Wikipedia as little as possible.


Knowing by them, the surname or patronymic of a famous person cannot be 100% sure that this is the person we need. For example, if we need to give a link to Alexander Alexandrovich Ivanov, there are a couple of people on Wikipedia with the same data and do not automatically determine who is suitable for us. The same situation with the names of books and films. For example, we need an article about a book, an article about a film can be written on Wikipedia. My solution is to make a search query using the wiki and already choose which one is more suitable.

Api wiki

Wikipedia has a very powerful api www.mediawiki.org/wiki/API/ru
All parameters are described here en.wikipedia.org/w/api.php (eng)
Of the many parameters we need

Action - the action that we want to commit. We need a search Action = opensearch

Search - what are we looking for. For example, "Master and Margarita"
search =% EC% E0% F1% F2% E5% F0% 20% E8% 20% EC% E0% F0% E3% E0% F0% E8% F2% E0

Prop - what characteristics of the page we want to get. We need general information about the page: title, description
prop = info

Format - the format in which the result is returned. For search, use xml
format = xml

inprop - what additional information we want to get. We still need a link to the Wikipedia page, so
inprop = url

Fully string with all parameters will be
ru.wikipedia.org/w/api.php?action=opensearch&search=%EC%E0%F1%F2%E5%F0%20%E8%20%EC%E0%F0%E3%E0%F0%E8%F2 % E0 & prop = info & format = xml & inprop = url

example php function to get data about the page

function get_wiki_url($title)
$fp = fsockopen( "ru.wikipedia.org" , 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n" ;
} else {
$ out = "GET /w/api.php?action=opensearch&search=" .urlencode($title). "& prop=info&format=xml&inprop=url HTTP/1.1\r\n" ;
$ out .= "Host: ru.wikipedia.org\r\n" ;
// User-Agent.
$ out .= "User-Agent: MyCuteBot/0.1\r\n" ;
$ out .= "Connection: Close\r\n\r\n" ;
fwrite($fp, $ out );
$str = '' ;
// xml
while (!feof($fp)) {
$tmp_str = fgets($fp, 128);
if ($str != '' || substr($tmp_str,0,2)== '<?' )
$str .= $tmp_str;
$xml = simplexml_load_string($str);
return $xml->Section->Item;
$pages_data = get_wiki_url( " " );

* This source code was highlighted with Source Code Highlighter .

Fully example and script .

Maybe the time has come when there will be a link to Wikipedia next to every known name or term?

Source: https://habr.com/ru/post/104480/

All Articles