Convert Special:BookSources to use OOUI
authorSethakill <sethakill@outlook.com>
Tue, 3 May 2016 08:03:27 +0000 (10:03 +0200)
committerSethakill <sethakill@outlook.com>
Sat, 7 May 2016 14:45:40 +0000 (16:45 +0200)
Bug: T117747
Change-Id: Ieae79e8ab3fefb1b2af73961785fc63cafb2c9b6

includes/specials/SpecialBooksources.php

index fe90a4f..11faa28 100644 (file)
@@ -32,7 +32,7 @@ class SpecialBookSources extends SpecialPage {
        /**
         * ISBN passed to the page, if any
         */
-       private $isbn = '';
+       protected $isbn = '';
 
        public function __construct() {
                parent::__construct( 'Booksources' );
@@ -44,17 +44,23 @@ class SpecialBookSources extends SpecialPage {
         * @param string $isbn ISBN passed as a subpage parameter
         */
        public function execute( $isbn ) {
+               $out = $this->getOutput();
+
                $this->setHeaders();
                $this->outputHeader();
+
                $this->isbn = self::cleanIsbn( $isbn ?: $this->getRequest()->getText( 'isbn' ) );
-               $this->getOutput()->addHTML( $this->makeForm() );
+
+               $this->buildForm();
+
                if ( $this->isbn !== '' ) {
                        if ( !self::isValidISBN( $this->isbn ) ) {
-                               $this->getOutput()->wrapWikiMsg(
+                               $out->wrapWikiMsg(
                                        "<div class=\"error\">\n$1\n</div>",
                                        'booksources-invalid-isbn'
                                );
                        }
+
                        $this->showList();
                }
        }
@@ -115,36 +121,25 @@ class SpecialBookSources extends SpecialPage {
 
        /**
         * Generate a form to allow users to enter an ISBN
-        *
-        * @return string
         */
-       private function makeForm() {
-               $form = Html::openElement( 'fieldset' ) . "\n";
-               $form .= Html::element(
-                       'legend',
-                       [],
-                       $this->msg( 'booksources-search-legend' )->text()
-               ) . "\n";
-               $form .= Html::openElement( 'form', [ 'method' => 'get', 'action' => wfScript() ] ) . "\n";
-               $form .= Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() ) . "\n";
-               $form .= '<p>' . Xml::inputLabel(
-                       $this->msg( 'booksources-isbn' )->text(),
-                       'isbn',
-                       'isbn',
-                       20,
-                       $this->isbn,
-                       [ 'autofocus' => '', 'class' => 'mw-ui-input-inline' ]
-               );
-
-               $form .= '&#160;' . Html::submitButton(
-                       $this->msg( 'booksources-search' )->text(),
-                       [], [ 'mw-ui-progressive' ]
-               ) . "</p>\n";
-
-               $form .= Html::closeElement( 'form' ) . "\n";
-               $form .= Html::closeElement( 'fieldset' ) . "\n";
-
-               return $form;
+       private function buildForm() {
+               $formDescriptor = [
+                       'isbn' => [
+                               'type' => 'text',
+                               'name' => 'isbn',
+                               'label-message' => 'booksources-isbn',
+                               'default' => $this->isbn,
+                               'autofocus' => true,
+                               'required' => true,
+                       ],
+               ];
+
+               $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
+                       ->setWrapperLegendMsg( 'booksources-search-legend' )
+                       ->setSubmitTextMsg( 'booksources-search' )
+                       ->setMethod( 'get' )
+                       ->prepareForm()
+                       ->displayForm( false );
        }
 
        /**
@@ -155,11 +150,13 @@ class SpecialBookSources extends SpecialPage {
         * @return bool
         */
        private function showList() {
+               $out = $this->getOutput();
+
                global $wgContLang;
 
                # Hook to allow extensions to insert additional HTML,
                # e.g. for API-interacting plugins and so on
-               Hooks::run( 'BookInformation', [ $this->isbn, $this->getOutput() ] );
+               Hooks::run( 'BookInformation', [ $this->isbn, $out ] );
 
                # Check for a local page such as Project:Book_sources and use that if available
                $page = $this->msg( 'booksources' )->inContentLanguage()->text();
@@ -172,7 +169,7 @@ class SpecialBookSources extends SpecialPage {
                                // XXX: in the future, this could be stored as structured data, defining a list of book sources
 
                                $text = $content->getNativeData();
-                               $this->getOutput()->addWikiText( str_replace( 'MAGICNUMBER', $this->isbn, $text ) );
+                               $out->addWikiText( str_replace( 'MAGICNUMBER', $this->isbn, $text ) );
 
                                return true;
                        } else {
@@ -181,13 +178,13 @@ class SpecialBookSources extends SpecialPage {
                }
 
                # Fall back to the defaults given in the language file
-               $this->getOutput()->addWikiMsg( 'booksources-text' );
-               $this->getOutput()->addHTML( '<ul>' );
+               $out->addWikiMsg( 'booksources-text' );
+               $out->addHTML( '<ul>' );
                $items = $wgContLang->getBookstoreList();
                foreach ( $items as $label => $url ) {
-                       $this->getOutput()->addHTML( $this->makeListItem( $label, $url ) );
+                       $out->addHTML( $this->makeListItem( $label, $url ) );
                }
-               $this->getOutput()->addHTML( '</ul>' );
+               $out->addHTML( '</ul>' );
 
                return true;
        }