Use Doxygen @addtogroup instead of phpdoc @package && @subpackage
[lhc/web/wiklou.git] / includes / SpecialNewimages.php
1 <?php
2 /**
3 *
4 * @addtogroup SpecialPage
5 */
6
7 /**
8 *
9 */
10 function wfSpecialNewimages( $par, $specialPage ) {
11 global $wgUser, $wgOut, $wgLang, $wgRequest, $wgGroupPermissions;
12
13 $wpIlMatch = $wgRequest->getText( 'wpIlMatch' );
14 $dbr =& wfGetDB( DB_SLAVE );
15 $sk = $wgUser->getSkin();
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 }
74
75 $where = array();
76 $searchpar = '';
77 if ( $wpIlMatch != '' ) {
78 $nt = Title::newFromUrl( $wpIlMatch );
79 if($nt ) {
80 $m = $dbr->strencode( strtolower( $nt->getDBkey() ) );
81 $m = str_replace( '%', "\\%", $m );
82 $m = str_replace( '_', "\\_", $m );
83 $where[] = "LCASE(img_name) LIKE '%{$m}%'";
84 $searchpar = '&wpIlMatch=' . urlencode( $wpIlMatch );
85 }
86 }
87
88 $invertSort = false;
89 if( $until = $wgRequest->getVal( 'until' ) ) {
90 $where[] = 'img_timestamp < ' . $dbr->timestamp( $until );
91 }
92 if( $from = $wgRequest->getVal( 'from' ) ) {
93 $where[] = 'img_timestamp >= ' . $dbr->timestamp( $from );
94 $invertSort = true;
95 }
96 $sql='SELECT img_size, img_name, img_user, img_user_text,'.
97 "img_description,img_timestamp FROM $image";
98
99 if($hidebotsql) {
100 $sql .= $hidebotsql;
101 $where[]='ug_group IS NULL';
102 }
103 if(count($where)) {
104 $sql.=' WHERE '.$dbr->makeList($where, LIST_AND);
105 }
106 $sql.=' ORDER BY img_timestamp '. ( $invertSort ? '' : ' DESC' );
107 $sql.=' LIMIT '.($limit+1);
108 $res = $dbr->query($sql, 'wfSpecialNewImages');
109
110 /**
111 * We have to flip things around to get the last N after a certain date
112 */
113 $images = array();
114 while ( $s = $dbr->fetchObject( $res ) ) {
115 if( $invertSort ) {
116 array_unshift( $images, $s );
117 } else {
118 array_push( $images, $s );
119 }
120 }
121 $dbr->freeResult( $res );
122
123 $gallery = new ImageGallery();
124 $firstTimestamp = null;
125 $lastTimestamp = null;
126 $shownImages = 0;
127 foreach( $images as $s ) {
128 if( ++$shownImages > $limit ) {
129 # One extra just to test for whether to show a page link;
130 # don't actually show it.
131 break;
132 }
133
134 $name = $s->img_name;
135 $ut = $s->img_user_text;
136
137 $nt = Title::newFromText( $name, NS_IMAGE );
138 $img = new Image( $nt );
139 $ul = $sk->makeLinkObj( Title::makeTitle( NS_USER, $ut ), $ut );
140
141 $gallery->add( $img, "$ul<br />\n<i>".$wgLang->timeanddate( $s->img_timestamp, true )."</i><br />\n" );
142
143 $timestamp = wfTimestamp( TS_MW, $s->img_timestamp );
144 if( empty( $firstTimestamp ) ) {
145 $firstTimestamp = $timestamp;
146 }
147 $lastTimestamp = $timestamp;
148 }
149
150 $bydate = wfMsg( 'bydate' );
151 $lt = $wgLang->formatNum( min( $shownImages, $limit ) );
152 if ($shownav) {
153 $text = wfMsgExt( 'imagelisttext', array('parse'), $lt, $bydate );
154 $wgOut->addHTML( $text . "\n" );
155 }
156
157 $sub = wfMsg( 'ilsubmit' );
158 $titleObj = SpecialPage::getTitleFor( 'Newimages' );
159 $action = $titleObj->escapeLocalURL( $hidebots ? '' : 'hidebots=0' );
160 if ($shownav) {
161 $wgOut->addHTML( "<form id=\"imagesearch\" method=\"post\" action=\"" .
162 "{$action}\">" .
163 Xml::input( 'wpIlMatch', 20, $wpIlMatch ) . ' ' .
164 Xml::submitButton( $sub, array( 'name' => 'wpIlSubmit' ) ) .
165 "</form>" );
166 }
167
168 /**
169 * Paging controls...
170 */
171
172 # If we change bot visibility, this needs to be carried along.
173 if(!$hidebots) {
174 $botpar='&hidebots=0';
175 } else {
176 $botpar='';
177 }
178 $now = wfTimestampNow();
179 $date = $wgLang->timeanddate( $now, true );
180 $dateLink = $sk->makeKnownLinkObj( $titleObj, wfMsgHtml( 'sp-newimages-showfrom', $date ), 'from='.$now.$botpar.$searchpar );
181
182 $botLink = $sk->makeKnownLinkObj($titleObj, wfMsgHtml( 'showhidebots', ($hidebots ? wfMsgHtml('show') : wfMsgHtml('hide'))),'hidebots='.($hidebots ? '0' : '1').$searchpar);
183
184 $prevLink = wfMsgHtml( 'prevn', $wgLang->formatNum( $limit ) );
185 if( $firstTimestamp && $firstTimestamp != $latestTimestamp ) {
186 $prevLink = $sk->makeKnownLinkObj( $titleObj, $prevLink, 'from=' . $firstTimestamp . $botpar . $searchpar );
187 }
188
189 $nextLink = wfMsgHtml( 'nextn', $wgLang->formatNum( $limit ) );
190 if( $shownImages > $limit && $lastTimestamp ) {
191 $nextLink = $sk->makeKnownLinkObj( $titleObj, $nextLink, 'until=' . $lastTimestamp.$botpar.$searchpar );
192 }
193
194 $prevnext = '<p>' . $botLink . ' '. wfMsgHtml( 'viewprevnext', $prevLink, $nextLink, $dateLink ) .'</p>';
195
196 if ($shownav)
197 $wgOut->addHTML( $prevnext );
198
199 if( count( $images ) ) {
200 $wgOut->addHTML( $gallery->toHTML() );
201 if ($shownav)
202 $wgOut->addHTML( $prevnext );
203 } else {
204 $wgOut->addWikiText( wfMsg( 'noimages' ) );
205 }
206 }
207
208 ?>