* (bug 7405) Make Linker methods static. Patch by Dan Li.
[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 $wgOut, $wgLang, $wgContLang, $wgRequest, $wgGroupPermissions;
13
14 $wpIlMatch = $wgRequest->getText( 'wpIlMatch' );
15 $dbr =& wfGetDB( DB_SLAVE );
16 $shownav = !$specialPage->including();
17 $hidebots = $wgRequest->getBool('hidebots',1);
18
19 $hidebotsql = '';
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
32 /* If not bot groups, do not set $hidebotsql */
33 if ($botconds) {
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 $hidebotsql = " LEFT OUTER JOIN $ug ON img_user=ug_user AND ($isbotmember)";
43 }
44 }
45
46 $image = $dbr->tableName('image');
47
48 $sql="SELECT img_timestamp from $image";
49 if ($hidebotsql) {
50 $sql .= "$hidebotsql 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 if ( $parval = intval( $par ) )
70 if ( $parval <= $limit && $parval > 0 )
71 $limit = $parval;
72
73 $where = array();
74 $searchpar = '';
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 $searchpar = '&wpIlMatch=' . urlencode( $wpIlMatch );
83 }
84 }
85
86 $invertSort = false;
87 if( $until = $wgRequest->getVal( 'until' ) ) {
88 $where[] = 'img_timestamp < ' . $dbr->timestamp( $until );
89 }
90 if( $from = $wgRequest->getVal( 'from' ) ) {
91 $where[] = 'img_timestamp >= ' . $dbr->timestamp( $from );
92 $invertSort = true;
93 }
94 $sql='SELECT img_size, img_name, img_user, img_user_text,'.
95 "img_description,img_timestamp FROM $image";
96
97 if($hidebotsql) {
98 $sql .= $hidebotsql;
99 $where[]='ug_group IS NULL';
100 }
101 if(count($where)) {
102 $sql.=' WHERE '.$dbr->makeList($where, LIST_AND);
103 }
104 $sql.=' ORDER BY img_timestamp '. ( $invertSort ? '' : ' DESC' );
105 $sql.=' LIMIT '.($limit+1);
106 $res = $dbr->query($sql, 'wfSpecialNewImages');
107
108 /**
109 * We have to flip things around to get the last N after a certain date
110 */
111 $images = array();
112 while ( $s = $dbr->fetchObject( $res ) ) {
113 if( $invertSort ) {
114 array_unshift( $images, $s );
115 } else {
116 array_push( $images, $s );
117 }
118 }
119 $dbr->freeResult( $res );
120
121 $gallery = new ImageGallery();
122 $firstTimestamp = null;
123 $lastTimestamp = null;
124 $shownImages = 0;
125 foreach( $images as $s ) {
126 if( ++$shownImages > $limit ) {
127 # One extra just to test for whether to show a page link;
128 # don't actually show it.
129 break;
130 }
131
132 $name = $s->img_name;
133 $ut = $s->img_user_text;
134
135 $nt = Title::newFromText( $name, NS_IMAGE );
136 $img = new Image( $nt );
137 $ul = Linker::makeLinkObj( Title::makeTitle( NS_USER, $ut ), $ut );
138
139 $gallery->add( $img, "$ul<br />\n<i>".$wgLang->timeanddate( $s->img_timestamp, true )."</i><br />\n" );
140
141 $timestamp = wfTimestamp( TS_MW, $s->img_timestamp );
142 if( empty( $firstTimestamp ) ) {
143 $firstTimestamp = $timestamp;
144 }
145 $lastTimestamp = $timestamp;
146 }
147
148 $bydate = wfMsg( 'bydate' );
149 $lt = $wgLang->formatNum( min( $shownImages, $limit ) );
150 if ($shownav) {
151 $text = wfMsgExt( 'imagelisttext', array('parse'), $lt, $bydate );
152 $wgOut->addHTML( $text . "\n" );
153 }
154
155 $sub = wfMsg( 'ilsubmit' );
156 $titleObj = SpecialPage::getTitleFor( 'Newimages' );
157 $action = $titleObj->escapeLocalURL( $hidebots ? '' : 'hidebots=0' );
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 = Linker::makeKnownLinkObj( $titleObj, wfMsg( 'sp-newimages-showfrom', $date ), 'from='.$now.$botpar.$searchpar );
180
181 $botLink = Linker::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 = Linker::makeKnownLinkObj( $titleObj, $prevLink, 'from=' . $firstTimestamp . $botpar . $searchpar );
186 }
187
188 $nextLink = wfMsg( 'nextn', $wgLang->formatNum( $limit ) );
189 if( $shownImages > $limit && $lastTimestamp ) {
190 $nextLink = Linker::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 ?>