* (bug 6448) Allow filtering of Special:Newpages according to username
[lhc/web/wiklou.git] / includes / SpecialNewpages.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /**
9 *
10 * @package MediaWiki
11 * @subpackage SpecialPage
12 */
13 class NewPagesPage extends QueryPage {
14
15 var $namespace;
16 var $username = '';
17
18 function NewPagesPage( $namespace = NS_MAIN, $username = '' ) {
19 $this->namespace = $namespace;
20 $this->username = $username;
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 return $this->username ? ' AND rc_user_text = ' . $dbo->addQuotes( $this->username ) : '';
34 }
35
36 function getSQL() {
37 global $wgUser, $wgUseRCPatrol;
38 $usepatrol = ( $wgUseRCPatrol && $wgUser->isAllowed( 'patrol' ) ) ? 1 : 0;
39 $dbr =& wfGetDB( DB_SLAVE );
40 extract( $dbr->tableNames( 'recentchanges', 'page', 'text' ) );
41
42 $uwhere = $this->makeUserWhere( $dbr );
43
44 # FIXME: text will break with compression
45 return
46 "SELECT 'Newpages' as type,
47 rc_namespace AS namespace,
48 rc_title AS title,
49 rc_cur_id AS cur_id,
50 rc_user AS user,
51 rc_user_text AS user_text,
52 rc_comment as comment,
53 rc_timestamp AS timestamp,
54 rc_timestamp AS value,
55 '{$usepatrol}' as usepatrol,
56 rc_patrolled AS patrolled,
57 rc_id AS rcid,
58 page_len as length,
59 page_latest as rev_id
60 FROM $recentchanges,$page
61 WHERE rc_cur_id=page_id AND rc_new=1
62 AND rc_namespace=" . $this->namespace . " AND page_is_redirect=0
63 {$uwhere}";
64 }
65
66 function preprocessResults( &$dbo, &$res ) {
67 # Do a batch existence check on the user and talk pages
68 $linkBatch = new LinkBatch();
69 while( $row = $dbo->fetchObject( $res ) ) {
70 $linkBatch->addObj( Title::makeTitleSafe( NS_USER, $row->user_text ) );
71 $linkBatch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_text ) );
72 }
73 $linkBatch->execute();
74 # Seek to start
75 if( $dbo->numRows( $res ) > 0 )
76 $dbo->dataSeek( $res, 0 );
77 }
78
79 /**
80 * Format a row, providing the timestamp, links to the page/history, size, user links, and a comment
81 *
82 * @param $skin Skin to use
83 * @param $result Result row
84 * @return string
85 */
86 function formatResult( $skin, $result ) {
87 global $wgLang, $wgContLang;
88 $dm = $wgContLang->getDirMark();
89
90 $title = Title::makeTitleSafe( $result->namespace, $result->title );
91 $time = $wgLang->timeAndDate( $result->timestamp, true );
92 $plink = $skin->makeKnownLinkObj( $title, '', $this->patrollable( $result ) ? 'rcid=' . $result->rcid : '' );
93 $hist = $skin->makeKnownLinkObj( $title, wfMsgHtml( 'hist' ), 'action=history' );
94 $length = wfMsgHtml( 'nbytes', $wgLang->formatNum( htmlspecialchars( $result->length ) ) );
95 $ulink = $skin->userLink( $result->user, $result->user_text ) . $skin->userToolLinks( $result->user, $result->user_text );
96 $comment = $skin->commentBlock( $result->comment );
97
98 return "{$time} {$dm}{$plink} ({$hist}) {$dm}[{$length}] {$dm}{$ulink} {$comment}";
99 }
100
101 /**
102 * Should a specific result row provide "patrollable" links?
103 *
104 * @param $result Result row
105 * @return bool
106 */
107 function patrollable( $result ) {
108 global $wgUser, $wgUseRCPatrol;
109 return $wgUseRCPatrol && $wgUser->isAllowed( 'patrol' ) && !$result->patrolled;
110 }
111
112 function feedItemDesc( $row ) {
113 if( isset( $row->rev_id ) ) {
114 $revision = Revision::newFromId( $row->rev_id );
115 if( $revision ) {
116 return '<p>' . htmlspecialchars( wfMsg( 'summary' ) ) . ': ' .
117 htmlspecialchars( $revision->getComment() ) . "</p>\n<hr />\n<div>" .
118 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
119 }
120 }
121 return parent::feedItemDesc( $row );
122 }
123
124 /**
125 * Show a form for filtering namespace and username
126 *
127 * @return string
128 */
129 function getPageHeader() {
130 $self = Title::makeTitle( NS_SPECIAL, $this->getName() );
131 $form = wfOpenElement( 'form', array( 'method' => 'post', 'action' => $self->getLocalUrl() ) );
132 $form .= '<table><tr><td align="right">' . wfMsgHtml( 'namespace' ) . '</td>';
133 $form .= '<td>' . HtmlNamespaceSelector( $this->namespace ) . '</td><tr>';
134 $form .= '<tr><td align="right">' . wfMsgHtml( 'newpages-username' ) . '</td>';
135 $form .= '<td>' . wfInput( 'username', 30, $this->username ) . '</td></tr>';
136 $form .= '<tr><td></td><td>' . wfSubmitButton( wfMsg( 'allpagessubmit' ) ) . '</td></tr></table>';
137 $form .= wfHidden( 'offset', $this->offset ) . wfHidden( 'limit', $this->limit ) . '</form>';
138 return $form;
139 }
140
141 /**
142 * Link parameters
143 *
144 * @return array
145 */
146 function linkParameters() {
147 return( array( 'namespace' => $this->namespace, 'username' => $this->username ) );
148 }
149
150 }
151
152 /**
153 * constructor
154 */
155 function wfSpecialNewpages($par, $specialPage) {
156 global $wgRequest, $wgContLang;
157
158 list( $limit, $offset ) = wfCheckLimits();
159 $namespace = NS_MAIN;
160 $username = '';
161
162 if ( $par ) {
163 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
164 foreach ( $bits as $bit ) {
165 if ( 'shownav' == $bit )
166 $shownavigation = true;
167 if ( is_numeric( $bit ) )
168 $limit = $bit;
169
170 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) )
171 $limit = intval($m[1]);
172 if ( preg_match( '/^offset=(\d+)$/', $bit, $m ) )
173 $offset = intval($m[1]);
174 if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
175 $ns = $wgContLang->getNsIndex( $m[1] );
176 if( $ns !== false ) {
177 $namespace = $ns;
178 }
179 }
180 }
181 } else {
182 if( $ns = $wgRequest->getInt( 'namespace', 0 ) )
183 $namespace = $ns;
184 if( $un = $wgRequest->getText( 'username' ) )
185 $username = $un;
186 }
187
188 if ( ! isset( $shownavigation ) )
189 $shownavigation = ! $specialPage->including();
190
191 $npp = new NewPagesPage( $namespace, $username );
192
193 if ( ! $npp->doFeed( $wgRequest->getVal( 'feed' ), $limit ) )
194 $npp->doQuery( $offset, $limit, $shownavigation );
195 }
196
197 ?>