MediaWiki  REL1_24
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,
00106         $comment, $actiontext = ''
00107     ) {
00108         global $wgFeedDiffCutoff, $wgLang;
00109         wfProfileIn( __METHOD__ );
00110 
00111         // log entries
00112         $completeText = '<p>' . implode( ' ',
00113             array_filter(
00114                 array(
00115                     $actiontext,
00116                     Linker::formatComment( $comment ) ) ) ) . "</p>\n";
00117 
00118         // NOTE: Check permissions for anonymous users, not current user.
00119         //       No "privileged" version should end up in the cache.
00120         //       Most feed readers will not log in anyway.
00121         $anon = new User();
00122         $accErrors = $title->getUserPermissionsErrors( 'read', $anon, true );
00123 
00124         // Can't diff special pages, unreadable pages or pages with no new revision
00125         // to compare against: just return the text.
00126         if ( $title->getNamespace() < 0 || $accErrors || !$newid ) {
00127             wfProfileOut( __METHOD__ );
00128             return $completeText;
00129         }
00130 
00131         if ( $oldid ) {
00132             wfProfileIn( __METHOD__ . "-dodiff" );
00133 
00134             #$diffText = $de->getDiff( wfMessage( 'revisionasof',
00135             #   $wgLang->timeanddate( $timestamp ),
00136             #   $wgLang->date( $timestamp ),
00137             #   $wgLang->time( $timestamp ) )->text(),
00138             #   wfMessage( 'currentrev' )->text() );
00139 
00140             $diffText = '';
00141             // Don't bother generating the diff if we won't be able to show it
00142             if ( $wgFeedDiffCutoff > 0 ) {
00143                 $rev = Revision::newFromId( $oldid );
00144 
00145                 if ( !$rev ) {
00146                     $diffText = false;
00147                 } else {
00148                     $context = clone RequestContext::getMain();
00149                     $context->setTitle( $title );
00150 
00151                     $contentHandler = $rev->getContentHandler();
00152                     $de = $contentHandler->createDifferenceEngine( $context, $oldid, $newid );
00153                     $diffText = $de->getDiff(
00154                         wfMessage( 'previousrevision' )->text(), // hack
00155                         wfMessage( 'revisionasof',
00156                             $wgLang->timeanddate( $timestamp ),
00157                             $wgLang->date( $timestamp ),
00158                             $wgLang->time( $timestamp ) )->text() );
00159                 }
00160             }
00161 
00162             if ( $wgFeedDiffCutoff <= 0 || ( strlen( $diffText ) > $wgFeedDiffCutoff ) ) {
00163                 // Omit large diffs
00164                 $diffText = self::getDiffLink( $title, $newid, $oldid );
00165             } elseif ( $diffText === false ) {
00166                 // Error in diff engine, probably a missing revision
00167                 $diffText = "<p>Can't load revision $newid</p>";
00168             } else {
00169                 // Diff output fine, clean up any illegal UTF-8
00170                 $diffText = UtfNormal::cleanUp( $diffText );
00171                 $diffText = self::applyDiffStyle( $diffText );
00172             }
00173             wfProfileOut( __METHOD__ . "-dodiff" );
00174         } else {
00175             $rev = Revision::newFromId( $newid );
00176             if ( $wgFeedDiffCutoff <= 0 || is_null( $rev ) ) {
00177                 $newContent = ContentHandler::getForTitle( $title )->makeEmptyContent();
00178             } else {
00179                 $newContent = $rev->getContent();
00180             }
00181 
00182             if ( $newContent instanceof TextContent ) {
00183                 // only textual content has a "source view".
00184                 $text = $newContent->getNativeData();
00185 
00186                 if ( $wgFeedDiffCutoff <= 0 || strlen( $text ) > $wgFeedDiffCutoff ) {
00187                     $html = null;
00188                 } else {
00189                     $html = nl2br( htmlspecialchars( $text ) );
00190                 }
00191             } else {
00192                 //XXX: we could get an HTML representation of the content via getParserOutput, but that may
00193                 //     contain JS magic and generally may not be suitable for inclusion in a feed.
00194                 //     Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
00195                 //Compare also ApiFeedContributions::feedItemDesc
00196                 $html = null;
00197             }
00198 
00199             if ( $html === null ) {
00200 
00201                 // Omit large new page diffs, bug 29110
00202                 // Also use diff link for non-textual content
00203                 $diffText = self::getDiffLink( $title, $newid );
00204             } else {
00205                 $diffText = '<p><b>' . wfMessage( 'newpage' )->text() . '</b></p>' .
00206                     '<div>' . $html . '</div>';
00207             }
00208         }
00209         $completeText .= $diffText;
00210 
00211         wfProfileOut( __METHOD__ );
00212         return $completeText;
00213     }
00214 
00224     protected static function getDiffLink( Title $title, $newid, $oldid = null ) {
00225         $queryParameters = array( 'diff' => $newid );
00226         if ( $oldid != null ) {
00227             $queryParameters['oldid'] = $oldid;
00228         }
00229         $diffUrl = $title->getFullURL( $queryParameters );
00230 
00231         $diffLink = Html::element( 'a', array( 'href' => $diffUrl ),
00232             wfMessage( 'showdiff' )->inContentLanguage()->text() );
00233 
00234         return $diffLink;
00235     }
00236 
00245     public static function applyDiffStyle( $text ) {
00246         $styles = array(
00247             'diff'             => 'background-color: white; color:black;',
00248             'diff-otitle'      => 'background-color: white; color:black; text-align: center;',
00249             'diff-ntitle'      => 'background-color: white; color:black; text-align: center;',
00250             'diff-addedline'   => 'color:black; font-size: 88%; border-style: solid; '
00251                 . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; '
00252                 . 'vertical-align: top; white-space: pre-wrap;',
00253             'diff-deletedline' => 'color:black; font-size: 88%; border-style: solid; '
00254                 . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; '
00255                 . 'vertical-align: top; white-space: pre-wrap;',
00256             'diff-context'     => 'background-color: #f9f9f9; color: #333333; font-size: 88%; '
00257                 . 'border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; '
00258                 . 'border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;',
00259             'diffchange'       => 'font-weight: bold; text-decoration: none;',
00260         );
00261 
00262         foreach ( $styles as $class => $style ) {
00263             $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
00264                 "\\1style=\"$style\"\\3", $text );
00265         }
00266 
00267         return $text;
00268     }
00269 
00270 }