| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\AbuseFilter\View; |
| 4 |
|
| 5 |
use DifferenceEngine; |
| 6 |
use MediaWiki\Content\TextContent; |
| 7 |
use MediaWiki\Context\IContextSource; |
| 8 |
use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager; |
| 9 |
use MediaWiki\Extension\AbuseFilter\Filter\ClosestFilterVersionNotFoundException; |
| 10 |
use MediaWiki\Extension\AbuseFilter\Filter\FilterNotFoundException; |
| 11 |
use MediaWiki\Extension\AbuseFilter\Filter\FilterVersionNotFoundException; |
| 12 |
use MediaWiki\Extension\AbuseFilter\Filter\HistoryFilter; |
| 13 |
use MediaWiki\Extension\AbuseFilter\FilterLookup; |
| 14 |
use MediaWiki\Extension\AbuseFilter\SpecsFormatter; |
| 15 |
use MediaWiki\Extension\AbuseFilter\TableDiffFormatterFullContext; |
| 16 |
use MediaWiki\Html\Html; |
| 17 |
use MediaWiki\Linker\Linker; |
| 18 |
use MediaWiki\Linker\LinkRenderer; |
| 19 |
use OOUI; |
| 20 |
use Wikimedia\Diff\Diff; |
| 21 |
|
| 22 |
class AbuseFilterViewDiff extends AbuseFilterView { |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public $oldVersion; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public $newVersion; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public $nextHistoryId; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
private $filter; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
private $specsFormatter; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
private $filterLookup; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function __construct( |
| 58 |
AbuseFilterPermissionManager $afPermManager, |
| 59 |
SpecsFormatter $specsFormatter, |
| 60 |
FilterLookup $filterLookup, |
| 61 |
IContextSource $context, |
| 62 |
LinkRenderer $linkRenderer, |
| 63 |
string $basePageName, |
| 64 |
array $params |
| 65 |
) { |
| 66 |
parent::__construct( $afPermManager, $context, $linkRenderer, $basePageName, $params ); |
| 67 |
$this->specsFormatter = $specsFormatter; |
| 68 |
$this->specsFormatter->setMessageLocalizer( $this->getContext() ); |
| 69 |
$this->filterLookup = $filterLookup; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public function show() { |
| 76 |
$show = $this->loadData(); |
| 77 |
$out = $this->getOutput(); |
| 78 |
$out->enableOOUI(); |
| 79 |
$out->addModuleStyles( [ 'oojs-ui.styles.icons-movement' ] ); |
| 80 |
|
| 81 |
$links = []; |
| 82 |
if ( $this->filter ) { |
| 83 |
$links['abusefilter-history-backedit'] = $this->getTitle( $this->filter )->getFullURL(); |
| 84 |
$links['abusefilter-diff-backhistory'] = $this->getTitle( "history/$this->filter" )->getFullURL(); |
| 85 |
} |
| 86 |
|
| 87 |
foreach ( $links as $msg => $href ) { |
| 88 |
$links[$msg] = new OOUI\ButtonWidget( [ |
| 89 |
'label' => $this->msg( $msg )->text(), |
| 90 |
'href' => $href |
| 91 |
] ); |
| 92 |
} |
| 93 |
|
| 94 |
$backlinks = new OOUI\HorizontalLayout( [ 'items' => array_values( $links ) ] ); |
| 95 |
$out->addHTML( $backlinks ); |
| 96 |
|
| 97 |
if ( $show ) { |
| 98 |
$out->addHTML( $this->formatDiff() ); |
| 99 |
|
| 100 |
$buttons = []; |
| 101 |
$oldHistoryID = $this->oldVersion->getHistoryID(); |
| 102 |
if ( $this->filterLookup->getFirstFilterVersionID( $this->filter ) !== $oldHistoryID ) { |
| 103 |
|
| 104 |
$href = $this->getTitle( "history/$this->filter/diff/prev/$oldHistoryID" )->getFullURL(); |
| 105 |
$buttons[] = new OOUI\ButtonWidget( [ |
| 106 |
'label' => $this->msg( 'abusefilter-diff-prev' )->text(), |
| 107 |
'href' => $href, |
| 108 |
'icon' => 'previous' |
| 109 |
] ); |
| 110 |
} |
| 111 |
|
| 112 |
if ( $this->nextHistoryId !== null ) { |
| 113 |
|
| 114 |
$href = $this->getTitle( "history/$this->filter/diff/prev/$this->nextHistoryId" )->getFullURL(); |
| 115 |
$buttons[] = new OOUI\ButtonWidget( [ |
| 116 |
'label' => $this->msg( 'abusefilter-diff-next' )->text(), |
| 117 |
'href' => $href, |
| 118 |
'icon' => 'next' |
| 119 |
] ); |
| 120 |
} |
| 121 |
|
| 122 |
if ( count( $buttons ) > 0 ) { |
| 123 |
$buttons = new OOUI\HorizontalLayout( [ |
| 124 |
'items' => $buttons, |
| 125 |
'classes' => [ 'mw-abusefilter-history-buttons' ] |
| 126 |
] ); |
| 127 |
$out->addHTML( $buttons ); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public function loadData() { |
| 136 |
$oldSpec = $this->mParams[3]; |
| 137 |
$newSpec = $this->mParams[4]; |
| 138 |
|
| 139 |
if ( !is_numeric( $this->mParams[1] ) ) { |
| 140 |
$this->getOutput()->addWikiMsg( 'abusefilter-diff-invalid' ); |
| 141 |
return false; |
| 142 |
} |
| 143 |
$this->filter = (int)$this->mParams[1]; |
| 144 |
|
| 145 |
$this->oldVersion = $this->loadSpec( $oldSpec, $newSpec ); |
| 146 |
$this->newVersion = $this->loadSpec( $newSpec, $oldSpec ); |
| 147 |
|
| 148 |
if ( $this->oldVersion === null || $this->newVersion === null ) { |
| 149 |
$this->getOutput()->addWikiMsg( 'abusefilter-diff-invalid' ); |
| 150 |
return false; |
| 151 |
} |
| 152 |
|
| 153 |
if ( !$this->afPermManager->canViewPrivateFilters( $this->getAuthority() ) && |
| 154 |
( $this->oldVersion->isHidden() || $this->newVersion->isHidden() ) |
| 155 |
) { |
| 156 |
$this->getOutput()->addWikiMsg( 'abusefilter-history-error-hidden' ); |
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
if ( !$this->afPermManager->canViewProtectedVariables( $this->getAuthority() ) && |
| 161 |
( $this->oldVersion->isProtected() || $this->newVersion->isProtected() ) |
| 162 |
) { |
| 163 |
$this->getOutput()->addWikiMsg( 'abusefilter-history-error-protected' ); |
| 164 |
return false; |
| 165 |
} |
| 166 |
|
| 167 |
try { |
| 168 |
$this->nextHistoryId = $this->filterLookup->getClosestVersion( |
| 169 |
$this->newVersion->getHistoryID(), |
| 170 |
$this->filter, |
| 171 |
FilterLookup::DIR_NEXT |
| 172 |
)->getHistoryID(); |
| 173 |
} catch ( ClosestFilterVersionNotFoundException $_ ) { |
| 174 |
$this->nextHistoryId = null; |
| 175 |
} |
| 176 |
|
| 177 |
return true; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
public function loadSpec( $spec, $otherSpec ): ?HistoryFilter { |
| 186 |
static $dependentSpecs = [ 'prev', 'next' ]; |
| 187 |
static $cache = []; |
| 188 |
|
| 189 |
if ( isset( $cache[$spec] ) ) { |
| 190 |
return $cache[$spec]; |
| 191 |
} |
| 192 |
|
| 193 |
$filterObj = null; |
| 194 |
if ( ( $spec === 'prev' || $spec === 'next' ) && !in_array( $otherSpec, $dependentSpecs ) ) { |
| 195 |
$other = $this->loadSpec( $otherSpec, $spec ); |
| 196 |
|
| 197 |
if ( !$other ) { |
| 198 |
return null; |
| 199 |
} |
| 200 |
|
| 201 |
$dir = $spec === 'prev' ? FilterLookup::DIR_PREV : FilterLookup::DIR_NEXT; |
| 202 |
try { |
| 203 |
$filterObj = $this->filterLookup->getClosestVersion( $other->getHistoryID(), $this->filter, $dir ); |
| 204 |
} catch ( ClosestFilterVersionNotFoundException $_ ) { |
| 205 |
$t = $this->getTitle( "history/$this->filter/item/" . $other->getHistoryID() ); |
| 206 |
$this->getOutput()->redirect( $t->getFullURL() ); |
| 207 |
return null; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
if ( $filterObj === null ) { |
| 212 |
try { |
| 213 |
if ( is_numeric( $spec ) ) { |
| 214 |
$filterObj = $this->filterLookup->getFilterVersion( (int)$spec ); |
| 215 |
} elseif ( $spec === 'cur' ) { |
| 216 |
$filterObj = $this->filterLookup->getLastHistoryVersion( $this->filter ); |
| 217 |
} |
| 218 |
} catch ( FilterNotFoundException | FilterVersionNotFoundException $_ ) { |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
$cache[$spec] = $filterObj; |
| 223 |
return $cache[$spec]; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
private function getVersionHeading( HistoryFilter $filterVersion ) { |
| 231 |
$text = $this->getLanguage()->userTimeAndDate( |
| 232 |
$filterVersion->getTimestamp(), |
| 233 |
$this->getUser() |
| 234 |
); |
| 235 |
$history_id = $filterVersion->getHistoryID(); |
| 236 |
$title = $this->getTitle( "history/$this->filter/item/$history_id" ); |
| 237 |
|
| 238 |
$versionLink = $this->linkRenderer->makeLink( $title, $text ); |
| 239 |
$userLink = Linker::userLink( |
| 240 |
$filterVersion->getUserID(), |
| 241 |
$filterVersion->getUserName() |
| 242 |
); |
| 243 |
return Html::rawElement( |
| 244 |
'th', |
| 245 |
[], |
| 246 |
$this->msg( 'abusefilter-diff-version' ) |
| 247 |
->rawParams( $versionLink, $userLink ) |
| 248 |
->params( $filterVersion->getUserName() ) |
| 249 |
->parse() |
| 250 |
); |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
public function formatDiff() { |
| 257 |
$oldVersion = $this->oldVersion; |
| 258 |
$newVersion = $this->newVersion; |
| 259 |
|
| 260 |
|
| 261 |
$headings = Html::rawElement( |
| 262 |
'th', |
| 263 |
[], |
| 264 |
$this->msg( 'abusefilter-diff-item' )->parse() |
| 265 |
); |
| 266 |
$headings .= $this->getVersionHeading( $oldVersion ); |
| 267 |
$headings .= $this->getVersionHeading( $newVersion ); |
| 268 |
$headings = Html::rawElement( 'tr', [], $headings ); |
| 269 |
|
| 270 |
$body = ''; |
| 271 |
|
| 272 |
$info = $this->getDiffRow( 'abusefilter-edit-description', $oldVersion->getName(), $newVersion->getName() ); |
| 273 |
$info .= $this->getDiffRow( |
| 274 |
'abusefilter-edit-group', |
| 275 |
$this->specsFormatter->nameGroup( $oldVersion->getGroup() ), |
| 276 |
$this->specsFormatter->nameGroup( $newVersion->getGroup() ) |
| 277 |
); |
| 278 |
$info .= $this->getDiffRow( |
| 279 |
'abusefilter-edit-flags', |
| 280 |
$this->specsFormatter->formatFilterFlags( $oldVersion, $this->getLanguage() ), |
| 281 |
$this->specsFormatter->formatFilterFlags( $newVersion, $this->getLanguage() ) |
| 282 |
); |
| 283 |
|
| 284 |
$info .= $this->getDiffRow( 'abusefilter-edit-notes', $oldVersion->getComments(), $newVersion->getComments() ); |
| 285 |
if ( $info !== '' ) { |
| 286 |
$body .= $this->getHeaderRow( 'abusefilter-diff-info' ) . $info; |
| 287 |
} |
| 288 |
|
| 289 |
$pattern = $this->getDiffRow( 'abusefilter-edit-rules', $oldVersion->getRules(), $newVersion->getRules() ); |
| 290 |
if ( $pattern !== '' ) { |
| 291 |
$body .= $this->getHeaderRow( 'abusefilter-diff-pattern' ) . $pattern; |
| 292 |
} |
| 293 |
|
| 294 |
$actions = $this->getDiffRow( |
| 295 |
'abusefilter-edit-consequences', |
| 296 |
$this->stringifyActions( $oldVersion->getActions() ) ?: [ '' ], |
| 297 |
$this->stringifyActions( $newVersion->getActions() ) ?: [ '' ] |
| 298 |
); |
| 299 |
if ( $actions !== '' ) { |
| 300 |
$body .= $this->getHeaderRow( 'abusefilter-edit-consequences' ) . $actions; |
| 301 |
} |
| 302 |
|
| 303 |
$tableHead = Html::rawElement( 'thead', [], $headings ); |
| 304 |
$tableBody = Html::rawElement( 'tbody', [], $body ); |
| 305 |
$table = Html::rawElement( |
| 306 |
'table', |
| 307 |
[ 'class' => 'wikitable' ], |
| 308 |
$tableHead . $tableBody |
| 309 |
); |
| 310 |
|
| 311 |
return Html::rawElement( 'h2', [], $this->msg( 'abusefilter-diff-title' )->parse() ) . |
| 312 |
$table; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
private function stringifyActions( array $actions ): array { |
| 320 |
$lines = []; |
| 321 |
|
| 322 |
ksort( $actions ); |
| 323 |
$language = $this->getLanguage(); |
| 324 |
foreach ( $actions as $action => $parameters ) { |
| 325 |
$lines[] = $this->specsFormatter->formatAction( $action, $parameters, $language ); |
| 326 |
} |
| 327 |
|
| 328 |
return $lines; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
public function getHeaderRow( $key ) { |
| 336 |
$msg = $this->msg( $key )->parse(); |
| 337 |
return Html::rawElement( |
| 338 |
'tr', |
| 339 |
[ 'class' => 'mw-abusefilter-diff-header' ], |
| 340 |
Html::rawElement( 'th', [ 'colspan' => 3 ], $msg ) |
| 341 |
); |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
public function getDiffRow( $key, $old, $new ) { |
| 351 |
if ( !is_array( $old ) ) { |
| 352 |
$old = explode( "\n", TextContent::normalizeLineEndings( $old ) ); |
| 353 |
} |
| 354 |
if ( !is_array( $new ) ) { |
| 355 |
$new = explode( "\n", TextContent::normalizeLineEndings( $new ) ); |
| 356 |
} |
| 357 |
|
| 358 |
if ( $old === $new ) { |
| 359 |
return ''; |
| 360 |
} |
| 361 |
|
| 362 |
$diffEngine = new DifferenceEngine( $this->getContext() ); |
| 363 |
|
| 364 |
$diffEngine->showDiffStyle(); |
| 365 |
|
| 366 |
$diff = new Diff( $old, $new ); |
| 367 |
$formatter = new TableDiffFormatterFullContext(); |
| 368 |
$formattedDiff = $diffEngine->addHeader( $formatter->format( $diff ), '', '' ); |
| 369 |
|
| 370 |
$heading = Html::rawElement( 'th', [], $this->msg( $key )->parse() ); |
| 371 |
$bodyCell = Html::rawElement( 'td', [ 'colspan' => 2 ], $formattedDiff ); |
| 372 |
return Html::rawElement( |
| 373 |
'tr', |
| 374 |
[], |
| 375 |
$heading . $bodyCell |
| 376 |
) . "\n"; |
| 377 |
} |
| 378 |
} |