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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
|
<?php
/**
* Contain log classes
*
* Copyright © 2002, 2004 Brooke Vibber <bvibber@wikimedia.org>
* https://www.mediawiki.org/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
use MediaWiki\Context\RequestContext;
use MediaWiki\Language\Language;
use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
use MediaWiki\Message\Message;
use MediaWiki\SpecialPage\SpecialPage;
use MediaWiki\StubObject\StubUserLang;
use MediaWiki\Title\Title;
use MediaWiki\User\User;
use MediaWiki\User\UserIdentity;
/**
* Class to simplify the use of log pages.
* The logs are now kept in a table which is easier to manage and trim
* than ever-growing wiki pages.
*
* @newable
* @note marked as newable in 1.35 for lack of a better alternative,
* but should become a stateless service, use the command pattern.
*/
class LogPage {
public const DELETED_ACTION = 1;
public const DELETED_COMMENT = 2;
public const DELETED_USER = 4;
public const DELETED_RESTRICTED = 8;
// Convenience fields
public const SUPPRESSED_USER = self::DELETED_USER | self::DELETED_RESTRICTED;
public const SUPPRESSED_ACTION = self::DELETED_ACTION | self::DELETED_RESTRICTED;
/** @var bool */
public $updateRecentChanges;
/** @var bool */
public $sendToUDP;
/** @var string Plaintext version of the message for IRC */
private $ircActionText;
/** @var string Plaintext version of the message */
private $actionText;
/** @var string One of '', 'block', 'protect', 'rights', 'delete',
* 'upload', 'move'
*/
private $type;
/** @var string One of '', 'block', 'protect', 'rights', 'delete',
* 'upload', 'move', 'move_redir'
*/
private $action;
/** @var string Comment associated with action */
private $comment;
/** @var string Blob made of a parameters array */
private $params;
/** @var UserIdentity The user doing the action */
private $performer;
/** @var Title */
private $target;
/**
* @stable to call
* @param string $type One of '', 'block', 'protect', 'rights', 'delete',
* 'upload', 'move'
* @param bool $rc Whether to update recent changes as well as the logging table
* @param string $udp Pass 'UDP' to send to the UDP feed if NOT sent to RC
*/
public function __construct( $type, $rc = true, $udp = 'skipUDP' ) {
$this->type = $type;
$this->updateRecentChanges = $rc;
$this->sendToUDP = ( $udp == 'UDP' );
}
/**
* @return int The log_id of the inserted log entry
*/
protected function saveContent() {
$logRestrictions = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogRestrictions );
$dbw = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase();
$now = wfTimestampNow();
$actorId = MediaWikiServices::getInstance()->getActorNormalization()
->acquireActorId( $this->performer, $dbw );
$data = [
'log_type' => $this->type,
'log_action' => $this->action,
'log_timestamp' => $dbw->timestamp( $now ),
'log_actor' => $actorId,
'log_namespace' => $this->target->getNamespace(),
'log_title' => $this->target->getDBkey(),
'log_page' => $this->target->getArticleID(),
'log_params' => $this->params
];
$data += MediaWikiServices::getInstance()->getCommentStore()->insert(
$dbw,
'log_comment',
$this->comment
);
$dbw->newInsertQueryBuilder()
->insertInto( 'logging' )
->row( $data )
->caller( __METHOD__ )->execute();
$newId = $dbw->insertId();
# And update recentchanges
if ( $this->updateRecentChanges ) {
$titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
RecentChange::notifyLog(
$now, $titleObj, $this->performer, $this->getRcComment(), '',
$this->type, $this->action, $this->target, $this->comment,
$this->params, $newId, $this->getRcCommentIRC()
);
} elseif ( $this->sendToUDP ) {
# Don't send private logs to UDP
if ( isset( $logRestrictions[$this->type] ) && $logRestrictions[$this->type] != '*' ) {
return $newId;
}
// Notify external application via UDP.
// We send this to IRC but do not want to add it the RC table.
$titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
$rc = RecentChange::newLogEntry(
$now, $titleObj, $this->performer, $this->getRcComment(), '',
$this->type, $this->action, $this->target, $this->comment,
$this->params, $newId, $this->getRcCommentIRC()
);
$rc->notifyRCFeeds();
}
return $newId;
}
/**
* Get the RC comment from the last addEntry() call
*
* @return string
*/
public function getRcComment() {
$rcComment = $this->actionText;
if ( $this->comment != '' ) {
if ( $rcComment == '' ) {
$rcComment = $this->comment;
} else {
$rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
$this->comment;
}
}
return $rcComment;
}
/**
* Get the RC comment from the last addEntry() call for IRC
*
* @return string
*/
public function getRcCommentIRC() {
$rcComment = $this->ircActionText;
if ( $this->comment != '' ) {
if ( $rcComment == '' ) {
$rcComment = $this->comment;
} else {
$rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
$this->comment;
}
}
return $rcComment;
}
/**
* Get the comment from the last addEntry() call
* @return string
*/
public function getComment() {
return $this->comment;
}
/**
* Get the list of valid log types
*
* @return string[]
*/
public static function validTypes() {
$logTypes = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogTypes );
return $logTypes;
}
/**
* Is $type a valid log type
*
* @param string $type Log type to check
* @return bool
*/
public static function isLogType( $type ) {
return in_array( $type, self::validTypes() );
}
/**
* Generate text for a log entry.
* Only LogFormatter should call this function.
*
* @param string $type Log type
* @param string $action Log action
* @param Title|null $title
* @param Skin|null $skin Skin object or null. If null, we want to use the wiki
* content language, since that will go to the IRC feed.
* @param array $params
* @param bool $filterWikilinks Whether to filter wiki links
* @return string HTML
*/
public static function actionText( $type, $action, $title = null, $skin = null,
$params = [], $filterWikilinks = false
) {
global $wgLang;
$config = MediaWikiServices::getInstance()->getMainConfig();
$key = "$type/$action";
$logActions = $config->get( MainConfigNames::LogActions );
if ( isset( $logActions[$key] ) ) {
$message = $logActions[$key];
} else {
wfDebug( "LogPage::actionText - unknown action $key" );
$message = "log-unknown-action";
$params = [ $key ];
}
if ( $skin === null ) {
$langObj = MediaWikiServices::getInstance()->getContentLanguage();
$langObjOrNull = null;
} else {
// TODO Is $skin->getLanguage() safe here?
StubUserLang::unstub( $wgLang );
$langObj = $wgLang;
$langObjOrNull = $wgLang;
}
if ( $title === null ) {
$rv = wfMessage( $message )->inLanguage( $langObj )->escaped();
} else {
$titleLink = self::getTitleLink( $title, $langObjOrNull );
if ( count( $params ) == 0 ) {
$rv = wfMessage( $message )->rawParams( $titleLink )
->inLanguage( $langObj )->escaped();
} else {
array_unshift( $params, $titleLink );
$rv = wfMessage( $message )->rawParams( $params )
->inLanguage( $langObj )->escaped();
}
}
// For the perplexed, this feature was added in r7855 by Erik.
// The feature was added because we liked adding [[$1]] in our log entries
// but the log entries are parsed as Wikitext on RecentChanges but as HTML
// on Special:Log. The hack is essentially that [[$1]] represented a link
// to the title in question. The first parameter to the HTML version (Special:Log)
// is that link in HTML form, and so this just gets rid of the ugly [[]].
// However, this is a horrible hack and it doesn't work like you expect if, say,
// you want to link to something OTHER than the title of the log entry.
// The real problem, which Erik was trying to fix (and it sort-of works now) is
// that the same messages are being treated as both wikitext *and* HTML.
if ( $filterWikilinks ) {
$rv = str_replace( '[[', '', $rv );
$rv = str_replace( ']]', '', $rv );
}
return $rv;
}
/**
* @param Title $title
* @param ?Language $lang
* @return string HTML
*/
private static function getTitleLink( Title $title, ?Language $lang ): string {
if ( !$lang ) {
return $title->getPrefixedText();
}
$services = MediaWikiServices::getInstance();
$linkRenderer = $services->getLinkRenderer();
if ( $title->isSpecialPage() ) {
[ $name, $par ] = $services->getSpecialPageFactory()->resolveAlias( $title->getDBkey() );
if ( $name === 'Log' ) {
$logPage = new LogPage( $par );
return wfMessage( 'parentheses' )
->rawParams( $linkRenderer->makeLink( $title, $logPage->getName()->text() ) )
->inLanguage( $lang )
->escaped();
}
}
return $linkRenderer->makeLink( $title );
}
/**
* Add a log entry
*
* @param string $action One of '', 'block', 'protect', 'rights', 'delete',
* 'upload', 'move', 'move_redir'
* @param Title $target
* @param string|null $comment Description associated
* @param array $params Parameters passed later to wfMessage function
* @param int|UserIdentity $performer The user doing the action, or their user id.
* Calling with user ID is deprecated since 1.36.
*
* @return int The log_id of the inserted log entry
*/
public function addEntry( $action, $target, $comment, $params, $performer ) {
// FIXME $params is only documented to accept an array
if ( !is_array( $params ) ) {
$params = [ $params ];
}
# Trim spaces on user supplied text
$comment = trim( $comment ?? '' );
$this->action = $action;
$this->target = $target;
$this->comment = $comment;
$this->params = self::makeParamBlob( $params );
if ( !is_object( $performer ) ) {
$performer = User::newFromId( $performer );
}
$this->performer = $performer;
$logEntry = new ManualLogEntry( $this->type, $action );
$logEntry->setTarget( $target );
$logEntry->setPerformer( $performer );
$logEntry->setParameters( $params );
// All log entries using the LogPage to insert into the logging table
// are using the old logging system and therefore the legacy flag is
// needed to say the LogFormatter the parameters have numeric keys
$logEntry->setLegacy( true );
$formatter = MediaWikiServices::getInstance()->getLogFormatterFactory()->newFromEntry( $logEntry );
$context = RequestContext::newExtraneousContext( $target );
$formatter->setContext( $context );
$this->actionText = $formatter->getPlainActionText();
$this->ircActionText = $formatter->getIRCActionText();
return $this->saveContent();
}
/**
* Add relations to log_search table
*
* @param string $field
* @param array $values
* @param int $logid
* @return bool
*/
public function addRelations( $field, $values, $logid ) {
if ( !strlen( $field ) || !$values ) {
return false;
}
$insert = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase()
->newInsertQueryBuilder()
->insertInto( 'log_search' )
->ignore();
foreach ( $values as $value ) {
$insert->row( [ 'ls_field' => $field, 'ls_value' => $value, 'ls_log_id' => $logid ] );
}
$insert->caller( __METHOD__ )->execute();
return true;
}
/**
* Create a blob from a parameter array
*
* @param array $params
* @return string
*/
public static function makeParamBlob( $params ) {
return implode( "\n", $params );
}
/**
* Extract a parameter array from a blob
*
* @param string $blob
* @return array
*/
public static function extractParams( $blob ) {
if ( $blob === '' ) {
return [];
} else {
return explode( "\n", $blob );
}
}
/**
* Name of the log.
* @return Message
* @since 1.19
*/
public function getName() {
$logNames = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogNames );
// BC
$key = $logNames[$this->type] ?? 'log-name-' . $this->type;
return wfMessage( $key );
}
/**
* Description of this log type.
* @return Message
* @since 1.19
*/
public function getDescription() {
$logHeaders = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogHeaders );
// BC
$key = $logHeaders[$this->type] ?? 'log-description-' . $this->type;
return wfMessage( $key );
}
/**
* Returns the right needed to read this log type.
* @return string
* @since 1.19
*/
public function getRestriction() {
$logRestrictions = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogRestrictions );
// The empty string fallback will
// always return true in permission check
return $logRestrictions[$this->type] ?? '';
}
/**
* Tells if this log is not viewable by all.
* @return bool
* @since 1.19
*/
public function isRestricted() {
$restriction = $this->getRestriction();
return $restriction !== '' && $restriction !== '*';
}
}
|