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