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