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 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
|
<?php
/**
* Contains classes for formatting log entries
*
* @file
* @author Niklas Laxström
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
* @since 1.19
*/
/**
* Implements the default log formatting.
* Can be overridden by subclassing and setting
* $wgLogActionsHandlers['type/subtype'] = 'class'; or
* $wgLogActionsHandlers['type/*'] = 'class';
* @since 1.19
*/
class LogFormatter {
// Audience options for viewing usernames, comments, and actions
const FOR_PUBLIC = 1;
const FOR_THIS_USER = 2;
// Static->
/**
* Constructs a new formatter suitable for given entry.
* @param $entry LogEntry
* @return LogFormatter
*/
public static function newFromEntry( LogEntry $entry ) {
global $wgLogActionsHandlers;
$fulltype = $entry->getFullType();
$wildcard = $entry->getType() . '/*';
$handler = '';
if ( isset( $wgLogActionsHandlers[$fulltype] ) ) {
$handler = $wgLogActionsHandlers[$fulltype];
} elseif ( isset( $wgLogActionsHandlers[$wildcard] ) ) {
$handler = $wgLogActionsHandlers[$wildcard];
}
if ( $handler !== '' && is_string( $handler ) && class_exists( $handler ) ) {
return new $handler( $entry );
}
return new LegacyLogFormatter( $entry );
}
/**
* Handy shortcut for constructing a formatter directly from
* database row.
* @param $row
* @see DatabaseLogEntry::getSelectQueryData
* @return LogFormatter
*/
public static function newFromRow( $row ) {
return self::newFromEntry( DatabaseLogEntry::newFromRow( $row ) );
}
// Nonstatic->
/// @var LogEntry
protected $entry;
/// Integer constant for handling log_deleted
protected $audience = self::FOR_PUBLIC;
/// Whether to output user tool links
protected $linkFlood = false;
/**
* Set to true if we are constructing a message text that is going to
* be included in page history or send to IRC feed. Links are replaced
* with plaintext or with [[pagename]] kind of syntax, that is parsed
* by page histories and IRC feeds.
* @var boolean
*/
protected $plaintext = false;
protected $irctext = false;
protected function __construct( LogEntry $entry ) {
$this->entry = $entry;
$this->context = RequestContext::getMain();
}
/**
* Replace the default context
* @param $context IContextSource
*/
public function setContext( IContextSource $context ) {
$this->context = $context;
}
/**
* Set the visibility restrictions for displaying content.
* If set to public, and an item is deleted, then it will be replaced
* with a placeholder even if the context user is allowed to view it.
* @param $audience integer self::FOR_THIS_USER or self::FOR_PUBLIC
*/
public function setAudience( $audience ) {
$this->audience = ( $audience == self::FOR_THIS_USER )
? self::FOR_THIS_USER
: self::FOR_PUBLIC;
}
/**
* Check if a log item can be displayed
* @param $field integer LogPage::DELETED_* constant
* @return bool
*/
protected function canView( $field ) {
if ( $this->audience == self::FOR_THIS_USER ) {
return LogEventsList::userCanBitfield(
$this->entry->getDeleted(), $field, $this->context->getUser() );
} else {
return !$this->entry->isDeleted( $field );
}
}
/**
* If set to true, will produce user tool links after
* the user name. This should be replaced with generic
* CSS/JS solution.
* @param $value boolean
*/
public function setShowUserToolLinks( $value ) {
$this->linkFlood = $value;
}
/**
* Ugly hack to produce plaintext version of the message.
* Usually you also want to set extraneous request context
* to avoid formatting for any particular user.
* @see getActionText()
* @return string text
*/
public function getPlainActionText() {
$this->plaintext = true;
$text = $this->getActionText();
$this->plaintext = false;
return $text;
}
/**
* Even uglier hack to maintain backwards compatibilty with IRC bots
* (bug 34508).
* @see getActionText()
* @return string text
*/
public function getIRCActionText() {
$this->plaintext = true;
$text = $this->getActionText();
$entry = $this->entry;
$parameters = $entry->getParameters();
// @see LogPage::actionText()
$msgOpts = array( 'parsemag', 'escape', 'replaceafter', 'content' );
// Text of title the action is aimed at.
$target = $entry->getTarget()->getPrefixedText() ;
$text = null;
switch( $entry->getType() ) {
case 'move':
switch( $entry->getSubtype() ) {
case 'move':
$movesource = $parameters['4::target'];
$text = wfMsgExt( '1movedto2', $msgOpts, $target, $movesource );
break;
case 'move_redir':
$movesource = $parameters['4::target'];
$text = wfMsgExt( '1movedto2_redir', $msgOpts, $target, $movesource );
break;
case 'move-noredirect':
break;
case 'move_redir-noredirect':
break;
}
break;
case 'delete':
switch( $entry->getSubtype() ) {
case 'delete':
$text = wfMsgExt( 'deletedarticle', $msgOpts, $target );
break;
case 'restore':
$text = wfMsgExt( 'undeletedarticle', $msgOpts, $target );
break;
//case 'revision': // Revision deletion
//case 'event': // Log deletion
// see https://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/includes/LogPage.php?&pathrev=97044&r1=97043&r2=97044
//default:
}
break;
case 'patrol':
// https://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/includes/PatrolLog.php?&pathrev=97495&r1=97494&r2=97495
// Create a diff link to the patrolled revision
if ( $entry->getSubtype() === 'patrol' ) {
$diffLink = htmlspecialchars(
wfMsgForContent( 'patrol-log-diff', $parameters['4::curid'] ) );
$text = wfMsgForContent( 'patrol-log-line', $diffLink, "[[$target]]", "" );
} else {
// broken??
}
break;
case 'newusers':
switch( $entry->getSubtype() ) {
case 'newusers':
case 'create':
$text = wfMsgExt( 'newuserlog-create-entry', $msgOpts /* no params */ );
break;
case 'create2':
$text = wfMsgExt( 'newuserlog-create2-entry', $msgOpts, $target );
break;
case 'autocreate':
$text = wfMsgExt( 'newuserlog-autocreate-entry', $msgOpts /* no params */ );
break;
}
break;
case 'upload':
switch( $entry->getSubtype() ) {
case 'upload':
$text = wfMsgExt( 'uploadedimage', $msgOpts, $target );
break;
case 'overwrite':
$text = wfMsgExt( 'overwroteimage', $msgOpts, $target );
break;
}
break;
// case 'suppress' --private log -- aaron (sign your messages so we know who to blame in a few years :-D)
// default:
}
if( is_null( $text ) ) {
$text = $this->getPlainActionText();
}
$this->plaintext = false;
return $text;
}
/**
* Gets the log action, including username.
* @return string HTML
*/
public function getActionText() {
if ( $this->canView( LogPage::DELETED_ACTION ) ) {
$element = $this->getActionMessage();
if ( $element instanceof Message ) {
$element = $this->plaintext ? $element->text() : $element->escaped();
}
if ( $this->entry->isDeleted( LogPage::DELETED_ACTION ) ) {
$element = $this->styleRestricedElement( $element );
}
} else {
$performer = $this->getPerformerElement() . $this->msg( 'word-separator' )->text();
$element = $performer . $this->getRestrictedElement( 'rev-deleted-event' );
}
return $element;
}
/**
* Returns a sentence describing the log action. Usually
* a Message object is returned, but old style log types
* and entries might return pre-escaped html string.
* @return Message|pre-escaped html
*/
protected function getActionMessage() {
$message = $this->msg( $this->getMessageKey() );
$message->params( $this->getMessageParameters() );
return $message;
}
/**
* Returns a key to be used for formatting the action sentence.
* Default is logentry-TYPE-SUBTYPE for modern logs. Legacy log
* types will use custom keys, and subclasses can also alter the
* key depending on the entry itself.
* @return string message key
*/
protected function getMessageKey() {
$type = $this->entry->getType();
$subtype = $this->entry->getSubtype();
return "logentry-$type-$subtype";
}
/**
* Extracts the optional extra parameters for use in action messages.
* The array indexes start from number 3.
* @return array
*/
protected function extractParameters() {
$entry = $this->entry;
$params = array();
if ( $entry->isLegacy() ) {
foreach ( $entry->getParameters() as $index => $value ) {
$params[$index + 3] = $value;
}
}
// Filter out parameters which are not in format #:foo
foreach ( $entry->getParameters() as $key => $value ) {
if ( strpos( $key, ':' ) === false ) continue;
list( $index, $type, $name ) = explode( ':', $key, 3 );
$params[$index - 1] = $value;
}
/* Message class doesn't like non consecutive numbering.
* Fill in missing indexes with empty strings to avoid
* incorrect renumbering.
*/
if ( count( $params ) ) {
$max = max( array_keys( $params ) );
for ( $i = 4; $i < $max; $i++ ) {
if ( !isset( $params[$i] ) ) {
$params[$i] = '';
}
}
}
return $params;
}
/**
* Formats parameters intented for action message from
* array of all parameters. There are three hardcoded
* parameters (array is zero-indexed, this list not):
* - 1: user name with premade link
* - 2: usable for gender magic function
* - 3: target page with premade link
* @return array
*/
protected function getMessageParameters() {
if ( isset( $this->parsedParameters ) ) {
return $this->parsedParameters;
}
$entry = $this->entry;
$params = $this->extractParameters();
$params[0] = Message::rawParam( $this->getPerformerElement() );
$params[1] = $entry->getPerformer()->getName();
$params[2] = Message::rawParam( $this->makePageLink( $entry->getTarget() ) );
// Bad things happens if the numbers are not in correct order
ksort( $params );
return $this->parsedParameters = $params;
}
/**
* Helper to make a link to the page, taking the plaintext
* value in consideration.
* @param $title Title the page
* @param $parameters array query parameters
* @return String
*/
protected function makePageLink( Title $title = null, $parameters = array() ) {
if ( !$this->plaintext ) {
$link = Linker::link( $title, null, array(), $parameters );
} else {
if ( !$title instanceof Title ) {
throw new MWException( "Expected title, got null" );
}
$link = '[[' . $title->getPrefixedText() . ']]';
}
return $link;
}
/**
* Provides the name of the user who performed the log action.
* Used as part of log action message or standalone, depending
* which parts of the log entry has been hidden.
*/
public function getPerformerElement() {
if ( $this->canView( LogPage::DELETED_USER ) ) {
$performer = $this->entry->getPerformer();
$element = $this->makeUserLink( $performer );
if ( $this->entry->isDeleted( LogPage::DELETED_USER ) ) {
$element = $this->styleRestricedElement( $element );
}
} else {
$element = $this->getRestrictedElement( 'rev-deleted-user' );
}
return $element;
}
/**
* Gets the luser provided comment
* @return string HTML
*/
public function getComment() {
if ( $this->canView( LogPage::DELETED_COMMENT ) ) {
$comment = Linker::commentBlock( $this->entry->getComment() );
// No hard coded spaces thanx
$element = ltrim( $comment );
if ( $this->entry->isDeleted( LogPage::DELETED_COMMENT ) ) {
$element = $this->styleRestricedElement( $element );
}
} else {
$element = $this->getRestrictedElement( 'rev-deleted-comment' );
}
return $element;
}
/**
* Helper method for displaying restricted element.
* @param $message string
* @return string HTML or wikitext
*/
protected function getRestrictedElement( $message ) {
if ( $this->plaintext ) {
return $this->msg( $message )->text();
}
$content = $this->msg( $message )->escaped();
$attribs = array( 'class' => 'history-deleted' );
return Html::rawElement( 'span', $attribs, $content );
}
/**
* Helper method for styling restricted element.
* @param $content string
* @return string HTML or wikitext
*/
protected function styleRestricedElement( $content ) {
if ( $this->plaintext ) {
return $content;
}
$attribs = array( 'class' => 'history-deleted' );
return Html::rawElement( 'span', $attribs, $content );
}
/**
* Shortcut for wfMessage which honors local context.
* @todo Would it be better to require replacing the global context instead?
* @param $key string
* @return Message
*/
protected function msg( $key ) {
return wfMessage( $key )
->inLanguage( $this->context->getLanguage() )
->title( $this->context->getTitle() );
}
protected function makeUserLink( User $user ) {
if ( $this->plaintext ) {
$element = $user->getName();
} else {
$element = Linker::userLink(
$user->getId(),
$user->getName()
);
if ( $this->linkFlood ) {
$element .= Linker::userToolLinks(
$user->getId(),
$user->getName(),
true, // Red if no edits
0, // Flags
$user->getEditCount()
);
}
}
return $element;
}
/**
* @return Array of titles that should be preloaded with LinkBatch.
*/
public function getPreloadTitles() {
return array();
}
}
/**
* This class formats all log entries for log types
* which have not been converted to the new system.
* This is not about old log entries which store
* parameters in a different format - the new
* LogFormatter classes have code to support formatting
* those too.
* @since 1.19
*/
class LegacyLogFormatter extends LogFormatter {
protected function getActionMessage() {
$entry = $this->entry;
$action = LogPage::actionText(
$entry->getType(),
$entry->getSubtype(),
$entry->getTarget(),
$this->plaintext ? null : $this->context->getSkin(),
(array)$entry->getParameters(),
!$this->plaintext // whether to filter [[]] links
);
$performer = $this->getPerformerElement();
return $performer . $this->msg( 'word-separator' )->text() . $action;
}
}
/**
* This class formats move log entries.
* @since 1.19
*/
class MoveLogFormatter extends LogFormatter {
public function getPreloadTitles() {
$params = $this->extractParameters();
return array( Title::newFromText( $params[3] ) );
}
protected function getMessageKey() {
$key = parent::getMessageKey();
$params = $this->getMessageParameters();
if ( isset( $params[4] ) && $params[4] === '1' ) {
$key .= '-noredirect';
}
return $key;
}
protected function getMessageParameters() {
$params = parent::getMessageParameters();
$oldname = $this->makePageLink( $this->entry->getTarget(), array( 'redirect' => 'no' ) );
$newname = $this->makePageLink( Title::newFromText( $params[3] ) );
$params[2] = Message::rawParam( $oldname );
$params[3] = Message::rawParam( $newname );
return $params;
}
}
/**
* This class formats delete log entries.
* @since 1.19
*/
class DeleteLogFormatter extends LogFormatter {
protected function getMessageKey() {
$key = parent::getMessageKey();
if ( in_array( $this->entry->getSubtype(), array( 'event', 'revision' ) ) ) {
if ( count( $this->getMessageParameters() ) < 5 ) {
return "$key-legacy";
}
}
return $key;
}
protected function getMessageParameters() {
if ( isset( $this->parsedParametersDeleteLog ) ) {
return $this->parsedParametersDeleteLog;
}
$params = parent::getMessageParameters();
$subtype = $this->entry->getSubtype();
if ( in_array( $subtype, array( 'event', 'revision' ) ) ) {
if (
($subtype === 'event' && count( $params ) === 6 ) ||
($subtype === 'revision' && isset( $params[3] ) && $params[3] === 'revision' )
) {
$paramStart = $subtype === 'revision' ? 4 : 3;
$old = $this->parseBitField( $params[$paramStart+1] );
$new = $this->parseBitField( $params[$paramStart+2] );
list( $hid, $unhid, $extra ) = RevisionDeleter::getChanges( $new, $old );
$changes = array();
foreach ( $hid as $v ) {
$changes[] = $this->msg( "$v-hid" )->plain();
}
foreach ( $unhid as $v ) {
$changes[] = $this->msg( "$v-unhid" )->plain();
}
foreach ( $extra as $v ) {
$changes[] = $this->msg( $v )->plain();
}
$changeText = $this->context->getLanguage()->listToText( $changes );
$newParams = array_slice( $params, 0, 3 );
$newParams[3] = $changeText;
$count = count( explode( ',', $params[$paramStart] ) );
$newParams[4] = $this->context->getLanguage()->formatNum( $count );
return $this->parsedParametersDeleteLog = $newParams;
} else {
return $this->parsedParametersDeleteLog = array_slice( $params, 0, 3 );
}
}
return $this->parsedParametersDeleteLog = $params;
}
protected function parseBitField( $string ) {
// Input is like ofield=2134 or just the number
if ( strpos( $string, 'field=' ) === 1 ) {
list( , $field ) = explode( '=', $string );
return (int) $field;
} else {
return (int) $string;
}
}
}
/**
* This class formats patrol log entries.
* @since 1.19
*/
class PatrolLogFormatter extends LogFormatter {
protected function getMessageKey() {
$key = parent::getMessageKey();
$params = $this->getMessageParameters();
if ( isset( $params[5] ) && $params[5] ) {
$key .= '-auto';
}
return $key;
}
protected function getMessageParameters() {
$params = parent::getMessageParameters();
$newParams = array_slice( $params, 0, 3 );
$target = $this->entry->getTarget();
$oldid = $params[3];
$revision = $this->context->getLanguage()->formatNum( $oldid, true );
if ( $this->plaintext ) {
$revlink = $revision;
} elseif ( $target->exists() ) {
$query = array(
'oldid' => $oldid,
'diff' => 'prev'
);
$revlink = Linker::link( $target, htmlspecialchars( $revision ), array(), $query );
} else {
$revlink = htmlspecialchars( $revision );
}
$newParams[3] = Message::rawParam( $revlink );
return $newParams;
}
}
/**
* This class formats new user log entries.
* @since 1.19
*/
class NewUsersLogFormatter extends LogFormatter {
protected function getMessageParameters() {
$params = parent::getMessageParameters();
if ( $this->entry->getSubtype() === 'create2' ) {
if ( isset( $params[3] ) ) {
$target = User::newFromId( $params[3] );
} else {
$target = User::newFromName( $this->entry->getTarget()->getText(), false );
}
$params[2] = Message::rawParam( $this->makeUserLink( $target ) );
$params[3] = $target->getName();
}
return $params;
}
public function getComment() {
$timestamp = wfTimestamp( TS_MW, $this->entry->getTimestamp() );
if ( $timestamp < '20080129000000' ) {
# Suppress $comment from old entries (before 2008-01-29),
# not needed and can contain incorrect links
return '';
}
return parent::getComment();
}
}
|