SpecialNewimages.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * @file
  4. * @ingroup SpecialPage
  5. * FIXME: this code is crap, should use Pager and Database::select().
  6. */
  7. function wfSpecialNewimages( $par, $specialPage ) {
  8. global $wgUser, $wgOut, $wgLang, $wgRequest, $wgMiserMode;
  9. $wpIlMatch = $wgRequest->getText( 'wpIlMatch' );
  10. $dbr = wfGetDB( DB_SLAVE );
  11. $sk = $wgUser->getSkin();
  12. $shownav = !$specialPage->including();
  13. $hidebots = $wgRequest->getBool( 'hidebots' , 1 );
  14. $hidebotsql = '';
  15. if ( $hidebots ) {
  16. # Make a list of group names which have the 'bot' flag set.
  17. $botconds = array();
  18. foreach ( User::getGroupsWithPermission('bot') as $groupname ) {
  19. $botconds[] = 'ug_group = ' . $dbr->addQuotes( $groupname );
  20. }
  21. # If not bot groups, do not set $hidebotsql
  22. if ( $botconds ) {
  23. $isbotmember = $dbr->makeList( $botconds, LIST_OR );
  24. # This join, in conjunction with WHERE ug_group IS NULL, returns
  25. # only those rows from IMAGE where the uploading user is not a mem-
  26. # ber of a group which has the 'bot' permission set.
  27. $ug = $dbr->tableName( 'user_groups' );
  28. $hidebotsql = " LEFT JOIN $ug ON img_user=ug_user AND ($isbotmember)";
  29. }
  30. }
  31. $image = $dbr->tableName( 'image' );
  32. $sql = "SELECT img_timestamp from $image";
  33. if ($hidebotsql) {
  34. $sql .= "$hidebotsql WHERE ug_group IS NULL";
  35. }
  36. $sql .= ' ORDER BY img_timestamp DESC LIMIT 1';
  37. $res = $dbr->query( $sql, __FUNCTION__ );
  38. $row = $dbr->fetchRow( $res );
  39. if( $row !== false ) {
  40. $ts = $row[0];
  41. } else {
  42. $ts = false;
  43. }
  44. $dbr->freeResult( $res );
  45. $sql = '';
  46. # If we were clever, we'd use this to cache.
  47. $latestTimestamp = wfTimestamp( TS_MW, $ts );
  48. # Hardcode this for now.
  49. $limit = 48;
  50. if ( $parval = intval( $par ) ) {
  51. if ( $parval <= $limit && $parval > 0 ) {
  52. $limit = $parval;
  53. }
  54. }
  55. $where = array();
  56. $searchpar = '';
  57. if ( $wpIlMatch != '' && !$wgMiserMode) {
  58. $nt = Title::newFromUrl( $wpIlMatch );
  59. if( $nt ) {
  60. $m = $dbr->escapeLike( strtolower( $nt->getDBkey() ) );
  61. $where[] = "LOWER(img_name) LIKE '%{$m}%'";
  62. $searchpar = '&wpIlMatch=' . urlencode( $wpIlMatch );
  63. }
  64. }
  65. $invertSort = false;
  66. if( $until = $wgRequest->getVal( 'until' ) ) {
  67. $where[] = "img_timestamp < '" . $dbr->timestamp( $until ) . "'";
  68. }
  69. if( $from = $wgRequest->getVal( 'from' ) ) {
  70. $where[] = "img_timestamp >= '" . $dbr->timestamp( $from ) . "'";
  71. $invertSort = true;
  72. }
  73. $sql='SELECT img_size, img_name, img_user, img_user_text,'.
  74. "img_description,img_timestamp FROM $image";
  75. if( $hidebotsql ) {
  76. $sql .= $hidebotsql;
  77. $where[] = 'ug_group IS NULL';
  78. }
  79. if( count( $where ) ) {
  80. $sql .= ' WHERE ' . $dbr->makeList( $where, LIST_AND );
  81. }
  82. $sql.=' ORDER BY img_timestamp '. ( $invertSort ? '' : ' DESC' );
  83. $sql.=' LIMIT ' . ( $limit + 1 );
  84. $res = $dbr->query( $sql, __FUNCTION__ );
  85. /**
  86. * We have to flip things around to get the last N after a certain date
  87. */
  88. $images = array();
  89. while ( $s = $dbr->fetchObject( $res ) ) {
  90. if( $invertSort ) {
  91. array_unshift( $images, $s );
  92. } else {
  93. array_push( $images, $s );
  94. }
  95. }
  96. $dbr->freeResult( $res );
  97. $gallery = new ImageGallery();
  98. $firstTimestamp = null;
  99. $lastTimestamp = null;
  100. $shownImages = 0;
  101. foreach( $images as $s ) {
  102. $shownImages++;
  103. if( $shownImages > $limit ) {
  104. # One extra just to test for whether to show a page link;
  105. # don't actually show it.
  106. break;
  107. }
  108. $name = $s->img_name;
  109. $ut = $s->img_user_text;
  110. $nt = Title::newFromText( $name, NS_FILE );
  111. $ul = $sk->makeLinkObj( Title::makeTitle( NS_USER, $ut ), $ut );
  112. $gallery->add( $nt, "$ul<br />\n<i>".$wgLang->timeanddate( $s->img_timestamp, true )."</i><br />\n" );
  113. $timestamp = wfTimestamp( TS_MW, $s->img_timestamp );
  114. if( empty( $firstTimestamp ) ) {
  115. $firstTimestamp = $timestamp;
  116. }
  117. $lastTimestamp = $timestamp;
  118. }
  119. $titleObj = SpecialPage::getTitleFor( 'Newimages' );
  120. $action = $titleObj->getLocalURL( $hidebots ? '' : 'hidebots=0' );
  121. if ( $shownav && !$wgMiserMode ) {
  122. $wgOut->addHTML(
  123. Xml::openElement( 'form', array( 'action' => $action, 'method' => 'post', 'id' => 'imagesearch' ) ) .
  124. Xml::fieldset( wfMsg( 'newimages-legend' ) ) .
  125. Xml::inputLabel( wfMsg( 'newimages-label' ), 'wpIlMatch', 'wpIlMatch', 20, $wpIlMatch ) . ' ' .
  126. Xml::submitButton( wfMsg( 'ilsubmit' ), array( 'name' => 'wpIlSubmit' ) ) .
  127. Xml::closeElement( 'fieldset' ) .
  128. Xml::closeElement( 'form' )
  129. );
  130. }
  131. $bydate = wfMsg( 'bydate' );
  132. $lt = $wgLang->formatNum( min( $shownImages, $limit ) );
  133. if ( $shownav ) {
  134. $text = wfMsgExt( 'imagelisttext', array('parse'), $lt, $bydate );
  135. $wgOut->addHTML( $text . "\n" );
  136. }
  137. /**
  138. * Paging controls...
  139. */
  140. # If we change bot visibility, this needs to be carried along.
  141. if( !$hidebots ) {
  142. $botpar = '&hidebots=0';
  143. } else {
  144. $botpar = '';
  145. }
  146. $now = wfTimestampNow();
  147. $d = $wgLang->date( $now, true );
  148. $t = $wgLang->time( $now, true );
  149. $dateLink = $sk->makeKnownLinkObj( $titleObj, wfMsgHtml( 'sp-newimages-showfrom', $d, $t ),
  150. 'from='.$now.$botpar.$searchpar );
  151. $botLink = $sk->makeKnownLinkObj($titleObj, wfMsgHtml( 'showhidebots',
  152. ($hidebots ? wfMsgHtml('show') : wfMsgHtml('hide'))),'hidebots='.($hidebots ? '0' : '1').$searchpar);
  153. $opts = array( 'parsemag', 'escapenoentities' );
  154. $prevLink = wfMsgExt( 'pager-newer-n', $opts, $wgLang->formatNum( $limit ) );
  155. if( $firstTimestamp && $firstTimestamp != $latestTimestamp ) {
  156. $prevLink = $sk->makeKnownLinkObj( $titleObj, $prevLink, 'from=' . $firstTimestamp . $botpar . $searchpar );
  157. }
  158. $nextLink = wfMsgExt( 'pager-older-n', $opts, $wgLang->formatNum( $limit ) );
  159. if( $shownImages > $limit && $lastTimestamp ) {
  160. $nextLink = $sk->makeKnownLinkObj( $titleObj, $nextLink, 'until=' . $lastTimestamp.$botpar.$searchpar );
  161. }
  162. $prevnext = '<p>' . $botLink . ' '. wfMsgHtml( 'viewprevnext', $prevLink, $nextLink, $dateLink ) .'</p>';
  163. if ($shownav)
  164. $wgOut->addHTML( $prevnext );
  165. if( count( $images ) ) {
  166. $wgOut->addHTML( $gallery->toHTML() );
  167. if ($shownav)
  168. $wgOut->addHTML( $prevnext );
  169. } else {
  170. $wgOut->addWikiMsg( 'noimages' );
  171. }
  172. }