wiki.techinc.nl/includes/specials/SpecialNewPages.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

537 lines
16 KiB
PHP
Raw Normal View History

<?php
2010-06-21 12:59:04 +00:00
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2010-06-21 12:59:04 +00:00
* http://www.gnu.org/copyleft/gpl.html
*
* @file
2010-06-21 12:59:04 +00:00
*/
namespace MediaWiki\Specials;
use HtmlArmor;
use MediaWiki\Cache\LinkBatchFactory;
use MediaWiki\ChangeTags\ChangeTagsStore;
use MediaWiki\CommentFormatter\RowCommentFormatter;
use MediaWiki\Content\IContentHandlerFactory;
use MediaWiki\Feed\FeedItem;
use MediaWiki\Html\FormOptions;
use MediaWiki\Html\Html;
use MediaWiki\HTMLForm\HTMLForm;
use MediaWiki\MainConfigNames;
use MediaWiki\Pager\NewPagesPager;
use MediaWiki\Permissions\GroupPermissionsLookup;
use MediaWiki\Revision\RevisionLookup;
use MediaWiki\Revision\SlotRecord;
use MediaWiki\SpecialPage\IncludableSpecialPage;
use MediaWiki\Title\NamespaceInfo;
use MediaWiki\Title\Title;
use MediaWiki\User\Options\UserOptionsLookup;
use MediaWiki\User\TempUser\TempUserConfig;
/**
* List of newly created pages
*
* @see SpecialRecentChanges
* @see SpecialNewFiles
* @ingroup SpecialPage
*/
class SpecialNewPages extends IncludableSpecialPage {
2011-02-21 17:03:18 +00:00
/**
* @var FormOptions
*/
protected $opts;
/** @var array[] */
protected $customFilters;
2011-02-21 17:03:18 +00:00
/** @var bool */
2008-05-11 21:43:16 +00:00
protected $showNavigation = false;
private LinkBatchFactory $linkBatchFactory;
private IContentHandlerFactory $contentHandlerFactory;
private GroupPermissionsLookup $groupPermissionsLookup;
private RevisionLookup $revisionLookup;
private NamespaceInfo $namespaceInfo;
private UserOptionsLookup $userOptionsLookup;
private RowCommentFormatter $rowCommentFormatter;
private ChangeTagsStore $changeTagsStore;
private TempUserConfig $tempUserConfig;
/**
* @param LinkBatchFactory $linkBatchFactory
* @param IContentHandlerFactory $contentHandlerFactory
* @param GroupPermissionsLookup $groupPermissionsLookup
* @param RevisionLookup $revisionLookup
* @param NamespaceInfo $namespaceInfo
* @param UserOptionsLookup $userOptionsLookup
* @param RowCommentFormatter $rowCommentFormatter
* @param ChangeTagsStore $changeTagsStore
* @param TempUserConfig $tempUserConfig
*/
public function __construct(
LinkBatchFactory $linkBatchFactory,
IContentHandlerFactory $contentHandlerFactory,
GroupPermissionsLookup $groupPermissionsLookup,
RevisionLookup $revisionLookup,
NamespaceInfo $namespaceInfo,
UserOptionsLookup $userOptionsLookup,
RowCommentFormatter $rowCommentFormatter,
ChangeTagsStore $changeTagsStore,
TempUserConfig $tempUserConfig
) {
parent::__construct( 'Newpages' );
$this->linkBatchFactory = $linkBatchFactory;
$this->contentHandlerFactory = $contentHandlerFactory;
$this->groupPermissionsLookup = $groupPermissionsLookup;
$this->revisionLookup = $revisionLookup;
$this->namespaceInfo = $namespaceInfo;
$this->userOptionsLookup = $userOptionsLookup;
$this->rowCommentFormatter = $rowCommentFormatter;
$this->changeTagsStore = $changeTagsStore;
$this->tempUserConfig = $tempUserConfig;
}
/**
* @param string|null $par
*/
2008-05-11 21:43:16 +00:00
protected function setup( $par ) {
$opts = new FormOptions();
2008-05-14 05:18:27 +00:00
$this->opts = $opts; // bind
2008-05-11 21:43:16 +00:00
$opts->add( 'hideliu', false );
$opts->add(
'hidepatrolled',
$this->userOptionsLookup->getBoolOption( $this->getUser(), 'newpageshidepatrolled' )
);
$opts->add( 'hidebots', false );
$opts->add( 'hideredirs', true );
$opts->add(
'limit',
$this->userOptionsLookup->getIntOption( $this->getUser(), 'rclimit' )
);
$opts->add( 'offset', '' );
2008-05-11 21:43:16 +00:00
$opts->add( 'namespace', '0' );
$opts->add( 'username', '' );
$opts->add( 'feed', '' );
$opts->add( 'tagfilter', '' );
$opts->add( 'tagInvert', false );
$opts->add( 'invert', false );
$opts->add( 'associated', false );
$opts->add( 'size-mode', 'max' );
$opts->add( 'size', 0 );
2008-05-11 21:43:16 +00:00
$this->customFilters = [];
Hooks::run() call site migration Migrate all callers of Hooks::run() to use the new HookContainer/HookRunner system. General principles: * Use DI if it is already used. We're not changing the way state is managed in this patch. * HookContainer is always injected, not HookRunner. HookContainer is a service, it's a more generic interface, it is the only thing that provides isRegistered() which is needed in some cases, and a HookRunner can be efficiently constructed from it (confirmed by benchmark). Because HookContainer is needed for object construction, it is also needed by all factories. * "Ask your friendly local base class". Big hierarchies like SpecialPage and ApiBase have getHookContainer() and getHookRunner() methods in the base class, and classes that extend that base class are not expected to know or care where the base class gets its HookContainer from. * ProtectedHookAccessorTrait provides protected getHookContainer() and getHookRunner() methods, getting them from the global service container. The point of this is to ease migration to DI by ensuring that call sites ask their local friendly base class rather than getting a HookRunner from the service container directly. * Private $this->hookRunner. In some smaller classes where accessor methods did not seem warranted, there is a private HookRunner property which is accessed directly. Very rarely (two cases), there is a protected property, for consistency with code that conventionally assumes protected=private, but in cases where the class might actually be overridden, a protected accessor is preferred over a protected property. * The last resort: Hooks::runner(). Mostly for static, file-scope and global code. In a few cases it was used for objects with broken construction schemes, out of horror or laziness. Constructors with new required arguments: * AuthManager * BadFileLookup * BlockManager * ClassicInterwikiLookup * ContentHandlerFactory * ContentSecurityPolicy * DefaultOptionsManager * DerivedPageDataUpdater * FullSearchResultWidget * HtmlCacheUpdater * LanguageFactory * LanguageNameUtils * LinkRenderer * LinkRendererFactory * LocalisationCache * MagicWordFactory * MessageCache * NamespaceInfo * PageEditStash * PageHandlerFactory * PageUpdater * ParserFactory * PermissionManager * RevisionStore * RevisionStoreFactory * SearchEngineConfig * SearchEngineFactory * SearchFormWidget * SearchNearMatcher * SessionBackend * SpecialPageFactory * UserNameUtils * UserOptionsManager * WatchedItemQueryService * WatchedItemStore Constructors with new optional arguments: * DefaultPreferencesFactory * Language * LinkHolderArray * MovePage * Parser * ParserCache * PasswordReset * Router setHookContainer() now required after construction: * AuthenticationProvider * ResourceLoaderModule * SearchEngine Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
$this->getHookRunner()->onSpecialNewPagesFilters( $this, $this->customFilters );
// @phan-suppress-next-line PhanEmptyForeach False positive
foreach ( $this->customFilters as $key => $params ) {
$opts->add( $key, $params['default'] );
}
$opts->fetchValuesFromRequest( $this->getRequest() );
if ( $par ) {
$this->parseParams( $par );
}
2008-05-11 21:43:16 +00:00
// The hideliu option is only available when anonymous users can create pages, as if specified when they
// cannot create pages it always would produce no results. Therefore, if anon users cannot create pages
// then set hideliu as false overriding the value provided by the user.
if ( !$this->canAnonymousUsersCreatePages() ) {
$opts->setValue( 'hideliu', false, true );
}
2008-05-11 21:43:16 +00:00
$opts->validateIntBounds( 'limit', 0, 5000 );
}
/**
* @param string $par
*/
2008-05-11 21:43:16 +00:00
protected function parseParams( $par ) {
$bits = preg_split( '/\s*,\s*/', trim( $par ) );
foreach ( $bits as $bit ) {
$m = [];
if ( $bit === 'shownav' ) {
2008-05-11 21:43:16 +00:00
$this->showNavigation = true;
} elseif ( $bit === 'hideliu' ) {
2008-05-11 21:43:16 +00:00
$this->opts->setValue( 'hideliu', true );
} elseif ( $bit === 'hidepatrolled' ) {
2008-05-11 21:43:16 +00:00
$this->opts->setValue( 'hidepatrolled', true );
} elseif ( $bit === 'hidebots' ) {
2008-05-11 21:43:16 +00:00
$this->opts->setValue( 'hidebots', true );
} elseif ( $bit === 'showredirs' ) {
$this->opts->setValue( 'hideredirs', false );
} elseif ( is_numeric( $bit ) ) {
2008-05-11 21:43:16 +00:00
$this->opts->setValue( 'limit', intval( $bit ) );
} elseif ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
$this->opts->setValue( 'limit', intval( $m[1] ) );
} elseif ( preg_match( '/^offset=([^=]+)$/', $bit, $m ) ) {
// PG offsets not just digits!
$this->opts->setValue( 'offset', intval( $m[1] ) );
} elseif ( preg_match( '/^username=(.*)$/', $bit, $m ) ) {
$this->opts->setValue( 'username', $m[1] );
} elseif ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
$ns = $this->getLanguage()->getNsIndex( $m[1] );
if ( $ns !== false ) {
$this->opts->setValue( 'namespace', $ns );
2008-05-11 21:43:16 +00:00
}
} else {
// T62424 try to interpret unrecognized parameters as a namespace
$ns = $this->getLanguage()->getNsIndex( $bit );
if ( $ns !== false ) {
$this->opts->setValue( 'namespace', $ns );
}
2008-05-11 21:43:16 +00:00
}
}
}
/**
* Show a form for filtering namespace and username
*
* @param string|null $par
*/
public function execute( $par ) {
$out = $this->getOutput();
$this->setHeaders();
$this->outputHeader();
$this->showNavigation = !$this->including(); // Maybe changed in setup
2008-05-11 21:43:16 +00:00
$this->setup( $par );
$this->addHelpLink( 'Help:New pages' );
if ( !$this->including() ) {
2008-05-14 05:18:27 +00:00
// Settings
$this->form();
2008-05-11 21:43:16 +00:00
$feedType = $this->opts->getValue( 'feed' );
if ( $feedType ) {
$this->feed( $feedType );
return;
}
$allValues = $this->opts->getAllValues();
unset( $allValues['feed'] );
$out->setFeedAppendQuery( wfArrayToCgi( $allValues ) );
}
$pager = $this->getNewPagesPager();
2008-05-11 21:43:16 +00:00
$pager->mLimit = $this->opts->getValue( 'limit' );
$pager->mOffset = $this->opts->getValue( 'offset' );
if ( $pager->getNumRows() ) {
2008-05-11 21:43:16 +00:00
$navigation = '';
if ( $this->showNavigation ) {
$navigation = $pager->getNavigationBar();
}
$out->addHTML( $navigation . $pager->getBody() . $navigation );
// Add styles for change tags
$out->addModuleStyles( 'mediawiki.interface.helpers.styles' );
2008-05-11 21:43:16 +00:00
} else {
$out->addWikiMsg( 'specialpage-empty' );
}
2008-05-11 21:43:16 +00:00
}
2008-05-11 21:43:16 +00:00
protected function filterLinks() {
// show/hide links
$showhide = [ $this->msg( 'show' )->escaped(), $this->msg( 'hide' )->escaped() ];
2008-05-11 21:43:16 +00:00
// Option value -> message mapping
$filters = [
'hideliu' => 'newpages-showhide-registered',
'hidepatrolled' => 'newpages-showhide-patrolled',
'hidebots' => 'newpages-showhide-bots',
'hideredirs' => 'newpages-showhide-redirect'
];
foreach ( $this->customFilters as $key => $params ) {
$filters[$key] = $params['msg'];
}
2008-05-11 21:43:16 +00:00
// Disable some if needed
if ( !$this->canAnonymousUsersCreatePages() ) {
unset( $filters['hideliu'] );
}
if ( !$this->getUser()->useNPPatrol() ) {
unset( $filters['hidepatrolled'] );
}
$links = [];
2008-05-11 21:43:16 +00:00
$changed = $this->opts->getChangedValues();
unset( $changed['offset'] ); // Reset offset if query type changes
2008-05-11 21:43:16 +00:00
// wfArrayToCgi(), called from LinkRenderer/Title, will not output null and false values
// to the URL, which would omit some options (T158504). Fix it by explicitly setting them
// to 0 or 1.
// Also do this only for boolean options, not eg. namespace or tagfilter
foreach ( $changed as $key => $value ) {
if ( array_key_exists( $key, $filters ) ) {
$changed[$key] = $changed[$key] ? '1' : '0';
}
}
$self = $this->getPageTitle();
$linkRenderer = $this->getLinkRenderer();
2008-05-11 21:43:16 +00:00
foreach ( $filters as $key => $msg ) {
$onoff = 1 - $this->opts->getValue( $key );
$link = $linkRenderer->makeLink(
$self,
new HtmlArmor( $showhide[$onoff] ),
[],
[ $key => $onoff ] + $changed
);
$links[$key] = $this->msg( $msg )->rawParams( $link )->escaped();
}
2008-05-11 21:43:16 +00:00
return $this->getLanguage()->pipeList( $links );
2008-05-11 21:43:16 +00:00
}
protected function form() {
$out = $this->getOutput();
2008-05-11 21:43:16 +00:00
// Consume values
2008-05-11 21:55:43 +00:00
$this->opts->consumeValue( 'offset' ); // don't carry offset, DWIW
2008-05-11 21:43:16 +00:00
$namespace = $this->opts->consumeValue( 'namespace' );
$username = $this->opts->consumeValue( 'username' );
$tagFilterVal = $this->opts->consumeValue( 'tagfilter' );
$tagInvertVal = $this->opts->consumeValue( 'tagInvert' );
$nsinvert = $this->opts->consumeValue( 'invert' );
$nsassociated = $this->opts->consumeValue( 'associated' );
2008-05-11 21:43:16 +00:00
$size = $this->opts->consumeValue( 'size' );
$max = $this->opts->consumeValue( 'size-mode' ) === 'max';
2008-05-11 21:43:16 +00:00
// Check username input validity
$ut = Title::makeTitleSafe( NS_USER, $username );
$userText = $ut ? $ut->getText() : '';
$formDescriptor = [
'namespace' => [
'type' => 'namespaceselect',
'name' => 'namespace',
'label-message' => 'namespace',
'default' => $namespace,
],
'nsinvert' => [
'type' => 'check',
'name' => 'invert',
'label-message' => 'invert',
'default' => $nsinvert,
'tooltip' => 'invert',
],
'nsassociated' => [
'type' => 'check',
'name' => 'associated',
'label-message' => 'namespace_association',
'default' => $nsassociated,
'tooltip' => 'namespace_association',
],
'tagFilter' => [
'type' => 'tagfilter',
'name' => 'tagfilter',
'label-message' => 'tag-filter',
'default' => $tagFilterVal,
],
'tagInvert' => [
'type' => 'check',
'name' => 'tagInvert',
'label-message' => 'invert',
'hide-if' => [ '===', 'tagFilter', '' ],
'default' => $tagInvertVal,
],
'username' => [
'type' => 'user',
'name' => 'username',
'label-message' => 'newpages-username',
'default' => $userText,
'id' => 'mw-np-username',
'size' => 30,
],
'size' => [
'type' => 'sizefilter',
'name' => 'size',
'default' => ( $max ? -1 : 1 ) * $size,
],
];
$htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() );
// Store query values in hidden fields so that form submission doesn't lose them
foreach ( $this->opts->getUnconsumedValues() as $key => $value ) {
$htmlForm->addHiddenField( $key, $value );
}
$htmlForm
->setMethod( 'get' )
->setFormIdentifier( 'newpagesform' )
// The form should be visible on each request (inclusive requests with submitted forms), so
// return always false here.
->setSubmitCallback(
static function () {
return false;
}
)
->setSubmitTextMsg( 'newpages-submit' )
->setWrapperLegendMsg( 'newpages' )
->addFooterHtml( Html::rawElement(
'div',
[],
$this->filterLinks()
) )
->show();
$out->addModuleStyles( 'mediawiki.special' );
2008-05-11 21:43:16 +00:00
}
private function getNewPagesPager() {
return new NewPagesPager(
$this->getContext(),
$this->getLinkRenderer(),
$this->groupPermissionsLookup,
$this->getHookContainer(),
$this->linkBatchFactory,
$this->namespaceInfo,
$this->changeTagsStore,
$this->rowCommentFormatter,
$this->contentHandlerFactory,
$this->tempUserConfig,
$this->opts,
);
}
/**
* Output a subscription feed listing recent edits to this page.
2010-05-04 19:28:13 +00:00
*
* @param string $type
*/
protected function feed( $type ) {
if ( !$this->getConfig()->get( MainConfigNames::Feed ) ) {
$this->getOutput()->addWikiMsg( 'feed-unavailable' );
return;
}
$feedClasses = $this->getConfig()->get( MainConfigNames::FeedClasses );
if ( !isset( $feedClasses[$type] ) ) {
$this->getOutput()->addWikiMsg( 'feed-invalid' );
return;
}
$feed = new $feedClasses[$type](
$this->feedTitle(),
$this->msg( 'tagline' )->text(),
$this->getPageTitle()->getFullURL()
);
$pager = $this->getNewPagesPager();
$limit = $this->opts->getValue( 'limit' );
$pager->mLimit = min( $limit, $this->getConfig()->get( MainConfigNames::FeedLimit ) );
$feed->outHeader();
if ( $pager->getNumRows() > 0 ) {
2011-02-17 16:29:13 +00:00
foreach ( $pager->mResult as $row ) {
$feed->outItem( $this->feedItem( $row ) );
}
}
$feed->outFooter();
}
protected function feedTitle() {
$desc = $this->getDescription()->text();
$code = $this->getConfig()->get( MainConfigNames::LanguageCode );
$sitename = $this->getConfig()->get( MainConfigNames::Sitename );
return "$sitename - $desc [$code]";
}
protected function feedItem( $row ) {
$title = Title::makeTitle( intval( $row->rc_namespace ), $row->rc_title );
if ( $title ) {
$date = $row->rc_timestamp;
$comments = $title->getTalkPage()->getFullURL();
return new FeedItem(
$title->getPrefixedText(),
$this->feedItemDesc( $row ),
$title->getFullURL(),
$date,
$this->feedItemAuthor( $row ),
$comments
);
} else {
return null;
}
}
2008-04-26 04:00:03 +00:00
protected function feedItemAuthor( $row ) {
return $row->rc_user_text ?? '';
}
protected function feedItemDesc( $row ) {
$revisionRecord = $this->revisionLookup->getRevisionById( $row->rev_id );
if ( !$revisionRecord ) {
return '';
}
$content = $revisionRecord->getContent( SlotRecord::MAIN );
if ( $content === null ) {
return '';
}
// XXX: include content model/type in feed item?
$revUser = $revisionRecord->getUser();
$revUserText = $revUser ? $revUser->getName() : '';
$revComment = $revisionRecord->getComment();
$revCommentText = $revComment ? $revComment->text : '';
return '<p>' . htmlspecialchars( $revUserText ) .
$this->msg( 'colon-separator' )->inContentLanguage()->escaped() .
htmlspecialchars( FeedItem::stripComment( $revCommentText ) ) .
"</p>\n<hr />\n<div>" .
nl2br( htmlspecialchars( $content->serialize() ) ) . "</div>";
}
/**
* @return bool Whether any users classed anonymous can create pages (when temporary accounts are enabled, then
* this definition includes temporary accounts).
*/
private function canAnonymousUsersCreatePages(): bool {
// Get all the groups which anon users can be in.
$anonGroups = [ '*' ];
if ( $this->tempUserConfig->isKnown() ) {
$anonGroups[] = 'temp';
}
// Check if any of the groups have the createpage or createtalk right.
foreach ( $anonGroups as $group ) {
$anonUsersCanCreatePages = $this->groupPermissionsLookup->groupHasPermission( $group, 'createpage' ) ||
$this->groupPermissionsLookup->groupHasPermission( $group, 'createtalk' );
if ( $anonUsersCanCreatePages ) {
return true;
}
}
return false;
}
protected function getGroupName() {
return 'changes';
}
protected function getCacheTTL() {
return 60 * 5;
}
}
/**
* Retain the old class name for backwards compatibility.
* @deprecated since 1.41
*/
class_alias( SpecialNewPages::class, 'SpecialNewpages' );