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