Merge "filebackend: avoid use of wfWikiId() in FileBackendGroup"
[lhc/web/wiklou.git] / includes / specials / SpecialContributions.php
index 9d5f430..8f92cd5 100644 (file)
@@ -23,7 +23,6 @@
 
 use MediaWiki\Block\DatabaseBlock;
 use MediaWiki\MediaWikiServices;
-use MediaWiki\Widget\DateInputWidget;
 
 /**
  * Special:Contributions, show user contributions in a paged list
@@ -43,11 +42,16 @@ class SpecialContributions extends IncludableSpecialPage {
                $out = $this->getOutput();
                // Modules required for viewing the list of contributions (also when included on other pages)
                $out->addModuleStyles( [
+                       'jquery.makeCollapsible.styles',
                        'mediawiki.interface.helpers.styles',
                        'mediawiki.special',
                        'mediawiki.special.changeslist',
                ] );
-               $out->addModules( 'mediawiki.special.recentchanges' );
+               $out->addModules( [
+                       'mediawiki.special.recentchanges',
+                       // Certain skins e.g. Minerva might have disabled this module.
+                       'mediawiki.page.ready'
+               ] );
                $this->addHelpLink( 'Help:User contributions' );
 
                $this->opts = [];
@@ -59,7 +63,7 @@ class SpecialContributions extends IncludableSpecialPage {
 
                if ( !strlen( $target ) ) {
                        if ( !$this->including() ) {
-                               $out->addHTML( $this->getForm() );
+                               $out->addHTML( $this->getForm( $this->opts ) );
                        }
 
                        return;
@@ -77,7 +81,7 @@ class SpecialContributions extends IncludableSpecialPage {
                if ( ExternalUserNames::isExternal( $target ) ) {
                        $userObj = User::newFromName( $target, false );
                        if ( !$userObj ) {
-                               $out->addHTML( $this->getForm() );
+                               $out->addHTML( $this->getForm( $this->opts ) );
                                return;
                        }
 
@@ -89,12 +93,12 @@ class SpecialContributions extends IncludableSpecialPage {
                } else {
                        $nt = Title::makeTitleSafe( NS_USER, $target );
                        if ( !$nt ) {
-                               $out->addHTML( $this->getForm() );
+                               $out->addHTML( $this->getForm( $this->opts ) );
                                return;
                        }
                        $userObj = User::newFromName( $nt->getText(), false );
                        if ( !$userObj ) {
-                               $out->addHTML( $this->getForm() );
+                               $out->addHTML( $this->getForm( $this->opts ) );
                                return;
                        }
                        $id = $userObj->getId();
@@ -114,19 +118,31 @@ class SpecialContributions extends IncludableSpecialPage {
                }
 
                $ns = $request->getVal( 'namespace', null );
-               if ( $ns !== null && $ns !== '' ) {
+               if ( $ns !== null && $ns !== '' && $ns !== 'all' ) {
                        $this->opts['namespace'] = intval( $ns );
                } else {
                        $this->opts['namespace'] = '';
                }
 
+               // Backwards compatibility: Before using OOUI form the old HTML form had
+               // fields for nsInvert and associated. These have now been replaced with the
+               // wpFilters query string parameters. These are retained to keep old URIs working.
                $this->opts['associated'] = $request->getBool( 'associated' );
                $this->opts['nsInvert'] = (bool)$request->getVal( 'nsInvert' );
+               $nsFilters = $request->getArray( 'wpfilters', null );
+               if ( $nsFilters !== null ) {
+                       $this->opts['associated'] = in_array( 'associated', $nsFilters );
+                       $this->opts['nsInvert'] = in_array( 'nsInvert', $nsFilters );
+               }
+
                $this->opts['tagfilter'] = (string)$request->getVal( 'tagfilter' );
 
                // Allows reverts to have the bot flag in recent changes. It is just here to
                // be passed in the form at the top of the page
-               if ( $user->isAllowed( 'markbotedits' ) && $request->getBool( 'bot' ) ) {
+               if ( MediaWikiServices::getInstance()
+                                ->getPermissionManager()
+                                ->userHasRight( $user, 'markbotedits' ) && $request->getBool( 'bot' )
+               ) {
                        $this->opts['bot'] = '1';
                }
 
@@ -146,7 +162,7 @@ class SpecialContributions extends IncludableSpecialPage {
                                "<div class=\"mw-negative-namespace-not-supported error\">\n\$1\n</div>",
                                [ 'negative-namespace-not-supported' ]
                        );
-                       $out->addHTML( $this->getForm() );
+                       $out->addHTML( $this->getForm( $this->opts ) );
                        return;
                }
 
@@ -198,9 +214,6 @@ class SpecialContributions extends IncludableSpecialPage {
                $this->addFeedLinks( $feedParams );
 
                if ( Hooks::run( 'SpecialContributionsBeforeMainOutput', [ $id, $userObj, $this ] ) ) {
-                       if ( !$this->including() ) {
-                               $out->addHTML( $this->getForm() );
-                       }
                        $pager = new ContribsPager( $this->getContext(), [
                                'target' => $target,
                                'namespace' => $this->opts['namespace'],
@@ -213,7 +226,10 @@ class SpecialContributions extends IncludableSpecialPage {
                                'hideMinor' => $this->opts['hideMinor'],
                                'nsInvert' => $this->opts['nsInvert'],
                                'associated' => $this->opts['associated'],
-                       ] );
+                       ], $this->getLinkRenderer() );
+                       if ( !$this->including() ) {
+                               $out->addHTML( $this->getForm( $this->opts ) );
+                       }
 
                        if ( IP::isValidRange( $target ) && !$pager->isQueryableRange( $target ) ) {
                                // Valid range, but outside CIDR limit.
@@ -364,6 +380,7 @@ class SpecialContributions extends IncludableSpecialPage {
 
                $linkRenderer = $sp->getLinkRenderer();
 
+               $tools = [];
                # No talk pages for IP ranges.
                if ( !$isRange ) {
                        $tools['user-talk'] = $linkRenderer->makeLink(
@@ -372,7 +389,9 @@ class SpecialContributions extends IncludableSpecialPage {
                        );
                }
 
-               if ( $sp->getUser()->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
+               # Block / Change block / Unblock links
+               $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
+               if ( $permissionManager->userHasRight( $sp->getUser(), 'block' ) ) {
                        if ( $target->getBlock() && $target->getBlock()->getType() != DatabaseBlock::TYPE_AUTO ) {
                                $tools['block'] = $linkRenderer->makeKnownLink( # Change block link
                                        SpecialPage::getTitleFor( 'Block', $username ),
@@ -399,7 +418,7 @@ class SpecialContributions extends IncludableSpecialPage {
                );
 
                # Suppression log link (T61120)
-               if ( $sp->getUser()->isAllowed( 'suppressionlog' ) ) {
+               if ( $permissionManager->userHasRight( $sp->getUser(), 'suppressionlog' ) ) {
                        $tools['log-suppression'] = $linkRenderer->makeKnownLink(
                                SpecialPage::getTitleFor( 'Log', 'suppress' ),
                                $sp->msg( 'sp-contributions-suppresslog', $username )->text(),
@@ -411,7 +430,7 @@ class SpecialContributions extends IncludableSpecialPage {
                # Don't show some links for IP ranges
                if ( !$isRange ) {
                        # Uploads: hide if IPs cannot upload (T220674)
-                       if ( !$isIP || $target->isAllowed( 'upload' ) ) {
+                       if ( !$isIP || $permissionManager->userHasRight( $target, 'upload' ) ) {
                                $tools['uploads'] = $linkRenderer->makeKnownLink(
                                        SpecialPage::getTitleFor( 'Listfiles', $username ),
                                        $sp->msg( 'sp-contributions-uploads' )->text()
@@ -427,7 +446,7 @@ class SpecialContributions extends IncludableSpecialPage {
 
                        # Add link to deleted user contributions for priviledged users
                        # Todo: T183457
-                       if ( $sp->getUser()->isAllowed( 'deletedhistory' ) ) {
+                       if ( $permissionManager->userHasRight( $sp->getUser(), 'deletedhistory' ) ) {
                                $tools['deletedcontribs'] = $linkRenderer->makeKnownLink(
                                        SpecialPage::getTitleFor( 'DeletedContributions', $username ),
                                        $sp->msg( 'sp-contributions-deleted', $username )->text()
@@ -452,52 +471,12 @@ class SpecialContributions extends IncludableSpecialPage {
 
        /**
         * Generates the namespace selector form with hidden attributes.
+        * @param array $pagerOptions with keys contribs, user, deletedOnly, limit, target, topOnly,
+        *  newOnly, hideMinor, namespace, associated, nsInvert, tagfilter, year, start, end
         * @return string HTML fragment
         */
