| 1 |
<?php |
| 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 |
use MediaWiki\Context\IContextSource; |
| 27 |
use MediaWiki\Html\Html; |
| 28 |
use MediaWiki\Linker\Linker; |
| 29 |
use MediaWiki\Linker\LinkRenderer; |
| 30 |
use MediaWiki\MainConfigNames; |
| 31 |
use MediaWiki\User\User; |
| 32 |
use MediaWiki\User\UserFactory; |
| 33 |
use MediaWiki\User\UserRigorOptions; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class CreditsAction extends FormlessAction { |
| 39 |
|
| 40 |
private LinkRenderer $linkRenderer; |
| 41 |
private UserFactory $userFactory; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public function __construct( |
| 50 |
Article $article, |
| 51 |
IContextSource $context, |
| 52 |
LinkRenderer $linkRenderer, |
| 53 |
UserFactory $userFactory |
| 54 |
) { |
| 55 |
parent::__construct( $article, $context ); |
| 56 |
$this->linkRenderer = $linkRenderer; |
| 57 |
$this->userFactory = $userFactory; |
| 58 |
} |
| 59 |
|
| 60 |
public function getName() { |
| 61 |
return 'credits'; |
| 62 |
} |
| 63 |
|
| 64 |
protected function getDescription() { |
| 65 |
return $this->msg( 'creditspage' )->escaped(); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public function onView() { |
| 74 |
$this->getOutput()->addModuleStyles( [ |
| 75 |
'mediawiki.action.styles', |
| 76 |
] ); |
| 77 |
|
| 78 |
if ( $this->getWikiPage()->getId() === 0 ) { |
| 79 |
$s = $this->msg( 'nocredits' )->parse(); |
| 80 |
} else { |
| 81 |
$s = $this->getCredits( -1 ); |
| 82 |
} |
| 83 |
|
| 84 |
return Html::rawElement( 'div', [ 'id' => 'mw-credits' ], $s ); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public function getCredits( $cnt, $showIfMax = true ) { |
| 95 |
$s = ''; |
| 96 |
|
| 97 |
if ( $cnt !== 0 ) { |
| 98 |
$s = $this->getAuthor(); |
| 99 |
if ( $cnt > 1 || $cnt < 0 ) { |
| 100 |
$s .= ' ' . $this->getContributors( $cnt - 1, $showIfMax ); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
return $s; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
private function getAuthor() { |
| 113 |
$page = $this->getWikiPage(); |
| 114 |
$user = $this->userFactory->newFromName( $page->getUserText(), UserRigorOptions::RIGOR_NONE ); |
| 115 |
|
| 116 |
$timestamp = $page->getTimestamp(); |
| 117 |
if ( $timestamp ) { |
| 118 |
$lang = $this->getLanguage(); |
| 119 |
$d = $lang->date( $page->getTimestamp(), true ); |
| 120 |
$t = $lang->time( $page->getTimestamp(), true ); |
| 121 |
} else { |
| 122 |
$d = ''; |
| 123 |
$t = ''; |
| 124 |
} |
| 125 |
|
| 126 |
return $this->msg( 'lastmodifiedatby', $d, $t )->rawParams( |
| 127 |
|
| 128 |
$this->userLink( $user ) )->params( $user->getName() )->escaped(); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
protected function canShowRealUserName() { |
| 138 |
$hiddenPrefs = $this->context->getConfig()->get( MainConfigNames::HiddenPrefs ); |
| 139 |
return !in_array( 'realname', $hiddenPrefs ); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
protected function getContributors( $cnt, $showIfMax ) { |
| 149 |
$contributors = $this->getWikiPage()->getContributors(); |
| 150 |
|
| 151 |
$others_link = false; |
| 152 |
|
| 153 |
|
| 154 |
if ( $cnt > 0 && $contributors->count() > $cnt ) { |
| 155 |
$others_link = $this->othersLink(); |
| 156 |
if ( !$showIfMax ) { |
| 157 |
return $this->msg( 'othercontribs' )->rawParams( |
| 158 |
$others_link )->params( $contributors->count() )->escaped(); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
$real_names = []; |
| 163 |
$user_names = []; |
| 164 |
$anon_ips = []; |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
foreach ( $contributors as $user ) { |
| 169 |
$cnt--; |
| 170 |
if ( $user->isRegistered() ) { |
| 171 |
$link = $this->link( $user ); |
| 172 |
if ( $this->canShowRealUserName() && $user->getRealName() ) { |
| 173 |
$real_names[] = $link; |
| 174 |
} else { |
| 175 |
$user_names[] = $link; |
| 176 |
} |
| 177 |
} else { |
| 178 |
$anon_ips[] = $this->link( $user ); |
| 179 |
} |
| 180 |
|
| 181 |
if ( $cnt === 0 ) { |
| 182 |
break; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
$lang = $this->getLanguage(); |
| 187 |
|
| 188 |
if ( count( $real_names ) ) { |
| 189 |
$real = $lang->listToText( $real_names ); |
| 190 |
} else { |
| 191 |
$real = false; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
if ( count( $user_names ) ) { |
| 196 |
$user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params( |
| 197 |
count( $user_names ) )->escaped(); |
| 198 |
} else { |
| 199 |
$user = false; |
| 200 |
} |
| 201 |
|
| 202 |
if ( count( $anon_ips ) ) { |
| 203 |
$anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params( |
| 204 |
count( $anon_ips ) )->escaped(); |
| 205 |
} else { |
| 206 |
$anon = false; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
$fullList = []; |
| 211 |
foreach ( [ $real, $user, $anon, $others_link ] as $s ) { |
| 212 |
if ( $s !== false ) { |
| 213 |
$fullList[] = $s; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
$count = count( $fullList ); |
| 218 |
|
| 219 |
|
| 220 |
return $count |
| 221 |
? $this->msg( 'othercontribs' )->rawParams( |
| 222 |
$lang->listToText( $fullList ) )->params( $count )->escaped() |
| 223 |
: ''; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
protected function link( User $user ) { |
| 232 |
if ( $this->canShowRealUserName() && !$user->isAnon() ) { |
| 233 |
$real = $user->getRealName(); |
| 234 |
if ( $real === '' ) { |
| 235 |
$real = $user->getName(); |
| 236 |
} |
| 237 |
} else { |
| 238 |
$real = $user->getName(); |
| 239 |
} |
| 240 |
|
| 241 |
return Linker::userLink( $user->getId(), $user->getName(), $real ); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
protected function userLink( User $user ) { |
| 250 |
$link = $this->link( $user ); |
| 251 |
if ( $user->isAnon() ) { |
| 252 |
return $this->msg( 'anonuser' )->rawParams( $link )->parse(); |
| 253 |
} elseif ( $this->canShowRealUserName() && $user->getRealName() ) { |
| 254 |
return $link; |
| 255 |
} else { |
| 256 |
return $this->msg( 'siteuser' )->rawParams( $link )->params( $user->getName() )->escaped(); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
protected function othersLink() { |
| 265 |
return $this->linkRenderer->makeKnownLink( |
| 266 |
$this->getTitle(), |
| 267 |
$this->msg( 'others' )->text(), |
| 268 |
[], |
| 269 |
[ 'action' => 'credits' ] |
| 270 |
); |
| 271 |
} |
| 272 |
} |