1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of dispositif/wikibot application |
4
|
|
|
* 2019 © 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
|
|
|
namespace App\Application; |
11
|
|
|
|
12
|
|
|
use App\Domain\Models\Wiki\ArticleOrLienBriseInterface; |
13
|
|
|
use App\Domain\Models\Wiki\LienBriseTemplate; |
14
|
|
|
use App\Domain\Publisher\ArticleFromURL; |
15
|
|
|
use App\Domain\Utils\WikiTextUtil; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Ref2ArticleProcess |
19
|
|
|
* |
20
|
|
|
* @package App\Application |
21
|
|
|
*/ |
22
|
|
|
class Ref2ArticleProcess |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var bool |
26
|
|
|
*/ |
27
|
|
|
private $warning = false; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Change tous les <ref>http://libe|lemonde|figaro</ref> en {article}. |
31
|
|
|
* |
32
|
|
|
* @param $text |
33
|
|
|
* |
34
|
|
|
* @return string|string[] |
35
|
|
|
* @throws \Exception |
36
|
|
|
*/ |
37
|
|
|
public function processText($text) |
38
|
|
|
{ |
39
|
|
|
$refs = WikiTextUtil::extractAllRefs($text); |
40
|
|
|
if (empty($refs)) { |
41
|
|
|
return $text; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
foreach ($refs as $ref) { |
45
|
|
|
$url = WikiTextUtil::stripFinalPoint(trim($ref[1])); |
46
|
|
|
$converter = new ArticleFromURL(new PublisherAction($url)); |
47
|
|
|
$articleOrLienBrise = $converter->getResult(); |
48
|
|
|
|
49
|
|
|
if (!$articleOrLienBrise instanceof ArticleOrLienBriseInterface) { |
50
|
|
|
continue; |
51
|
|
|
} |
52
|
|
|
if($articleOrLienBrise instanceof LienBriseTemplate){ |
53
|
|
|
$this->warning = true; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$serial = $articleOrLienBrise->serialize(true); |
|
|
|
|
57
|
|
|
$text = $this->replaceRefInText($ref, $serial, $text); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $text; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function hasWarning():bool |
64
|
|
|
{ |
65
|
|
|
return (bool)$this->warning; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function replaceRefInText(array $ref, string $replace, string $text) |
69
|
|
|
{ |
70
|
|
|
$replace .= '.'; // ending point |
71
|
|
|
$result = str_replace($ref[1], $replace, $ref[0]); |
72
|
|
|
echo "$result \n"; |
73
|
|
|
|
74
|
|
|
return str_replace($ref[0], $result, $text); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
} |
78
|
|
|
|