MediaWiki  REL1_20
FeedUtils.php
Go to the documentation of this file.
00001 <?php
00029 class FeedUtils {
00030 
00039         public static function checkPurge( $timekey, $key ) {
00040                 global $wgRequest, $wgUser, $messageMemc;
00041                 $purge = $wgRequest->getVal( 'action' ) === 'purge';
00042                 if ( $purge && $wgUser->isAllowed('purge') ) {
00043                         $messageMemc->delete( $timekey );
00044                         $messageMemc->delete( $key );
00045                 }
00046         }
00047 
00054         public static function checkFeedOutput( $type ) {
00055                 global $wgOut, $wgFeed, $wgFeedClasses;
00056 
00057                 if ( !$wgFeed ) {
00058                         $wgOut->addWikiMsg( 'feed-unavailable' );
00059                         return false;
00060                 }
00061 
00062                 if( !isset( $wgFeedClasses[$type] ) ) {
00063                         $wgOut->addWikiMsg( 'feed-invalid' );
00064                         return false;
00065                 }
00066 
00067                 return true;
00068         }
00069 
00076         public static function formatDiff( $row ) {
00077                 $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
00078                 $timestamp = wfTimestamp( TS_MW, $row->rc_timestamp );
00079                 $actiontext = '';
00080                 if( $row->rc_type == RC_LOG ) {
00081                         $rcRow = (array)$row; // newFromRow() only accepts arrays for RC rows
00082                         $actiontext = LogFormatter::newFromRow( $rcRow )->getActionText();
00083                 }
00084                 return self::formatDiffRow( $titleObj,
00085                         $row->rc_last_oldid, $row->rc_this_oldid,
00086                         $timestamp,
00087                         ($row->rc_deleted & Revision::DELETED_COMMENT)
00088                                 ? wfMessage('rev-deleted-comment')->escaped()
00089                                 : $row->rc_comment,
00090                         $actiontext 
00091                 );
00092         }
00093 
00105         public static function formatDiffRow( $title, $oldid, $newid, $timestamp, $comment, $actiontext='' ) {
00106                 global $wgFeedDiffCutoff, $wgLang;
00107                 wfProfileIn( __METHOD__ );
00108 
00109                 # log enties
00110                 $completeText = '<p>' . implode( ' ',
00111                         array_filter(
00112                                 array(
00113                                         $actiontext,
00114                                         Linker::formatComment( $comment ) ) ) ) . "</p>\n";
00115 
00116                 // NOTE: Check permissions for anonymous users, not current user.
00117                 //       No "privileged" version should end up in the cache.
00118                 //       Most feed readers will not log in anway.
00119                 $anon = new User();
00120                 $accErrors = $title->getUserPermissionsErrors( 'read', $anon, true );
00121 
00122                 // Can't diff special pages, unreadable pages or pages with no new revision
00123                 // to compare against: just return the text.
00124                 if( $title->getNamespace() < 0 || $accErrors || !$newid ) {
00125                         wfProfileOut( __METHOD__ );
00126                         return $completeText;
00127                 }
00128 
00129                 if( $oldid ) {
00130                         wfProfileIn( __METHOD__."-dodiff" );
00131 
00132                         #$diffText = $de->getDiff( wfMessage( 'revisionasof',
00133                         #       $wgLang->timeanddate( $timestamp ),
00134                         #       $wgLang->date( $timestamp ),
00135                         #       $wgLang->time( $timestamp ) )->text(),
00136                         #       wfMessage( 'currentrev' )->text() );
00137 
00138                         $diffText = '';
00139                         // Don't bother generating the diff if we won't be able to show it
00140                         if ( $wgFeedDiffCutoff > 0 ) {
00141                                 $de = new DifferenceEngine( $title, $oldid, $newid );
00142                                 $diffText = $de->getDiff(
00143                                         wfMessage( 'previousrevision' )->text(), // hack
00144                                         wfMessage( 'revisionasof',
00145                                         $wgLang->timeanddate( $timestamp ),
00146                                         $wgLang->date( $timestamp ),
00147                                         $wgLang->time( $timestamp ) )->text() );
00148                         }
00149 
00150                         if ( $wgFeedDiffCutoff <= 0 || ( strlen( $diffText ) > $wgFeedDiffCutoff ) ) {
00151                                 // Omit large diffs
00152                                 $diffText = self::getDiffLink( $title, $newid, $oldid );
00153                         } elseif ( $diffText === false ) {
00154                                 // Error in diff engine, probably a missing revision
00155                                 $diffText = "<p>Can't load revision $newid</p>";
00156                         } else {
00157                                 // Diff output fine, clean up any illegal UTF-8
00158                                 $diffText = UtfNormal::cleanUp( $diffText );
00159                                 $diffText = self::applyDiffStyle( $diffText );
00160                         }
00161                         wfProfileOut( __METHOD__."-dodiff" );
00162                 } else {
00163                         $rev = Revision::newFromId( $newid );
00164                         if( $wgFeedDiffCutoff <= 0 || is_null( $rev ) ) {
00165                                 $newtext = '';
00166                         } else {
00167                                 $newtext = $rev->getText();
00168                         }
00169                         if ( $wgFeedDiffCutoff <= 0 || strlen( $newtext ) > $wgFeedDiffCutoff ) {
00170                                 // Omit large new page diffs, bug 29110
00171                                 $diffText = self::getDiffLink( $title, $newid );
00172                         } else {
00173                                 $diffText = '<p><b>' . wfMessage( 'newpage' )->text() . '</b></p>' .
00174                                         '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
00175                         }
00176                 }
00177                 $completeText .= $diffText;
00178 
00179                 wfProfileOut( __METHOD__ );
00180                 return $completeText;
00181         }
00182 
00192         protected static function getDiffLink( Title $title, $newid, $oldid = null ) {
00193                 $queryParameters = ($oldid == null)
00194                         ? "diff={$newid}"
00195                         : "diff={$newid}&oldid={$oldid}" ;
00196                 $diffUrl = $title->getFullUrl( $queryParameters );
00197 
00198                 $diffLink = Html::element( 'a', array( 'href' => $diffUrl ),
00199                         wfMessage( 'showdiff' )->inContentLanguage()->text() );
00200 
00201                 return $diffLink;
00202         }
00203 
00212         public static function applyDiffStyle( $text ) {
00213                 $styles = array(
00214                         'diff'             => 'background-color: white; color:black;',
00215                         'diff-otitle'      => 'background-color: white; color:black;',
00216                         'diff-ntitle'      => 'background-color: white; color:black;',
00217                         'diff-addedline'   => 'background: #cfc; color:black; font-size: smaller;',
00218                         'diff-deletedline' => 'background: #ffa; color:black; font-size: smaller;',
00219                         'diff-context'     => 'background: #eee; color:black; font-size: smaller;',
00220                         'diffchange'       => 'color: red; font-weight: bold; text-decoration: none;',
00221                 );
00222 
00223                 foreach( $styles as $class => $style ) {
00224                         $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
00225                                 "\\1style=\"$style\"\\3", $text );
00226                 }
00227 
00228                 return $text;
00229         }
00230 
00231 }