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