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\Publisher\ArticleFromURL; |
14
|
|
|
use App\Domain\Utils\WikiTextUtil; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Ref2ArticleProcess |
18
|
|
|
* |
19
|
|
|
* @package App\Application |
20
|
|
|
*/ |
21
|
|
|
class Ref2ArticleProcess |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Change tous les <ref>http://libe|lemonde|figaro</ref> en {article}. |
26
|
|
|
* |
27
|
|
|
* @param $text |
28
|
|
|
* |
29
|
|
|
* @return string|string[] |
30
|
|
|
* @throws \Exception |
31
|
|
|
*/ |
32
|
|
|
public function processText($text) |
33
|
|
|
{ |
34
|
|
|
$refs = WikiTextUtil::extractAllRefs($text); |
35
|
|
|
if (empty($refs)) { |
36
|
|
|
return $text; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
foreach ($refs as $ref) { |
40
|
|
|
$url = WikiTextUtil::stripFinalPoint(trim($ref[1])); |
41
|
|
|
$converter = new ArticleFromURL(new PublisherAction($url)); |
42
|
|
|
$articleOrLienBrise = $converter->getResult(); |
43
|
|
|
|
44
|
|
|
if (!$articleOrLienBrise instanceof ArticleOrLienBriseInterface) { |
45
|
|
|
continue; |
46
|
|
|
} |
47
|
|
|
$serial = $articleOrLienBrise->serialize(true); |
|
|
|
|
48
|
|
|
$text = $this->replaceRefInText($ref, $serial, $text); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $text; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function replaceRefInText(array $ref, string $replace, string $text) |
55
|
|
|
{ |
56
|
|
|
$replace .= '.'; // ending point |
57
|
|
|
$result = str_replace($ref[1], $replace, $ref[0]); |
58
|
|
|
echo "$result \n"; |
59
|
|
|
|
60
|
|
|
return str_replace($ref[0], $result, $text); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
} |
64
|
|
|
|