New HTMLForm size filter field; add size filter to Special:Newpages
authorThis, that and the other <at.light@live.com.au>
Thu, 27 Oct 2016 01:23:18 +0000 (12:23 +1100)
committerThis, that and the other <at.light@live.com.au>
Thu, 27 Oct 2016 01:23:18 +0000 (12:23 +1100)
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
includes/htmlform/HTMLForm.php
includes/htmlform/fields/HTMLSizeFilterField.php [new file with mode: 0644]
includes/specials/SpecialNewpages.php
includes/specials/pagers/NewPagesPager.php

index 3a2d06f..8b1e9c5 100644 (file)
@@ -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',
index 1f4d99e..cadb502 100644 (file)
@@ -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 (file)
index 0000000..c767d8f
--- /dev/null
@@ -0,0 +1,72 @@
+<?php
+
+/**
+ * A size filter field for use on query-type special pages. It looks a bit like:
+ *
+ *    (o) Min size  ( ) Max size:  [       ] bytes
+ *
+ * Minimum size limits are represented using a positive integer, while maximum
+ * size limits are represented using a negative integer.
+ */
+class HTMLSizeFilterField extends HTMLIntField {
+       function getSize() {
+               return isset( $this->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 .= '&#160;' . Xml::radioLabel(
+                       $this->msg( 'maximum-size' )->text(),
+                       $this->mName . '-mode',
+                       'max',
+                       $this->mID . '-mode-max',
+                       $value < 0,
+                       $attribs
+               );
+               $html .= '&#160;' . parent::getInputHTML( $value ? abs( $value ) : '' );
+               $html .= '&#160;' . $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;
+       }
+}
index d719e53..be8ad8f 100755 (executable)
@@ -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() );
index 2d39f99..e298f10 100644 (file)
@@ -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 ) {