w/s diff. mostly eol w/s and using only tabs of width 4 to indent.
[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 $parval = intval( $par );
78 if ( $parval ) {
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 $until = $wgRequest->getVal( 'until' );
96 if( $until ) {
97 $where[] = "img_timestamp < '" . $dbr->timestamp( $until ) . "'";
98 }
99 $from = $wgRequest->getVal( 'from' );
100 if( $from ) {
101 $where[] = "img_timestamp >= '" . $dbr->timestamp( $from ) . "'";
102 $invertSort = true;
103 }
104 $sql = 'SELECT img_size, img_name, img_user, img_user_text,'.
105 "img_description,img_timestamp FROM $image";
106
107 if( $hidebotsql ) {
108 $sql .= $hidebotsql;
109 $where[] = 'ug_group IS NULL';
110 }
111 if( count( $where ) ) {
112 $sql .= ' WHERE ' . $dbr->makeList( $where, LIST_AND );
113 }
114 $sql.=' ORDER BY img_timestamp '. ( $invertSort ? '' : ' DESC' );
115 $sql = $dbr->limitResult($sql, ( $limit + 1 ), false);
116 $res = $dbr->query( $sql, __FUNCTION__ );
117
118 /**
119 * We have to flip things around to get the last N after a certain date
120 */
121 $images = array();
122 foreach ( $res as $s ) {
123 if( $invertSort ) {
124 array_unshift( $images, $s );
125 } else {
126 array_push( $images, $s );
127 }
128 }
129
130 $gallery = new ImageGallery();
131 $firstTimestamp = null;
132 $lastTimestamp = null;
133 $shownImages = 0;
134 foreach( $images as $s ) {
135 $shownImages++;
136 if( $shownImages > $limit ) {
137 # One extra just to test for whether to show a page link;
138 # don't actually show it.
139 break;
140 }
141
142 $name = $s->img_name;
143 $ut = $s->img_user_text;
144
145 $nt = Title::newFromText( $name, NS_FILE );
146 $ul = $sk->link( Title::makeTitle( NS_USER, $ut ), $ut );
147
148 $gallery->add( $nt, "$ul<br />\n<i>".htmlspecialchars($wgLang->timeanddate( $s->img_timestamp, true ))."</i><br />\n" );
149
150 $timestamp = wfTimestamp( TS_MW, $s->img_timestamp );
151 if( empty( $firstTimestamp ) ) {
152 $firstTimestamp = $timestamp;
153 }
154 $lastTimestamp = $timestamp;
155 }
156
157 $titleObj = SpecialPage::getTitleFor( 'Newimages' );
158 $action = $titleObj->getLocalURL( $hidebots ? '' : 'hidebots=0' );
159 if ( $shownav && !$wgMiserMode ) {
160 $wgOut->addHTML(
161 Xml::openElement( 'form', array( 'action' => $action, 'method' => 'post', 'id' => 'imagesearch' ) ) .
162 Xml::fieldset( wfMsg( 'newimages-legend' ) ) .
163 Xml::inputLabel( wfMsg( 'newimages-label' ), 'wpIlMatch', 'wpIlMatch', 20, $wpIlMatch ) . ' ' .
164 Xml::submitButton( wfMsg( 'ilsubmit' ), array( 'name' => 'wpIlSubmit' ) ) .
165 Xml::closeElement( 'fieldset' ) .
166 Xml::closeElement( 'form' )
167 );
168 }
169
170 $bydate = wfMsg( 'bydate' );
171 $lt = $wgLang->formatNum( min( $shownImages, $limit ) );
172 if ( $shownav ) {
173 $text = wfMsgExt( 'imagelisttext', array('parse'), $lt, $bydate );
174 $wgOut->addHTML( $text . "\n" );
175 }
176
177 /**
178 * Paging controls...
179 */
180
181 # If we change bot visibility, this needs to be carried along.
182 if( !$hidebots ) {
183 $botpar = array( 'hidebots' => 0 );
184 } else {
185 $botpar = array();
186 }
187 $now = wfTimestampNow();
188 $d = $wgLang->date( $now, true );
189 $t = $wgLang->time( $now, true );
190 $query = array_merge(
191 array( 'from' => $now ),
192 $botpar,
193 $searchpar
194 );
195
196 $dateLink = $sk->linkKnown(
197 $titleObj,
198 htmlspecialchars( wfMsg( 'sp-newimages-showfrom', $d, $t ) ),
199 array(),
200 $query
201 );
202
203 $query = array_merge(
204 array( 'hidebots' => ( $hidebots ? 0 : 1 ) ),
205 $searchpar
206 );
207
208 $showhide = $hidebots ? wfMsg( 'show' ) : wfMsg( 'hide' );
209
210 $botLink = $sk->linkKnown(
211 $titleObj,
212 htmlspecialchars( wfMsg( 'showhidebots', $showhide ) ),
213 array(),
214 $query
215 );
216
217 $opts = array( 'parsemag', 'escapenoentities' );
218 $prevLink = wfMsgExt( 'pager-newer-n', $opts, $wgLang->formatNum( $limit ) );
219 if( $firstTimestamp && $firstTimestamp != $latestTimestamp ) {
220 $query = array_merge(
221 array( 'from' => $firstTimestamp ),
222 $botpar,
223 $searchpar
224 );
225
226 $prevLink = $sk->linkKnown(
227 $titleObj,
228 $prevLink,
229 array(),
230 $query
231 );
232 }
233
234 $nextLink = wfMsgExt( 'pager-older-n', $opts, $wgLang->formatNum( $limit ) );
235 if( $invertSort || ( $shownImages > $limit && $lastTimestamp ) ) {
236 $query = array_merge(
237 array( 'until' => ( $lastTimestamp ? $lastTimestamp : "" ) ),
238 $botpar,
239 $searchpar
240 );
241
242 $nextLink = $sk->linkKnown(
243 $titleObj,
244 $nextLink,
245 array(),
246 $query
247 );
248
249 }
250
251 $prevnext = '<p>' . $botLink . ' '. wfMsgHtml( 'viewprevnext', $prevLink, $nextLink, $dateLink ) .'</p>';
252
253 if ($shownav)
254 $wgOut->addHTML( $prevnext );
255
256 if( count( $images ) ) {
257 $wgOut->addHTML( $gallery->toHTML() );
258 if ($shownav)
259 $wgOut->addHTML( $prevnext );
260 } else {
261 $wgOut->addWikiMsg( 'noimages' );
262 }
263 }