Pager.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. <?php
  2. /**
  3. * @defgroup Pager Pager
  4. *
  5. * @file
  6. * @ingroup Pager
  7. */
  8. /**
  9. * Basic pager interface.
  10. * @ingroup Pager
  11. */
  12. interface Pager {
  13. function getNavigationBar();
  14. function getBody();
  15. }
  16. /**
  17. * IndexPager is an efficient pager which uses a (roughly unique) index in the
  18. * data set to implement paging, rather than a "LIMIT offset,limit" clause.
  19. * In MySQL, such a limit/offset clause requires counting through the
  20. * specified number of offset rows to find the desired data, which can be
  21. * expensive for large offsets.
  22. *
  23. * ReverseChronologicalPager is a child class of the abstract IndexPager, and
  24. * contains some formatting and display code which is specific to the use of
  25. * timestamps as indexes. Here is a synopsis of its operation:
  26. *
  27. * * The query is specified by the offset, limit and direction (dir)
  28. * parameters, in addition to any subclass-specific parameters.
  29. * * The offset is the non-inclusive start of the DB query. A row with an
  30. * index value equal to the offset will never be shown.
  31. * * The query may either be done backwards, where the rows are returned by
  32. * the database in the opposite order to which they are displayed to the
  33. * user, or forwards. This is specified by the "dir" parameter, dir=prev
  34. * means backwards, anything else means forwards. The offset value
  35. * specifies the start of the database result set, which may be either
  36. * the start or end of the displayed data set. This allows "previous"
  37. * links to be implemented without knowledge of the index value at the
  38. * start of the previous page.
  39. * * An additional row beyond the user-specified limit is always requested.
  40. * This allows us to tell whether we should display a "next" link in the
  41. * case of forwards mode, or a "previous" link in the case of backwards
  42. * mode. Determining whether to display the other link (the one for the
  43. * page before the start of the database result set) can be done
  44. * heuristically by examining the offset.
  45. *
  46. * * An empty offset indicates that the offset condition should be omitted
  47. * from the query. This naturally produces either the first page or the
  48. * last page depending on the dir parameter.
  49. *
  50. * Subclassing the pager to implement concrete functionality should be fairly
  51. * simple, please see the examples in PageHistory.php and
  52. * SpecialIpblocklist.php. You just need to override formatRow(),
  53. * getQueryInfo() and getIndexField(). Don't forget to call the parent
  54. * constructor if you override it.
  55. *
  56. * @ingroup Pager
  57. */
  58. abstract class IndexPager implements Pager {
  59. public $mRequest;
  60. public $mLimitsShown = array( 20, 50, 100, 250, 500 );
  61. public $mDefaultLimit = 50;
  62. public $mOffset, $mLimit;
  63. public $mQueryDone = false;
  64. public $mDb;
  65. public $mPastTheEndRow;
  66. /**
  67. * The index to actually be used for ordering. This is a single string e-
  68. * ven if multiple orderings are supported.
  69. */
  70. protected $mIndexField;
  71. /** For pages that support multiple types of ordering, which one to use. */
  72. protected $mOrderType;
  73. /**
  74. * $mDefaultDirection gives the direction to use when sorting results:
  75. * false for ascending, true for descending. If $mIsBackwards is set, we
  76. * start from the opposite end, but we still sort the page itself according
  77. * to $mDefaultDirection. E.g., if $mDefaultDirection is false but we're
  78. * going backwards, we'll display the last page of results, but the last
  79. * result will be at the bottom, not the top.
  80. *
  81. * Like $mIndexField, $mDefaultDirection will be a single value even if the
  82. * class supports multiple default directions for different order types.
  83. */
  84. public $mDefaultDirection;
  85. public $mIsBackwards;
  86. /**
  87. * Result object for the query. Warning: seek before use.
  88. */
  89. public $mResult;
  90. public function __construct() {
  91. global $wgRequest, $wgUser;
  92. $this->mRequest = $wgRequest;
  93. # NB: the offset is quoted, not validated. It is treated as an
  94. # arbitrary string to support the widest variety of index types. Be
  95. # careful outputting it into HTML!
  96. $this->mOffset = $this->mRequest->getText( 'offset' );
  97. # Use consistent behavior for the limit options
  98. $this->mDefaultLimit = intval( $wgUser->getOption( 'rclimit' ) );
  99. list( $this->mLimit, /* $offset */ ) = $this->mRequest->getLimitOffset();
  100. $this->mIsBackwards = ( $this->mRequest->getVal( 'dir' ) == 'prev' );
  101. $this->mDb = wfGetDB( DB_SLAVE );
  102. $index = $this->getIndexField();
  103. $order = $this->mRequest->getVal( 'order' );
  104. if( is_array( $index ) && isset( $index[$order] ) ) {
  105. $this->mOrderType = $order;
  106. $this->mIndexField = $index[$order];
  107. } elseif( is_array( $index ) ) {
  108. # First element is the default
  109. reset( $index );
  110. list( $this->mOrderType, $this->mIndexField ) = each( $index );
  111. } else {
  112. # $index is not an array
  113. $this->mOrderType = null;
  114. $this->mIndexField = $index;
  115. }
  116. if( !isset( $this->mDefaultDirection ) ) {
  117. $dir = $this->getDefaultDirections();
  118. $this->mDefaultDirection = is_array( $dir )
  119. ? $dir[$this->mOrderType]
  120. : $dir;
  121. }
  122. }
  123. /**
  124. * Do the query, using information from the object context. This function
  125. * has been kept minimal to make it overridable if necessary, to allow for
  126. * result sets formed from multiple DB queries.
  127. */
  128. function doQuery() {
  129. # Use the child class name for profiling
  130. $fname = __METHOD__ . ' (' . get_class( $this ) . ')';
  131. wfProfileIn( $fname );
  132. $descending = ( $this->mIsBackwards == $this->mDefaultDirection );
  133. # Plus an extra row so that we can tell the "next" link should be shown
  134. $queryLimit = $this->mLimit + 1;
  135. $this->mResult = $this->reallyDoQuery( $this->mOffset, $queryLimit, $descending );
  136. $this->extractResultInfo( $this->mOffset, $queryLimit, $this->mResult );
  137. $this->mQueryDone = true;
  138. $this->preprocessResults( $this->mResult );
  139. $this->mResult->rewind(); // Paranoia
  140. wfProfileOut( $fname );
  141. }
  142. /**
  143. * Return the result wrapper.
  144. */
  145. function getResult() {
  146. return $this->mResult;
  147. }
  148. /**
  149. * Set the offset from an other source than $wgRequest
  150. */
  151. function setOffset( $offset ) {
  152. $this->mOffset = $offset;
  153. }
  154. /**
  155. * Set the limit from an other source than $wgRequest
  156. */
  157. function setLimit( $limit ) {
  158. $this->mLimit = $limit;
  159. }
  160. /**
  161. * Extract some useful data from the result object for use by
  162. * the navigation bar, put it into $this
  163. */
  164. function extractResultInfo( $offset, $limit, ResultWrapper $res ) {
  165. $numRows = $res->numRows();
  166. if ( $numRows ) {
  167. $row = $res->fetchRow();
  168. $firstIndex = $row[$this->mIndexField];
  169. # Discard the extra result row if there is one
  170. if ( $numRows > $this->mLimit && $numRows > 1 ) {
  171. $res->seek( $numRows - 1 );
  172. $this->mPastTheEndRow = $res->fetchObject();
  173. $indexField = $this->mIndexField;
  174. $this->mPastTheEndIndex = $this->mPastTheEndRow->$indexField;
  175. $res->seek( $numRows - 2 );
  176. $row = $res->fetchRow();
  177. $lastIndex = $row[$this->mIndexField];
  178. } else {
  179. $this->mPastTheEndRow = null;
  180. # Setting indexes to an empty string means that they will be
  181. # omitted if they would otherwise appear in URLs. It just so
  182. # happens that this is the right thing to do in the standard
  183. # UI, in all the relevant cases.
  184. $this->mPastTheEndIndex = '';
  185. $res->seek( $numRows - 1 );
  186. $row = $res->fetchRow();
  187. $lastIndex = $row[$this->mIndexField];
  188. }
  189. } else {
  190. $firstIndex = '';
  191. $lastIndex = '';
  192. $this->mPastTheEndRow = null;
  193. $this->mPastTheEndIndex = '';
  194. }
  195. if ( $this->mIsBackwards ) {
  196. $this->mIsFirst = ( $numRows < $limit );
  197. $this->mIsLast = ( $offset == '' );
  198. $this->mLastShown = $firstIndex;
  199. $this->mFirstShown = $lastIndex;
  200. } else {
  201. $this->mIsFirst = ( $offset == '' );
  202. $this->mIsLast = ( $numRows < $limit );
  203. $this->mLastShown = $lastIndex;
  204. $this->mFirstShown = $firstIndex;
  205. }
  206. }
  207. /**
  208. * Do a query with specified parameters, rather than using the object
  209. * context
  210. *
  211. * @param string $offset Index offset, inclusive
  212. * @param integer $limit Exact query limit
  213. * @param boolean $descending Query direction, false for ascending, true for descending
  214. * @return ResultWrapper
  215. */
  216. function reallyDoQuery( $offset, $limit, $descending ) {
  217. $fname = __METHOD__ . ' (' . get_class( $this ) . ')';
  218. $info = $this->getQueryInfo();
  219. $tables = $info['tables'];
  220. $fields = $info['fields'];
  221. $conds = isset( $info['conds'] ) ? $info['conds'] : array();
  222. $options = isset( $info['options'] ) ? $info['options'] : array();
  223. $join_conds = isset( $info['join_conds'] ) ? $info['join_conds'] : array();
  224. if ( $descending ) {
  225. $options['ORDER BY'] = $this->mIndexField;
  226. $operator = '>';
  227. } else {
  228. $options['ORDER BY'] = $this->mIndexField . ' DESC';
  229. $operator = '<';
  230. }
  231. if ( $offset != '' ) {
  232. $conds[] = $this->mIndexField . $operator . $this->mDb->addQuotes( $offset );
  233. }
  234. $options['LIMIT'] = intval( $limit );
  235. $res = $this->mDb->select( $tables, $fields, $conds, $fname, $options, $join_conds );
  236. return new ResultWrapper( $this->mDb, $res );
  237. }
  238. /**
  239. * Pre-process results; useful for performing batch existence checks, etc.
  240. *
  241. * @param ResultWrapper $result Result wrapper
  242. */
  243. protected function preprocessResults( $result ) {}
  244. /**
  245. * Get the formatted result list. Calls getStartBody(), formatRow() and
  246. * getEndBody(), concatenates the results and returns them.
  247. */
  248. function getBody() {
  249. if ( !$this->mQueryDone ) {
  250. $this->doQuery();
  251. }
  252. # Don't use any extra rows returned by the query
  253. $numRows = min( $this->mResult->numRows(), $this->mLimit );
  254. $s = $this->getStartBody();
  255. if ( $numRows ) {
  256. if ( $this->mIsBackwards ) {
  257. for ( $i = $numRows - 1; $i >= 0; $i-- ) {
  258. $this->mResult->seek( $i );
  259. $row = $this->mResult->fetchObject();
  260. $s .= $this->formatRow( $row );
  261. }
  262. } else {
  263. $this->mResult->seek( 0 );
  264. for ( $i = 0; $i < $numRows; $i++ ) {
  265. $row = $this->mResult->fetchObject();
  266. $s .= $this->formatRow( $row );
  267. }
  268. }
  269. } else {
  270. $s .= $this->getEmptyBody();
  271. }
  272. $s .= $this->getEndBody();
  273. return $s;
  274. }
  275. /**
  276. * Make a self-link
  277. */
  278. function makeLink($text, $query = null, $type=null) {
  279. if ( $query === null ) {
  280. return $text;
  281. }
  282. $attrs = array();
  283. if( in_array( $type, array( 'first', 'prev', 'next', 'last' ) ) ) {
  284. # HTML5 rel attributes
  285. $attrs['rel'] = $type;
  286. }
  287. if( $type ) {
  288. $attrs['class'] = "mw-{$type}link";
  289. }
  290. return $this->getSkin()->link( $this->getTitle(), $text,
  291. $attrs, $query + $this->getDefaultQuery(), array('noclasses','known') );
  292. }
  293. /**
  294. * Hook into getBody(), allows text to be inserted at the start. This
  295. * will be called even if there are no rows in the result set.
  296. */
  297. function getStartBody() {
  298. return '';
  299. }
  300. /**
  301. * Hook into getBody() for the end of the list
  302. */
  303. function getEndBody() {
  304. return '';
  305. }
  306. /**
  307. * Hook into getBody(), for the bit between the start and the
  308. * end when there are no rows
  309. */
  310. function getEmptyBody() {
  311. return '';
  312. }
  313. /**
  314. * Title used for self-links. Override this if you want to be able to
  315. * use a title other than $wgTitle
  316. */
  317. function getTitle() {
  318. return $GLOBALS['wgTitle'];
  319. }
  320. /**
  321. * Get the current skin. This can be overridden if necessary.
  322. */
  323. function getSkin() {
  324. if ( !isset( $this->mSkin ) ) {
  325. global $wgUser;
  326. $this->mSkin = $wgUser->getSkin();
  327. }
  328. return $this->mSkin;
  329. }
  330. /**
  331. * Get an array of query parameters that should be put into self-links.
  332. * By default, all parameters passed in the URL are used, except for a
  333. * short blacklist.
  334. */
  335. function getDefaultQuery() {
  336. if ( !isset( $this->mDefaultQuery ) ) {
  337. $this->mDefaultQuery = $_GET;
  338. unset( $this->mDefaultQuery['title'] );
  339. unset( $this->mDefaultQuery['dir'] );
  340. unset( $this->mDefaultQuery['offset'] );
  341. unset( $this->mDefaultQuery['limit'] );
  342. unset( $this->mDefaultQuery['order'] );
  343. unset( $this->mDefaultQuery['month'] );
  344. unset( $this->mDefaultQuery['year'] );
  345. }
  346. return $this->mDefaultQuery;
  347. }
  348. /**
  349. * Get the number of rows in the result set
  350. */
  351. function getNumRows() {
  352. if ( !$this->mQueryDone ) {
  353. $this->doQuery();
  354. }
  355. return $this->mResult->numRows();
  356. }
  357. /**
  358. * Get a URL query array for the prev, next, first and last links.
  359. */
  360. function getPagingQueries() {
  361. if ( !$this->mQueryDone ) {
  362. $this->doQuery();
  363. }
  364. # Don't announce the limit everywhere if it's the default
  365. $urlLimit = $this->mLimit == $this->mDefaultLimit ? '' : $this->mLimit;
  366. if ( $this->mIsFirst ) {
  367. $prev = false;
  368. $first = false;
  369. } else {
  370. $prev = array( 'dir' => 'prev', 'offset' => $this->mFirstShown, 'limit' => $urlLimit );
  371. $first = array( 'limit' => $urlLimit );
  372. }
  373. if ( $this->mIsLast ) {
  374. $next = false;
  375. $last = false;
  376. } else {
  377. $next = array( 'offset' => $this->mLastShown, 'limit' => $urlLimit );
  378. $last = array( 'dir' => 'prev', 'limit' => $urlLimit );
  379. }
  380. return array( 'prev' => $prev, 'next' => $next, 'first' => $first, 'last' => $last );
  381. }
  382. /**
  383. * Get paging links. If a link is disabled, the item from $disabledTexts
  384. * will be used. If there is no such item, the unlinked text from
  385. * $linkTexts will be used. Both $linkTexts and $disabledTexts are arrays
  386. * of HTML.
  387. */
  388. function getPagingLinks( $linkTexts, $disabledTexts = array() ) {
  389. $queries = $this->getPagingQueries();
  390. $links = array();
  391. foreach ( $queries as $type => $query ) {
  392. if ( $query !== false ) {
  393. $links[$type] = $this->makeLink( $linkTexts[$type], $queries[$type], $type );
  394. } elseif ( isset( $disabledTexts[$type] ) ) {
  395. $links[$type] = $disabledTexts[$type];
  396. } else {
  397. $links[$type] = $linkTexts[$type];
  398. }
  399. }
  400. return $links;
  401. }
  402. function getLimitLinks() {
  403. global $wgLang;
  404. $links = array();
  405. if ( $this->mIsBackwards ) {
  406. $offset = $this->mPastTheEndIndex;
  407. } else {
  408. $offset = $this->mOffset;
  409. }
  410. foreach ( $this->mLimitsShown as $limit ) {
  411. $links[] = $this->makeLink( $wgLang->formatNum( $limit ),
  412. array( 'offset' => $offset, 'limit' => $limit ), 'num' );
  413. }
  414. return $links;
  415. }
  416. /**
  417. * Abstract formatting function. This should return an HTML string
  418. * representing the result row $row. Rows will be concatenated and
  419. * returned by getBody()
  420. */
  421. abstract function formatRow( $row );
  422. /**
  423. * This function should be overridden to provide all parameters
  424. * needed for the main paged query. It returns an associative
  425. * array with the following elements:
  426. * tables => Table(s) for passing to Database::select()
  427. * fields => Field(s) for passing to Database::select(), may be *
  428. * conds => WHERE conditions
  429. * options => option array
  430. */
  431. abstract function getQueryInfo();
  432. /**
  433. * This function should be overridden to return the name of the index fi-
  434. * eld. If the pager supports multiple orders, it may return an array of
  435. * 'querykey' => 'indexfield' pairs, so that a request with &count=querykey
  436. * will use indexfield to sort. In this case, the first returned key is
  437. * the default.
  438. *
  439. * Needless to say, it's really not a good idea to use a non-unique index
  440. * for this! That won't page right.
  441. */
  442. abstract function getIndexField();
  443. /**
  444. * Return the default sorting direction: false for ascending, true for de-
  445. * scending. You can also have an associative array of ordertype => dir,
  446. * if multiple order types are supported. In this case getIndexField()
  447. * must return an array, and the keys of that must exactly match the keys
  448. * of this.
  449. *
  450. * For backward compatibility, this method's return value will be ignored
  451. * if $this->mDefaultDirection is already set when the constructor is
  452. * called, for instance if it's statically initialized. In that case the
  453. * value of that variable (which must be a boolean) will be used.
  454. *
  455. * Note that despite its name, this does not return the value of the
  456. * $this->mDefaultDirection member variable. That's the default for this
  457. * particular instantiation, which is a single value. This is the set of
  458. * all defaults for the class.
  459. */
  460. protected function getDefaultDirections() { return false; }
  461. }
  462. /**
  463. * IndexPager with an alphabetic list and a formatted navigation bar
  464. * @ingroup Pager
  465. */
  466. abstract class AlphabeticPager extends IndexPager {
  467. /**
  468. * Shamelessly stolen bits from ReverseChronologicalPager,
  469. * didn't want to do class magic as may be still revamped
  470. */
  471. function getNavigationBar() {
  472. global $wgLang;
  473. if( isset( $this->mNavigationBar ) ) {
  474. return $this->mNavigationBar;
  475. }
  476. $opts = array( 'parsemag', 'escapenoentities' );
  477. $linkTexts = array(
  478. 'prev' => wfMsgExt( 'prevn', $opts, $wgLang->formatNum( $this->mLimit ) ),
  479. 'next' => wfMsgExt( 'nextn', $opts, $wgLang->formatNum($this->mLimit ) ),
  480. 'first' => wfMsgExt( 'page_first', $opts ),
  481. 'last' => wfMsgExt( 'page_last', $opts )
  482. );
  483. $pagingLinks = $this->getPagingLinks( $linkTexts );
  484. $limitLinks = $this->getLimitLinks();
  485. $limits = $wgLang->pipeList( $limitLinks );
  486. $this->mNavigationBar =
  487. "(" . $wgLang->pipeList( array( $pagingLinks['first'], $pagingLinks['last'] ) ) . ") " .
  488. wfMsgHtml( 'viewprevnext', $pagingLinks['prev'],
  489. $pagingLinks['next'], $limits );
  490. if( !is_array( $this->getIndexField() ) ) {
  491. # Early return to avoid undue nesting
  492. return $this->mNavigationBar;
  493. }
  494. $extra = '';
  495. $first = true;
  496. $msgs = $this->getOrderTypeMessages();
  497. foreach( array_keys( $msgs ) as $order ) {
  498. if( $first ) {
  499. $first = false;
  500. } else {
  501. $extra .= wfMsgExt( 'pipe-separator' , 'escapenoentities' );
  502. }
  503. if( $order == $this->mOrderType ) {
  504. $extra .= wfMsgHTML( $msgs[$order] );
  505. } else {
  506. $extra .= $this->makeLink(
  507. wfMsgHTML( $msgs[$order] ),
  508. array( 'order' => $order )
  509. );
  510. }
  511. }
  512. if( $extra !== '' ) {
  513. $this->mNavigationBar .= " ($extra)";
  514. }
  515. return $this->mNavigationBar;
  516. }
  517. /**
  518. * If this supports multiple order type messages, give the message key for
  519. * enabling each one in getNavigationBar. The return type is an associa-
  520. * tive array whose keys must exactly match the keys of the array returned
  521. * by getIndexField(), and whose values are message keys.
  522. * @return array
  523. */
  524. protected function getOrderTypeMessages() {
  525. return null;
  526. }
  527. }
  528. /**
  529. * IndexPager with a formatted navigation bar
  530. * @ingroup Pager
  531. */
  532. abstract class ReverseChronologicalPager extends IndexPager {
  533. public $mDefaultDirection = true;
  534. public $mYear;
  535. public $mMonth;
  536. function __construct() {
  537. parent::__construct();
  538. }
  539. function getNavigationBar() {
  540. global $wgLang;
  541. if ( isset( $this->mNavigationBar ) ) {
  542. return $this->mNavigationBar;
  543. }
  544. $nicenumber = $wgLang->formatNum( $this->mLimit );
  545. $linkTexts = array(
  546. 'prev' => wfMsgExt( 'pager-newer-n', array( 'parsemag' ), $nicenumber ),
  547. 'next' => wfMsgExt( 'pager-older-n', array( 'parsemag' ), $nicenumber ),
  548. 'first' => wfMsgHtml( 'histlast' ),
  549. 'last' => wfMsgHtml( 'histfirst' )
  550. );
  551. $pagingLinks = $this->getPagingLinks( $linkTexts );
  552. $limitLinks = $this->getLimitLinks();
  553. $limits = $wgLang->pipeList( $limitLinks );
  554. $this->mNavigationBar = "({$pagingLinks['first']}" . wfMsgExt( 'pipe-separator' , 'escapenoentities' ) . "{$pagingLinks['last']}) " .
  555. wfMsgHtml("viewprevnext", $pagingLinks['prev'], $pagingLinks['next'], $limits);
  556. return $this->mNavigationBar;
  557. }
  558. function getDateCond( $year, $month ) {
  559. $year = intval($year);
  560. $month = intval($month);
  561. // Basic validity checks
  562. $this->mYear = $year > 0 ? $year : false;
  563. $this->mMonth = ($month > 0 && $month < 13) ? $month : false;
  564. // Given an optional year and month, we need to generate a timestamp
  565. // to use as "WHERE rev_timestamp <= result"
  566. // Examples: year = 2006 equals < 20070101 (+000000)
  567. // year=2005, month=1 equals < 20050201
  568. // year=2005, month=12 equals < 20060101
  569. if ( !$this->mYear && !$this->mMonth ) {
  570. return;
  571. }
  572. if ( $this->mYear ) {
  573. $year = $this->mYear;
  574. } else {
  575. // If no year given, assume the current one
  576. $year = gmdate( 'Y' );
  577. // If this month hasn't happened yet this year, go back to last year's month
  578. if( $this->mMonth > gmdate( 'n' ) ) {
  579. $year--;
  580. }
  581. }
  582. if ( $this->mMonth ) {
  583. $month = $this->mMonth + 1;
  584. // For December, we want January 1 of the next year
  585. if ($month > 12) {
  586. $month = 1;
  587. $year++;
  588. }
  589. } else {
  590. // No month implies we want up to the end of the year in question
  591. $month = 1;
  592. $year++;
  593. }
  594. // Y2K38 bug
  595. if ( $year > 2032 ) {
  596. $year = 2032;
  597. }
  598. $ymd = (int)sprintf( "%04d%02d01", $year, $month );
  599. if ( $ymd > 20320101 ) {
  600. $ymd = 20320101;
  601. }
  602. $this->mOffset = $this->mDb->timestamp( "${ymd}000000" );
  603. }
  604. }
  605. /**
  606. * Table-based display with a user-selectable sort order
  607. * @ingroup Pager
  608. */
  609. abstract class TablePager extends IndexPager {
  610. var $mSort;
  611. var $mCurrentRow;
  612. function __construct() {
  613. global $wgRequest;
  614. $this->mSort = $wgRequest->getText( 'sort' );
  615. if ( !array_key_exists( $this->mSort, $this->getFieldNames() ) ) {
  616. $this->mSort = $this->getDefaultSort();
  617. }
  618. if ( $wgRequest->getBool( 'asc' ) ) {
  619. $this->mDefaultDirection = false;
  620. } elseif ( $wgRequest->getBool( 'desc' ) ) {
  621. $this->mDefaultDirection = true;
  622. } /* Else leave it at whatever the class default is */
  623. parent::__construct();
  624. }
  625. function getStartBody() {
  626. global $wgStylePath;
  627. $tableClass = htmlspecialchars( $this->getTableClass() );
  628. $sortClass = htmlspecialchars( $this->getSortHeaderClass() );
  629. $s = "<table border='1' class=\"$tableClass\"><thead><tr>\n";
  630. $fields = $this->getFieldNames();
  631. # Make table header
  632. foreach ( $fields as $field => $name ) {
  633. if ( strval( $name ) == '' ) {
  634. $s .= "<th>&nbsp;</th>\n";
  635. } elseif ( $this->isFieldSortable( $field ) ) {
  636. $query = array( 'sort' => $field, 'limit' => $this->mLimit );
  637. if ( $field == $this->mSort ) {
  638. # This is the sorted column
  639. # Prepare a link that goes in the other sort order
  640. if ( $this->mDefaultDirection ) {
  641. # Descending
  642. $image = 'Arr_u.png';
  643. $query['asc'] = '1';
  644. $query['desc'] = '';
  645. $alt = htmlspecialchars( wfMsg( 'descending_abbrev' ) );
  646. } else {
  647. # Ascending
  648. $image = 'Arr_d.png';
  649. $query['asc'] = '';
  650. $query['desc'] = '1';
  651. $alt = htmlspecialchars( wfMsg( 'ascending_abbrev' ) );
  652. }
  653. $image = htmlspecialchars( "$wgStylePath/common/images/$image" );
  654. $link = $this->makeLink(
  655. "<img width=\"12\" height=\"12\" alt=\"$alt\" src=\"$image\" />" .
  656. htmlspecialchars( $name ), $query );
  657. $s .= "<th class=\"$sortClass\">$link</th>\n";
  658. } else {
  659. $s .= '<th>' . $this->makeLink( htmlspecialchars( $name ), $query ) . "</th>\n";
  660. }
  661. } else {
  662. $s .= '<th>' . htmlspecialchars( $name ) . "</th>\n";
  663. }
  664. }
  665. $s .= "</tr></thead><tbody>\n";
  666. return $s;
  667. }
  668. function getEndBody() {
  669. return "</tbody></table>\n";
  670. }
  671. function getEmptyBody() {
  672. $colspan = count( $this->getFieldNames() );
  673. $msgEmpty = wfMsgHtml( 'table_pager_empty' );
  674. return "<tr><td colspan=\"$colspan\">$msgEmpty</td></tr>\n";
  675. }
  676. function formatRow( $row ) {
  677. $rowClass = $this->getRowClass( $row );
  678. $s = "<tr class=\"$rowClass\">\n";
  679. $fieldNames = $this->getFieldNames();
  680. $this->mCurrentRow = $row; # In case formatValue needs to know
  681. foreach ( $fieldNames as $field => $name ) {
  682. $value = isset( $row->$field ) ? $row->$field : null;
  683. $formatted = strval( $this->formatValue( $field, $value ) );
  684. if ( $formatted == '' ) {
  685. $formatted = '&nbsp;';
  686. }
  687. $class = 'TablePager_col_' . htmlspecialchars( $field );
  688. $s .= "<td class=\"$class\">$formatted</td>\n";
  689. }
  690. $s .= "</tr>\n";
  691. return $s;
  692. }
  693. function getRowClass($row) {
  694. return '';
  695. }
  696. function getIndexField() {
  697. return $this->mSort;
  698. }
  699. function getTableClass() {
  700. return 'TablePager';
  701. }
  702. function getNavClass() {
  703. return 'TablePager_nav';
  704. }
  705. function getSortHeaderClass() {
  706. return 'TablePager_sort';
  707. }
  708. /**
  709. * A navigation bar with images
  710. */
  711. function getNavigationBar() {
  712. global $wgStylePath, $wgContLang;
  713. $path = "$wgStylePath/common/images";
  714. $labels = array(
  715. 'first' => 'table_pager_first',
  716. 'prev' => 'table_pager_prev',
  717. 'next' => 'table_pager_next',
  718. 'last' => 'table_pager_last',
  719. );
  720. $images = array(
  721. 'first' => $wgContLang->isRTL() ? 'arrow_last_25.png' : 'arrow_first_25.png',
  722. 'prev' => $wgContLang->isRTL() ? 'arrow_right_25.png' : 'arrow_left_25.png',
  723. 'next' => $wgContLang->isRTL() ? 'arrow_left_25.png' : 'arrow_right_25.png',
  724. 'last' => $wgContLang->isRTL() ? 'arrow_first_25.png' : 'arrow_last_25.png',
  725. );
  726. $disabledImages = array(
  727. 'first' => $wgContLang->isRTL() ? 'arrow_disabled_last_25.png' : 'arrow_disabled_first_25.png',
  728. 'prev' => $wgContLang->isRTL() ? 'arrow_disabled_right_25.png' : 'arrow_disabled_left_25.png',
  729. 'next' => $wgContLang->isRTL() ? 'arrow_disabled_left_25.png' : 'arrow_disabled_right_25.png',
  730. 'last' => $wgContLang->isRTL() ? 'arrow_disabled_first_25.png' : 'arrow_disabled_last_25.png',
  731. );
  732. $linkTexts = array();
  733. $disabledTexts = array();
  734. foreach ( $labels as $type => $label ) {
  735. $msgLabel = wfMsgHtml( $label );
  736. $linkTexts[$type] = "<img src=\"$path/{$images[$type]}\" alt=\"$msgLabel\"/><br/>$msgLabel";
  737. $disabledTexts[$type] = "<img src=\"$path/{$disabledImages[$type]}\" alt=\"$msgLabel\"/><br/>$msgLabel";
  738. }
  739. $links = $this->getPagingLinks( $linkTexts, $disabledTexts );
  740. $navClass = htmlspecialchars( $this->getNavClass() );
  741. $s = "<table class=\"$navClass\" align=\"center\" cellpadding=\"3\"><tr>\n";
  742. $cellAttrs = 'valign="top" align="center" width="' . 100 / count( $links ) . '%"';
  743. foreach ( $labels as $type => $label ) {
  744. $s .= "<td $cellAttrs>{$links[$type]}</td>\n";
  745. }
  746. $s .= "</tr></table>\n";
  747. return $s;
  748. }
  749. /**
  750. * Get a <select> element which has options for each of the allowed limits
  751. */
  752. function getLimitSelect() {
  753. global $wgLang;
  754. $s = "<select name=\"limit\">";
  755. foreach ( $this->mLimitsShown as $limit ) {
  756. $selected = $limit == $this->mLimit ? 'selected="selected"' : '';
  757. $formattedLimit = $wgLang->formatNum( $limit );
  758. $s .= "<option value=\"$limit\" $selected>$formattedLimit</option>\n";
  759. }
  760. $s .= "</select>";
  761. return $s;
  762. }
  763. /**
  764. * Get <input type="hidden"> elements for use in a method="get" form.
  765. * Resubmits all defined elements of the $_GET array, except for a
  766. * blacklist, passed in the $blacklist parameter.
  767. */
  768. function getHiddenFields( $blacklist = array() ) {
  769. $blacklist = (array)$blacklist;
  770. $query = $_GET;
  771. foreach ( $blacklist as $name ) {
  772. unset( $query[$name] );
  773. }
  774. $s = '';
  775. foreach ( $query as $name => $value ) {
  776. $encName = htmlspecialchars( $name );
  777. $encValue = htmlspecialchars( $value );
  778. $s .= "<input type=\"hidden\" name=\"$encName\" value=\"$encValue\"/>\n";
  779. }
  780. return $s;
  781. }
  782. /**
  783. * Get a form containing a limit selection dropdown
  784. */
  785. function getLimitForm() {
  786. # Make the select with some explanatory text
  787. $url = $this->getTitle()->escapeLocalURL();
  788. $msgSubmit = wfMsgHtml( 'table_pager_limit_submit' );
  789. return
  790. "<form method=\"get\" action=\"$url\">" .
  791. wfMsgHtml( 'table_pager_limit', $this->getLimitSelect() ) .
  792. "\n<input type=\"submit\" value=\"$msgSubmit\"/>\n" .
  793. $this->getHiddenFields( array('limit','title') ) .
  794. "</form>\n";
  795. }
  796. /**
  797. * Return true if the named field should be sortable by the UI, false
  798. * otherwise
  799. *
  800. * @param string $field
  801. */
  802. abstract function isFieldSortable( $field );
  803. /**
  804. * Format a table cell. The return value should be HTML, but use an empty
  805. * string not &nbsp; for empty cells. Do not include the <td> and </td>.
  806. *
  807. * The current result row is available as $this->mCurrentRow, in case you
  808. * need more context.
  809. *
  810. * @param string $name The database field name
  811. * @param string $value The value retrieved from the database
  812. */
  813. abstract function formatValue( $name, $value );
  814. /**
  815. * The database field name used as a default sort order
  816. */
  817. abstract function getDefaultSort();
  818. /**
  819. * An array mapping database field names to a textual description of the
  820. * field name, for use in the table header. The description should be plain
  821. * text, it will be HTML-escaped later.
  822. */
  823. abstract function getFieldNames();
  824. }