9d55f0bebe544426d46e576c1131af5e08f1a89b
[lhc/web/wiklou.git] / includes / SpecialNewimages.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 require_once( 'ImageGallery.php' );
9
10 /**
11 *
12 */
13 function wfSpecialNewimages() {
14 global $wgUser, $wgOut, $wgLang, $wgContLang, $wgRequest;
15
16 $wpIlMatch = $wgRequest->getText( 'wpIlMatch' );
17 $dbr =& wfGetDB( DB_SLAVE );
18 $sk = $wgUser->getSkin();
19
20 /** If we were clever, we'd use this to cache. */
21 $latestTimestamp = wfTimestamp( TS_MW, $dbr->selectField(
22 'image', 'img_timestamp',
23 '', 'wfSpecialNewimages',
24 array( 'ORDER BY' => 'img_timestamp DESC',
25 'LIMIT' => 1 ) ) );
26
27 /** Hardcode this for now. */
28 $limit = 48;
29
30 $where = array();
31 if ( !empty( $wpIlMatch ) ) {
32 $nt = Title::newFromUrl( $wpIlMatch );
33 if($nt ) {
34 $m = $dbr->strencode( strtolower( $nt->getDBkey() ) );
35 $m = str_replace( '%', "\\%", $m );
36 $m = str_replace( '_', "\\_", $m );
37 $where[] = "LCASE(img_name) LIKE '%{$m}%'";
38 }
39 }
40
41 $invertSort = false;
42 if( $until = $wgRequest->getVal( 'until' ) ) {
43 $where[] = 'img_timestamp < ' . $dbr->timestamp( $until );
44 }
45 if( $from = $wgRequest->getVal( 'from' ) ) {
46 $where[] = 'img_timestamp >= ' . $dbr->timestamp( $from );
47 $invertSort = true;
48 }
49
50 $res = $dbr->select( 'image',
51 array( 'img_size', 'img_name', 'img_user', 'img_user_text',
52 'img_description', 'img_timestamp' ),
53 $where,
54 'wfSpecialNewimages',
55 array( 'LIMIT' => $limit + 1,
56 'ORDER BY' => 'img_timestamp' . ( $invertSort ? '' : ' DESC' ) )
57 );
58
59 /**
60 * We have to flip things around to get the last N after a certain date
61 */
62 $images = array();
63 while ( $s = $dbr->fetchObject( $res ) ) {
64 if( $invertSort ) {
65 array_unshift( $images, $s );
66 } else {
67 array_push( $images, $s );
68 }
69 }
70 $dbr->freeResult( $res );
71
72 $gallery = new ImageGallery();
73 $firstTimestamp = null;
74 $lastTimestamp = null;
75 $shownImages = 0;
76 foreach( $images as $s ) {
77 if( ++$shownImages > $limit ) {
78 # One extra just to test for whether to show a page link;
79 # don't actually show it.
80 break;
81 }
82
83 $name = $s->img_name;
84 $ut = $s->img_user_text;
85
86 $nt = Title::newFromText( $name, NS_IMAGE );
87 $img = Image::newFromTitle( $nt );
88 $ul = $sk->makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
89
90 $gallery->add( $img, $ul.'<br /><i>'.$wgLang->timeanddate( $s->img_timestamp, true ).'</i><br />' );
91
92 $timestamp = wfTImestamp( TS_MW, $s->img_timestamp );
93 if( empty( $firstTimestamp ) ) {
94 $firstTimestamp = $timestamp;
95 }
96 $lastTimestamp = $timestamp;
97 }
98
99 $bydate = wfMsg( 'bydate' );
100 $lt = $wgLang->formatNum( min( $shownImages, $limit ) );
101 $text = wfMsg( "imagelisttext",
102 "<strong>{$lt}</strong>", "<strong>{$bydate}</strong>" );
103 $wgOut->addHTML( "<p>{$text}\n</p>" );
104
105 $cap = wfMsg( 'ilshowmatch' );
106 $sub = wfMsg( 'ilsubmit' );
107 $titleObj = Title::makeTitle( NS_SPECIAL, 'Newimages' );
108 $action = $titleObj->escapeLocalURL( "limit={$limit}" );
109
110 $wgOut->addHTML( "<form id=\"imagesearch\" method=\"post\" action=\"" .
111 "{$action}\">" .
112 "{$cap}: <input type='text' size='8' name=\"wpIlMatch\" value=\"" .
113 htmlspecialchars( $wpIlMatch ) . "\" /> " .
114 "<input type='submit' name=\"wpIlSubmit\" value=\"{$sub}\" /></form>" );
115 $here = $wgContLang->specialPage( 'Newimages' );
116
117 /**
118 * Paging controls...
119 */
120 $now = wfTimestamp( TS_MW );
121 $date = $wgLang->timeanddate( $now );
122 $dateLink = $sk->makeKnownLinkObj( $titleObj, wfMsg( 'rclistfrom', $date ), 'from=' . $now );
123
124 $prevLink = wfMsg( 'prevn', $wgLang->formatNum( $limit ) );
125 if( $firstTimestamp && $firstTimestamp != $latestTimestamp ) {
126 $prevLink = $sk->makeKnownLinkObj( $titleObj, $prevLink, 'from=' . $firstTimestamp );
127 }
128
129 $nextLink = wfMsg( 'nextn', $wgLang->formatNum( $limit ) );
130 if( $shownImages > $limit && $lastTimestamp ) {
131 $nextLink = $sk->makeKnownLinkObj( $titleObj, $nextLink, 'until=' . $lastTimestamp );
132 }
133
134 $prevnext = '<p>' . wfMsg( 'viewprevnext', $prevLink, $nextLink, $dateLink ) . '</p>';
135 $wgOut->addHTML( $prevnext );
136
137 if( count( $images ) ) {
138 $wgOut->addHTML( $gallery->toHTML() );
139 $wgOut->addHTML( $prevnext );
140 } else {
141 $wgOut->addWikiText( wfMsg( 'noimages' ) );
142 }
143 }
144
145 ?>