X-Git-Url: https://scripts.mit.edu/gitweb/autoinstallsdev/mediawiki.git/blobdiff_plain/19e297c21b10b1b8a3acad5e73fc71dcb35db44a..6932310fd58ebef145fa01eb76edf7150284d8ea:/includes/FeedUtils.php diff --git a/includes/FeedUtils.php b/includes/FeedUtils.php index 9daffc12..0def6a04 100644 --- a/includes/FeedUtils.php +++ b/includes/FeedUtils.php @@ -1,4 +1,25 @@ getVal( 'action' ) === 'purge'; - if ( $purge && $wgUser->isAllowed('purge') ) { - $messageMemc->delete( $timekey ); - $messageMemc->delete( $key ); + // Allow users with 'purge' right to clear feed caches + if ( $purge && $wgUser->isAllowed( 'purge' ) ) { + $cache = ObjectCache::getMainWANInstance(); + $cache->delete( $timekey, 1 ); + $cache->delete( $key, 1 ); } } /** * Check whether feeds can be used and that $type is a valid feed type * - * @param $type String: feed type, as requested by the user - * @return Boolean + * @param string $type Feed type, as requested by the user + * @return bool */ public static function checkFeedOutput( $type ) { - global $wgFeed, $wgFeedClasses; + global $wgOut, $wgFeed, $wgFeedClasses; if ( !$wgFeed ) { - global $wgOut; $wgOut->addWikiMsg( 'feed-unavailable' ); return false; } - if( !isset( $wgFeedClasses[$type] ) ) { - wfHttpError( 500, "Internal Server Error", "Unsupported feed type." ); + if ( !isset( $wgFeedClasses[$type] ) ) { + $wgOut->addWikiMsg( 'feed-invalid' ); return false; } @@ -50,137 +72,186 @@ class FeedUtils { /** * Format a diff for the newsfeed * - * @param $row Object: row from the recentchanges table - * @return String + * @param object $row Row from the recentchanges table, including fields as + * appropriate for CommentStore + * @return string */ public static function formatDiff( $row ) { - global $wgUser; - $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title ); $timestamp = wfTimestamp( TS_MW, $row->rc_timestamp ); $actiontext = ''; - if( $row->rc_type == RC_LOG ) { - if( $row->rc_deleted & LogPage::DELETED_ACTION ) { - $actiontext = wfMsgHtml('rev-deleted-event'); - } else { - $actiontext = LogPage::actionText( $row->rc_log_type, $row->rc_log_action, - $titleObj, $wgUser->getSkin(), LogPage::extractParams($row->rc_params,true,true) ); - } + if ( $row->rc_type == RC_LOG ) { + $rcRow = (array)$row; // newFromRow() only accepts arrays for RC rows + $actiontext = LogFormatter::newFromRow( $rcRow )->getActionText(); } return self::formatDiffRow( $titleObj, $row->rc_last_oldid, $row->rc_this_oldid, $timestamp, - ($row->rc_deleted & Revision::DELETED_COMMENT) ? wfMsgHtml('rev-deleted-comment') : $row->rc_comment, - $actiontext ); + $row->rc_deleted & Revision::DELETED_COMMENT + ? wfMessage( 'rev-deleted-comment' )->escaped() + : CommentStore::newKey( 'rc_comment' ) + // Legacy from RecentChange::selectFields() via ChangesListSpecialPage::doMainQuery() + ->getCommentLegacy( wfGetDB( DB_REPLICA ), $row )->text, + $actiontext + ); } /** * Really format a diff for the newsfeed * - * @param $title Title object - * @param $oldid Integer: old revision's id - * @param $newid Integer: new revision's id - * @param $timestamp Integer: new revision's timestamp - * @param $comment String: new revision's comment - * @param $actiontext String: text of the action; in case of log event - * @return String + * @param Title $title Title object + * @param int $oldid Old revision's id + * @param int $newid New revision's id + * @param int $timestamp New revision's timestamp + * @param string $comment New revision's comment + * @param string $actiontext Text of the action; in case of log event + * @return string */ - public static function formatDiffRow( $title, $oldid, $newid, $timestamp, $comment, $actiontext='' ) { - global $wgFeedDiffCutoff, $wgLang, $wgUser; - wfProfileIn( __METHOD__ ); + public static function formatDiffRow( $title, $oldid, $newid, $timestamp, + $comment, $actiontext = '' + ) { + global $wgFeedDiffCutoff, $wgLang; - $skin = $wgUser->getSkin(); - # log enties + // log entries $completeText = '

