Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 276
0.00% covered (danger)
0.00%
0 / 15
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialNewPages
0.00% covered (danger)
0.00%
0 / 275
0.00% covered (danger)
0.00%
0 / 15
2970
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 setup
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 1
12
 parseParams
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
210
 execute
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
30
 filterLinks
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 1
72
 form
0.00% covered (danger)
0.00%
0 / 82
0.00% covered (danger)
0.00%
0 / 1
20
 getNewPagesPager
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 feed
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
30
 feedTitle
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 feedItem
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
 feedItemAuthor
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 feedItemDesc
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
30
 canAnonymousUsersCreatePages
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 getGroupName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCacheTTL
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Implements Special:Newpages
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24namespace MediaWiki\Specials;
25
26use HtmlArmor;
27use MediaWiki\Cache\LinkBatchFactory;
28use MediaWiki\ChangeTags\ChangeTagsStore;
29use MediaWiki\CommentFormatter\RowCommentFormatter;
30use MediaWiki\Content\IContentHandlerFactory;
31use MediaWiki\Feed\FeedItem;
32use MediaWiki\Html\FormOptions;
33use MediaWiki\Html\Html;
34use MediaWiki\HTMLForm\HTMLForm;
35use MediaWiki\MainConfigNames;
36use MediaWiki\Pager\NewPagesPager;
37use MediaWiki\Permissions\GroupPermissionsLookup;
38use MediaWiki\Revision\RevisionLookup;
39use MediaWiki\Revision\SlotRecord;
40use MediaWiki\SpecialPage\IncludableSpecialPage;
41use MediaWiki\Title\NamespaceInfo;
42use MediaWiki\Title\Title;
43use MediaWiki\User\Options\UserOptionsLookup;
44
45/**
46 * A special page that list newly created pages
47 *
48 * @ingroup SpecialPage
49 */
50class SpecialNewPages extends IncludableSpecialPage {
51    /**
52     * @var FormOptions
53     */
54    protected $opts;
55    /** @var array[] */
56    protected $customFilters;
57
58    protected $showNavigation = false;
59
60    private LinkBatchFactory $linkBatchFactory;
61    private IContentHandlerFactory $contentHandlerFactory;
62    private GroupPermissionsLookup $groupPermissionsLookup;
63    private RevisionLookup $revisionLookup;
64    private NamespaceInfo $namespaceInfo;
65    private UserOptionsLookup $userOptionsLookup;
66    private RowCommentFormatter $rowCommentFormatter;
67    private ChangeTagsStore $changeTagsStore;
68
69    /**
70     * @param LinkBatchFactory $linkBatchFactory
71     * @param IContentHandlerFactory $contentHandlerFactory
72     * @param GroupPermissionsLookup $groupPermissionsLookup
73     * @param RevisionLookup $revisionLookup
74     * @param NamespaceInfo $namespaceInfo
75     * @param UserOptionsLookup $userOptionsLookup
76     * @param RowCommentFormatter $rowCommentFormatter
77     */
78    public function __construct(
79        LinkBatchFactory $linkBatchFactory,
80        IContentHandlerFactory $contentHandlerFactory,
81        GroupPermissionsLookup $groupPermissionsLookup,
82        RevisionLookup $revisionLookup,
83        NamespaceInfo $namespaceInfo,
84        UserOptionsLookup $userOptionsLookup,
85        RowCommentFormatter $rowCommentFormatter,
86        ChangeTagsStore $changeTagsStore
87    ) {
88        parent::__construct( 'Newpages' );
89        $this->linkBatchFactory = $linkBatchFactory;
90        $this->contentHandlerFactory = $contentHandlerFactory;
91        $this->groupPermissionsLookup = $groupPermissionsLookup;
92        $this->revisionLookup = $revisionLookup;
93        $this->namespaceInfo = $namespaceInfo;
94        $this->userOptionsLookup = $userOptionsLookup;
95        $this->rowCommentFormatter = $rowCommentFormatter;
96        $this->changeTagsStore = $changeTagsStore;
97    }
98
99    /**
100     * @param string|null $par
101     */
102    protected function setup( $par ) {
103        $opts = new FormOptions();
104        $this->opts = $opts; // bind
105        $opts->add( 'hideliu', false );
106        $opts->add(
107            'hidepatrolled',
108            $this->userOptionsLookup->getBoolOption( $this->getUser(), 'newpageshidepatrolled' )
109        );
110        $opts->add( 'hidebots', false );
111        $opts->add( 'hideredirs', true );
112        $opts->add(
113            'limit',
114            $this->userOptionsLookup->getIntOption( $this->getUser(), 'rclimit' )
115        );
116        $opts->add( 'offset', '' );
117        $opts->add( 'namespace', '0' );
118        $opts->add( 'username', '' );
119        $opts->add( 'feed', '' );
120        $opts->add( 'tagfilter', '' );
121        $opts->add( 'tagInvert', false );
122        $opts->add( 'invert', false );
123        $opts->add( 'associated', false );
124        $opts->add( 'size-mode', 'max' );
125        $opts->add( 'size', 0 );
126
127        $this->customFilters = [];
128        $this->getHookRunner()->onSpecialNewPagesFilters( $this, $this->customFilters );
129        // @phan-suppress-next-line PhanEmptyForeach False positive
130        foreach ( $this->customFilters as $key => $params ) {
131            $opts->add( $key, $params['default'] );
132        }
133
134        $opts->fetchValuesFromRequest( $this->getRequest() );
135        if ( $par ) {
136            $this->parseParams( $par );
137        }
138
139        $opts->validateIntBounds( 'limit', 0, 5000 );
140    }
141
142    /**
143     * @param string $par
144     */
145    protected function parseParams( $par ) {
146        $bits = preg_split( '/\s*,\s*/', trim( $par ) );
147        foreach ( $bits as $bit ) {
148            $m = [];
149            if ( $bit === 'shownav' ) {
150                $this->showNavigation = true;
151            } elseif ( $bit === 'hideliu' ) {
152                $this->opts->setValue( 'hideliu', true );
153            } elseif ( $bit === 'hidepatrolled' ) {
154                $this->opts->setValue( 'hidepatrolled', true );
155            } elseif ( $bit === 'hidebots' ) {
156                $this->opts->setValue( 'hidebots', true );
157            } elseif ( $bit === 'showredirs' ) {
158                $this->opts->setValue( 'hideredirs', false );
159            } elseif ( is_numeric( $bit ) ) {
160                $this->opts->setValue( 'limit', intval( $bit ) );
161            } elseif ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
162                $this->opts->setValue( 'limit', intval( $m[1] ) );
163            } elseif ( preg_match( '/^offset=([^=]+)$/', $bit, $m ) ) {
164                // PG offsets not just digits!
165                $this->opts->setValue( 'offset', intval( $m[1] ) );
166            } elseif ( preg_match( '/^username=(.*)$/', $bit, $m ) ) {
167                $this->opts->setValue( 'username', $m[1] );
168            } elseif ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
169                $ns = $this->getLanguage()->getNsIndex( $m[1] );
170                if ( $ns !== false ) {
171                    $this->opts->setValue( 'namespace', $ns );
172                }
173            } else {
174                // T62424 try to interpret unrecognized parameters as a namespace
175                $ns = $this->getLanguage()->getNsIndex( $bit );
176                if ( $ns !== false ) {
177                    $this->opts->setValue( 'namespace', $ns );
178                }
179            }
180        }
181    }
182
183    /**
184     * Show a form for filtering namespace and username
185     *
186     * @param string|null $par
187     */
188    public function execute( $par ) {
189        $out = $this->getOutput();
190
191        $this->setHeaders();
192        $this->outputHeader();
193
194        $this->showNavigation = !$this->including(); // Maybe changed in setup
195        $this->setup( $par );
196
197        $this->addHelpLink( 'Help:New pages' );
198
199        if ( !$this->including() ) {
200            // Settings
201            $this->form();
202
203            $feedType = $this->opts->getValue( 'feed' );
204            if ( $feedType ) {
205                $this->feed( $feedType );
206
207                return;
208            }
209
210            $allValues = $this->opts->getAllValues();
211            unset( $allValues['feed'] );
212            $out->setFeedAppendQuery( wfArrayToCgi( $allValues ) );
213        }
214
215        $pager = $this->getNewPagesPager();
216        $pager->mLimit = $this->opts->getValue( 'limit' );
217        $pager->mOffset = $this->opts->getValue( 'offset' );
218
219        if ( $pager->getNumRows() ) {
220            $navigation = '';
221            if ( $this->showNavigation ) {
222                $navigation = $pager->getNavigationBar();
223            }
224            $out->addHTML( $navigation . $pager->getBody() . $navigation );
225            // Add styles for change tags
226            $out->addModuleStyles( 'mediawiki.interface.helpers.styles' );
227        } else {
228            $out->addWikiMsg( 'specialpage-empty' );
229        }
230    }
231
232    protected function filterLinks() {
233        // show/hide links
234        $showhide = [ $this->msg( 'show' )->escaped(), $this->msg( 'hide' )->escaped() ];
235
236        // Option value -> message mapping
237        $filters = [
238            'hideliu' => 'newpages-showhide-registered',
239            'hidepatrolled' => 'newpages-showhide-patrolled',
240            'hidebots' => 'newpages-showhide-bots',
241            'hideredirs' => 'newpages-showhide-redirect'
242        ];
243        foreach ( $this->customFilters as $key => $params ) {
244            $filters[$key] = $params['msg'];
245        }
246
247        // Disable some if needed
248        if ( !$this->canAnonymousUsersCreatePages() ) {
249            unset( $filters['hideliu'] );
250        }
251        if ( !$this->getUser()->useNPPatrol() ) {
252            unset( $filters['hidepatrolled'] );
253        }
254
255        $links = [];
256        $changed = $this->opts->getChangedValues();
257        unset( $changed['offset'] ); // Reset offset if query type changes
258
259        // wfArrayToCgi(), called from LinkRenderer/Title, will not output null and false values
260        // to the URL, which would omit some options (T158504). Fix it by explicitly setting them
261        // to 0 or 1.
262        // Also do this only for boolean options, not eg. namespace or tagfilter
263        foreach ( $changed as $key => $value ) {
264            if ( array_key_exists( $key, $filters ) ) {
265                $changed[$key] = $changed[$key] ? '1' : '0';
266            }
267        }
268
269        $self = $this->getPageTitle();
270        $linkRenderer = $this->getLinkRenderer();
271        foreach ( $filters as $key => $msg ) {
272            $onoff = 1 - $this->opts->getValue( $key );
273            $link = $linkRenderer->makeLink(
274                $self,
275                new HtmlArmor( $showhide[$onoff] ),
276                [],
277                [ $key => $onoff ] + $changed
278            );
279            $links[$key] = $this->msg( $msg )->rawParams( $link )->escaped();
280        }
281
282        return $this->getLanguage()->pipeList( $links );
283    }
284
285    protected function form() {
286        $out = $this->getOutput();
287
288        // Consume values
289        $this->opts->consumeValue( 'offset' ); // don't carry offset, DWIW
290        $namespace = $this->opts->consumeValue( 'namespace' );
291        $username = $this->opts->consumeValue( 'username' );
292        $tagFilterVal = $this->opts->consumeValue( 'tagfilter' );
293        $tagInvertVal = $this->opts->consumeValue( 'tagInvert' );
294        $nsinvert = $this->opts->consumeValue( 'invert' );
295        $nsassociated = $this->opts->consumeValue( 'associated' );
296
297        $size = $this->opts->consumeValue( 'size' );
298        $max = $this->opts->consumeValue( 'size-mode' ) === 'max';
299
300        // Check username input validity
301        $ut = Title::makeTitleSafe( NS_USER, $username );
302        $userText = $ut ? $ut->getText() : '';
303
304        $formDescriptor = [
305            'namespace' => [
306                'type' => 'namespaceselect',
307                'name' => 'namespace',
308                'label-message' => 'namespace',
309                'default' => $namespace,
310            ],
311            'nsinvert' => [
312                'type' => 'check',
313                'name' => 'invert',
314                'label-message' => 'invert',
315                'default' => $nsinvert,
316                'tooltip' => 'invert',
317            ],
318            'nsassociated' => [
319                'type' => 'check',
320                'name' => 'associated',
321                'label-message' => 'namespace_association',
322                'default' => $nsassociated,
323                'tooltip' => 'namespace_association',
324            ],
325            'tagFilter' => [
326                'type' => 'tagfilter',
327                'name' => 'tagfilter',
328                'label-message' => 'tag-filter',
329                'default' => $tagFilterVal,
330            ],
331            'tagInvert' => [
332                'type' => 'check',
333                'name' => 'tagInvert',
334                'label-message' => 'invert',
335                'hide-if' => [ '===', 'tagFilter', '' ],
336                'default' => $tagInvertVal,
337            ],
338            'username' => [
339                'type' => 'user',
340                'name' => 'username',
341                'label-message' => 'newpages-username',
342                'default' => $userText,
343                'id' => 'mw-np-username',
344                'size' => 30,
345            ],
346            'size' => [
347                'type' => 'sizefilter',
348                'name' => 'size',
349                'default' => ( $max ? -1 : 1 ) * $size,
350            ],
351        ];
352
353        $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() );
354
355        // Store query values in hidden fields so that form submission doesn't lose them
356        foreach ( $this->opts->getUnconsumedValues() as $key => $value ) {
357            $htmlForm->addHiddenField( $key, $value );
358        }
359
360        $htmlForm
361            ->setMethod( 'get' )
362            ->setFormIdentifier( 'newpagesform' )
363            // The form should be visible on each request (inclusive requests with submitted forms), so
364            // return always false here.
365            ->setSubmitCallback(
366                static function () {
367                    return false;
368                }
369            )
370            ->setSubmitTextMsg( 'newpages-submit' )
371            ->setWrapperLegendMsg( 'newpages' )
372            ->addFooterHtml( Html::rawElement(
373                'div',
374                [],
375                $this->filterLinks()
376            ) )
377            ->show();
378        $out->addModuleStyles( 'mediawiki.special' );
379    }
380
381    private function getNewPagesPager() {
382        return new NewPagesPager(
383            $this->getContext(),
384            $this->getLinkRenderer(),
385            $this->groupPermissionsLookup,
386            $this->getHookContainer(),
387            $this->linkBatchFactory,
388            $this->namespaceInfo,
389            $this->changeTagsStore,
390            $this->rowCommentFormatter,
391            $this->contentHandlerFactory,
392            $this->opts,
393        );
394    }
395
396    /**
397     * Output a subscription feed listing recent edits to this page.
398     *
399     * @param string $type
400     */
401    protected function feed( $type ) {
402        if ( !$this->getConfig()->get( MainConfigNames::Feed ) ) {
403            $this->getOutput()->addWikiMsg( 'feed-unavailable' );
404
405            return;
406        }
407
408        $feedClasses = $this->getConfig()->get( MainConfigNames::FeedClasses );
409        if ( !isset( $feedClasses[$type] ) ) {
410            $this->getOutput()->addWikiMsg( 'feed-invalid' );
411
412            return;
413        }
414
415        $feed = new $feedClasses[$type](
416            $this->feedTitle(),
417            $this->msg( 'tagline' )->text(),
418            $this->getPageTitle()->getFullURL()
419        );
420
421        $pager = $this->getNewPagesPager();
422        $limit = $this->opts->getValue( 'limit' );
423        $pager->mLimit = min( $limit, $this->getConfig()->get( MainConfigNames::FeedLimit ) );
424
425        $feed->outHeader();
426        if ( $pager->getNumRows() > 0 ) {
427            foreach ( $pager->mResult as $row ) {
428                $feed->outItem( $this->feedItem( $row ) );
429            }
430        }
431        $feed->outFooter();
432    }
433
434    protected function feedTitle() {
435        $desc = $this->getDescription()->text();
436        $code = $this->getConfig()->get( MainConfigNames::LanguageCode );
437        $sitename = $this->getConfig()->get( MainConfigNames::Sitename );
438
439        return "$sitename - $desc [$code]";
440    }
441
442    protected function feedItem( $row ) {
443        $title = Title::makeTitle( intval( $row->rc_namespace ), $row->rc_title );
444        if ( $title ) {
445            $date = $row->rc_timestamp;
446            $comments = $title->getTalkPage()->getFullURL();
447
448            return new FeedItem(
449                $title->getPrefixedText(),
450                $this->feedItemDesc( $row ),
451                $title->getFullURL(),
452                $date,
453                $this->feedItemAuthor( $row ),
454                $comments
455            );
456        } else {
457            return null;
458        }
459    }
460
461    protected function feedItemAuthor( $row ) {
462        return $row->rc_user_text ?? '';
463    }
464
465    protected function feedItemDesc( $row ) {
466        $revisionRecord = $this->revisionLookup->getRevisionById( $row->rev_id );
467        if ( !$revisionRecord ) {
468            return '';
469        }
470
471        $content = $revisionRecord->getContent( SlotRecord::MAIN );
472        if ( $content === null ) {
473            return '';
474        }
475
476        // XXX: include content model/type in feed item?
477        $revUser = $revisionRecord->getUser();
478        $revUserText = $revUser ? $revUser->getName() : '';
479        $revComment = $revisionRecord->getComment();
480        $revCommentText = $revComment ? $revComment->text : '';
481        return '<p>' . htmlspecialchars( $revUserText ) .
482            $this->msg( 'colon-separator' )->inContentLanguage()->escaped() .
483            htmlspecialchars( FeedItem::stripComment( $revCommentText ) ) .
484            "</p>\n<hr />\n<div>" .
485            nl2br( htmlspecialchars( $content->serialize() ) ) . "</div>";
486    }
487
488    private function canAnonymousUsersCreatePages() {
489        return $this->groupPermissionsLookup->groupHasPermission( '*', 'createpage' ) ||
490            $this->groupPermissionsLookup->groupHasPermission( '*', 'createtalk' );
491    }
492
493    protected function getGroupName() {
494        return 'changes';
495    }
496
497    protected function getCacheTTL() {
498        return 60 * 5;
499    }
500}
501
502/**
503 * Retain the old class name for backwards compatibility.
504 * @deprecated since 1.41
505 */
506class_alias( SpecialNewPages::class, 'SpecialNewpages' );