This repository has been archived by the owner on Mar 3, 2024. It is now read-only.
/
HtmlToDiscordConverter.php
135 lines (119 loc) 路 3.59 KB
/
HtmlToDiscordConverter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace MediaWiki\Extension\DiscordRCFeed;
use ExtensionRegistry;
use Sanitizer;
use Title;
use User;
class HtmlToDiscordConverter {
// phpcs:disable Generic.Files.LineLength.TooLong
/** @var string */
private const REGEX_FOR_USER_LINK = '#<a[^>]+class=[\'"][^\'"]*mw-userlink[^\'"]*[\'"][^>]*><bdi>([^<+]+)</bdi></a>#';
/** @var string */
private const REGEX_FOR_USER_TOOLS = '#<span[^>]+class=[\'"][^\'"]*mw-usertoollinks[^\'"]*[\'"][^>]*>.+</span>#';
/** @var string */
private const REGEX_FOR_TITLE_LINK = '#<a[^>]+href=[\'"][^\'"=]*(?:wiki/|w/|index.php/)([^\'"]+)[\'"][^>]*title=[^>]*>([^<+]+)</a>#';
/** @var string */
private const REGEX_FOR_GENERAL_LINK = '#<a[^>]+href=[\'"]([^\'"]+)[\'"][^>]*>([^<+]+)</a>#';
// phpcs:enable Generic.Files.LineLength.TooLong
/** @var DiscordLinker */
private $linker;
/**
* @param DiscordLinker|null $linker
*/
public function __construct( $linker = null ) {
$this->linker = $linker ?? new DiscordLinker();
}
/**
* @param string $text
* @param bool $omitTools
* @return string
*/
public function convert( string $text, bool $omitTools = false ): string {
$text = $this->removeUserTools( $text );
$text = $this->convertUserName( $text );
$text = $this->convertTitleLinks( $text, $omitTools );
$text = $this->convertLinks( $text );
$text = Sanitizer::stripAllTags( $text );
return $text;
}
/**
* @param string $text
* @return string
*/
private function convertUserName( string $text ): string {
if ( preg_match_all( self::REGEX_FOR_USER_LINK, $text, $matches ) ) {
foreach ( $matches[0] as $i => $fullMatch ) {
$username = $matches[1][$i];
$user = User::newFromName( $username );
if ( !$user ) {
continue;
}
$replace = $this->linker->makeUserTextWithTools( $user );
$text = str_replace( $fullMatch, $replace, $text );
}
}
return $text;
}
/**
* @param string $text
* @return string
*/
private function removeUserTools( string $text ): string {
return preg_replace( self::REGEX_FOR_USER_TOOLS, '', $text );
}
/**
* @param string $text
* @param bool $omitTools
* @return string
*/
private function convertTitleLinks( string $text, bool $omitTools = false ): string {
if ( preg_match_all( self::REGEX_FOR_TITLE_LINK, $text, $matches ) ) {
foreach ( $matches[0] as $i => $fullMatch ) {
$url = $matches[1][$i];
$label = $matches[2][$i];
$title = Title::newFromUrl( $url );
if ( !$title ) {
continue;
}
if ( !$omitTools && self::shouldIncludeTitleLinks( $title ) ) {
$replace = $this->linker->makePageTextWithTools( $title );
} else {
$replace = DiscordLinker::makeLink( $title->getFullURL( '', false, PROTO_CURRENT ), $label );
}
$text = str_replace( $fullMatch, $replace, $text );
}
}
return $text;
}
/**
* @param Title $title
* @return bool
*/
private static function shouldIncludeTitleLinks( $title ): bool {
if ( $title->hasFragment() ) {
return false;
}
if ( ExtensionRegistry::getInstance()->isLoaded( 'Flow' ) ) {
if ( $title->getContentModel() == CONTENT_MODEL_FLOW_BOARD ) {
return false;
}
}
return true;
}
/**
* @param string $text
* @return string
*/
private function convertLinks( string $text ): string {
if ( preg_match_all( self::REGEX_FOR_GENERAL_LINK, $text, $matches ) ) {
foreach ( $matches[0] as $i => $fullMatch ) {
$url = $matches[1][$i];
$url = Util::urlIsLocal( $url ) ? wfExpandUrl( $url ) : '';
$label = $matches[2][$i];
$link = DiscordLinker::makeLink( $url, $label );
$text = str_replace( $fullMatch, $link, $text );
}
}
return $text;
}
}