Merge "Rename getCopyWarn() to getCopyrightWarning() and make it public and static"
[lhc/web/wiklou.git] / includes / ChangesList.php
index 3966e30..e5be12d 100644 (file)
@@ -1,10 +1,27 @@
 <?php
 /**
- * Classes to show various lists of changes:
+ * Classes to show lists of changes.
+ *
+ * These can be:
  * - watchlist
  * - related changes
  * - recent changes
  *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
  * @file
  */
 
@@ -106,7 +123,7 @@ class ChangesList extends ContextSource {
                if( !isset( $this->message ) ) {
                        foreach ( explode( ' ', 'cur diff hist last blocklink history ' .
                        'semicolon-separator pipe-separator' ) as $msg ) {
-                               $this->message[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
+                               $this->message[$msg] = $this->msg( $msg )->escaped();
                        }
                }
        }
@@ -182,22 +199,29 @@ class ChangesList extends ContextSource {
         * Show formatted char difference
         * @param $old Integer: bytes
         * @param $new Integer: bytes
+        * @param $context IContextSource context to use
         * @return String
         */
-       public static function showCharacterDifference( $old, $new ) {
-               global $wgRCChangedSizeThreshold, $wgLang, $wgMiserMode;
+       public static function showCharacterDifference( $old, $new, IContextSource $context = null ) {
+               global $wgRCChangedSizeThreshold, $wgMiserMode;
+
+               if ( !$context ) {
+                       $context = RequestContext::getMain();
+               }
+
                $szdiff = $new - $old;
 
-               $code = $wgLang->getCode();
+               $lang = $context->getLanguage();
+               $code = $lang->getCode();
                static $fastCharDiff = array();
                if ( !isset($fastCharDiff[$code]) ) {
-                       $fastCharDiff[$code] = $wgMiserMode || wfMsgNoTrans( 'rc-change-size' ) === '$1';
+                       $fastCharDiff[$code] = $wgMiserMode || $context->msg( 'rc-change-size' )->plain() === '$1';
                }
 
-               $formattedSize = $wgLang->formatNum($szdiff);
+               $formattedSize = $lang->formatNum( $szdiff );
 
                if ( !$fastCharDiff[$code] ) {
-                       $formattedSize = wfMsgExt( 'rc-change-size', array( 'parsemag' ), $formattedSize );
+                       $formattedSize = $context->msg( 'rc-change-size', $formattedSize )->text();
                }
 
                if( abs( $szdiff ) > abs( $wgRCChangedSizeThreshold ) ) {
@@ -217,11 +241,34 @@ class ChangesList extends ContextSource {
                        $formattedSizeClass = 'mw-plusminus-neg';
                }
 
-               $formattedTotalSize = wfMsgExt( 'rc-change-size-new', 'parsemag', $wgLang->formatNum( $new ) );
+               $formattedTotalSize = $context->msg( 'rc-change-size-new' )->numParams( $new )->text();
 
                return Html::element( $tag,
                        array( 'dir' => 'ltr', 'class' => $formattedSizeClass, 'title' => $formattedTotalSize ),
-                       wfMessage( 'parentheses', $formattedSize )->plain() ) . $wgLang->getDirMark();
+                       $context->msg( 'parentheses', $formattedSize )->plain() ) . $lang->getDirMark();
+       }
+
+       /**
+        * Format the character difference of one or several changes.
+        *
+        * @param $old RecentChange
+        * @param $new RecentChange last change to use, if not provided, $old will be used
+        * @return string HTML fragment
+        */
+       public function formatCharacterDifference( RecentChange $old, RecentChange $new = null ) {
+               $oldlen = $old->mAttribs['rc_old_len'];
+
+               if ( $new ) {
+                       $newlen = $new->mAttribs['rc_new_len'];
+               } else {
+                       $newlen = $old->mAttribs['rc_new_len'];
+               }
+
+               if( $oldlen === null || $newlen === null ) {
+                       return '';
+               }
+
+               return self::showCharacterDifference( $oldlen, $newlen, $this->getContext() );
        }
 
        /**
@@ -238,7 +285,7 @@ class ChangesList extends ContextSource {
 
        public function insertDateHeader( &$s, $rc_timestamp ) {
                # Make date header if necessary
-               $date = $this->getLanguage()->date( $rc_timestamp, true, true );
+               $date = $this->getLanguage()->userDate( $rc_timestamp, $this->getUser() );
                if( $date != $this->lastdate ) {
                        if( $this->lastdate != '' ) {
                                $s .= "</ul>\n";
@@ -341,7 +388,7 @@ class ChangesList extends ContextSource {
         */
        public function insertTimestamp( &$s, $rc ) {
                $s .= $this->message['semicolon-separator'] .
-                       $this->getLanguage()->time( $rc->mAttribs['rc_timestamp'], true, true ) . ' . . ';
+                       $this->getLanguage()->userTime( $rc->mAttribs['rc_timestamp'], $this->getUser() ) . ' . . ';
        }
 
        /**
@@ -352,7 +399,7 @@ class ChangesList extends ContextSource {
         */
        public function insertUserRelatedLinks( &$s, &$rc ) {
                if( $this->isDeleted( $rc, Revision::DELETED_USER ) ) {
-                       $s .= ' <span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
+                       $s .= ' <span class="history-deleted">' . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
                } else {
                        $s .= $this->getLanguage()->getDirMark() . Linker::userLink( $rc->mAttribs['rc_user'],
                                $rc->mAttribs['rc_user_text'] );
@@ -368,12 +415,13 @@ class ChangesList extends ContextSource {
         */
        public function insertLogEntry( $rc ) {
                $formatter = LogFormatter::newFromRow( $rc->mAttribs );
+               $formatter->setContext( $this->getContext() );
                $formatter->setShowUserToolLinks( true );
                $mark = $this->getLanguage()->getDirMark();
                return $formatter->getActionText() . " $mark" . $formatter->getComment();
        }
 
-       /** 
+       /**
         * Insert a formatted comment
         * @param $rc RecentChange
         * @return string
@@ -381,7 +429,7 @@ class ChangesList extends ContextSource {
        public function insertComment( $rc ) {
                if( $rc->mAttribs['rc_type'] != RC_MOVE && $rc->mAttribs['rc_type'] != RC_MOVE_OVER_REDIRECT ) {
                        if( $this->isDeleted( $rc, Revision::DELETED_COMMENT ) ) {
-                               return ' <span class="history-deleted">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span>';
+                               return ' <span class="history-deleted">' . $this->msg( 'rev-deleted-comment' )->escaped() . '</span>';
                        } else {
                                return Linker::commentBlock( $rc->mAttribs['rc_comment'], $rc->getTitle() );
                        }
@@ -405,8 +453,7 @@ class ChangesList extends ContextSource {
                static $cache = array();
                if( $count > 0 ) {
                        if( !isset( $cache[$count] ) ) {
-                               $cache[$count] = wfMsgExt( 'number_of_watching_users_RCview',
-                                       array('parsemag', 'escape' ), $this->getLanguage()->formatNum( $count ) );
+                               $cache[$count] = $this->msg( 'number_of_watching_users_RCview' )->numParams( $count )->escaped();
                        }
                        return $cache[$count];
                } else {
@@ -571,9 +618,9 @@ class OldChangesList extends ChangesList {
                # Edit/log timestamp
                $this->insertTimestamp( $s, $rc );
                # Bytes added or removed
-               if( $wgRCShowChangedSize ) {
-                       $cd = $rc->getCharacterDifference();
-                       if( $cd != '' ) {
+               if ( $wgRCShowChangedSize ) {
+                       $cd = $this->formatCharacterDifference( $rc );
+                       if ( $cd !== '' ) {
                                $s .= "$cd  . . ";
                        }
                }
@@ -597,8 +644,7 @@ class OldChangesList extends ChangesList {
 
                # How many users watch this page
                if( $rc->numberofWatchingusers > 0 ) {
-                       $s .= ' ' . wfMsgExt( 'number_of_watching_users_RCview',
-                               array( 'parsemag', 'escape' ), $this->getLanguage()->formatNum( $rc->numberofWatchingusers ) );
+                       $s .= ' ' . $this->numberofWatchingusers( $rc->numberofWatchingusers );
                }
 
                if( $this->watchlist ) {
@@ -650,7 +696,7 @@ class EnhancedChangesList extends ChangesList {
                $curIdEq = array( 'curid' => $rc->mAttribs['rc_cur_id'] );
 
                # If it's a new day, add the headline and flush the cache
-               $date = $this->getLanguage()->date( $rc->mAttribs['rc_timestamp'], true );
+               $date = $this->getLanguage()->userDate( $rc->mAttribs['rc_timestamp'], $this->getUser() );
                $ret = '';
                if( $date != $this->lastdate ) {
                        # Process current cache
@@ -698,7 +744,7 @@ class EnhancedChangesList extends ChangesList {
                        $showdifflinks = false;
                }
 
-               $time = $this->getLanguage()->time( $rc->mAttribs['rc_timestamp'], true, true );
+               $time = $this->getLanguage()->userTime( $rc->mAttribs['rc_timestamp'], $this->getUser() );
                $rc->watched = $watched;
                $rc->link = $clink;
                $rc->timestamp = $time;
@@ -747,7 +793,7 @@ class EnhancedChangesList extends ChangesList {
 
                # Make user links
                if( $this->isDeleted( $rc, Revision::DELETED_USER ) ) {
-                       $rc->userlink = ' <span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
+                       $rc->userlink = ' <span class="history-deleted">' . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
                } else {
                        $rc->userlink = Linker::userLink( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
                        $rc->usertalklink = Linker::userToolLinks( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
@@ -857,8 +903,10 @@ class EnhancedChangesList extends ChangesList {
                        array_push( $users, $text );
                }
 
-               $users = ' <span class="changedby">[' .
-                       implode( $this->message['semicolon-separator'], $users ) . ']</span>';
+               $users = ' <span class="changedby">'
+                       . $this->msg( 'brackets' )->rawParams(
+                               implode( $this->message['semicolon-separator'], $users )
+                       )->escaped() . '</span>';
 
                $tl = '<span class="mw-collapsible-toggle mw-enhancedchanges-arrow"></span>';
                $r .= "<td>$tl</td>";
@@ -876,7 +924,7 @@ class EnhancedChangesList extends ChangesList {
 
                # Article link
                if( $namehidden ) {
-                       $r .= ' <span class="history-deleted">' . wfMsgHtml( 'rev-deleted-event' ) . '</span>';
+                       $r .= ' <span class="history-deleted">' . $this->msg( 'rev-deleted-event' )->escaped() . '</span>';
                } elseif( $allLogs ) {
                        $r .= $this->maybeWatchedLink( $block[0]->link, $block[0]->watched );
                } else {
@@ -890,11 +938,11 @@ class EnhancedChangesList extends ChangesList {
                $n = count($block);
                static $nchanges = array();
                if ( !isset( $nchanges[$n] ) ) {
-                       $nchanges[$n] = wfMsgExt( 'nchanges', array( 'parsemag', 'escape' ), $this->getLanguage()->formatNum( $n ) );
+                       $nchanges[$n] = $this->msg( 'nchanges' )->numParams( $n )->escaped();
                }
                # Total change link
                $r .= ' ';
-                $logtext = '';
+               $logtext = '';
                if( !$allLogs ) {
                        if( !ChangesList::userCan( $rcObj, Revision::DELETED_TEXT, $this->getUser() ) ) {
                                $logtext .= $nchanges[$n];
@@ -933,7 +981,7 @@ class EnhancedChangesList extends ChangesList {
                                );
                }
 
-               if( $logtext != '' ) {
+               if( $logtext !== '' ) {
                        $r .= $this->msg( 'parentheses' )->rawParams( $logtext )->escaped();
                }
 
@@ -951,8 +999,7 @@ class EnhancedChangesList extends ChangesList {
                                $first--;
                        }
                        # Get net change
-                       $chardiff = $rcObj->getCharacterDifference( $block[$first]->mAttribs['rc_old_len'],
-                               $block[$last]->mAttribs['rc_new_len'] );
+                       $chardiff = $this->formatCharacterDifference( $block[$first], $block[$last] );
 
                        if( $chardiff == '' ) {
                                $r .= ' ';
@@ -1008,13 +1055,16 @@ class EnhancedChangesList extends ChangesList {
                        $r .= $link . '</span>';
 
                        if ( !$type == RC_LOG || $type == RC_NEW ) {
-                               $r .= $this->msg( 'parentheses' )->rawParams( $rcObj->curlink . $this->message['pipe-separator'] . $rcObj->lastlink )->escaped();
+                               $r .= ' ' . $this->msg( 'parentheses' )->rawParams( $rcObj->curlink . $this->message['pipe-separator'] . $rcObj->lastlink )->escaped();
                        }
                        $r .= ' . . ';
 
                        # Character diff
-                       if( $wgRCShowChangedSize && $rcObj->getCharacterDifference() ) {
-                               $r .= $rcObj->getCharacterDifference() . ' . . ' ;
+                       if ( $wgRCShowChangedSize ) {
+                               $cd = $this->formatCharacterDifference( $rcObj );
+                               if ( $cd !== '' ) {
+                                       $r .= "$cd . . ";
+                               }
                        }
 
                        if ( $rcObj->mAttribs['rc_type'] == RC_LOG ) {
@@ -1063,9 +1113,8 @@ class EnhancedChangesList extends ChangesList {
         * @return String: HTML <img> tag
         */
        protected function sideArrow() {
-               global $wgLang;
-               $dir = $wgLang->isRTL() ? 'l' : 'r';
-               return $this->arrow( $dir, '+', wfMsg( 'rc-enhanced-expand' ) );
+               $dir = $this->getLanguage()->isRTL() ? 'l' : 'r';
+               return $this->arrow( $dir, '+', $this->msg( 'rc-enhanced-expand' )->text() );
        }
 
        /**
@@ -1074,7 +1123,7 @@ class EnhancedChangesList extends ChangesList {
         * @return String: HTML <img> tag
         */
        protected function downArrow() {
-               return $this->arrow( 'd', '-', wfMsg( 'rc-enhanced-hide' ) );
+               return $this->arrow( 'd', '-', $this->msg( 'rc-enhanced-hide' )->text() );
        }
 
        /**
@@ -1134,7 +1183,7 @@ class EnhancedChangesList extends ChangesList {
                # Diff and hist links
                if ( $type != RC_LOG ) {
                        $query['action'] = 'history';
-                       $r .= $this->msg( 'parentheses' )->rawParams( $rcObj->difflink . $this->message['pipe-separator'] . Linker::linkKnown(
+                       $r .= ' ' . $this->msg( 'parentheses' )->rawParams( $rcObj->difflink . $this->message['pipe-separator'] . Linker::linkKnown(
                                $rcObj->getTitle(),
                                $this->message['hist'],
                                array(),
@@ -1143,16 +1192,19 @@ class EnhancedChangesList extends ChangesList {
                }
                $r .= ' . . ';
                # Character diff
-               if( $wgRCShowChangedSize && ($cd = $rcObj->getCharacterDifference()) ) {
-                       $r .= "$cd . . ";
+               if ( $wgRCShowChangedSize ) {
+                       $cd = $this->formatCharacterDifference( $rcObj );
+                       if ( $cd !== '' ) {
+                               $r .= "$cd . . ";
+                       }
                }
 
                if ( $type == RC_LOG ) {
                        $r .= $this->insertLogEntry( $rcObj );
-               } else { 
+               } else {
                        $r .= ' '.$rcObj->userlink . $rcObj->usertalklink;
                        $r .= $this->insertComment( $rcObj );
-                       $r .= $this->insertRollback( $r, $rcObj );
+                       $this->insertRollback( $r, $rcObj );
                }
 
                # Tags