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