Followup r104353, updating Special:ProtectedTitles
authorJohn Du Hart <johnduhart@users.mediawiki.org>
Mon, 28 Nov 2011 00:31:05 +0000 (00:31 +0000)
committerJohn Du Hart <johnduhart@users.mediawiki.org>
Mon, 28 Nov 2011 00:31:05 +0000 (00:31 +0000)
Adds HTML form fields for namespaces and restriction levels
Adds getVal() to HTMLForm

includes/HTMLForm.php
includes/Pager.php
includes/specials/SpecialProtectedtitles.php

index 8baf2ca..85b5038 100644 (file)
@@ -72,6 +72,8 @@ class HTMLForm extends ContextSource {
                'submit' => 'HTMLSubmitField',
                'hidden' => 'HTMLHiddenField',
                'edittools' => 'HTMLEditTools',
+               'namespaces' => 'HTMLNamespacesField',
+               'restrictionlevels' => 'HTMLRestrictionLevelsField',
 
                # HTMLTextField will output the correct type="" attribute automagically.
                # There are about four zillion other HTML5 input types, like url, but
@@ -854,6 +856,16 @@ class HTMLForm extends ContextSource {
        public function getFlatFields() {
                return $this->mFlatFields;
        }
+
+       /**
+        * Returns a value of a field
+        *
+        * @param $field string Field name
+        * @return mixed
+        */
+       public function getVal( $field ) {
+               return $this->mFieldData[$field];
+       }
 }
 
 /**
@@ -2004,3 +2016,84 @@ class HTMLEditTools extends HTMLFormField {
                        . "</div></td></tr>\n";
        }
 }
+
+/**
+ * Dropdown for namespaces
+ */
+class HTMLNamespacesField extends HTMLSelectField {
+       function __construct( $params ) {
+               global $wgContLang;
+               parent::__construct( $params );
+
+               $namespaces = $wgContLang->getFormattedNamespaces();
+
+               $options = array();
+               $options[ wfMessage( 'namespacesall' )->escaped() ] = ''; // TODO: Make an option
+
+               foreach ( $namespaces as $index => $name ) {
+                       // Don't include things like SpecialPages
+                       if ( $index < NS_MAIN ) {
+                               continue;
+                       }
+
+                       if ( $index === 0 ) {
+                               $name = wfMessage( 'blanknamespace' )->escaped();
+                       }
+
+                       $options[$name] = $index;
+               }
+
+               $this->mParams['options'] = $options;
+       }
+}
+
+/**
+ * Dropdown for protection levels
+ */
+class HTMLRestrictionLevelsField extends HTMLSelectField {
+
+       /**
+        * Should this field be displayed? If it hits a condition where it should
+        * be hidden, set this to false.
+        *
+        * @var bool
+        */
+       protected $enabled = true;
+
+       function __construct( $params ) {
+               global $wgRestrictionLevels;
+               parent::__construct( $params );
+
+               $options = array( wfMsg('restriction-level-all') => 0 ); // Temporary array
+
+               // First pass to load the level names
+               foreach( $wgRestrictionLevels as $type ) {
+                       if ( $type != '' && $type != '*' ) {
+                               $text = wfMsg("restriction-level-$type");
+                               $options[$text] = $type;
+                       }
+               }
+
+               // Is there only one level (aside from "all")?
+               if( count($options) <= 2 ) {
+                       $this->enabled = false;
+                       return;
+               }
+
+               $this->mParams['options'] = $options;
+       }
+
+       /**
+        * Returns false where
+        *
+        * @param $value
+        * @return String
+        */
+       function getTableRow( $value ) {
+               if ( $this->enabled ) {
+                       return parent::getTableRow( $value );
+               }
+
+               return '';
+       }
+}
\ No newline at end of file
index 41e9501..375040d 100644 (file)
@@ -99,6 +99,13 @@ abstract class IndexPager extends ContextSource implements Pager {
 
        protected $mLastShown, $mFirstShown, $mPastTheEndIndex, $mDefaultQuery, $mNavigationBar;
 
+       /**
+        * HTMLForm object
+        *
+        * @var HTMLForm
+        */
+       protected $mHTMLForm;
+
        /**
         * Result object for the query. Warning: seek before use.
         *
@@ -569,27 +576,25 @@ abstract class IndexPager extends ContextSource implements Pager {
                        throw new MWException( __METHOD__ . " was called without any form fields being defined" );
                }
 
-               $form = new HTMLForm( $this->getHTMLFormFields(), $this->getContext() );
-               $form->setMethod( 'get' );
-               $form->setWrapperLegendMsg( $this->getHTMLFormLegend() );
-               $form->setSubmitTextMsg( $this->getHTMLFormSubmit() );
-               $this->addHiddenFields( $form );
-               $this->modifyHTMLForm( $form );
-               $form->prepareForm();
+               $this->mHTMLForm = new HTMLForm( $this->getHTMLFormFields(), $this->getContext() );
+               $this->mHTMLForm->setMethod( 'get' );
+               $this->mHTMLForm->setWrapperLegendMsg( $this->getHTMLFormLegend() );
+               $this->mHTMLForm->setSubmitTextMsg( $this->getHTMLFormSubmit() );
+               $this->addHiddenFields();
+               $this->modifyHTMLForm( $this->mHTMLForm );
+               $this->mHTMLForm->prepareForm();
 
-               return $form->getHTML( '' );
+               return $this->mHTMLForm->getHTML( '' );
        }
 
        /**
         * Adds hidden elements to forms for things that are in the query string.
         * This is so that parameters like offset stick through form submissions
-        *
-        * @param HTMLForm $form
         */
-       protected function addHiddenFields( HTMLForm $form ) {
+       protected function addHiddenFields() {
                $query = $this->getRequest()->getQueryValues();
                $fieldsBlacklist = array( 'title' );
-               $fields = $form->getFlatFields();
+               $fields = $this->mHTMLForm->getFlatFields();
                foreach ( $fields as $name => $field ) {
                        $fieldsBlacklist[] = $field->getName();
                }
@@ -597,7 +602,7 @@ abstract class IndexPager extends ContextSource implements Pager {
                        if ( in_array( $name, $fieldsBlacklist ) ) {
                                continue;
                        }
-                       $form->addHiddenField( $name, $value );
+                       $this->mHTMLForm->addHiddenField( $name, $value );
                }
        }
 
index 982feb6..17d3016 100644 (file)
@@ -28,9 +28,6 @@
  */
 class SpecialProtectedtitles extends SpecialPage {
 
-       protected $IdLevel = 'level';
-       protected $IdType  = 'type';
-
        public function __construct() {
                parent::__construct( 'Protectedtitles' );
        }
@@ -44,16 +41,8 @@ class SpecialProtectedtitles extends SpecialPage {
                        Title::purgeExpiredRestrictions();
                }
 
-               $request = $this->getRequest();
-               $type = $request->getVal( $this->IdType );
-               $level = $request->getVal( $this->IdLevel );
-               $sizetype = $request->getVal( 'sizetype' );
-               $size = $request->getIntOrNull( 'size' );
-               $NS = $request->getIntOrNull( 'namespace' );
-
-               $pager = new ProtectedTitlesPager( $this, array(), $type, $level, $NS, $sizetype, $size );
-
-               $this->getOutput()->addHTML( $this->showOptions( $NS, $type, $level ) );
+               $pager = new ProtectedTitlesPager( $this );
+               $this->getOutput()->addHTML( $pager->buildHTMLForm() );
 
                if ( $pager->getNumRows() ) {
                        $s = $pager->getNavigationBar();
@@ -70,6 +59,7 @@ class SpecialProtectedtitles extends SpecialPage {
        /**
         * Callback function to output a restriction
         *
+        * @param $row
         * @return string
         */
        function formatRow( $row ) {
@@ -107,89 +97,30 @@ class SpecialProtectedtitles extends SpecialPage {
 
                return '<li>' . $lang->specialList( $link, implode( $description_items, ', ' ) ) . "</li>\n";
        }
+}
 
+/**
+ * @todo document
+ * @ingroup Pager
+ */
+class ProtectedTitlesPager extends AlphabeticPager {
        /**
-        * @param $namespace Integer:
-        * @param $type string
-        * @param $level string
-        * @private
+        * @var SpecialProtectedtitles
         */
-       function showOptions( $namespace, $type='edit', $level ) {
-               global $wgScript;
-               $action = htmlspecialchars( $wgScript );
-               $title = $this->getTitle();
-               $special = htmlspecialchars( $title->getPrefixedDBkey() );
-               return "<form action=\"$action\" method=\"get\">\n" .
-                       '<fieldset>' .
-                       Xml::element( 'legend', array(), wfMsg( 'protectedtitles' ) ) .
-                       Html::hidden( 'title', $special ) . "&#160;\n" .
-                       $this->getNamespaceMenu( $namespace ) . "&#160;\n" .
-                       $this->getLevelMenu( $level ) . "&#160;\n" .
-                       "&#160;" . Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
-                       "</fieldset></form>";
-       }
+       public $mForm;
 
        /**
-        * Prepare the namespace filter drop-down; standard namespace
-        * selector, sans the MediaWiki namespace
-        *
-        * @param $namespace Mixed: pre-select namespace
-        * @return string
+        * @var array
         */
-       function getNamespaceMenu( $namespace = null ) {
-               return Xml::label( wfMsg( 'namespace' ), 'namespace' )
-                       . '&#160;'
-                       . Xml::namespaceSelector( $namespace, '' );
-       }
+       public $mConds;
 
        /**
-        * @return string Formatted HTML
-        * @private
+        * @param $form SpecialProtectedtitles
+        * @param $conds array
         */
-       function getLevelMenu( $pr_level ) {
-               global $wgRestrictionLevels;
-
-               $m = array( wfMsg('restriction-level-all') => 0 ); // Temporary array
-               $options = array();
-
-               // First pass to load the log names
-               foreach( $wgRestrictionLevels as $type ) {
-                       if ( $type !='' && $type !='*') {
-                               $text = wfMsg("restriction-level-$type");
-                               $m[$text] = $type;
-                       }
-               }
-               // Is there only one level (aside from "all")?
-               if( count($m) <= 2 ) {
-                       return '';
-               }
-               // Third pass generates sorted XHTML content
-               foreach( $m as $text => $type ) {
-                       $selected = ($type == $pr_level );
-                       $options[] = Xml::option( $text, $type, $selected );
-               }
-
-               return
-                       Xml::label( wfMsg('restriction-level') , $this->IdLevel ) . '&#160;' .
-                       Xml::tags( 'select',
-                               array( 'id' => $this->IdLevel, 'name' => $this->IdLevel ),
-                               implode( "\n", $options ) );
-       }
-}
-
-/**
- * @todo document
- * @ingroup Pager
- */
-class ProtectedTitlesPager extends AlphabeticPager {
-       public $mForm, $mConds;
-
-       function __construct( $form, $conds = array(), $type, $level, $namespace, $sizetype='', $size=0 ) {
+       function __construct( $form, $conds = array() ) {
                $this->mForm = $form;
                $this->mConds = $conds;
-               $this->level = $level;
-               $this->namespace = $namespace;
-               $this->size = intval($size);
                parent::__construct( $form->getContext() );
        }
 
@@ -225,10 +156,14 @@ class ProtectedTitlesPager extends AlphabeticPager {
        function getQueryInfo() {
                $conds = $this->mConds;
                $conds[] = 'pt_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
-               if( $this->level )
-                       $conds['pt_create_perm'] = $this->level;
-               if( !is_null($this->namespace) )
-                       $conds[] = 'pt_namespace=' . $this->mDb->addQuotes( $this->namespace );
+
+               if ( $this->mHTMLForm->getVal( 'Level' ) ) {
+                       $conds['pt_create_perm'] = $this->mHTMLForm->getVal( 'Level' );
+               }
+               if ( $this->mHTMLForm->getVal( 'Namespace' ) !== '' ) {
+                       $conds['pt_namespace'] = $this->mHTMLForm->getVal( 'Namespace' );
+               }
+
                return array(
                        'tables' => 'protected_titles',
                        'fields' => 'pt_namespace,pt_title,pt_create_perm,pt_expiry,pt_timestamp',
@@ -239,5 +174,28 @@ class ProtectedTitlesPager extends AlphabeticPager {
        function getIndexField() {
                return 'pt_timestamp';
        }
+
+       protected function getHTMLFormFields() {
+               return array(
+                        'Namespace' => array(
+                                'type' => 'namespaces',
+                                'label-message' => 'namespace',
+                        ),
+                        'Level' => array(
+                                'type' => 'restrictionlevels',
+                                'label-message' => 'restriction-level',
+                        ),
+                );
+       }
+
+       protected function getHTMLFormSubmit() {
+               return 'allpagessubmit';
+       }
+
+       protected function getHTMLFormLegend() {
+               return 'protectedtitles';
+       }
+
+
 }