' . implode( ' ', array_filter( - array( + [ $actiontext, - $skin->formatComment( $comment ) ) ) ) . "

\n"; + Linker::formatComment( $comment ) ] ) ) . "

\n"; - //NOTE: Check permissions for anonymous users, not current user. - // No "privileged" version should end up in the cache. - // Most feed readers will not log in anway. + // NOTE: Check permissions for anonymous users, not current user. + // No "privileged" version should end up in the cache. + // Most feed readers will not log in anyway. $anon = new User(); $accErrors = $title->getUserPermissionsErrors( 'read', $anon, true ); - if( $title->getNamespace() >= 0 && !$accErrors && $newid ) { - if( $oldid ) { - wfProfileIn( __METHOD__."-dodiff" ); - - #$diffText = $de->getDiff( wfMsg( 'revisionasof', - # $wgLang->timeanddate( $timestamp ), - # $wgLang->date( $timestamp ), - # $wgLang->time( $timestamp ) ), - # wfMsg( 'currentrev' ) ); - - // Don't bother generating the diff if we won't be able to show it - if ( $wgFeedDiffCutoff > 0 ) { - $de = new DifferenceEngine( $title, $oldid, $newid ); + // Can't diff special pages, unreadable pages or pages with no new revision + // to compare against: just return the text. + if ( $title->getNamespace() < 0 || $accErrors || !$newid ) { + return $completeText; + } + + if ( $oldid ) { + $diffText = ''; + // Don't bother generating the diff if we won't be able to show it + if ( $wgFeedDiffCutoff > 0 ) { + $rev = Revision::newFromId( $oldid ); + + if ( !$rev ) { + $diffText = false; + } else { + $context = clone RequestContext::getMain(); + $context->setTitle( $title ); + + $contentHandler = $rev->getContentHandler(); + $de = $contentHandler->createDifferenceEngine( $context, $oldid, $newid ); $diffText = $de->getDiff( - wfMsg( 'previousrevision' ), // hack - wfMsg( 'revisionasof', + wfMessage( 'previousrevision' )->text(), // hack + wfMessage( 'revisionasof', $wgLang->timeanddate( $timestamp ), $wgLang->date( $timestamp ), - $wgLang->time( $timestamp ) ) ); + $wgLang->time( $timestamp ) )->text() ); } + } - if ( $wgFeedDiffCutoff <= 0 || ( strlen( $diffText ) > $wgFeedDiffCutoff ) ) { - // Omit large diffs - $diffLink = $title->escapeFullUrl( - 'diff=' . $newid . - '&oldid=' . $oldid ); - $diffText = '' . - htmlspecialchars( wfMsgForContent( 'showdiff' ) ) . - ''; - } elseif ( $diffText === false ) { - // Error in diff engine, probably a missing revision - $diffText = "

Can't load revision $newid

"; - } else { - // Diff output fine, clean up any illegal UTF-8 - $diffText = UtfNormal::cleanUp( $diffText ); - $diffText = self::applyDiffStyle( $diffText ); - } - wfProfileOut( __METHOD__."-dodiff" ); + if ( $wgFeedDiffCutoff <= 0 || ( strlen( $diffText ) > $wgFeedDiffCutoff ) ) { + // Omit large diffs + $diffText = self::getDiffLink( $title, $newid, $oldid ); + } elseif ( $diffText === false ) { + // Error in diff engine, probably a missing revision + $diffText = "

Can't load revision $newid

