MediaWiki  REL1_19
FeedUtils.php
Go to the documentation of this file.
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 $wgOut, $wgFeed, $wgFeedClasses;
00035 
00036                 if ( !$wgFeed ) {
00037                         $wgOut->addWikiMsg( 'feed-unavailable' );
00038                         return false;
00039                 }
00040 
00041                 if( !isset( $wgFeedClasses[$type] ) ) {
00042                         $wgOut->addWikiMsg( 'feed-invalid' );
00043                         return false;
00044                 }
00045 
00046                 return true;
00047         }
00048 
00055         public static function formatDiff( $row ) {
00056                 $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
00057                 $timestamp = wfTimestamp( TS_MW, $row->rc_timestamp );
00058                 $actiontext = '';
00059                 if( $row->rc_type == RC_LOG ) {
00060                         $rcRow = (array)$row; // newFromRow() only accepts arrays for RC rows
00061                         $actiontext = LogFormatter::newFromRow( $rcRow )->getActionText();
00062                 }
00063                 return self::formatDiffRow( $titleObj,
00064                         $row->rc_last_oldid, $row->rc_this_oldid,
00065                         $timestamp,
00066                         ($row->rc_deleted & Revision::DELETED_COMMENT)
00067                                 ? wfMsgHtml('rev-deleted-comment') 
00068                                 : $row->rc_comment,
00069                         $actiontext 
00070                 );
00071         }
00072 
00084         public static function formatDiffRow( $title, $oldid, $newid, $timestamp, $comment, $actiontext='' ) {
00085                 global $wgFeedDiffCutoff, $wgLang;
00086                 wfProfileIn( __METHOD__ );
00087 
00088                 # log enties
00089                 $completeText = '<p>' . implode( ' ',
00090                         array_filter(
00091                                 array(
00092                                         $actiontext,
00093                                         Linker::formatComment( $comment ) ) ) ) . "</p>\n";
00094 
00095                 // NOTE: Check permissions for anonymous users, not current user.
00096                 //       No "privileged" version should end up in the cache.
00097                 //       Most feed readers will not log in anway.
00098                 $anon = new User();
00099                 $accErrors = $title->getUserPermissionsErrors( 'read', $anon, true );
00100 
00101                 // Can't diff special pages, unreadable pages or pages with no new revision
00102                 // to compare against: just return the text.
00103                 if( $title->getNamespace() < 0 || $accErrors || !$newid ) {
00104                         wfProfileOut( __METHOD__ );
00105                         return $completeText;
00106                 }
00107 
00108                 if( $oldid ) {
00109                         wfProfileIn( __METHOD__."-dodiff" );
00110 
00111                         #$diffText = $de->getDiff( wfMsg( 'revisionasof',
00112                         #       $wgLang->timeanddate( $timestamp ),
00113                         #       $wgLang->date( $timestamp ),
00114                         #       $wgLang->time( $timestamp ) ),
00115                         #       wfMsg( 'currentrev' ) );
00116 
00117                         // Don't bother generating the diff if we won't be able to show it
00118                         if ( $wgFeedDiffCutoff > 0 ) {
00119                                 $de = new DifferenceEngine( $title, $oldid, $newid );
00120                                 $diffText = $de->getDiff(
00121                                         wfMsg( 'previousrevision' ), // hack
00122                                         wfMsg( 'revisionasof',
00123                                         $wgLang->timeanddate( $timestamp ),
00124                                         $wgLang->date( $timestamp ),
00125                                         $wgLang->time( $timestamp ) ) );
00126                         }
00127 
00128                         if ( $wgFeedDiffCutoff <= 0 || ( strlen( $diffText ) > $wgFeedDiffCutoff ) ) {
00129                                 // Omit large diffs
00130                                 $diffText = self::getDiffLink( $title, $newid, $oldid );
00131                         } elseif ( $diffText === false ) {
00132                                 // Error in diff engine, probably a missing revision
00133                                 $diffText = "<p>Can't load revision $newid</p>";
00134                         } else {
00135                                 // Diff output fine, clean up any illegal UTF-8
00136                                 $diffText = UtfNormal::cleanUp( $diffText );
00137                                 $diffText = self::applyDiffStyle( $diffText );
00138                         }
00139                         wfProfileOut( __METHOD__."-dodiff" );
00140                 } else {
00141                         $rev = Revision::newFromId( $newid );
00142                         if( $wgFeedDiffCutoff <= 0 || is_null( $rev ) ) {
00143                                 $newtext = '';
00144                         } else {
00145                                 $newtext = $rev->getText();
00146                         }
00147                         if ( $wgFeedDiffCutoff <= 0 || strlen( $newtext ) > $wgFeedDiffCutoff ) {
00148                                 // Omit large new page diffs, bug 29110
00149                                 $diffText = self::getDiffLink( $title, $newid );
00150                         } else {
00151                                 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
00152                                         '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
00153                         }
00154                 }
00155                 $completeText .= $diffText;
00156 
00157                 wfProfileOut( __METHOD__ );
00158                 return $completeText;
00159         }
00160 
00169         protected static function getDiffLink( Title $title, $newid, $oldid = null ) {
00170                 $queryParameters = ($oldid == null)
00171                         ? "diff={$newid}"
00172                         : "diff={$newid}&oldid={$oldid}" ;
00173                 $diffUrl = $title->getFullUrl( $queryParameters );
00174 
00175                 $diffLink = Html::element( 'a', array( 'href' => $diffUrl ),
00176                         wfMsgForContent( 'showdiff' ) );
00177 
00178                 return $diffLink;
00179         }
00180 
00189         public static function applyDiffStyle( $text ) {
00190                 $styles = array(
00191                         'diff'             => 'background-color: white; color:black;',
00192                         'diff-otitle'      => 'background-color: white; color:black;',
00193                         'diff-ntitle'      => 'background-color: white; color:black;',
00194                         'diff-addedline'   => 'background: #cfc; color:black; font-size: smaller;',
00195                         'diff-deletedline' => 'background: #ffa; color:black; font-size: smaller;',
00196                         'diff-context'     => 'background: #eee; color:black; font-size: smaller;',
00197                         'diffchange'       => 'color: red; font-weight: bold; text-decoration: none;',
00198                 );
00199 
00200                 foreach( $styles as $class => $style ) {
00201                         $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
00202                                 "\\1style=\"$style\"\\3", $text );
00203                 }
00204 
00205                 return $text;
00206         }
00207 
00208 }