-       protected function getForm() {
+       protected function getForm( array $pagerOptions ) {
                $this->opts['title'] = $this->getPageTitle()->getPrefixedText();
-               if ( !isset( $this->opts['target'] ) ) {
-                       $this->opts['target'] = '';
-               } else {
-                       $this->opts['target'] = str_replace( '_', ' ', $this->opts['target'] );
-               }
-
-               if ( !isset( $this->opts['namespace'] ) ) {
-                       $this->opts['namespace'] = '';
-               }
-
-               if ( !isset( $this->opts['nsInvert'] ) ) {
-                       $this->opts['nsInvert'] = '';
-               }
-
-               if ( !isset( $this->opts['associated'] ) ) {
-                       $this->opts['associated'] = false;
-               }
-
-               if ( !isset( $this->opts['start'] ) ) {
-                       $this->opts['start'] = '';
-               }
-
-               if ( !isset( $this->opts['end'] ) ) {
-                       $this->opts['end'] = '';
-               }
-
-               if ( !isset( $this->opts['tagfilter'] ) ) {
-                       $this->opts['tagfilter'] = '';
-               }
-
-               if ( !isset( $this->opts['topOnly'] ) ) {
-                       $this->opts['topOnly'] = false;
-               }
-
-               if ( !isset( $this->opts['newOnly'] ) ) {
-                       $this->opts['newOnly'] = false;
-               }
-
-               if ( !isset( $this->opts['hideMinor'] ) ) {
-                       $this->opts['hideMinor'] = false;
-               }
-
                // Modules required only for the form
                $this->getOutput()->addModules( [
                        'mediawiki.userSuggest',
@@ -505,15 +484,7 @@ class SpecialContributions extends IncludableSpecialPage {
                ] );
                $this->getOutput()->addModuleStyles( 'mediawiki.widgets.DateInputWidget.styles' );
                $this->getOutput()->enableOOUI();
-
-               $form = Html::openElement(
-                       'form',
-                       [
-                               'method' => 'get',
-                               'action' => wfScript(),
-                               'class' => 'mw-contributions-form'
-                       ]
-               );
+               $fields = [];
 
                # Add hidden params for tracking except for parameters in $skipParameters
                $skipParameters = [
@@ -521,7 +492,6 @@ class SpecialContributions extends IncludableSpecialPage {
                        'nsInvert',
                        'deletedOnly',
                        'target',
-                       'contribs',
                        'year',
                        'month',
                        'start',
@@ -537,208 +507,171 @@ class SpecialContributions extends IncludableSpecialPage {
                        if ( in_array( $name, $skipParameters ) ) {
                                continue;
                        }
-                       $form .= "\t" . Html::hidden( $name, $value ) . "\n";
-               }
 
-               $tagFilter = ChangeTags::buildTagFilterSelector(
-                       $this->opts['tagfilter'], false, $this->getContext() );
-
-               if ( $tagFilter ) {
-                       $filterSelection = Html::rawElement(
-                               'div',
-                               [],
-                               implode( "\u{00A0}", $tagFilter )
-                       );
-               } else {
-                       $filterSelection = Html::rawElement( 'div', [], '' );
-               }
-
-               $labelUsername = Xml::radioLabel(
-                       $this->msg( 'sp-contributions-username' )->text(),
-                       'contribs',
-                       'user',
-                       'user',
-                       true,
-                       [ 'class' => 'mw-input' ]
-               );
-               $input = Html::input(
-                       'target',
-                       $this->opts['target'],
-                       'text',
-                       [
-                               'id' => 'mw-target-user-or-ip',
-                               'size' => '40',
-                               'class' => [
-                                       'mw-input',
-                                       'mw-ui-input-inline',
-                                       'mw-autocomplete-user', // used by mediawiki.userSuggest
-                               ],
-                       ] + (
-                               // Only autofocus if target hasn't been specified
-                               $this->opts['target'] ? [] : [ 'autofocus' => true ]
-                       )
-               );
-
-               $targetSelection = Html::rawElement(
-                       'div',
-                       [],
-                       $labelUsername . ' ' . $input . ' '
-               );
-
-               $hidden = $this->opts['namespace'] === '' ? ' mw-input-hidden' : '';
-               $namespaceSelection = Xml::tags(
-                       'div',
-                       [],
-                       Xml::label(
-                               $this->msg( 'namespace' )->text(),
-                               'namespace'
-                       ) . "\u{00A0}" .
-                       Html::namespaceSelector(
-                               [ 'selected' => $this->opts['namespace'], 'all' => '', 'in-user-lang' => true ],
-                               [
-                                       'name' => 'namespace',
-                                       'id' => 'namespace',
-                                       'class' => 'namespaceselector',
-                               ]
-                       ) . "\u{00A0}" .
-                               Html::rawElement(
-                                       'span',
-                                       [ 'class' => 'mw-input-with-label' . $hidden ],
-                                       Xml::checkLabel(
-                                               $this->msg( 'invert' )->text(),
-                                               'nsInvert',
-                                               'nsinvert',
-                                               $this->opts['nsInvert'],
-                                               [
-                                                       'title' => $this->msg( 'tooltip-invert' )->text(),
-                                                       'class' => 'mw-input'
-                                               ]
-                                       ) . "\u{00A0}"
-                               ) .
-                               Html::rawElement( 'span', [ 'class' => 'mw-input-with-label' . $hidden ],
-                                       Xml::checkLabel(
-                                               $this->msg( 'namespace_association' )->text(),
-                                               'associated',
-                                               'nsassociated',
-                                               $this->opts['associated'],
-                                               [
-                                                       'title' => $this->msg( 'tooltip-namespace_association' )->text(),
-                                                       'class' => 'mw-input'
-                                               ]
-                                       ) . "\u{00A0}"
-                               )
-               );
+                       $fields[$name] = [
+                               'name' => $name,
+                               'type' => 'hidden',
+                               'default' => $value,
+                       ];
+               }
+
+               $target = $this->opts['target'] ?? null;
+               $fields['target'] = [
+                       'type' => 'text',
+                       'cssclass' => 'mw-autocomplete-user mw-ui-input-inline mw-input',
+                       'default' => $target ?
+                               str_replace( '_', ' ', $target ) : '' ,
+                       'label' => $this->msg( 'sp-contributions-username' )->text(),
+                       'name' => 'target',
+                       'id' => 'mw-target-user-or-ip',
+                       'size' => 40,
+                       'autofocus' => !$target,
+                       'section' => 'contribs-top',
+               ];
 
-               $filters = [];
-
-               if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
-                       $filters[] = Html::rawElement(
-                               'span',
-                               [ 'class' => 'mw-input-with-label' ],
-                               Xml::checkLabel(
-                                       $this->msg( 'history-show-deleted' )->text(),
-                                       'deletedOnly',
-                                       'mw-show-deleted-only',
-                                       $this->opts['deletedOnly'],
-                                       [ 'class' => 'mw-input' ]
-                               )
-                       );
-               }
+               $ns = $this->opts['namespace'] ?? null;
+               $fields['namespace'] = [
+                       'type' => 'namespaceselect',
+                       'label' => $this->msg( 'namespace' )->text(),
+                       'name' => 'namespace',
+                       'cssclass' => 'namespaceselector',
+                       'default' => $ns,
+                       'id' => 'namespace',
+                       'section' => 'contribs-top',
+               ];
+               $request = $this->getRequest();
+               $nsFilters = $request->getArray( 'wpfilters' );
+               $fields['nsFilters'] = [
+                       'class' => 'HTMLMultiSelectField',
+                       'label' => '',
+                       'name' => 'wpfilters',
+                       'flatlist' => true,
+                       // Only shown when namespaces are selected.
+                       'cssclass' => $ns === '' ?
+                               'contribs-ns-filters mw-input-with-label mw-input-hidden' :
+                               'contribs-ns-filters mw-input-with-label',
+                       // `contribs-ns-filters` class allows these fields to be toggled on/off by JavaScript.
+                       // See resources/src/mediawiki.special.recentchanges.js
+                       'infusable' => true,
+                       'options' => [
+                               $this->msg( 'invert' )->text() => 'nsInvert',
+                               $this->msg( 'namespace_association' )->text() => 'associated',
+                       ],
+                       'default' => $nsFilters,
+                       'section' => 'contribs-top',
+               ];
+               $fields['tagfilter'] = [
+                       'type' => 'tagfilter',
+                       'cssclass' => 'mw-tagfilter-input',
+                       'id' => 'tagfilter',
+                       'label-message' => [ 'tag-filter', 'parse' ],
+                       'name' => 'tagfilter',
+                       'size' => 20,
+                       'section' => 'contribs-top',
+               ];
 
-               $filters[] = Html::rawElement(
-                       'span',
-                       [ 'class' => 'mw-input-with-label' ],
-                       Xml::checkLabel(
-                               $this->msg( 'sp-contributions-toponly' )->text(),
-                               'topOnly',
-                               'mw-show-top-only',
-                               $this->opts['topOnly'],
-                               [ 'class' => 'mw-input' ]
-                       )
-               );
-               $filters[] = Html::rawElement(
-                       'span',
-                       [ 'class' => 'mw-input-with-label' ],
-                       Xml::checkLabel(
-                               $this->msg( 'sp-contributions-newonly' )->text(),
-                               'newOnly',
-                               'mw-show-new-only',
-                               $this->opts['newOnly'],
-                               [ 'class' => 'mw-input' ]
-                       )
-               );
-               $filters[] = Html::rawElement(
-                       'span',
-                       [ 'class' => 'mw-input-with-label' ],
-                       Xml::checkLabel(
-                               $this->msg( 'sp-contributions-hideminor' )->text(),
-                               'hideMinor',
-                               'mw-hide-minor-edits',
-                               $this->opts['hideMinor'],
-                               [ 'class' => 'mw-input' ]
-                       )
-               );
+               if ( MediaWikiServices::getInstance()
+                       ->getPermissionManager()
+                       ->userHasRight( $this->getUser(), 'deletedhistory' )
+               ) {
+                       $fields['deletedOnly'] = [
+                               'type' => 'check',
+                               'id' => 'mw-show-deleted-only',
+                               'label' => $this->msg( 'history-show-deleted' )->text(),
+                               'name' => 'deletedOnly',
+                               'section' => 'contribs-top',
+                       ];
+               }
+
+               $fields['topOnly'] = [
+                       'type' => 'check',
+                       'id' => 'mw-show-top-only',
+                       'label' => $this->msg( 'sp-contributions-toponly' )->text(),
+                       'name' => 'topOnly',
+                       'section' => 'contribs-top',
+               ];
+               $fields['newOnly'] = [
+                       'type' => 'check',
+                       'id' => 'mw-show-new-only',
+                       'label' => $this->msg( 'sp-contributions-newonly' )->text(),
+                       'name' => 'newOnly',
+                       'section' => 'contribs-top',
+               ];
+               $fields['hideMinor'] = [
+                       'type' => 'check',
+                       'cssclass' => 'mw-hide-minor-edits',
+                       'id' => 'mw-show-new-only',
+                       'label' => $this->msg( 'sp-contributions-hideminor' )->text(),
+                       'name' => 'hideMinor',
+                       'section' => 'contribs-top',
+               ];
 
+               // Allow additions at this point to the filters.
+               $rawFilters = [];
                Hooks::run(
                        'SpecialContributions::getForm::filters',
-                       [ $this, &$filters ]
-               );
-
-               $extraOptions = Html::rawElement(
-                       'div',
-                       [],
-                       implode( '', $filters )
+                       [ $this, &$rawFilters ]
                );
+               foreach ( $rawFilters as $filter ) {
+                       // Backwards compatibility support for previous hook function signature.
+                       if ( is_string( $filter ) ) {
+                               $fields[] = [
+                                       'type' => 'info',
+                                       'default' => $filter,
+                                       'raw' => true,
+                                       'section' => 'contribs-top',
+                               ];
+                               wfDeprecated(
+                                       __METHOD__ .
+                                       ' returning string[]',
+                                       '1.33'
+                               );
+                       } else {
+                               // Preferred append method.
+                               $fields[] = $filter;
+                       }
+               }
 
-               $dateRangeSelection = Html::rawElement(
-                       'div',
-                       [],
-                       Xml::label( $this->msg( 'date-range-from' )->text(), 'mw-date-start' ) . ' ' .
-                       new DateInputWidget( [
-                               'infusable' => true,
-                               'id' => 'mw-date-start',
-                               'name' => 'start',
-                               'value' => $this->opts['start'],
-                               'longDisplayFormat' => true,
-                       ] ) . '<br>' .
-                       Xml::label( $this->msg( 'date-range-to' )->text(), 'mw-date-end' ) . ' ' .
-                       new DateInputWidget( [
-                               'infusable' => true,
-                               'id' => 'mw-date-end',
-                               'name' => 'end',
-                               'value' => $this->opts['end'],
-                               'longDisplayFormat' => true,
-                       ] )
-               );
+               $fields['start'] = [
+                       'type' => 'date',
+                       'default' => '',
+                       'id' => 'mw-date-start',
+                       'label' => $this->msg( 'date-range-from' )->text(),
+                       'name' => 'start',
+                       'section' => 'contribs-date',
+               ];
+               $fields['end'] = [
+                       'type' => 'date',
+                       'default' => '',
+                       'id' => 'mw-date-end',
+                       'label' => $this->msg( 'date-range-to' )->text(),
+                       'name' => 'end',
+                       'section' => 'contribs-date',
+               ];
 
-               $submit = Xml::tags( 'div', [],
-                       Html::submitButton(
-                               $this->msg( 'sp-contributions-submit' )->text(),
-                               [ 'class' => 'mw-submit' ], [ 'mw-ui-progressive' ]
+               $htmlForm = HTMLForm::factory( 'ooui', $fields, $this->getContext() );
+               $htmlForm
+                       ->setMethod( 'get' )
+                       // When offset is defined, the user is paging through results
+                       // so we hide the form by default to allow users to focus on browsing
+                       // rather than defining search parameters
+                       ->setCollapsibleOptions(
+                               ( $pagerOptions['target'] ?? null ) ||
+                               ( $pagerOptions['start'] ?? null ) ||
+                               ( $pagerOptions['end'] ?? null )
                        )
-               );
-
-               $form .= Xml::fieldset(
-                       $this->msg( 'sp-contributions-search' )->text(),
-                       $targetSelection .
-                       $namespaceSelection .
-                       $filterSelection .
-                       $extraOptions .
-                       $dateRangeSelection .
-                       $submit,
-                       [ 'class' => 'mw-contributions-table' ]
-               );
+                       ->setAction( wfScript() )
+                       ->setSubmitText( $this->msg( 'sp-contributions-submit' )->text() )
+                       ->setWrapperLegend( $this->msg( 'sp-contributions-search' )->text() );
 
                $explain = $this->msg( 'sp-contributions-explain' );
                if ( !$explain->isBlank() ) {
-                       $form .= Html::rawElement(
-                               'p', [ 'id' => 'mw-sp-contributions-explain' ], $explain->parse()
-                       );
+                       $htmlForm->addFooterText( "<p id='mw-sp-contributions-explain'>{$explain->parse()}</p>" );
                }
 
-               $form .= Xml::closeElement( 'form' );
+               $htmlForm->loadData();
 
-               return $form;
+               return $htmlForm->getHTML( false );
        }
 
        /**