Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 45 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
NewsletterContent | |
0.00% |
0 / 45 |
|
0.00% |
0 / 9 |
600 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
validateUserName | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
isValid | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
42 | |||
decode | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
90 | |||
onSuccess | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getDescription | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getMainPage | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getPublishers | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getTextForSummary | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\Newsletter\Content; |
4 | |
5 | use JsonContent; |
6 | use MediaWiki\MediaWikiServices; |
7 | use MediaWiki\Title\Title; |
8 | use MediaWiki\User\User; |
9 | |
10 | /** |
11 | * @license GPL-2.0-or-later |
12 | * @author tonythomas |
13 | */ |
14 | class NewsletterContent extends JsonContent { |
15 | |
16 | /** |
17 | * @var string|null |
18 | */ |
19 | private $description; |
20 | |
21 | /** |
22 | * @var Title |
23 | */ |
24 | private $mainPage; |
25 | |
26 | /** |
27 | * @var array|null |
28 | */ |
29 | protected $publishers; |
30 | |
31 | /** |
32 | * Whether $description and $targets have been populated |
33 | * @var bool |
34 | */ |
35 | private $decoded = false; |
36 | |
37 | /** |
38 | * @param string $text |
39 | */ |
40 | public function __construct( $text ) { |
41 | parent::__construct( $text, 'NewsletterContent' ); |
42 | } |
43 | |
44 | /** |
45 | * Validate username and make sure it exists |
46 | * |
47 | * @param string $userName |
48 | * @return bool |
49 | */ |
50 | private function validateUserName( $userName ) { |
51 | $user = User::newFromName( $userName ); |
52 | if ( !$user ) { |
53 | return false; |
54 | } |
55 | // If this user never existed |
56 | if ( !$user->getId() ) { |
57 | return false; |
58 | } |
59 | |
60 | return true; |
61 | } |
62 | |
63 | /** |
64 | * @return bool |
65 | */ |
66 | public function isValid() { |
67 | $this->decode(); |
68 | |
69 | if ( !is_string( $this->description ) || !( $this->mainPage instanceof Title ) || |
70 | !is_array( $this->publishers ) |
71 | ) { |
72 | return false; |
73 | } |
74 | |
75 | foreach ( $this->publishers as $publisher ) { |
76 | if ( !$this->validateUserName( $publisher ) ) { |
77 | return false; |
78 | } |
79 | } |
80 | |
81 | return true; |
82 | } |
83 | |
84 | /** |
85 | * Decode the JSON encoded args |
86 | * |
87 | * @return bool |
88 | */ |
89 | protected function decode() { |
90 | if ( $this->decoded ) { |
91 | return true; |
92 | } |
93 | $jsonParse = $this->getData(); |
94 | $data = $jsonParse->isGood() ? $jsonParse->getValue() : null; |
95 | |
96 | if ( $data ) { |
97 | $this->description = $data->description ?? null; |
98 | $this->mainPage = !empty( $data->mainpage ) ? Title::newFromText( $data->mainpage ) : |
99 | Title::makeTitle( NS_SPECIAL, 'Badtitle' ); |
100 | if ( isset( $data->publishers ) && is_array( $data->publishers ) ) { |
101 | $this->publishers = []; |
102 | foreach ( $data->publishers as $publisher ) { |
103 | if ( !is_string( $publisher ) ) { |
104 | $this->publishers = null; |
105 | break; |
106 | } |
107 | $this->publishers[] = $publisher; |
108 | } |
109 | } else { |
110 | $this->publishers = null; |
111 | } |
112 | } |
113 | $this->decoded = true; |
114 | return true; |
115 | } |
116 | |
117 | public function onSuccess() { |
118 | // No-op: We have already redirected. |
119 | } |
120 | |
121 | /** |
122 | * @return string |
123 | */ |
124 | public function getDescription() { |
125 | $this->decode(); |
126 | return $this->description; |
127 | } |
128 | |
129 | /** |
130 | * @return Title |
131 | */ |
132 | public function getMainPage() { |
133 | $this->decode(); |
134 | return $this->mainPage; |
135 | } |
136 | |
137 | /** |
138 | * @return array |
139 | */ |
140 | public function getPublishers() { |
141 | $this->decode(); |
142 | return $this->publishers; |
143 | } |
144 | |
145 | /** |
146 | * Override TextContent::getTextForSummary |
147 | * @param int $maxLength Maximum length, in characters (not bytes). |
148 | * @return string |
149 | */ |
150 | public function getTextForSummary( $maxLength = 250 ) { |
151 | $contLang = MediaWikiServices::getInstance()->getContentLanguage(); |
152 | |
153 | $truncatedtext = $contLang->truncateForVisual( |
154 | preg_replace( "/[\n\r]/", ' ', $this->getDescription() ), max( 0, $maxLength ) |
155 | ); |
156 | |
157 | return $truncatedtext; |
158 | } |
159 | |
160 | } |