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