* Wont work
[lhc/web/wiklou.git] / includes / SpecialNewpages.php
1 <?php
2 /**
3 *
4 * @addtogroup SpecialPage
5 */
6
7 /**
8 * implements Special:Newpages
9 * @addtogroup SpecialPage
10 */
11 class NewPagesPage extends QueryPage {
12
13 var $namespace;
14 var $username = '';
15 var $hideliu;
16
17 function NewPagesPage( $namespace = NS_MAIN, $username = '', $hideliu= false ) {
18 $this->namespace = $namespace;
19 $this->username = $username;
20 $this->hideliu = $hideliu;
21 }
22
23 function getName() {
24 return 'Newpages';
25 }
26
27 function isExpensive() {
28 # Indexed on RC, and will *not* work with querycache yet.
29 return false;
30 }
31
32 function makeUserWhere( &$dbo ) {
33 global $wgGroupPermissions;
34 if ($wgGroupPermissions['*']['createpage'] == true && $this->hideliu) {
35 return ' AND rc_user = 0';
36 } else {
37 $title = Title::makeTitleSafe( NS_USER, $this->username );
38 if( $title ) {
39 return ' AND rc_user_text = ' . $dbo->addQuotes( $title->getText() );
40 } else {
41 return '';
42 }
43 }
44 }
45
46 private function makeNamespaceWhere() {
47 return $this->namespace !== 'all'
48 ? ' AND rc_namespace = ' . intval( $this->namespace )
49 : '';
50 }
51
52 function getSQL() {
53 global $wgUser, $wgUseNPPatrol;
54 $usepatrol = ( $wgUseNPPatrol && $wgUser->isAllowed( 'patrol' ) ) ? 1 : 0;
55 $dbr = wfGetDB( DB_SLAVE );
56 list( $recentchanges, $page ) = $dbr->tableNamesN( 'recentchanges', 'page' );
57
58 $nsfilter = $this->makeNamespaceWhere();
59 $uwhere = $this->makeUserWhere( $dbr );
60
61 # FIXME: text will break with compression
62 return
63 "SELECT 'Newpages' as type,
64 rc_namespace AS namespace,
65 rc_title AS title,
66 rc_cur_id AS cur_id,
67 rc_user AS \"user\",
68 rc_user_text AS user_text,
69 rc_comment as \"comment\",
70 rc_timestamp AS timestamp,
71 rc_timestamp AS value,
72 '{$usepatrol}' as usepatrol,
73 rc_patrolled AS patrolled,
74 rc_id AS rcid,
75 page_len as length,
76 page_latest as rev_id
77 FROM $recentchanges,$page
78 WHERE rc_cur_id=page_id AND rc_new=1
79 {$nsfilter}
80 AND page_is_redirect = 0
81 {$uwhere}";
82 }
83
84 function preprocessResults( &$dbo, &$res ) {
85 # Do a batch existence check on the user and talk pages
86 $linkBatch = new LinkBatch();
87 while( $row = $dbo->fetchObject( $res ) ) {
88 $linkBatch->addObj( Title::makeTitleSafe( NS_USER, $row->user_text ) );
89 $linkBatch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_text ) );
90 }
91 $linkBatch->execute();
92 # Seek to start
93 if( $dbo->numRows( $res ) > 0 )
94 $dbo->dataSeek( $res, 0 );
95 }
96
97 /**
98 * Format a row, providing the timestamp, links to the page/history, size, user links, and a comment
99 *
100 * @param $skin Skin to use
101 * @param $result Result row
102 * @return string
103 */
104 function formatResult( $skin, $result ) {
105 global $wgLang, $wgContLang;
106 $dm = $wgContLang->getDirMark();
107
108 $title = Title::makeTitleSafe( $result->namespace, $result->title );
109 $time = $wgLang->timeAndDate( $result->timestamp, true );
110 $plink = $skin->makeKnownLinkObj( $title, '', $this->patrollable( $result ) ? 'rcid=' . $result->rcid : '' );
111 $hist = $skin->makeKnownLinkObj( $title, wfMsgHtml( 'hist' ), 'action=history' );
112 $length = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ), $wgLang->formatNum( htmlspecialchars( $result->length ) ) );
113 $ulink = $skin->userLink( $result->user, $result->user_text ) . ' ' . $skin->userToolLinks( $result->user, $result->user_text );
114 $comment = $skin->commentBlock( $result->comment );
115
116 return "{$time} {$dm}{$plink} ({$hist}) {$dm}[{$length}] {$dm}{$ulink} {$comment}";
117 }
118
119 /**
120 * Should a specific result row provide "patrollable" links?
121 *
122 * @param $result Result row
123 * @return bool
124 */
125 function patrollable( $result ) {
126 global $wgUser, $wgUseNPPatrol;
127 return $wgUseNPPatrol && $wgUser->isAllowed( 'patrol' ) && !$result->patrolled;
128 }
129
130 function feedItemDesc( $row ) {
131 if( isset( $row->rev_id ) ) {
132 $revision = Revision::newFromId( $row->rev_id );
133 if( $revision ) {
134 return '<p>' . htmlspecialchars( wfMsg( 'summary' ) ) . ': ' .
135 htmlspecialchars( $revision->getComment() ) . "</p>\n<hr />\n<div>" .
136 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
137 }
138 }
139 return parent::feedItemDesc( $row );
140 }
141
142 /**
143 * Show a form for filtering namespace and username
144 *
145 * @return string
146 */
147 function getPageHeader() {
148 global $wgScript, $wgContLang, $wgGroupPermissions;
149 $align = $wgContLang->isRTL() ? 'left' : 'right';
150 $self = SpecialPage::getTitleFor( $this->getName() );
151 $form = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
152 Xml::hidden( 'title', $self->getPrefixedDBkey() ) .
153 Xml::openElement( 'table' ) .
154 "<tr>
155 <td align=\"$align\">" .
156 Xml::label( wfMsg( 'namespace' ), 'namespace' ) .
157 "</td>
158 <td>" .
159 Xml::namespaceSelector( intval( $this->namespace ), 'all' ) .
160 "</td>
161 </tr>
162 <tr>
163 <td align=\"$align\">" .
164 Xml::label( wfMsg( 'newpages-username' ), 'mw-np-username' ) .
165 "</td>
166 <td>" .
167 Xml::input( 'username', 30, $this->username, array( 'id' => 'mw-np-username' ) ) .
168 "</td>
169 </tr>";
170 if ($wgGroupPermissions['*']['createpage'] == true) {
171 $form = $form . "<tr><td></td>
172 <td colspan=\"2\">" . Xml::checkLabel( wfMsgHtml( 'rcshowhideliu', wfMsg( 'hide' ) ),
173 'hideliu', 'hideliu', $this->hideliu, array( 'id' => 'mw-np-hideliu' ) ) . "
174 </td></tr>";
175 }
176 $form = $form . "
177 <tr> <td></td>
178 <td>" .
179 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
180 "</td>
181 </tr>" .
182 Xml::closeElement( 'table' ) .
183 Xml::hidden( 'offset', $this->offset ) .
184 Xml::hidden( 'limit', $this->limit ) .
185 Xml::closeElement( 'form' );
186 return $form;
187 }
188
189 /**
190 * Link parameters
191 *
192 * @return array
193 */
194 function linkParameters() {
195 return( array( 'namespace' => $this->namespace, 'username' => $this->username, 'hideliu' => $this->hideliu ) );
196 }
197
198 }
199
200 /**
201 * constructor
202 */
203 function wfSpecialNewpages($par, $specialPage) {
204 global $wgRequest, $wgContLang;
205
206 list( $limit, $offset ) = wfCheckLimits();
207 $namespace = NS_MAIN;
208 $username = '';
209 $hideliu = false;
210
211 if ( $par ) {
212 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
213 foreach ( $bits as $bit ) {
214 if ( 'shownav' == $bit )
215 $shownavigation = true;
216 if ( 'hideliu' == $bit )
217 $hideliu = true;
218 if ( is_numeric( $bit ) )
219 $limit = $bit;
220
221 $m = array();
222 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) )
223 $limit = intval($m[1]);
224 if ( preg_match( '/^offset=(\d+)$/', $bit, $m ) )
225 $offset = intval($m[1]);
226 if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
227 $ns = $wgContLang->getNsIndex( $m[1] );
228 if( $ns !== false ) {
229 $namespace = $ns;
230 }
231 }
232 }
233 } else {
234 if( $ns = $wgRequest->getText( 'namespace', NS_MAIN ) )
235 $namespace = $ns;
236 if( $un = $wgRequest->getText( 'username' ) )
237 $username = $un;
238 if( $hliu = $wgRequest->getBool( 'hideliu' ) )
239 $hideliu = $hliu;
240
241 }
242
243 if ( ! isset( $shownavigation ) )
244 $shownavigation = ! $specialPage->including();
245
246 $npp = new NewPagesPage( $namespace, $username, $hideliu );
247
248 if ( ! $npp->doFeed( $wgRequest->getVal( 'feed' ), $limit ) )
249 $npp->doQuery( $offset, $limit, $shownavigation );
250 }