596221501af4424f5d41f6a75faf716e2246de12
[lhc/web/wiklou.git] / includes / specials / SpecialNewimages.php
1 <?php
2 /**
3 * Implements Special:Newimages
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * @todo FIXME: this code is crap, should use Pager and Database::select().
26 */
27 function wfSpecialNewimages( $par, $specialPage ) {
28 global $wgUser, $wgOut, $wgLang, $wgRequest, $wgMiserMode;
29
30 $wpIlMatch = $wgRequest->getText( 'wpIlMatch' );
31 $dbr = wfGetDB( DB_SLAVE );
32 $sk = $wgUser->getSkin();
33 $shownav = !$specialPage->including();
34 $hidebots = $wgRequest->getBool( 'hidebots' , 1 );
35
36 $hidebotsql = '';
37 if ( $hidebots ) {
38 # Make a list of group names which have the 'bot' flag set.
39 $botconds = array();
40 foreach ( User::getGroupsWithPermission('bot') as $groupname ) {
41 $botconds[] = 'ug_group = ' . $dbr->addQuotes( $groupname );
42 }
43
44 # If not bot groups, do not set $hidebotsql
45 if ( $botconds ) {
46 $isbotmember = $dbr->makeList( $botconds, LIST_OR );
47
48 # This join, in conjunction with WHERE ug_group IS NULL, returns
49 # only those rows from IMAGE where the uploading user is not a mem-
50 # ber of a group which has the 'bot' permission set.
51 $ug = $dbr->tableName( 'user_groups' );
52 $hidebotsql = " LEFT JOIN $ug ON img_user=ug_user AND ($isbotmember)";
53 }
54 }
55
56 $image = $dbr->tableName( 'image' );
57
58 $sql = "SELECT img_timestamp from $image";
59 if ($hidebotsql) {
60 $sql .= "$hidebotsql WHERE ug_group IS NULL";
61 }
62 $sql .= ' ORDER BY img_timestamp DESC';
63 $sql = $dbr->limitResult($sql, 1, false);
64 $res = $dbr->query( $sql, __FUNCTION__ );
65 $row = $dbr->fetchRow( $res );
66 if( $row !== false ) {
67 $ts = $row[0];
68 } else {
69 $ts = false;
70 }
71
72 # If we were clever, we'd use this to cache.
73 $latestTimestamp = wfTimestamp( TS_MW, $ts );
74
75 # Hardcode this for now.
76 $limit = 48;
77
78 if ( $parval = intval( $par ) ) {
79 if ( $parval <= $limit && $parval > 0 ) {
80 $limit = $parval;
81 }
82 }
83
84 $where = array();
85 $searchpar = array();
86 if ( $wpIlMatch != '' && !$wgMiserMode) {
87 $nt = Title::newFromURL( $wpIlMatch );
88 if( $nt ) {
89 $where[] = 'LOWER(img_name) ' . $dbr->buildLike( $dbr->anyString(), strtolower( $nt->getDBkey() ), $dbr->anyString() );
90 $searchpar['wpIlMatch'] = $wpIlMatch;
91 }
92 }
93
94 $invertSort = false;
95 if( $until = $wgRequest->getVal( 'until' ) ) {
96 $where[] = "img_timestamp < '" . $dbr->timestamp( $until ) . "'";
97 }
98 if( $from = $wgRequest->getVal( 'from' ) ) {
99 $where[] = "img_timestamp >= '" . $dbr->timestamp( $from ) . "'";
100 $invertSort = true;
101 }
102 $sql = 'SELECT img_size, img_name, img_user, img_user_text,'.
103 "img_description,img_timestamp FROM $image";
104
105 if( $hidebotsql ) {
106 $sql .= $hidebotsql;
107 $where[] = 'ug_group IS NULL';
108 }
109 if( count( $where ) ) {
110 $sql .= ' WHERE ' . $dbr->makeList( $where, LIST_AND );
111 }
112 $sql.=' ORDER BY img_timestamp '. ( $invertSort ? '' : ' DESC' );
113 $sql = $dbr->limitResult($sql, ( $limit + 1 ), false);
114 $res = $dbr->query( $sql, __FUNCTION__ );
115
116 /**
117 * We have to flip things around to get the last N after a certain date
118 */
119 $images = array();
120 foreach ( $res as $s ) {
121 if( $invertSort ) {
122 array_unshift( $images, $s );
123 } else {
124 array_push( $images, $s );
125 }
126 }
127
128 $gallery = new ImageGallery();
129 $firstTimestamp = null;
130 $lastTimestamp = null;
131 $shownImages = 0;
132 foreach( $images as $s ) {
133 $shownImages++;
134 if( $shownImages > $limit ) {
135 # One extra just to test for whether to show a page link;
136 # don't actually show it.
137 break;
138 }
139
140 $name = $s->img_name;
141 $ut = $s->img_user_text;
142
143 $nt = Title::newFromText( $name, NS_FILE );
144 $ul = $sk->link( Title::makeTitle( NS_USER, $ut ), $ut );
145
146 $gallery->add( $nt, "$ul<br />\n<i>".htmlspecialchars($wgLang->timeanddate( $s->img_timestamp, true ))."</i><br />\n" );
147
148 $timestamp = wfTimestamp( TS_MW, $s->img_timestamp );
149 if( empty( $firstTimestamp ) ) {
150 $firstTimestamp = $timestamp;
151 }
152 $lastTimestamp = $timestamp;
153 }
154
155 $titleObj = SpecialPage::getTitleFor( 'Newimages' );
156 $action = $titleObj->getLocalURL( $hidebots ? '' : 'hidebots=0' );
157 if ( $shownav && !$wgMiserMode ) {
158 $wgOut->addHTML(
159 Xml::openElement( 'form', array( 'action' => $action, 'method' => 'post', 'id' => 'imagesearch' ) ) .
160 Xml::fieldset( wfMsg( 'newimages-legend' ) ) .
161 Xml::inputLabel( wfMsg( 'newimages-label' ), 'wpIlMatch', 'wpIlMatch', 20, $wpIlMatch ) . ' ' .
162 Xml::submitButton( wfMsg( 'ilsubmit' ), array( 'name' => 'wpIlSubmit' ) ) .
163 Xml::closeElement( 'fieldset' ) .
164 Xml::closeElement( 'form' )
165 );
166 }
167
168 $bydate = wfMsg( 'bydate' );
169 $lt = $wgLang->formatNum( min( $shownImages, $limit ) );
170 if ( $shownav ) {
171 $text = wfMsgExt( 'imagelisttext', array('parse'), $lt, $bydate );
172 $wgOut->addHTML( $text . "\n" );
173 }
174
175 /**
176 * Paging controls...
177 */
178
179 # If we change bot visibility, this needs to be carried along.
180 if( !$hidebots ) {
181 $botpar = array( 'hidebots' => 0 );
182 } else {
183 $botpar = array();
184 }
185 $now = wfTimestampNow();
186 $d = $wgLang->date( $now, true );
187 $t = $wgLang->time( $now, true );
188 $query = array_merge(
189 array( 'from' => $now ),
190 $botpar,
191 $searchpar
192 );
193
194 $dateLink = $sk->linkKnown(
195 $titleObj,
196 htmlspecialchars( wfMsg( 'sp-newimages-showfrom', $d, $t ) ),
197 array(),
198 $query
199 );
200
201 $query = array_merge(
202 array( 'hidebots' => ( $hidebots ? 0 : 1 ) ),
203 $searchpar
204 );
205
206 $showhide = $hidebots ? wfMsg( 'show' ) : wfMsg( 'hide' );
207
208 $botLink = $sk->linkKnown(
209 $titleObj,
210 htmlspecialchars( wfMsg( 'showhidebots', $showhide ) ),
211 array(),
212 $query
213 );
214
215 $opts = array( 'parsemag', 'escapenoentities' );
216 $prevLink = wfMsgExt( 'pager-newer-n', $opts, $wgLang->formatNum( $limit ) );
217 if( $firstTimestamp && $firstTimestamp != $latestTimestamp ) {
218 $query = array_merge(
219 array( 'from' => $firstTimestamp ),
220 $botpar,
221 $searchpar
222 );
223
224 $prevLink = $sk->linkKnown(
225 $titleObj,
226 $prevLink,
227 array(),
228 $query
229 );
230 }
231
232 $nextLink = wfMsgExt( 'pager-older-n', $opts, $wgLang->formatNum( $limit ) );
233 if( $invertSort || ( $shownImages > $limit && $lastTimestamp ) ) {
234 $query = array_merge(
235 array( 'until' => ( $lastTimestamp ? $lastTimestamp : "" ) ),
236 $botpar,
237 $searchpar
238 );
239
240 $nextLink = $sk->linkKnown(
241 $titleObj,
242 $nextLink,
243 array(),
244 $query
245 );
246
247 }
248
249 $prevnext = '<p>' . $botLink . ' '. wfMsgHtml( 'viewprevnext', $prevLink, $nextLink, $dateLink ) .'</p>';
250
251 if ($shownav)
252 $wgOut->addHTML( $prevnext );
253
254 if( count( $images ) ) {
255 $wgOut->addHTML( $gallery->toHTML() );
256 if ($shownav)
257 $wgOut->addHTML( $prevnext );
258 } else {
259 $wgOut->addWikiMsg( 'noimages' );
260 }
261 }