00001 <?php
00002
00008 class FeedUtils {
00009
00018 public static function checkPurge( $timekey, $key ) {
00019 global $wgRequest, $wgUser, $messageMemc;
00020 $purge = $wgRequest->getVal( 'action' ) === 'purge';
00021 if ( $purge && $wgUser->isAllowed('purge') ) {
00022 $messageMemc->delete( $timekey );
00023 $messageMemc->delete( $key );
00024 }
00025 }
00026
00033 public static function checkFeedOutput( $type ) {
00034 global $wgFeed, $wgFeedClasses;
00035
00036 if ( !$wgFeed ) {
00037 global $wgOut;
00038 $wgOut->addWikiMsg( 'feed-unavailable' );
00039 return false;
00040 }
00041
00042 if( !isset( $wgFeedClasses[$type] ) ) {
00043 wfHttpError( 500, "Internal Server Error", "Unsupported feed type." );
00044 return false;
00045 }
00046
00047 return true;
00048 }
00049
00056 public static function formatDiff( $row ) {
00057 global $wgUser;
00058
00059 $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
00060 $timestamp = wfTimestamp( TS_MW, $row->rc_timestamp );
00061 $actiontext = '';
00062 if( $row->rc_type == RC_LOG ) {
00063 if( $row->rc_deleted & LogPage::DELETED_ACTION ) {
00064 $actiontext = wfMsgHtml('rev-deleted-event');
00065 } else {
00066 $actiontext = LogPage::actionText( $row->rc_log_type, $row->rc_log_action,
00067 $titleObj, $wgUser->getSkin(), LogPage::extractParams($row->rc_params,true,true) );
00068 }
00069 }
00070 return self::formatDiffRow( $titleObj,
00071 $row->rc_last_oldid, $row->rc_this_oldid,
00072 $timestamp,
00073 ($row->rc_deleted & Revision::DELETED_COMMENT) ? wfMsgHtml('rev-deleted-comment') : $row->rc_comment,
00074 $actiontext );
00075 }
00076
00088 public static function formatDiffRow( $title, $oldid, $newid, $timestamp, $comment, $actiontext='' ) {
00089 global $wgFeedDiffCutoff, $wgContLang, $wgUser;
00090 wfProfileIn( __METHOD__ );
00091
00092 $skin = $wgUser->getSkin();
00093 # log enties
00094 $completeText = '<p>' . implode( ' ',
00095 array_filter(
00096 array(
00097 $actiontext,
00098 $skin->formatComment( $comment ) ) ) ) . "</p>\n";
00099
00100
00101
00102
00103 $anon = new User();
00104 $accErrors = $title->getUserPermissionsErrors( 'read', $anon, true );
00105
00106 if( $title->getNamespace() >= 0 && !$accErrors && $newid ) {
00107 if( $oldid ) {
00108 wfProfileIn( __METHOD__."-dodiff" );
00109
00110 #$diffText = $de->getDiff( wfMsg( 'revisionasof',
00111 # $wgContLang->timeanddate( $timestamp ),
00112 # $wgContLang->date( $timestamp ),
00113 # $wgContLang->time( $timestamp ) ),
00114 # wfMsg( 'currentrev' ) );
00115
00116
00117 if ( $wgFeedDiffCutoff > 0 ) {
00118 $de = new DifferenceEngine( $title, $oldid, $newid );
00119 $diffText = $de->getDiff(
00120 wfMsg( 'previousrevision' ),
00121 wfMsg( 'revisionasof',
00122 $wgContLang->timeanddate( $timestamp ),
00123 $wgContLang->date( $timestamp ),
00124 $wgContLang->time( $timestamp ) ) );
00125 }
00126
00127 if ( ( strlen( $diffText ) > $wgFeedDiffCutoff ) || ( $wgFeedDiffCutoff <= 0 ) ) {
00128
00129 $diffLink = $title->escapeFullUrl(
00130 'diff=' . $newid .
00131 '&oldid=' . $oldid );
00132 $diffText = '<a href="' .
00133 $diffLink .
00134 '">' .
00135 htmlspecialchars( wfMsgForContent( 'showdiff' ) ) .
00136 '</a>';
00137 } elseif ( $diffText === false ) {
00138
00139 $diffText = "<p>Can't load revision $newid</p>";
00140 } else {
00141
00142 $diffText = UtfNormal::cleanUp( $diffText );
00143 $diffText = self::applyDiffStyle( $diffText );
00144 }
00145 wfProfileOut( __METHOD__."-dodiff" );
00146 } else {
00147 $rev = Revision::newFromId( $newid );
00148 if( is_null( $rev ) ) {
00149 $newtext = '';
00150 } else {
00151 $newtext = $rev->getText();
00152 }
00153 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
00154 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
00155 }
00156 $completeText .= $diffText;
00157 }
00158
00159 wfProfileOut( __METHOD__ );
00160 return $completeText;
00161 }
00162
00172 public static function applyDiffStyle( $text ) {
00173 $styles = array(
00174 'diff' => 'background-color: white; color:black;',
00175 'diff-otitle' => 'background-color: white; color:black;',
00176 'diff-ntitle' => 'background-color: white; color:black;',
00177 'diff-addedline' => 'background: #cfc; color:black; font-size: smaller;',
00178 'diff-deletedline' => 'background: #ffa; color:black; font-size: smaller;',
00179 'diff-context' => 'background: #eee; color:black; font-size: smaller;',
00180 'diffchange' => 'color: red; font-weight: bold; text-decoration: none;',
00181 );
00182
00183 foreach( $styles as $class => $style ) {
00184 $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
00185 "\\1style=\"$style\"\\3", $text );
00186 }
00187
00188 return $text;
00189 }
00190
00191 }