1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fowp\WordPressPluginRetriever; |
4
|
|
|
|
5
|
|
|
use Fowp\WordPressPluginRetriever\Export\Exporter; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* This retriever fetches plugin information from the WordPress.org plugin |
10
|
|
|
* directory. |
11
|
|
|
* |
12
|
|
|
* @author [email protected] |
13
|
|
|
*/ |
14
|
|
|
class Retriever |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* The official API endpoint from the WordPress directory. |
18
|
|
|
*/ |
19
|
|
|
const WORDPRESS_API_ENDPOINT = 'https://api.wordpress.org/plugins/info/1.2/?action=query_plugins&request%5Bpage%5D=##pagenumber##&request%5Bper_page%5D=##perpage##'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The maximal number of pages that are in the directory. This depends |
23
|
|
|
* on the $requestsForPage parameter and will ne calculated while processing |
24
|
|
|
* the first response. |
25
|
|
|
* |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
private int $maxNumberOfPages = 888; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Guzzle http client used to retrieve the plugin information. |
32
|
|
|
* |
33
|
|
|
* @var Client |
34
|
|
|
*/ |
35
|
|
|
private Client $client; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The constructor |
39
|
|
|
* |
40
|
|
|
* If the client is not set the retriever will initiate a simple client |
41
|
|
|
* on its own. |
42
|
|
|
* |
43
|
|
|
* @param Client|null $client |
44
|
|
|
*/ |
45
|
|
|
public function __construct(Client $client = null) |
46
|
|
|
{ |
47
|
|
|
if (is_null($client)) { |
48
|
|
|
$this->client = new Client(); |
49
|
|
|
} else { |
50
|
|
|
$this->client = $client; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Use this function to retrieve all plugins from the WordPress.org directory. |
56
|
|
|
* |
57
|
|
|
* This function will not return any data. In order to process the data you have to use |
58
|
|
|
* an exporter. |
59
|
|
|
*/ |
60
|
|
|
public function retrieve(int $requestsPerPage = 400, int $maxPages = -1, Exporter $exporter = null): void |
61
|
|
|
{ |
62
|
|
|
$page = 1; |
63
|
|
|
|
64
|
|
|
if ($maxPages === -1) $maxPages = 10000000; |
65
|
|
|
|
66
|
|
|
while ($page < $this->maxNumberOfPages && $page <= $maxPages) { |
67
|
|
|
$url = $this->getApiUrl($page, $requestsPerPage); |
68
|
|
|
$plainPluginBlock = $this->fetchPluginBlock($url); |
69
|
|
|
$this->maxNumberOfPages = $plainPluginBlock['info']['pages']; |
70
|
|
|
$exporter?->process($plainPluginBlock['plugins'], $page, $this->maxNumberOfPages); |
71
|
|
|
$page++; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$exporter?->finish(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Fetch one single page / block from the WordPress endpoint. |
79
|
|
|
*/ |
80
|
|
|
private function fetchPluginBlock(string $url): array |
81
|
|
|
{ |
82
|
|
|
$response = $this->client->request('GET', $url); |
83
|
|
|
$jsonString = ((string)$response->getBody()); |
84
|
|
|
return json_decode($jsonString, true); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the corresponding URL for the defined requests parameters. |
89
|
|
|
*/ |
90
|
|
|
private function getApiUrl(int $pageNumber, int $requestsPerPage): string |
91
|
|
|
{ |
92
|
|
|
$url = str_replace('##pagenumber##', $pageNumber, self::WORDPRESS_API_ENDPOINT); |
93
|
|
|
$url = str_replace('##perpage##', $requestsPerPage, $url); |
94
|
|
|
|
95
|
|
|
return $url; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|