00001 <?php
00002
00003
00004 class FeedUtils {
00005
00006 public static function checkPurge( $timekey, $key ) {
00007 global $wgRequest, $wgUser, $messageMemc;
00008 $purge = $wgRequest->getVal( 'action' ) === 'purge';
00009 if ( $purge && $wgUser->isAllowed('purge') ) {
00010 $messageMemc->delete( $timekey );
00011 $messageMemc->delete( $key );
00012 }
00013 }
00014
00015 public static function checkFeedOutput( $type ) {
00016 global $wgFeed, $wgOut, $wgFeedClasses;
00017
00018 if ( !$wgFeed ) {
00019 global $wgOut;
00020 $wgOut->addWikiMsg( 'feed-unavailable' );
00021 return false;
00022 }
00023
00024 if( !isset( $wgFeedClasses[$type] ) ) {
00025 wfHttpError( 500, "Internal Server Error", "Unsupported feed type." );
00026 return false;
00027 }
00028
00029 return true;
00030 }
00031
00035 public static function formatDiff( $row ) {
00036 global $wgUser;
00037
00038 $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
00039 $timestamp = wfTimestamp( TS_MW, $row->rc_timestamp );
00040 $actiontext = '';
00041 if( $row->rc_type == RC_LOG ) {
00042 if( $row->rc_deleted & LogPage::DELETED_ACTION ) {
00043 $actiontext = wfMsgHtml('rev-deleted-event');
00044 } else {
00045 $actiontext = LogPage::actionText( $row->rc_log_type, $row->rc_log_action,
00046 $titleObj, $wgUser->getSkin(), LogPage::extractParams($row->rc_params,true,true) );
00047 }
00048 }
00049 return self::formatDiffRow( $titleObj,
00050 $row->rc_last_oldid, $row->rc_this_oldid,
00051 $timestamp,
00052 ($row->rc_deleted & Revision::DELETED_COMMENT) ? wfMsgHtml('rev-deleted-comment') : $row->rc_comment,
00053 $actiontext );
00054 }
00055
00056 public static function formatDiffRow( $title, $oldid, $newid, $timestamp, $comment, $actiontext='' ) {
00057 global $wgFeedDiffCutoff, $wgContLang, $wgUser;
00058 wfProfileIn( __FUNCTION__ );
00059
00060 $skin = $wgUser->getSkin();
00061 # log enties
00062 $completeText = '<p>' . implode( ' ',
00063 array_filter(
00064 array(
00065 $actiontext,
00066 $skin->formatComment( $comment ) ) ) ) . "</p>\n";
00067
00068
00069
00070
00071 $anon = new User();
00072 $accErrors = $title->getUserPermissionsErrors( 'read', $anon, true );
00073
00074 if( $title->getNamespace() >= 0 && !$accErrors ) {
00075 if( $oldid ) {
00076 wfProfileIn( __FUNCTION__."-dodiff" );
00077
00078 #$diffText = $de->getDiff( wfMsg( 'revisionasof',
00079 # $wgContLang->timeanddate( $timestamp ) ),
00080 # wfMsg( 'currentrev' ) );
00081
00082
00083 if ( $wgFeedDiffCutoff > 0 ) {
00084 $de = new DifferenceEngine( $title, $oldid, $newid );
00085 $diffText = $de->getDiff(
00086 wfMsg( 'previousrevision' ),
00087 wfMsg( 'revisionasof',
00088 $wgContLang->timeanddate( $timestamp ) ) );
00089 }
00090
00091 if ( ( strlen( $diffText ) > $wgFeedDiffCutoff ) || ( $wgFeedDiffCutoff <= 0 ) ) {
00092
00093 $diffLink = $title->escapeFullUrl(
00094 'diff=' . $newid .
00095 '&oldid=' . $oldid );
00096 $diffText = '<a href="' .
00097 $diffLink .
00098 '">' .
00099 htmlspecialchars( wfMsgForContent( 'showdiff' ) ) .
00100 '</a>';
00101 } elseif ( $diffText === false ) {
00102
00103 $diffText = "<p>Can't load revision $newid</p>";
00104 } else {
00105
00106 $diffText = UtfNormal::cleanUp( $diffText );
00107 $diffText = self::applyDiffStyle( $diffText );
00108 }
00109 wfProfileOut( __FUNCTION__."-dodiff" );
00110 } else {
00111 $rev = Revision::newFromId( $newid );
00112 if( is_null( $rev ) ) {
00113 $newtext = '';
00114 } else {
00115 $newtext = $rev->getText();
00116 }
00117 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
00118 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
00119 }
00120 $completeText .= $diffText;
00121 }
00122
00123 wfProfileOut( __FUNCTION__ );
00124 return $completeText;
00125 }
00126
00136 public static function applyDiffStyle( $text ) {
00137 $styles = array(
00138 'diff' => 'background-color: white; color:black;',
00139 'diff-otitle' => 'background-color: white; color:black;',
00140 'diff-ntitle' => 'background-color: white; color:black;',
00141 'diff-addedline' => 'background: #cfc; color:black; font-size: smaller;',
00142 'diff-deletedline' => 'background: #ffa; color:black; font-size: smaller;',
00143 'diff-context' => 'background: #eee; color:black; font-size: smaller;',
00144 'diffchange' => 'color: red; font-weight: bold; text-decoration: none;',
00145 );
00146
00147 foreach( $styles as $class => $style ) {
00148 $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
00149 "\\1style=\"$style\"\\3", $text );
00150 }
00151
00152 return $text;
00153 }
00154
00155 }