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