MediaWiki  REL1_21
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 entries
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 anyway.
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                                 $rev = Revision::newFromId( $oldid );
00142 
00143                                 if ( !$rev ) {
00144                                         $diffText = false;
00145                                 } else {
00146                                         $context = clone RequestContext::getMain();
00147                                         $context->setTitle( $title );
00148 
00149                                         $contentHandler = $rev->getContentHandler();
00150                                         $de = $contentHandler->createDifferenceEngine( $context, $oldid, $newid );
00151                                         $diffText = $de->getDiff(
00152                                                 wfMessage( 'previousrevision' )->text(), // hack
00153                                                 wfMessage( 'revisionasof',
00154                                                         $wgLang->timeanddate( $timestamp ),
00155                                                         $wgLang->date( $timestamp ),
00156                                                         $wgLang->time( $timestamp ) )->text() );
00157                                 }
00158                         }
00159 
00160                         if ( $wgFeedDiffCutoff <= 0 || ( strlen( $diffText ) > $wgFeedDiffCutoff ) ) {
00161                                 // Omit large diffs
00162                                 $diffText = self::getDiffLink( $title, $newid, $oldid );
00163                         } elseif ( $diffText === false ) {
00164                                 // Error in diff engine, probably a missing revision
00165                                 $diffText = "<p>Can't load revision $newid</p>";
00166                         } else {
00167                                 // Diff output fine, clean up any illegal UTF-8
00168                                 $diffText = UtfNormal::cleanUp( $diffText );
00169                                 $diffText = self::applyDiffStyle( $diffText );
00170                         }
00171                         wfProfileOut( __METHOD__."-dodiff" );
00172                 } else {
00173                         $rev = Revision::newFromId( $newid );
00174                         if( $wgFeedDiffCutoff <= 0 || is_null( $rev ) ) {
00175                                 $newContent = ContentHandler::getForTitle( $title )->makeEmptyContent();
00176                         } else {
00177                                 $newContent = $rev->getContent();
00178                         }
00179 
00180                         if ( $newContent instanceof TextContent ) {
00181                                 // only textual content has a "source view".
00182                                 $text = $newContent->getNativeData();
00183 
00184                                 if ( $wgFeedDiffCutoff <= 0 || strlen( $text ) > $wgFeedDiffCutoff ) {
00185                                         $html = null;
00186                                 } else {
00187                                         $html = nl2br( htmlspecialchars( $text ) );
00188                                 }
00189                         } else {
00190                                 //XXX: we could get an HTML representation of the content via getParserOutput, but that may
00191                                 //     contain JS magic and generally may not be suitable for inclusion in a feed.
00192                                 //     Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
00193                                 //Compare also ApiFeedContributions::feedItemDesc
00194                                 $html = null;
00195                         }
00196 
00197                         if ( $html === null ) {
00198 
00199                                 // Omit large new page diffs, bug 29110
00200                                 // Also use diff link for non-textual content
00201                                 $diffText = self::getDiffLink( $title, $newid );
00202                         } else {
00203                                 $diffText = '<p><b>' . wfMessage( 'newpage' )->text() . '</b></p>' .
00204                                         '<div>' . $html . '</div>';
00205                         }
00206                 }
00207                 $completeText .= $diffText;
00208 
00209                 wfProfileOut( __METHOD__ );
00210                 return $completeText;
00211         }
00212 
00222         protected static function getDiffLink( Title $title, $newid, $oldid = null ) {
00223                 $queryParameters = ($oldid == null)
00224                         ? "diff={$newid}"
00225                         : "diff={$newid}&oldid={$oldid}";
00226                 $diffUrl = $title->getFullUrl( $queryParameters );
00227 
00228                 $diffLink = Html::element( 'a', array( 'href' => $diffUrl ),
00229                         wfMessage( 'showdiff' )->inContentLanguage()->text() );
00230 
00231                 return $diffLink;
00232         }
00233 
00242         public static function applyDiffStyle( $text ) {
00243                 $styles = array(
00244                         'diff'             => 'background-color: white; color:black;',
00245                         'diff-otitle'      => 'background-color: white; color:black; text-align: center;',
00246                         'diff-ntitle'      => 'background-color: white; color:black; text-align: center;',
00247                         '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;',
00248                         '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;',
00249                         '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;',
00250                         'diffchange'       => 'font-weight: bold; text-decoration: none;',
00251                 );
00252 
00253                 foreach( $styles as $class => $style ) {
00254                         $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
00255                                 "\\1style=\"$style\"\\3", $text );
00256                 }
00257 
00258                 return $text;
00259         }
00260 
00261 }