"; + } else { + // Diff output fine, clean up any illegal UTF-8 + $diffText = UtfNormal\Validator::cleanUp( $diffText ); + $diffText = self::applyDiffStyle( $diffText ); + } + } else { + $rev = Revision::newFromId( $newid ); + if ( $wgFeedDiffCutoff <= 0 || is_null( $rev ) ) { + $newContent = ContentHandler::getForTitle( $title )->makeEmptyContent(); } else { - $rev = Revision::newFromId( $newid ); - if( is_null( $rev ) ) { - $newtext = ''; + $newContent = $rev->getContent(); + } + + if ( $newContent instanceof TextContent ) { + // only textual content has a "source view". + $text = $newContent->getNativeData(); + + if ( $wgFeedDiffCutoff <= 0 || strlen( $text ) > $wgFeedDiffCutoff ) { + $html = null; } else { - $newtext = $rev->getText(); + $html = nl2br( htmlspecialchars( $text ) ); } - $diffText = '

' . wfMsg( 'newpage' ) . '

' . - '
' . nl2br( htmlspecialchars( $newtext ) ) . '
'; + } else { + // XXX: we could get an HTML representation of the content via getParserOutput, but that may + // contain JS magic and generally may not be suitable for inclusion in a feed. + // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method. + // Compare also ApiFeedContributions::feedItemDesc + $html = null; + } + + if ( $html === null ) { + // Omit large new page diffs, T31110 + // Also use diff link for non-textual content + $diffText = self::getDiffLink( $title, $newid ); + } else { + $diffText = '

' . wfMessage( 'newpage' )->text() . '

' . + '
' . $html . '
'; } - $completeText .= $diffText; } + $completeText .= $diffText; - wfProfileOut( __METHOD__ ); return $completeText; } + /** + * Generates a diff link. Used when the full diff is not wanted for example + * when $wgFeedDiffCutoff is 0. + * + * @param Title $title Title object: used to generate the diff URL + * @param int $newid Newid for this diff + * @param int|null $oldid Oldid for the diff. Null means it is a new article + * @return string + */ + protected static function getDiffLink( Title $title, $newid, $oldid = null ) { + $queryParameters = [ 'diff' => $newid ]; + if ( $oldid != null ) { + $queryParameters['oldid'] = $oldid; + } + $diffUrl = $title->getFullURL( $queryParameters ); + + $diffLink = Html::element( 'a', [ 'href' => $diffUrl ], + wfMessage( 'showdiff' )->inContentLanguage()->text() ); + + return $diffLink; + } + /** * Hacky application of diff styles for the feeds. * Might be 'cleaner' to use DOM or XSLT or something, * but *gack* it's a pain in the ass. * - * @param $text String: diff's HTML output - * @return String: modified HTML - * @private + * @param string $text Diff's HTML output + * @return string Modified HTML */ public static function applyDiffStyle( $text ) { - $styles = array( + $styles = [ 'diff' => 'background-color: white; color:black;', - 'diff-otitle' => 'background-color: white; color:black;', - 'diff-ntitle' => 'background-color: white; color:black;', - 'diff-addedline' => 'background: #cfc; color:black; font-size: smaller;', - 'diff-deletedline' => 'background: #ffa; color:black; font-size: smaller;', - 'diff-context' => 'background: #eee; color:black; font-size: smaller;', - 'diffchange' => 'color: red; font-weight: bold; text-decoration: none;', - ); + 'diff-otitle' => 'background-color: white; color:black; text-align: center;', + 'diff-ntitle' => 'background-color: white; color:black; text-align: center;', + 'diff-addedline' => 'color:black; font-size: 88%; border-style: solid; ' + . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; ' + . 'vertical-align: top; white-space: pre-wrap;', + 'diff-deletedline' => 'color:black; font-size: 88%; border-style: solid; ' + . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; ' + . 'vertical-align: top; white-space: pre-wrap;', + 'diff-context' => 'background-color: #f9f9f9; color: #333333; font-size: 88%; ' + . 'border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; ' + . 'border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;', + 'diffchange' => 'font-weight: bold; text-decoration: none;', + ]; - foreach( $styles as $class => $style ) { + foreach ( $styles as $class => $style ) { $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/", "\\1style=\"$style\"\\3", $text ); }