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