MediaWiki  1.32.0
ReverseChronologicalPager.php
Go to the documentation of this file.
1 <?php
21 use Wikimedia\Timestamp\TimestampException;
22 
28 abstract class ReverseChronologicalPager extends IndexPager {
30  public $mYear;
31  public $mMonth;
32  public $mDay;
33 
34  public function getNavigationBar() {
35  if ( !$this->isNavigationBarShown() ) {
36  return '';
37  }
38 
39  if ( isset( $this->mNavigationBar ) ) {
40  return $this->mNavigationBar;
41  }
42 
43  $linkTexts = [
44  'prev' => $this->msg( 'pager-newer-n' )->numParams( $this->mLimit )->escaped(),
45  'next' => $this->msg( 'pager-older-n' )->numParams( $this->mLimit )->escaped(),
46  'first' => $this->msg( 'histlast' )->escaped(),
47  'last' => $this->msg( 'histfirst' )->escaped()
48  ];
49 
50  $pagingLinks = $this->getPagingLinks( $linkTexts );
51  $limitLinks = $this->getLimitLinks();
52  $limits = $this->getLanguage()->pipeList( $limitLinks );
53  $firstLastLinks = $this->msg( 'parentheses' )->rawParams( "{$pagingLinks['first']}" .
54  $this->msg( 'pipe-separator' )->escaped() .
55  "{$pagingLinks['last']}" )->escaped();
56 
57  $this->mNavigationBar = $firstLastLinks . ' ' .
58  $this->msg( 'viewprevnext' )->rawParams(
59  $pagingLinks['prev'], $pagingLinks['next'], $limits )->escaped();
60 
61  return $this->mNavigationBar;
62  }
63 
73  public function getDateCond( $year, $month, $day = -1 ) {
74  $year = (int)$year;
75  $month = (int)$month;
76  $day = (int)$day;
77 
78  // Basic validity checks for year and month
79  // If year and month are invalid, don't update the mOffset
80  if ( $year <= 0 && ( $month <= 0 || $month >= 13 ) ) {
81  return null;
82  }
83 
84  $timestamp = self::getOffsetDate( $year, $month, $day );
85 
86  try {
87  // The timestamp used for DB queries is at midnight of the *next* day after the selected date.
88  $selectedDate = new DateTime( $timestamp->getTimestamp( TS_ISO_8601 ) );
89  $selectedDate = $selectedDate->modify( '-1 day' );
90 
91  $this->mYear = (int)$selectedDate->format( 'Y' );
92  $this->mMonth = (int)$selectedDate->format( 'm' );
93  $this->mDay = (int)$selectedDate->format( 'd' );
94  $this->mOffset = $this->mDb->timestamp( $timestamp->getTimestamp() );
95  } catch ( TimestampException $e ) {
96  // Invalid user provided timestamp (T149257)
97  return null;
98  }
99 
100  return $this->mOffset;
101  }
102 
118  public static function getOffsetDate( $year, $month, $day = -1 ) {
119  // Given an optional year, month, and day, we need to generate a timestamp
120  // to use as "WHERE rev_timestamp <= result"
121  // Examples: year = 2006 equals < 20070101 (+000000)
122  // year=2005, month=1 equals < 20050201
123  // year=2005, month=12 equals < 20060101
124  // year=2005, month=12, day=5 equals < 20051206
125  if ( $year <= 0 ) {
126  // If no year given, assume the current one
127  $timestamp = MWTimestamp::getInstance();
128  $year = $timestamp->format( 'Y' );
129  // If this month hasn't happened yet this year, go back to last year's month
130  if ( $month > $timestamp->format( 'n' ) ) {
131  $year--;
132  }
133  }
134 
135  if ( $month && $month > 0 && $month < 13 ) {
136  // Day validity check after we have month and year checked
137  $day = checkdate( $month, $day, $year ) ? $day : false;
138 
139  if ( $day && $day > 0 ) {
140  // If we have a day, we want up to the day immediately afterward
141  $day++;
142 
143  // Did we overflow the current month?
144  if ( !checkdate( $month, $day, $year ) ) {
145  $day = 1;
146  $month++;
147  }
148  } else {
149  // If no day, assume beginning of next month
150  $day = 1;
151  $month++;
152  }
153 
154  // Did we overflow the current year?
155  if ( $month > 12 ) {
156  $month = 1;
157  $year++;
158  }
159 
160  } else {
161  // No month implies we want up to the end of the year in question
162  $month = 1;
163  $day = 1;
164  $year++;
165  }
166 
167  // Y2K38 bug
168  if ( $year > 2032 ) {
169  $year = 2032;
170  }
171 
172  $ymd = (int)sprintf( "%04d%02d%02d", $year, $month, $day );
173 
174  if ( $ymd > 20320101 ) {
175  $ymd = 20320101;
176  }
177 
178  return MWTimestamp::getInstance( "${ymd}000000" );
179  }
180 }
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:118
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:73
ReverseChronologicalPager\$mDay
$mDay
Definition: ReverseChronologicalPager.php:32
ReverseChronologicalPager\$mDefaultDirection
$mDefaultDirection
Definition: ReverseChronologicalPager.php:29
ContextSource\msg
msg( $key)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:168
IndexPager\getLimitLinks
getLimitLinks()
Definition: IndexPager.php:646
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
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:2213
IndexPager\$mOffset
$mOffset
Definition: IndexPager.php:81
ReverseChronologicalPager\$mYear
$mYear
Definition: ReverseChronologicalPager.php:30
IndexPager\DIR_DESCENDING
const DIR_DESCENDING
Definition: IndexPager.php:76
IndexPager\isNavigationBarShown
isNavigationBarShown()
Returns whether to show the "navigation bar".
Definition: IndexPager.php:607
ReverseChronologicalPager\$mMonth
$mMonth
Definition: ReverseChronologicalPager.php:31
IndexPager
IndexPager is an efficient pager which uses a (roughly unique) index in the data set to implement pag...
Definition: IndexPager.php:69
IndexPager\$mNavigationBar
$mNavigationBar
Definition: IndexPager.php:118
ReverseChronologicalPager
Efficient paging for SQL queries.
Definition: ReverseChronologicalPager.php:28
ReverseChronologicalPager\getNavigationBar
getNavigationBar()
Definition: ReverseChronologicalPager.php:34
IndexPager\getPagingLinks
getPagingLinks( $linkTexts, $disabledTexts=[])
Get paging links.
Definition: IndexPager.php:625