[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/changes/ -> RCCacheEntryFactory.php (source)

   1  <?php
   2  /**
   3   * Creates a RCCacheEntry from a RecentChange to use in EnhancedChangesList
   4   *
   5   * This program is free software; you can redistribute it and/or modify
   6   * it under the terms of the GNU General Public License as published by
   7   * the Free Software Foundation; either version 2 of the License, or
   8   * (at your option) any later version.
   9   *
  10   * This program is distributed in the hope that it will be useful,
  11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13   * GNU General Public License for more details.
  14   *
  15   * You should have received a copy of the GNU General Public License along
  16   * with this program; if not, write to the Free Software Foundation, Inc.,
  17   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18   * http://www.gnu.org/copyleft/gpl.html
  19   *
  20   * @file
  21   */
  22  
  23  class RCCacheEntryFactory {
  24  
  25      /* @var IContextSource */
  26      private $context;
  27  
  28      /* @var string[] */
  29      private $messages;
  30  
  31      /**
  32       * @param IContextSource $context
  33       * @param string[] $messages
  34       */
  35  	public function __construct( IContextSource $context, $messages ) {
  36          $this->context = $context;
  37          $this->messages = $messages;
  38      }
  39  
  40      /**
  41       * @param RecentChange $baseRC
  42       * @param bool $watched
  43       *
  44       * @return RCCacheEntry
  45       */
  46  	public function newFromRecentChange( RecentChange $baseRC, $watched ) {
  47          $user = $this->context->getUser();
  48          $counter = $baseRC->counter;
  49  
  50          $cacheEntry = RCCacheEntry::newFromParent( $baseRC );
  51  
  52          // Should patrol-related stuff be shown?
  53          $cacheEntry->unpatrolled = ChangesList::isUnpatrolled( $baseRC, $user );
  54  
  55          $cacheEntry->watched = $cacheEntry->mAttribs['rc_type'] == RC_LOG ? false : $watched;
  56          $cacheEntry->numberofWatchingusers = $baseRC->numberofWatchingusers;
  57  
  58          $cacheEntry->link = $this->buildCLink( $cacheEntry );
  59          $cacheEntry->timestamp = $this->buildTimestamp( $cacheEntry );
  60  
  61          // Make "cur" and "diff" links.  Do not use link(), it is too slow if
  62          // called too many times (50% of CPU time on RecentChanges!).
  63          $showDiffLinks = $this->showDiffLinks( $cacheEntry, $user );
  64  
  65          $cacheEntry->difflink = $this->buildDiffLink( $cacheEntry, $showDiffLinks, $counter );
  66          $cacheEntry->curlink = $this->buildCurLink( $cacheEntry, $showDiffLinks, $counter );
  67          $cacheEntry->lastlink = $this->buildLastLink( $cacheEntry, $showDiffLinks );
  68  
  69          // Make user links
  70          $cacheEntry->userlink = $this->getUserLink( $cacheEntry );
  71  
  72          if ( !ChangesList::isDeleted( $cacheEntry, Revision::DELETED_USER ) ) {
  73              $cacheEntry->usertalklink = Linker::userToolLinks(
  74                  $cacheEntry->mAttribs['rc_user'],
  75                  $cacheEntry->mAttribs['rc_user_text']
  76              );
  77          }
  78  
  79          return $cacheEntry;
  80      }
  81  
  82      /**
  83       * @param RecentChange $cacheEntry
  84       * @param User $user
  85       *
  86       * @return bool
  87       */
  88  	private function showDiffLinks( RecentChange $cacheEntry, User $user ) {
  89          return ChangesList::userCan( $cacheEntry, Revision::DELETED_TEXT, $user );
  90      }
  91  
  92      /**
  93       * @param RecentChange $cacheEntry
  94       *
  95       * @return string
  96       */
  97  	private function buildCLink( RecentChange $cacheEntry ) {
  98          $type = $cacheEntry->mAttribs['rc_type'];
  99  
 100          // New unpatrolled pages
 101          if ( $cacheEntry->unpatrolled && $type == RC_NEW ) {
 102              $clink = Linker::linkKnown( $cacheEntry->getTitle() );
 103          // Log entries
 104          } elseif ( $type == RC_LOG ) {
 105              $logType = $cacheEntry->mAttribs['rc_log_type'];
 106  
 107              if ( $logType ) {
 108                  $clink = $this->getLogLink( $logType );
 109              } else {
 110                  wfDebugLog( 'recentchanges', 'Unexpected log entry with no log type in recent changes' );
 111                  $clink = Linker::link( $cacheEntry->getTitle() );
 112              }
 113          // Log entries (old format) and special pages
 114          } elseif ( $cacheEntry->mAttribs['rc_namespace'] == NS_SPECIAL ) {
 115              wfDebugLog( 'recentchanges', 'Unexpected special page in recentchanges' );
 116              $clink = '';
 117          // Edits
 118          } else {
 119              $clink = Linker::linkKnown( $cacheEntry->getTitle() );
 120          }
 121  
 122          return $clink;
 123      }
 124  
 125  	private function getLogLink( $logType ) {
 126          $logtitle = SpecialPage::getTitleFor( 'Log', $logType );
 127          $logpage = new LogPage( $logType );
 128          $logname = $logpage->getName()->escaped();
 129  
 130          $logLink = $this->context->msg( 'parentheses' )
 131              ->rawParams( Linker::linkKnown( $logtitle, $logname ) )->escaped();
 132  
 133          return $logLink;
 134      }
 135  
 136      /**
 137       * @param RecentChange $cacheEntry
 138       *
 139       * @return string
 140       */
 141  	private function buildTimestamp( RecentChange $cacheEntry ) {
 142          return $this->context->getLanguage()->userTime(
 143              $cacheEntry->mAttribs['rc_timestamp'],
 144              $this->context->getUser()
 145          );
 146      }
 147  
 148      /**
 149       * @param RecentChange $recentChange
 150       *
 151       * @return array
 152       */
 153  	private function buildCurQueryParams( RecentChange $recentChange ) {
 154          return array(
 155              'curid' => $recentChange->mAttribs['rc_cur_id'],
 156              'diff' => 0,
 157              'oldid' => $recentChange->mAttribs['rc_this_oldid']
 158          );
 159      }
 160  
 161      /**
 162       * @param RecentChange $cacheEntry
 163       * @param bool $showDiffLinks
 164       * @param int $counter
 165       *
 166       * @return string
 167       */
 168  	private function buildCurLink( RecentChange $cacheEntry, $showDiffLinks, $counter ) {
 169          $queryParams = $this->buildCurQueryParams( $cacheEntry );
 170          $curMessage = $this->getMessage( 'cur' );
 171          $logTypes = array( RC_LOG );
 172  
 173          if ( !$showDiffLinks || in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
 174              $curLink = $curMessage;
 175          } else {
 176              $curUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
 177              $curLink = "<a href=\"$curUrl\" tabindex=\"$counter\">$curMessage</a>";
 178          }
 179  
 180          return $curLink;
 181      }
 182  
 183      /**
 184       * @param RecentChange $recentChange
 185       *
 186       * @return array
 187       */
 188  	private function buildDiffQueryParams( RecentChange $recentChange ) {
 189          return array(
 190              'curid' => $recentChange->mAttribs['rc_cur_id'],
 191              'diff' => $recentChange->mAttribs['rc_this_oldid'],
 192              'oldid' => $recentChange->mAttribs['rc_last_oldid']
 193          );
 194      }
 195  
 196      /**
 197       * @param RecentChange $cacheEntry
 198       * @param bool $showDiffLinks
 199       * @param int $counter
 200       *
 201       * @return string
 202       */
 203  	private function buildDiffLink( RecentChange $cacheEntry, $showDiffLinks, $counter ) {
 204          $queryParams = $this->buildDiffQueryParams( $cacheEntry );
 205          $diffMessage = $this->getMessage( 'diff' );
 206          $logTypes = array( RC_NEW, RC_LOG );
 207  
 208          if ( !$showDiffLinks ) {
 209              $diffLink = $diffMessage;
 210          } elseif ( in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
 211              $diffLink = $diffMessage;
 212          } else {
 213              $diffUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
 214              $diffLink = "<a href=\"$diffUrl\" tabindex=\"$counter\">$diffMessage</a>";
 215          }
 216  
 217          return $diffLink;
 218      }
 219  
 220      /**
 221       * @param RecentChange $cacheEntry
 222       * @param bool $showDiffLinks
 223       *
 224       * @return string
 225       */
 226  	private function buildLastLink( RecentChange $cacheEntry, $showDiffLinks ) {
 227          $lastOldid = $cacheEntry->mAttribs['rc_last_oldid'];
 228          $lastMessage = $this->getMessage( 'last' );
 229          $type = $cacheEntry->mAttribs['rc_type'];
 230          $logTypes = array( RC_LOG );
 231  
 232          // Make "last" link
 233          if ( !$showDiffLinks || !$lastOldid || in_array( $type, $logTypes ) ) {
 234              $lastLink = $lastMessage;
 235          } else {
 236              $lastLink = Linker::linkKnown(
 237                  $cacheEntry->getTitle(),
 238                  $lastMessage,
 239                  array(),
 240                  $this->buildDiffQueryParams( $cacheEntry )
 241              );
 242          }
 243  
 244          return $lastLink;
 245      }
 246  
 247      /**
 248       * @param RecentChange $cacheEntry
 249       *
 250       * @return string
 251       */
 252  	private function getUserLink( RecentChange $cacheEntry ) {
 253          if ( ChangesList::isDeleted( $cacheEntry, Revision::DELETED_USER ) ) {
 254              $userLink = ' <span class="history-deleted">' .
 255                  $this->context->msg( 'rev-deleted-user' )->escaped() . '</span>';
 256          } else {
 257              $userLink = Linker::userLink(
 258                  $cacheEntry->mAttribs['rc_user'],
 259                  $cacheEntry->mAttribs['rc_user_text']
 260              );
 261          }
 262  
 263          return $userLink;
 264      }
 265  
 266      /**
 267       * @param string $key
 268       *
 269       * @return string
 270       */
 271  	private function getMessage( $key ) {
 272          return $this->messages[$key];
 273      }
 274  
 275  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1