| 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 |
namespace MediaWiki\Feed; |
| 25 |
|
| 26 |
use MediaWiki\Content\TextContent; |
| 27 |
use MediaWiki\Context\DerivativeContext; |
| 28 |
use MediaWiki\Context\RequestContext; |
| 29 |
use MediaWiki\Html\Html; |
| 30 |
use MediaWiki\MainConfigNames; |
| 31 |
use MediaWiki\MediaWikiServices; |
| 32 |
use MediaWiki\Output\OutputPage; |
| 33 |
use MediaWiki\Revision\RevisionRecord; |
| 34 |
use MediaWiki\Revision\SlotRecord; |
| 35 |
use MediaWiki\Title\Title; |
| 36 |
use UtfNormal; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
class FeedUtils { |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public static function checkFeedOutput( $type, $output = null ) { |
| 55 |
$feed = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::Feed ); |
| 56 |
$feedClasses = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::FeedClasses ); |
| 57 |
if ( $output === null ) { |
| 58 |
|
| 59 |
global $wgOut; |
| 60 |
$output = $wgOut; |
| 61 |
} |
| 62 |
|
| 63 |
if ( !$feed ) { |
| 64 |
$output->addWikiMsg( 'feed-unavailable' ); |
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
if ( !isset( $feedClasses[$type] ) ) { |
| 69 |
$output->addWikiMsg( 'feed-invalid' ); |
| 70 |
return false; |
| 71 |
} |
| 72 |
|
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public static function formatDiff( $row, $formattedComment = null ) { |
| 86 |
$titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title ); |
| 87 |
$timestamp = wfTimestamp( TS_MW, $row->rc_timestamp ); |
| 88 |
$actiontext = ''; |
| 89 |
if ( $row->rc_type == RC_LOG ) { |
| 90 |
$rcRow = (array)$row; |
| 91 |
$actiontext = MediaWikiServices::getInstance()->getLogFormatterFactory() |
| 92 |
->newFromRow( $rcRow )->getActionText(); |
| 93 |
} |
| 94 |
if ( $row->rc_deleted & RevisionRecord::DELETED_COMMENT ) { |
| 95 |
$formattedComment = wfMessage( 'rev-deleted-comment' )->escaped(); |
| 96 |
} elseif ( $formattedComment === null ) { |
| 97 |
$services = MediaWikiServices::getInstance(); |
| 98 |
$formattedComment = $services->getCommentFormatter()->format( |
| 99 |
$services->getCommentStore()->getComment( 'rc_comment', $row )->text ); |
| 100 |
} |
| 101 |
return self::formatDiffRow2( $titleObj, |
| 102 |
$row->rc_last_oldid, $row->rc_this_oldid, |
| 103 |
$timestamp, |
| 104 |
$formattedComment, |
| 105 |
$actiontext |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public static function formatDiffRow( $title, $oldid, $newid, $timestamp, |
| 123 |
$comment, $actiontext = '' |
| 124 |
) { |
| 125 |
$formattedComment = MediaWikiServices::getInstance()->getCommentFormatter() |
| 126 |
->format( $comment ); |
| 127 |
return self::formatDiffRow2( $title, $oldid, $newid, $timestamp, |
| 128 |
$formattedComment, $actiontext ); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public static function formatDiffRow2( |
| 144 |
$title, $oldid, $newid, $timestamp, $formattedComment, $actiontext = '' |
| 145 |
) { |
| 146 |
$feedDiffCutoff = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::FeedDiffCutoff ); |
| 147 |
|
| 148 |
|
| 149 |
$unwrappedText = implode( |
| 150 |
' ', |
| 151 |
array_filter( [ $actiontext, $formattedComment ] ) |
| 152 |
); |
| 153 |
$completeText = Html::rawElement( 'p', [], $unwrappedText ) . "\n"; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
$services = MediaWikiServices::getInstance(); |
| 159 |
$anon = $services->getUserFactory()->newAnonymous(); |
| 160 |
$permManager = $services->getPermissionManager(); |
| 161 |
$userCan = $permManager->userCan( |
| 162 |
'read', |
| 163 |
$anon, |
| 164 |
$title |
| 165 |
); |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
if ( $title->getNamespace() < 0 || !$userCan || !$newid ) { |
| 170 |
return $completeText; |
| 171 |
} |
| 172 |
|
| 173 |
$revLookup = $services->getRevisionLookup(); |
| 174 |
$contentHandlerFactory = $services->getContentHandlerFactory(); |
| 175 |
if ( $oldid ) { |
| 176 |
$diffText = ''; |
| 177 |
|
| 178 |
if ( $feedDiffCutoff > 0 ) { |
| 179 |
$revRecord = $revLookup->getRevisionById( $oldid ); |
| 180 |
|
| 181 |
if ( !$revRecord ) { |
| 182 |
$diffText = false; |
| 183 |
} else { |
| 184 |
$context = new DerivativeContext( RequestContext::getMain() ); |
| 185 |
$context->setTitle( $title ); |
| 186 |
|
| 187 |
$model = $revRecord->getSlot( |
| 188 |
SlotRecord::MAIN, |
| 189 |
RevisionRecord::RAW |
| 190 |
)->getModel(); |
| 191 |
$contentHandler = $contentHandlerFactory->getContentHandler( $model ); |
| 192 |
$de = $contentHandler->createDifferenceEngine( $context, $oldid, $newid ); |
| 193 |
$lang = $context->getLanguage(); |
| 194 |
$user = $context->getUser(); |
| 195 |
$diffText = $de->getDiff( |
| 196 |
$context->msg( 'previousrevision' )->text(), |
| 197 |
$context->msg( 'revisionasof', |
| 198 |
$lang->userTimeAndDate( $timestamp, $user ), |
| 199 |
$lang->userDate( $timestamp, $user ), |
| 200 |
$lang->userTime( $timestamp, $user ) )->text() ); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
if ( $feedDiffCutoff <= 0 || ( strlen( $diffText ) > $feedDiffCutoff ) ) { |
| 205 |
|
| 206 |
$diffText = self::getDiffLink( $title, $newid, $oldid ); |
| 207 |
} elseif ( $diffText === false ) { |
| 208 |
|
| 209 |
$diffText = Html::rawElement( |
| 210 |
'p', |
| 211 |
[], |
| 212 |
"Can't load revision $newid" |
| 213 |
); |
| 214 |
} else { |
| 215 |
|
| 216 |
$diffText = UtfNormal\Validator::cleanUp( $diffText ); |
| 217 |
$diffText = self::applyDiffStyle( $diffText ); |
| 218 |
} |
| 219 |
} else { |
| 220 |
$revRecord = $revLookup->getRevisionById( $newid ); |
| 221 |
if ( $feedDiffCutoff <= 0 || $revRecord === null ) { |
| 222 |
$newContent = $contentHandlerFactory |
| 223 |
->getContentHandler( $title->getContentModel() ) |
| 224 |
->makeEmptyContent(); |
| 225 |
} else { |
| 226 |
$newContent = $revRecord->getContent( SlotRecord::MAIN ); |
| 227 |
} |
| 228 |
|
| 229 |
if ( $newContent instanceof TextContent ) { |
| 230 |
|
| 231 |
$text = $newContent->getText(); |
| 232 |
|
| 233 |
if ( $feedDiffCutoff <= 0 || strlen( $text ) > $feedDiffCutoff ) { |
| 234 |
$html = null; |
| 235 |
} else { |
| 236 |
$html = nl2br( htmlspecialchars( $text ) ); |
| 237 |
} |
| 238 |
} else { |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
$html = null; |
| 244 |
} |
| 245 |
|
| 246 |
if ( $html === null ) { |
| 247 |
|
| 248 |
|
| 249 |
$diffText = self::getDiffLink( $title, $newid ); |
| 250 |
} else { |
| 251 |
$diffText = Html::rawElement( |
| 252 |
'p', |
| 253 |
[], |
| 254 |
Html::element( 'b', [], wfMessage( 'newpage' )->text() ) |
| 255 |
); |
| 256 |
$diffText .= Html::rawElement( 'div', [], $html ); |
| 257 |
} |
| 258 |
} |
| 259 |
$completeText .= $diffText; |
| 260 |
|
| 261 |
return $completeText; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
protected static function getDiffLink( Title $title, $newid, $oldid = null ) { |
| 274 |
$queryParameters = [ 'diff' => $newid ]; |
| 275 |
if ( $oldid != null ) { |
| 276 |
$queryParameters['oldid'] = $oldid; |
| 277 |
} |
| 278 |
$diffUrl = $title->getFullURL( $queryParameters ); |
| 279 |
|
| 280 |
$diffLink = Html::element( 'a', [ 'href' => $diffUrl ], |
| 281 |
wfMessage( 'showdiff' )->inContentLanguage()->text() ); |
| 282 |
|
| 283 |
return $diffLink; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
public static function applyDiffStyle( $text ) { |
| 295 |
$styles = [ |
| 296 |
'diff' => 'background-color: #fff; color: #202122;', |
| 297 |
'diff-otitle' => 'background-color: #fff; color: #202122; text-align: center;', |
| 298 |
'diff-ntitle' => 'background-color: #fff; color: #202122; text-align: center;', |
| 299 |
'diff-addedline' => 'color: #202122; font-size: 88%; border-style: solid; ' |
| 300 |
. 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; ' |
| 301 |
. 'vertical-align: top; white-space: pre-wrap;', |
| 302 |
'diff-deletedline' => 'color: #202122; font-size: 88%; border-style: solid; ' |
| 303 |
. 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; ' |
| 304 |
. 'vertical-align: top; white-space: pre-wrap;', |
| 305 |
'diff-context' => 'background-color: #f8f9fa; color: #202122; font-size: 88%; ' |
| 306 |
. 'border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; ' |
| 307 |
. 'border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;', |
| 308 |
'diffchange' => 'font-weight: bold; text-decoration: none;', |
| 309 |
]; |
| 310 |
|
| 311 |
foreach ( $styles as $class => $style ) { |
| 312 |
$text = preg_replace( '/(<\w+\b[^<>]*)\bclass=([\'"])(?:[^\'"]*\s)?' . |
| 313 |
preg_quote( $class ) . '(?:\s[^\'"]*)?\2(?=[^<>]*>)/', |
| 314 |
'$1style="' . $style . '"', $text ); |
| 315 |
} |
| 316 |
|
| 317 |
return $text; |
| 318 |
} |
| 319 |
|
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
class_alias( FeedUtils::class, 'FeedUtils' ); |