MediaWiki  1.33.0
ReverseChronologicalPager.php
Go to the documentation of this file.
1 <?php
21 use Wikimedia\Timestamp\TimestampException;
22 
28 abstract class ReverseChronologicalPager extends IndexPager {
32  public $mYear;
34  public $mMonth;
36  public $mDay;
37 
38  public function getNavigationBar() {
39  if ( !$this->isNavigationBarShown() ) {
40  return '';
41  }
42 
43  if ( isset( $this->mNavigationBar ) ) {
44  return $this->mNavigationBar;
45  }
46 
47  $linkTexts = [
48  'prev' => $this->msg( 'pager-newer-n' )->numParams( $this->mLimit )->escaped(),
49  'next' => $this->msg( 'pager-older-n' )->numParams( $this->mLimit )->escaped(),
50  'first' => $this->msg( 'histlast' )->escaped(),
51  'last' => $this->msg( 'histfirst' )->escaped()
52  ];
53 
54  $pagingLinks = $this->getPagingLinks( $linkTexts );
55  $limitLinks = $this->getLimitLinks();
56  $limits = $this->getLanguage()->pipeList( $limitLinks );
57  $firstLastLinks = $this->msg( 'parentheses' )->rawParams( "{$pagingLinks['first']}" .
58  $this->msg( 'pipe-separator' )->escaped() .
59  "{$pagingLinks['last']}" )->escaped();
60 
61  $this->mNavigationBar = $firstLastLinks . ' ' .
62  $this->msg( 'viewprevnext' )->rawParams(
63  $pagingLinks['prev'], $pagingLinks['next'], $limits )->escaped();
64 
65  return $this->mNavigationBar;
66  }
67 
77  public function getDateCond( $year, $month, $day = -1 ) {
78  $year = (int)$year;
79  $month = (int)$month;
80  $day = (int)$day;
81 
82  // Basic validity checks for year and month
83  // If year and month are invalid, don't update the mOffset
84  if ( $year <= 0 && ( $month <= 0 || $month >= 13 ) ) {
85  return null;
86  }
87 
88  $timestamp = self::getOffsetDate( $year, $month, $day );
89 
90  try {
91  // The timestamp used for DB queries is at midnight of the *next* day after the selected date.
92  $selectedDate = new DateTime( $timestamp->getTimestamp( TS_ISO_8601 ) );
93  $selectedDate = $selectedDate->modify( '-1 day' );
94 
95  $this->mYear = (int)$selectedDate->format( 'Y' );
96  $this->mMonth = (int)$selectedDate->format( 'm' );
97  $this->mDay = (int)$selectedDate->format( 'd' );
98  $this->mOffset = $this->mDb->timestamp( $timestamp->getTimestamp() );
99  } catch ( TimestampException $e ) {
100  // Invalid user provided timestamp (T149257)
101  return null;
102  }
103 
104  return $this->mOffset;
105  }
106 
122  public static function getOffsetDate( $year, $month, $day = -1 ) {
123  // Given an optional year, month, and day, we need to generate a timestamp
124  // to use as "WHERE rev_timestamp <= result"
125  // Examples: year = 2006 equals < 20070101 (+000000)
126  // year=2005, month=1 equals < 20050201
127  // year=2005, month=12 equals < 20060101
128  // year=2005, month=12, day=5 equals < 20051206
129  if ( $year <= 0 ) {
130  // If no year given, assume the current one
131  $timestamp = MWTimestamp::getInstance();
132  $year = $timestamp->format( 'Y' );
133  // If this month hasn't happened yet this year, go back to last year's month
134  if ( $month > $timestamp->format( 'n' ) ) {
135  $year--;
136  }
137  }
138 
139  if ( $month && $month > 0 && $month < 13 ) {
140  // Day validity check after we have month and year checked
141  $day = checkdate( $month, $day, $year ) ? $day : false;
142 
143  if ( $day && $day > 0 ) {
144  // If we have a day, we want up to the day immediately afterward
145  $day++;
146 
147  // Did we overflow the current month?
148  if ( !checkdate( $month, $day, $year ) ) {
149  $day = 1;
150  $month++;
151  }
152  } else {
153  // If no day, assume beginning of next month
154  $day = 1;
155  $month++;
156  }
157 
158  // Did we overflow the current year?
159  if ( $month > 12 ) {
160  $month = 1;
161  $year++;
162  }
163 
164  } else {
165  // No month implies we want up to the end of the year in question
166  $month = 1;
167  $day = 1;
168  $year++;
169  }
170 
171  // Y2K38 bug
172  if ( $year > 2032 ) {
173  $year = 2032;
174  }
175 
176  $ymd = (int)sprintf( "%04d%02d%02d", $year, $month, $day );
177 
178  if ( $ymd > 20320101 ) {
179  $ymd = 20320101;
180  }
181 
182  return MWTimestamp::getInstance( "${ymd}000000" );
183  }
184 }
ReverseChronologicalPager\getOffsetDate
static getOffsetDate( $year, $month, $day=-1)
Core logic of determining the mOffset timestamp such that we can get all items with a timestamp up to...
Definition: ReverseChronologicalPager.php:122
ReverseChronologicalPager\getDateCond
getDateCond( $year, $month, $day=-1)
Set and return the mOffset timestamp such that we can get all revisions with a timestamp up to the sp...
Definition: ReverseChronologicalPager.php:77
ReverseChronologicalPager\$mMonth
int $mMonth
Definition: ReverseChronologicalPager.php:34
ContextSource\msg
msg( $key)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:168
IndexPager\$mNavigationBar
string $mNavigationBar
Definition: IndexPager.php:143
IndexPager\getLimitLinks
getLimitLinks()
Definition: IndexPager.php:690
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
ContextSource\getLanguage
getLanguage()
Definition: ContextSource.php:128
ReverseChronologicalPager\$mDefaultDirection
bool $mDefaultDirection
Definition: ReverseChronologicalPager.php:30
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
MWTimestamp\getInstance
static getInstance( $ts=false)
Get a timestamp instance in GMT.
Definition: MWTimestamp.php:39
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2162
ReverseChronologicalPager\$mYear
int $mYear
Definition: ReverseChronologicalPager.php:32
IndexPager\DIR_DESCENDING
const DIR_DESCENDING
Backwards-compatible constant for $mDefaultDirection field (do not change)
Definition: IndexPager.php:73
IndexPager\isNavigationBarShown
isNavigationBarShown()
Returns whether to show the "navigation bar".
Definition: IndexPager.php:651
ReverseChronologicalPager\$mDay
int $mDay
Definition: ReverseChronologicalPager.php:36
IndexPager
IndexPager is an efficient pager which uses a (roughly unique) index in the data set to implement pag...
Definition: IndexPager.php:69
ReverseChronologicalPager
Efficient paging for SQL queries.
Definition: ReverseChronologicalPager.php:28
IndexPager\$mOffset
mixed $mOffset
The starting point to enumerate entries.
Definition: IndexPager.php:87
ReverseChronologicalPager\getNavigationBar
getNavigationBar()
Definition: ReverseChronologicalPager.php:38
IndexPager\getPagingLinks
getPagingLinks( $linkTexts, $disabledTexts=[])
Get paging links.
Definition: IndexPager.php:669