illuminated/wikipedia-grabber

Grab Wikipedia (or another MediaWiki) page in Laravel.

5.6.0 2018-07-16 10:52 UTC

README

StyleCI Build Status Coverage Status

Latest Stable Version Latest Unstable Version Total Downloads License

Grab Wikipedia (or another MediaWiki) page in Laravel.

Laravel Wikipedia Grabber
5.5.* 5.5.*
5.6.* 5.6.*

Table of contents

Usage

  1. Install package through composer:

    composer require illuminated/wikipedia-grabber
  2. Use Wikipedia class:

    use Wikipedia;
    
    echo (new Wikipedia)->page('Donald Trump');

    Check it out on a live demo now.

Formats

Supported formats:

Change format in your config (see Configuration):

'format' => 'bulma',

Or use proper helper methods on the fly:

echo (new Wikipedia)->page('Donald Trump')->bootstrap();

Languages

Only en and ru languages are supported now.

English is the default language. But you can change it:

echo (new Wikipedia('ru'))->page('Donald Trump');

Methods

You get an object returned, so:

$page = (new Wikipedia)->page('President Trump');

if ($page->isSuccess()) {
    echo $page->getId();    // 4848272
    echo $page->getTitle(); // Donald Trump
    echo $page;             // These two are the same
    echo $page->getBody();  // These two are the same
}

Here is an example of the successfully grabbed page:

$page = (new Wikipedia)->page('Donald Trump');

$page->isSuccess();         // true
$page->isMissing();         // false
$page->isInvalid();         // false
$page->isDisambiguation();  // false

And here is an example of the successfully grabbed disambiguation page:

$page = (new Wikipedia)->page('David Taylor');

$page->isSuccess();         // true
$page->isInvalid();         // false
$page->isMissing();         // false
$page->isDisambiguation();  // true

Preview

The preview consists of an intro section and the main image. It has the same API:

echo (new Wikipedia)->preview('Donald Trump');

Advanced

Configuration

You can publish config to override some settings:

php artisan vendor:publish --provider="Illuminated\Wikipedia\ServiceProvider"

It is highly recommended to override user_agent, at least:

'user_agent' => 'Application Name (http://example.com; foo@example.com)',

Get by id

Just pass an integer to the method:

echo (new Wikipedia)->page(4848272);

MediaWiki

You are not limited to Wikipedia. Grab the pages from any MediaWiki site:

use MediaWiki;

echo (new MediaWiki('https://foopedia.org/w/api.php'))->page('Donald Trump');

Modifications

You can append section to the end:

echo (new Wikipedia)
        ->page('Donald Trump')
        ->append('Hey!', 'Please, donate me $1M, Mr. Trump!');

Or take the full control and change sections as you wish:

$page = (new Wikipedia)->page('Donald Trump');

$sections = $page->getSections();

// ...

Caching, caching, caching!

Each time you grab a page - you do the real API calls!

Use caching to increase your application speed and reduce API load:

$html = Cache::remember($key, $minutes, function () {
    return (new Wikipedia)->page('Donald Trump')->getBody();
});