From 07782d176b83f0b99f677ce9fb77fc062fec462a Mon Sep 17 00:00:00 2001 From: "This, that and the other" Date: Thu, 27 Oct 2016 12:23:18 +1100 Subject: [PATCH] New HTMLForm size filter field; add size filter to Special:Newpages The conversion of SpecialNewpages to HTMLForm seems to be half-finished. It's not using HTMLForm to read in the request query, which means we have to roll our own logic. Kind of defeats the purpose of using HTMLForm in the first place. When ProtectedPages is converted to HTMLForm (T117722), it can use this new field type. Bug: T12817 Change-Id: I069609fbb37b18c3df25156779ad7ac7cd5d6813 --- autoload.php | 1 + includes/htmlform/HTMLForm.php | 1 + .../htmlform/fields/HTMLSizeFilterField.php | 72 +++++++++++++++++++ includes/specials/SpecialNewpages.php | 10 +++ includes/specials/pagers/NewPagesPager.php | 9 +++ 5 files changed, 93 insertions(+) create mode 100644 includes/htmlform/fields/HTMLSizeFilterField.php diff --git a/autoload.php b/autoload.php index 3a2d06ff20..8b1e9c55f4 100644 --- a/autoload.php +++ b/autoload.php @@ -556,6 +556,7 @@ $wgAutoloadLocalClasses = [ 'HTMLSelectNamespace' => __DIR__ . '/includes/htmlform/fields/HTMLSelectNamespace.php', 'HTMLSelectNamespaceWithButton' => __DIR__ . '/includes/htmlform/fields/HTMLSelectNamespaceWithButton.php', 'HTMLSelectOrOtherField' => __DIR__ . '/includes/htmlform/fields/HTMLSelectOrOtherField.php', + 'HTMLSizeFilterField' => __DIR__ . '/includes/htmlform/fields/HTMLSizeFilterField.php', 'HTMLSubmitField' => __DIR__ . '/includes/htmlform/fields/HTMLSubmitField.php', 'HTMLTagFilter' => __DIR__ . '/includes/htmlform/fields/HTMLTagFilter.php', 'HTMLTextAreaField' => __DIR__ . '/includes/htmlform/fields/HTMLTextAreaField.php', diff --git a/includes/htmlform/HTMLForm.php b/includes/htmlform/HTMLForm.php index 1f4d99ef66..cadb5023e2 100644 --- a/includes/htmlform/HTMLForm.php +++ b/includes/htmlform/HTMLForm.php @@ -147,6 +147,7 @@ class HTMLForm extends ContextSource { 'namespaceselect' => 'HTMLSelectNamespace', 'namespaceselectwithbutton' => 'HTMLSelectNamespaceWithButton', 'tagfilter' => 'HTMLTagFilter', + 'sizefilter' => 'HTMLSizeFilterField', 'submit' => 'HTMLSubmitField', 'hidden' => 'HTMLHiddenField', 'edittools' => 'HTMLEditTools', diff --git a/includes/htmlform/fields/HTMLSizeFilterField.php b/includes/htmlform/fields/HTMLSizeFilterField.php new file mode 100644 index 0000000000..c767d8fda4 --- /dev/null +++ b/includes/htmlform/fields/HTMLSizeFilterField.php @@ -0,0 +1,72 @@ +mParams['size'] ) ? $this->mParams['size'] : 9; + } + + function getInputHTML( $value ) { + $attribs = []; + if ( !empty( $this->mParams['disabled'] ) ) { + $attribs['disabled'] = 'disabled'; + } + + $html = Xml::radioLabel( + $this->msg( 'minimum-size' )->text(), + $this->mName . '-mode', + 'min', + $this->mID . '-mode-min', + $value >= 0, + $attribs + ); + $html .= ' ' . Xml::radioLabel( + $this->msg( 'maximum-size' )->text(), + $this->mName . '-mode', + 'max', + $this->mID . '-mode-max', + $value < 0, + $attribs + ); + $html .= ' ' . parent::getInputHTML( $value ? abs( $value ) : '' ); + $html .= ' ' . $this->msg( 'pagesize' )->parse(); + + return $html; + } + + // No OOUI yet + function getInputOOUI( $value ) { + return false; + } + + /** + * @param WebRequest $request + * + * @return string + */ + function loadDataFromRequest( $request ) { + $size = $request->getInt( $this->mName ); + if ( !$size ) { + return $this->getDefault(); + } + $size = abs( $size ); + + // negative numbers represent "max", positive numbers represent "min" + if ( $request->getVal( $this->mName . '-mode' ) === 'max' ) { + return -$size; + } else { + return $size; + } + } + + protected function needsLabel() { + return false; + } +} diff --git a/includes/specials/SpecialNewpages.php b/includes/specials/SpecialNewpages.php index d719e53bdd..be8ad8fb40 100755 --- a/includes/specials/SpecialNewpages.php +++ b/includes/specials/SpecialNewpages.php @@ -54,6 +54,8 @@ class SpecialNewpages extends IncludableSpecialPage { $opts->add( 'feed', '' ); $opts->add( 'tagfilter', '' ); $opts->add( 'invert', false ); + $opts->add( 'size-mode', 'max' ); + $opts->add( 'size', 0 ); $this->customFilters = []; Hooks::run( 'SpecialNewPagesFilters', [ $this, &$this->customFilters ] ); @@ -214,6 +216,9 @@ class SpecialNewpages extends IncludableSpecialPage { $tagFilterVal = $this->opts->consumeValue( 'tagfilter' ); $nsinvert = $this->opts->consumeValue( 'invert' ); + $size = $this->opts->consumeValue( 'size' ); + $max = $this->opts->consumeValue( 'size-mode' ) === 'max'; + // Check username input validity $ut = Title::makeTitleSafe( NS_USER, $username ); $userText = $ut ? $ut->getText() : ''; @@ -254,6 +259,11 @@ class SpecialNewpages extends IncludableSpecialPage { 'size' => 30, 'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest ], + 'size' => [ + 'type' => 'sizefilter', + 'name' => 'size', + 'default' => -$max * $size, + ], ]; $htmlForm = new HTMLForm( $form, $this->getContext() ); diff --git a/includes/specials/pagers/NewPagesPager.php b/includes/specials/pagers/NewPagesPager.php index 2d39f99dc7..e298f103a2 100644 --- a/includes/specials/pagers/NewPagesPager.php +++ b/includes/specials/pagers/NewPagesPager.php @@ -48,6 +48,15 @@ class NewPagesPager extends ReverseChronologicalPager { $username = $this->opts->getValue( 'username' ); $user = Title::makeTitleSafe( NS_USER, $username ); + $size = abs( intval( $this->opts->getValue( 'size' ) ) ); + if ( $size > 0 ) { + if ( $this->opts->getValue( 'size-mode' ) === 'max' ) { + $conds[] = 'page_len <= ' . $size; + } else { + $conds[] = 'page_len >= ' . $size; + } + } + $rcIndexes = []; if ( $namespace !== false ) { -- 2.20.1