Merge "resourceloader: Switch params for private addLink() function"
[lhc/web/wiklou.git] / includes / specials / SpecialBooksources.php
index 53798c0..ea9ddaf 100644 (file)
@@ -21,6 +21,8 @@
  * @ingroup SpecialPage
  */
 
+use MediaWiki\MediaWikiServices;
+
 /**
  * Special page outputs information on sourcing a book with a particular ISBN
  * The parser creates links to this page when dealing with ISBNs in wikitext
  * @ingroup SpecialPage
  */
 class SpecialBookSources extends SpecialPage {
-       /**
-        * ISBN passed to the page, if any
-        */
-       private $isbn = '';
-
-       /**
-        * Constructor
-        */
        public function __construct() {
                parent::__construct( 'Booksources' );
        }
 
        /**
-        * Show the special page
-        *
-        * @param string $isbn ISBN passed as a subpage parameter
+        * @param string|null $isbn ISBN passed as a subpage parameter
         */
        public function execute( $isbn ) {
+               $out = $this->getOutput();
+
                $this->setHeaders();
                $this->outputHeader();
-               $this->isbn = self::cleanIsbn( $isbn ? $isbn : $this->getRequest()->getText( 'isbn' ) );
-               $this->getOutput()->addHTML( $this->makeForm() );
-               if ( strlen( $this->isbn ) > 0 ) {
-                       if ( !self::isValidISBN( $this->isbn ) ) {
-                               $this->getOutput()->wrapWikiMsg(
+
+               // User provided ISBN
+               $isbn = $isbn ?: $this->getRequest()->getText( 'isbn' );
+               $isbn = trim( $isbn );
+
+               $this->buildForm( $isbn );
+
+               if ( $isbn !== '' ) {
+                       if ( !self::isValidISBN( $isbn ) ) {
+                               $out->wrapWikiMsg(
                                        "<div class=\"error\">\n$1\n</div>",
                                        'booksources-invalid-isbn'
                                );
                        }
-                       $this->showList();
+
+                       $this->showList( $isbn );
                }
        }
 
        /**
-        * Returns whether a given ISBN (10 or 13) is valid. True indicates validity.
+        * Return whether a given ISBN (10 or 13) is valid.
+        *
         * @param string $isbn ISBN passed for check
         * @return bool
         */
@@ -118,50 +119,45 @@ class SpecialBookSources extends SpecialPage {
        /**
         * Generate a form to allow users to enter an ISBN
         *
-        * @return string
+        * @param string $isbn
         */
-       private function makeForm() {
-               $form = Html::openElement( 'fieldset' ) . "\n";
-               $form .= Html::element(
-                       'legend',
-                       array(),
-                       $this->msg( 'booksources-search-legend' )->text()
-               ) . "\n";
-               $form .= Html::openElement( 'form', array( '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,
-                       array( 'autofocus' => '', 'class' => 'mw-ui-input-inline' )
-               );
-
-               $form .= '&#160;' . Html::submitButton(
-                       $this->msg( 'booksources-search' )->text(),
-                       array(), array( 'mw-ui-progressive' )
-               ) . "</p>\n";
-
-               $form .= Html::closeElement( 'form' ) . "\n";
-               $form .= Html::closeElement( 'fieldset' ) . "\n";
-
-               return $form;
+       private function buildForm( $isbn ) {
+               $formDescriptor = [
+                       'isbn' => [
+                               'type' => 'text',
+                               'name' => 'isbn',
+                               'label-message' => 'booksources-isbn',
+                               'default' => $isbn,
+                               'autofocus' => true,
+                               'required' => true,
+                       ],
+               ];
+
+               $context = new DerivativeContext( $this->getContext() );
+               $context->setTitle( $this->getPageTitle() );
+               HTMLForm::factory( 'ooui', $formDescriptor, $context )
+                       ->setWrapperLegendMsg( 'booksources-search-legend' )
+                       ->setSubmitTextMsg( 'booksources-search' )
+                       ->setMethod( 'get' )
+                       ->prepareForm()
+                       ->displayForm( false );
        }
 
        /**
         * Determine where to get the list of book sources from,
         * format and output them
         *
+        * @param string $isbn
         * @throws MWException
-        * @return string
+        * @return bool
         */
-       private function showList() {
-               global $wgContLang;
+       private function showList( $isbn ) {
+               $out = $this->getOutput();
 
+               $isbn = self::cleanIsbn( $isbn );
                # Hook to allow extensions to insert additional HTML,
                # e.g. for API-interacting plugins and so on
-               Hooks::run( 'BookInformation', array( $this->isbn, $this->getOutput() ) );
+               Hooks::run( 'BookInformation', [ $isbn, $out ] );
 
                # Check for a local page such as Project:Book_sources and use that if available
                $page = $this->msg( 'booksources' )->inContentLanguage()->text();
@@ -173,8 +169,8 @@ class SpecialBookSources extends SpecialPage {
                        if ( $content instanceof TextContent ) {
                                // 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 ) );
+                               $text = $content->getText();
+                               $out->addWikiTextAsInterface( str_replace( 'MAGICNUMBER', $isbn, $text ) );
 
                                return true;
                        } else {
@@ -183,13 +179,13 @@ class SpecialBookSources extends SpecialPage {
                }
 
                # Fall back to the defaults given in the language file
-               $this->getOutput()->addWikiMsg( 'booksources-text' );
-               $this->getOutput()->addHTML( '<ul>' );
-               $items = $wgContLang->getBookstoreList();
+               $out->addWikiMsg( 'booksources-text' );
+               $out->addHTML( '<ul>' );
+               $items = MediaWikiServices::getInstance()->getContentLanguage()->getBookstoreList();
                foreach ( $items as $label => $url ) {
-                       $this->getOutput()->addHTML( $this->makeListItem( $label, $url ) );
+                       $out->addHTML( $this->makeListItem( $isbn, $label, $url ) );
                }
-               $this->getOutput()->addHTML( '</ul>' );
+               $out->addHTML( '</ul>' );
 
                return true;
        }
@@ -197,15 +193,17 @@ class SpecialBookSources extends SpecialPage {
        /**
         * Format a book source list item
         *
+        * @param string $isbn
         * @param string $label Book source label
         * @param string $url Book source URL
         * @return string
         */
-       private function makeListItem( $label, $url ) {
-               $url = str_replace( '$1', $this->isbn, $url );
+       private function makeListItem( $isbn, $label, $url ) {
+               $url = str_replace( '$1', $isbn, $url );
 
-               return Html::rawElement( 'li', array(),
-                       Html::element( 'a', array( 'href' => $url, 'class' => 'external' ), $label ) );
+               return Html::rawElement( 'li', [],
+                       Html::element( 'a', [ 'href' => $url, 'class' => 'external' ], $label )
+               );
        }
 
        protected function getGroupName() {