1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of dispositif/wikibot application (@github) |
4
|
|
|
* 2019/2020 © Philippe M. <[email protected]> |
5
|
|
|
* For the full copyright and MIT license information, please view the license file. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace App\Infrastructure; |
12
|
|
|
|
13
|
|
|
use Mediawiki\Api\UsageException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* List of wiki-pages titles. |
17
|
|
|
* Class PageList |
18
|
|
|
* |
19
|
|
|
* @package App\Infrastructure |
20
|
|
|
*/ |
21
|
|
|
class PageList implements PageListInterface |
22
|
|
|
{ |
23
|
|
|
protected $titles; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* PageList constructor. |
27
|
|
|
* |
28
|
|
|
* @param $titles |
29
|
|
|
*/ |
30
|
|
|
public function __construct(array $titles) { $this->titles = $titles; } |
31
|
|
|
|
32
|
|
|
public function getPageTitles(): array |
33
|
|
|
{ |
34
|
|
|
return $this->titles; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function count():int |
38
|
|
|
{ |
39
|
|
|
return count($this->titles); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param $filename |
44
|
|
|
* |
45
|
|
|
* @return PageList |
46
|
|
|
*/ |
47
|
|
|
public static function FromFile(string $filename): PageList |
48
|
|
|
{ |
49
|
|
|
$names = @file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
50
|
|
|
|
51
|
|
|
$titles = []; |
52
|
|
|
if (!empty($names)) { |
53
|
|
|
foreach ($names as $name) { |
54
|
|
|
$title = trim($name); |
55
|
|
|
if (!empty($title)) { |
56
|
|
|
$titles[] = $title; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return new PageList((array)$titles); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $categoryName |
66
|
|
|
* |
67
|
|
|
* @return PageList |
68
|
|
|
* @throws UsageException |
69
|
|
|
*/ |
70
|
|
|
public static function FromWikiCategory(string $categoryName): PageList |
71
|
|
|
{ |
72
|
|
|
$wiki = ServiceFactory::wikiApi(); |
73
|
|
|
$wikiPages = $wiki->newPageListGetter()->getPageListFromCategoryName('Catégorie:'.$categoryName); |
74
|
|
|
$wikiPages = $wikiPages->toArray(); |
75
|
|
|
$titles = []; |
76
|
|
|
foreach ($wikiPages as $wikiPage) { |
77
|
|
|
$title = $wikiPage->getPageIdentifier()->getTitle()->getText(); |
78
|
|
|
$title = str_replace('Talk:', 'Discussion:', $title); // todo refac |
79
|
|
|
$titles[] = $title; |
80
|
|
|
} |
81
|
|
|
// arsort($titles); |
82
|
|
|
|
83
|
|
|
return new PageList($titles); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|