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