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