| 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\RequestContext; |
| 27 |
use MediaWiki\Language\Language; |
| 28 |
use MediaWiki\MainConfigNames; |
| 29 |
use MediaWiki\MediaWikiServices; |
| 30 |
use MediaWiki\Message\Message; |
| 31 |
use MediaWiki\SpecialPage\SpecialPage; |
| 32 |
use MediaWiki\StubObject\StubUserLang; |
| 33 |
use MediaWiki\Title\Title; |
| 34 |
use MediaWiki\User\User; |
| 35 |
use MediaWiki\User\UserIdentity; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
class LogPage { |
| 47 |
public const DELETED_ACTION = 1; |
| 48 |
public const DELETED_COMMENT = 2; |
| 49 |
public const DELETED_USER = 4; |
| 50 |
public const DELETED_RESTRICTED = 8; |
| 51 |
|
| 52 |
|
| 53 |
public const SUPPRESSED_USER = self::DELETED_USER | self::DELETED_RESTRICTED; |
| 54 |
public const SUPPRESSED_ACTION = self::DELETED_ACTION | self::DELETED_RESTRICTED; |
| 55 |
|
| 56 |
|
| 57 |
public $updateRecentChanges; |
| 58 |
|
| 59 |
|
| 60 |
public $sendToUDP; |
| 61 |
|
| 62 |
|
| 63 |
private $ircActionText; |
| 64 |
|
| 65 |
|
| 66 |
private $actionText; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
private $type; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
private $action; |
| 77 |
|
| 78 |
|
| 79 |
private $comment; |
| 80 |
|
| 81 |
|
| 82 |
private $params; |
| 83 |
|
| 84 |
|
| 85 |
private $performer; |
| 86 |
|
| 87 |
|
| 88 |
private $target; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public function __construct( $type, $rc = true, $udp = 'skipUDP' ) { |
| 98 |
$this->type = $type; |
| 99 |
$this->updateRecentChanges = $rc; |
| 100 |
$this->sendToUDP = ( $udp == 'UDP' ); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
protected function saveContent() { |
| 107 |
$logRestrictions = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogRestrictions ); |
| 108 |
|
| 109 |
$dbw = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase(); |
| 110 |
|
| 111 |
$now = wfTimestampNow(); |
| 112 |
$actorId = MediaWikiServices::getInstance()->getActorNormalization() |
| 113 |
->acquireActorId( $this->performer, $dbw ); |
| 114 |
$data = [ |
| 115 |
'log_type' => $this->type, |
| 116 |
'log_action' => $this->action, |
| 117 |
'log_timestamp' => $dbw->timestamp( $now ), |
| 118 |
'log_actor' => $actorId, |
| 119 |
'log_namespace' => $this->target->getNamespace(), |
| 120 |
'log_title' => $this->target->getDBkey(), |
| 121 |
'log_page' => $this->target->getArticleID(), |
| 122 |
'log_params' => $this->params |
| 123 |
]; |
| 124 |
$data += MediaWikiServices::getInstance()->getCommentStore()->insert( |
| 125 |
$dbw, |
| 126 |
'log_comment', |
| 127 |
$this->comment |
| 128 |
); |
| 129 |
$dbw->newInsertQueryBuilder() |
| 130 |
->insertInto( 'logging' ) |
| 131 |
->row( $data ) |
| 132 |
->caller( __METHOD__ )->execute(); |
| 133 |
$newId = $dbw->insertId(); |
| 134 |
|
| 135 |
|
| 136 |
if ( $this->updateRecentChanges ) { |
| 137 |
$titleObj = SpecialPage::getTitleFor( 'Log', $this->type ); |
| 138 |
|
| 139 |
RecentChange::notifyLog( |
| 140 |
$now, $titleObj, $this->performer, $this->getRcComment(), '', |
| 141 |
$this->type, $this->action, $this->target, $this->comment, |
| 142 |
$this->params, $newId, $this->getRcCommentIRC() |
| 143 |
); |
| 144 |
} elseif ( $this->sendToUDP ) { |
| 145 |
|
| 146 |
if ( isset( $logRestrictions[$this->type] ) && $logRestrictions[$this->type] != '*' ) { |
| 147 |
return $newId; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
$titleObj = SpecialPage::getTitleFor( 'Log', $this->type ); |
| 153 |
$rc = RecentChange::newLogEntry( |
| 154 |
$now, $titleObj, $this->performer, $this->getRcComment(), '', |
| 155 |
$this->type, $this->action, $this->target, $this->comment, |
| 156 |
$this->params, $newId, $this->getRcCommentIRC() |
| 157 |
); |
| 158 |
$rc->notifyRCFeeds(); |
| 159 |
} |
| 160 |
|
| 161 |
return $newId; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public function getRcComment() { |
| 170 |
$rcComment = $this->actionText; |
| 171 |
|
| 172 |
if ( $this->comment != '' ) { |
| 173 |
if ( $rcComment == '' ) { |
| 174 |
$rcComment = $this->comment; |
| 175 |
} else { |
| 176 |
$rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . |
| 177 |
$this->comment; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
return $rcComment; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public function getRcCommentIRC() { |
| 190 |
$rcComment = $this->ircActionText; |
| 191 |
|
| 192 |
if ( $this->comment != '' ) { |
| 193 |
if ( $rcComment == '' ) { |
| 194 |
$rcComment = $this->comment; |
| 195 |
} else { |
| 196 |
$rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . |
| 197 |
$this->comment; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
return $rcComment; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public function getComment() { |
| 209 |
return $this->comment; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
public static function validTypes() { |
| 218 |
$logTypes = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogTypes ); |
| 219 |
|
| 220 |
return $logTypes; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public static function isLogType( $type ) { |
| 230 |
return in_array( $type, self::validTypes() ); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
public static function actionText( $type, $action, $title = null, $skin = null, |
| 247 |
$params = [], $filterWikilinks = false |
| 248 |
) { |
| 249 |
global $wgLang; |
| 250 |
$config = MediaWikiServices::getInstance()->getMainConfig(); |
| 251 |
$key = "$type/$action"; |
| 252 |
|
| 253 |
$logActions = $config->get( MainConfigNames::LogActions ); |
| 254 |
|
| 255 |
if ( isset( $logActions[$key] ) ) { |
| 256 |
$message = $logActions[$key]; |
| 257 |
} else { |
| 258 |
wfDebug( "LogPage::actionText - unknown action $key" ); |
| 259 |
$message = "log-unknown-action"; |
| 260 |
$params = [ $key ]; |
| 261 |
} |
| 262 |
|
| 263 |
if ( $skin === null ) { |
| 264 |
$langObj = MediaWikiServices::getInstance()->getContentLanguage(); |
| 265 |
$langObjOrNull = null; |
| 266 |
} else { |
| 267 |
|
| 268 |
StubUserLang::unstub( $wgLang ); |
| 269 |
$langObj = $wgLang; |
| 270 |
$langObjOrNull = $wgLang; |
| 271 |
} |
| 272 |
if ( $title === null ) { |
| 273 |
$rv = wfMessage( $message )->inLanguage( $langObj )->escaped(); |
| 274 |
} else { |
| 275 |
$titleLink = self::getTitleLink( $title, $langObjOrNull ); |
| 276 |
|
| 277 |
if ( count( $params ) == 0 ) { |
| 278 |
$rv = wfMessage( $message )->rawParams( $titleLink ) |
| 279 |
->inLanguage( $langObj )->escaped(); |
| 280 |
} else { |
| 281 |
array_unshift( $params, $titleLink ); |
| 282 |
|
| 283 |
$rv = wfMessage( $message )->rawParams( $params ) |
| 284 |
->inLanguage( $langObj )->escaped(); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
if ( $filterWikilinks ) { |
| 299 |
$rv = str_replace( '[[', '', $rv ); |
| 300 |
$rv = str_replace( ']]', '', $rv ); |
| 301 |
} |
| 302 |
|
| 303 |
return $rv; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
private static function getTitleLink( Title $title, ?Language $lang ): string { |
| 312 |
if ( !$lang ) { |
| 313 |
return $title->getPrefixedText(); |
| 314 |
} |
| 315 |
|
| 316 |
$services = MediaWikiServices::getInstance(); |
| 317 |
$linkRenderer = $services->getLinkRenderer(); |
| 318 |
|
| 319 |
if ( $title->isSpecialPage() ) { |
| 320 |
[ $name, $par ] = $services->getSpecialPageFactory()->resolveAlias( $title->getDBkey() ); |
| 321 |
|
| 322 |
if ( $name === 'Log' ) { |
| 323 |
$logPage = new LogPage( $par ?? '' ); |
| 324 |
return wfMessage( 'parentheses' ) |
| 325 |
->rawParams( $linkRenderer->makeLink( $title, $logPage->getName()->text() ) ) |
| 326 |
->inLanguage( $lang ) |
| 327 |
->escaped(); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
return $linkRenderer->makeLink( $title ); |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
public function addEntry( $action, $target, $comment, $params, $performer ) { |
| 348 |
|
| 349 |
if ( !is_array( $params ) ) { |
| 350 |
$params = [ $params ]; |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
$comment = trim( $comment ?? '' ); |
| 355 |
|
| 356 |
$this->action = $action; |
| 357 |
$this->target = $target; |
| 358 |
$this->comment = $comment; |
| 359 |
$this->params = self::makeParamBlob( $params ); |
| 360 |
|
| 361 |
if ( !is_object( $performer ) ) { |
| 362 |
$performer = User::newFromId( $performer ); |
| 363 |
} |
| 364 |
|
| 365 |
$this->performer = $performer; |
| 366 |
|
| 367 |
$logEntry = new ManualLogEntry( $this->type, $action ); |
| 368 |
$logEntry->setTarget( $target ); |
| 369 |
$logEntry->setPerformer( $performer ); |
| 370 |
$logEntry->setParameters( $params ); |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
$logEntry->setLegacy( true ); |
| 375 |
|
| 376 |
$formatter = MediaWikiServices::getInstance()->getLogFormatterFactory()->newFromEntry( $logEntry ); |
| 377 |
$context = RequestContext::newExtraneousContext( $target ); |
| 378 |
$formatter->setContext( $context ); |
| 379 |
|
| 380 |
$this->actionText = $formatter->getPlainActionText(); |
| 381 |
$this->ircActionText = $formatter->getIRCActionText(); |
| 382 |
|
| 383 |
return $this->saveContent(); |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
public function addRelations( $field, $values, $logid ) { |
| 395 |
if ( !strlen( $field ) || !$values ) { |
| 396 |
return false; |
| 397 |
} |
| 398 |
$insert = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase() |
| 399 |
->newInsertQueryBuilder() |
| 400 |
->insertInto( 'log_search' ) |
| 401 |
->ignore(); |
| 402 |
foreach ( $values as $value ) { |
| 403 |
$insert->row( [ 'ls_field' => $field, 'ls_value' => $value, 'ls_log_id' => $logid ] ); |
| 404 |
} |
| 405 |
$insert->caller( __METHOD__ )->execute(); |
| 406 |
|
| 407 |
return true; |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
public static function makeParamBlob( $params ) { |
| 417 |
return implode( "\n", $params ); |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
public static function extractParams( $blob ) { |
| 427 |
if ( $blob === '' ) { |
| 428 |
return []; |
| 429 |
} else { |
| 430 |
return explode( "\n", $blob ); |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
public function getName() { |
| 440 |
$logNames = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogNames ); |
| 441 |
|
| 442 |
|
| 443 |
$key = $logNames[$this->type] ?? 'log-name-' . $this->type; |
| 444 |
|
| 445 |
return wfMessage( $key ); |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
public function getDescription() { |
| 454 |
$logHeaders = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogHeaders ); |
| 455 |
|
| 456 |
$key = $logHeaders[$this->type] ?? 'log-description-' . $this->type; |
| 457 |
|
| 458 |
return wfMessage( $key ); |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
public function getRestriction() { |
| 467 |
$logRestrictions = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogRestrictions ); |
| 468 |
|
| 469 |
|
| 470 |
return $logRestrictions[$this->type] ?? ''; |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
public function isRestricted() { |
| 479 |
$restriction = $this->getRestriction(); |
| 480 |
|
| 481 |
return $restriction !== '' && $restriction !== '*'; |
| 482 |
} |
| 483 |
} |