Skip to content

Commit

Permalink
Merge branch 'master' into 5.6
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-ivanov committed Jul 24, 2018
2 parents 430e3c7 + 6f223e7 commit 8ee73a4
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 43 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Grab Wikipedia (or another MediaWiki) page in Laravel.
- [Languages](#languages)
- [Methods](#methods)
- [Preview](#preview)
- [Random](#random)
- [Advanced](#advanced)
- [Configuration](#configuration)
- [Get by id](#get-by-id)
Expand Down Expand Up @@ -126,6 +127,20 @@ The preview consists of an intro section and the main image. It has the same API
echo (new Wikipedia)->preview('Donald Trump');
```

## Random

You can grab random page:

```php
echo (new Wikipedia)->random();
```

Or random preview:

```php
echo (new Wikipedia)->randomPreview();
```

## Advanced

### Configuration
Expand All @@ -150,6 +165,12 @@ Just pass an integer to the method:
echo (new Wikipedia)->page(4848272);
```

The same is true for preview:

```php
echo (new Wikipedia)->preview(4848272);
```

### MediaWiki

You are not limited to Wikipedia. Grab the pages from any MediaWiki site:
Expand Down
86 changes: 43 additions & 43 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions src/Grabber.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use GuzzleHttp\Client;
use Illuminated\Wikipedia\Grabber\Page;
use Illuminated\Wikipedia\Grabber\Preview;
use Illuminated\Wikipedia\Grabber\Random;

abstract class Grabber
{
Expand Down Expand Up @@ -35,6 +36,21 @@ public function preview($title)
return new Preview($this->client, $title);
}

public function random()
{
return $this->page($this->randomTitle());
}

public function randomPreview()
{
return $this->preview($this->randomTitle());
}

protected function randomTitle()
{
return (new Random($this->client))->title();
}

abstract protected function baseUri();

protected function userAgent()
Expand Down
49 changes: 49 additions & 0 deletions src/Grabber/Random.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Illuminated\Wikipedia\Grabber;

use GuzzleHttp\Client;

class Random
{
protected $client;

public function __construct(Client $client)
{
$this->client = $client;
}

public function title()
{
$response = head($this->request($this->params())['query']['random']);

return $response['title'];
}

/**
* @see https://www.mediawiki.org/wiki/API:Query#Getting_a_list_of_page_IDs - FormatVersion
* @see https://en.wikipedia.org/w/api.php?action=help&modules=query+random - Random
*/
protected function params()
{
return [
'query' => [
'action' => 'query',
'format' => 'json',
'formatversion' => 2,
'list' => 'random',
'rnnamespace' => 0,
'rnfilterredir' => 'nonredirects',
'rnlimit' => 1,
],
];
}

protected function request(array $params)
{
return json_decode(
$this->client->get('', $params)->getBody(),
true
);
}
}

0 comments on commit 8ee73a4

Please sign in to comment.