followup to r67856: don't pass null as a query parameter value, even if it seems...
[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';
44 $sql = $dbr->limitResult($sql, 1, false);
45 $res = $dbr->query( $sql, __FUNCTION__ );
46 $row = $dbr->fetchRow( $res );
47 if( $row !== false ) {
48 $ts = $row[0];
49 } else {
50 $ts = false;
51 }
52 $dbr->freeResult( $res );
53 $sql = '';
54
55 # If we were clever, we'd use this to cache.
56 $latestTimestamp = wfTimestamp( TS_MW, $ts );
57
58 # Hardcode this for now.
59 $limit = 48;
60
61 if ( $parval = intval( $par ) ) {
62 if ( $parval <= $limit && $parval > 0 ) {
63 $limit = $parval;
64 }
65 }
66
67 $where = array();
68 $searchpar = array();
69 if ( $wpIlMatch != '' && !$wgMiserMode) {
70 $nt = Title::newFromURL( $wpIlMatch );
71 if( $nt ) {
72 $where[] = 'LOWER(img_name) ' . $dbr->buildLike( $dbr->anyString(), strtolower( $nt->getDBkey() ), $dbr->anyString() );
73 $searchpar['wpIlMatch'] = $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 = $dbr->limitResult($sql, ( $limit + 1 ), false);
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->link( Title::makeTitle( NS_USER, $ut ), $ut );
129
130 $gallery->add( $nt, "$ul<br />\n<i>".htmlspecialchars($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 = array( 'hidebots' => 0 );
166 } else {
167 $botpar = array();
168 }
169 $now = wfTimestampNow();
170 $d = $wgLang->date( $now, true );
171 $t = $wgLang->time( $now, true );
172 $query = array_merge(
173 array( 'from' => $now ),
174 $botpar,
175 $searchpar
176 );
177
178 $dateLink = $sk->linkKnown(
179 $titleObj,
180 htmlspecialchars( wfMsg( 'sp-newimages-showfrom', $d, $t ) ),
181 array(),
182 $query
183 );
184
185 $query = array_merge(
186 array( 'hidebots' => ( $hidebots ? 0 : 1 ) ),
187 $searchpar
188 );
189
190 $showhide = $hidebots ? wfMsg( 'show' ) : wfMsg( 'hide' );
191
192 $botLink = $sk->linkKnown(
193 $titleObj,
194 htmlspecialchars( wfMsg( 'showhidebots', $showhide ) ),
195 array(),
196 $query
197 );
198
199 $opts = array( 'parsemag', 'escapenoentities' );
200 $prevLink = wfMsgExt( 'pager-newer-n', $opts, $wgLang->formatNum( $limit ) );
201 if( $firstTimestamp && $firstTimestamp != $latestTimestamp ) {
202 $query = array_merge(
203 array( 'from' => $firstTimestamp ),
204 $botpar,
205 $searchpar
206 );
207
208 $prevLink = $sk->linkKnown(
209 $titleObj,
210 $prevLink,
211 array(),
212 $query
213 );
214 }
215
216 $nextLink = wfMsgExt( 'pager-older-n', $opts, $wgLang->formatNum( $limit ) );
217 if( $invertSort || ( $shownImages > $limit && $lastTimestamp ) ) {
218 $query = array_merge(
219 array( 'until' => ( $lastTimestamp ? $lastTimestamp : "" ) ),
220 $botpar,
221 $searchpar
222 );
223
224 $nextLink = $sk->linkKnown(
225 $titleObj,
226 $nextLink,
227 array(),
228 $query
229 );
230
231 }
232
233 $prevnext = '<p>' . $botLink . ' '. wfMsgHtml( 'viewprevnext', $prevLink, $nextLink, $dateLink ) .'</p>';
234
235 if ($shownav)
236 $wgOut->addHTML( $prevnext );
237
238 if( count( $images ) ) {
239 $wgOut->addHTML( $gallery->toHTML() );
240 if ($shownav)
241 $wgOut->addHTML( $prevnext );
242 } else {
243 $wgOut->addWikiMsg( 'noimages' );
244 }
245 }