* (bug 8164) Special:Booksources should use GET for form submission
authorRob Church <robchurch@users.mediawiki.org>
Sat, 16 Dec 2006 19:20:44 +0000 (19:20 +0000)
committerRob Church <robchurch@users.mediawiki.org>
Sat, 16 Dec 2006 19:20:44 +0000 (19:20 +0000)
* Rewrite Special:Booksources to clean up interface and remove redundant code

RELEASE-NOTES
includes/AutoLoader.php
includes/SpecialBooksources.php
includes/SpecialPage.php
languages/messages/MessagesEn.php

index 76a1e85..2926358 100644 (file)
@@ -324,6 +324,8 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
   MediaWiki:Sitenotice and MediaWiki:Anonnotice.
 * (bug 8044) When redirecting from the canonical name of the special page
   to the localised one, parameters/subpages are omitted
+* (bug 8164) Special:Booksources should use GET for form submission
+* Rewrite Special:Booksources to clean up interface and remove redundant code
 
 == Languages updated ==
 
index 041df09..8de5608 100644 (file)
@@ -158,7 +158,7 @@ function __autoload($className) {
                'SpecialAllpages' => 'includes/SpecialAllpages.php',
                'AncientPagesPage' => 'includes/SpecialAncientpages.php',
                'IPBlockForm' => 'includes/SpecialBlockip.php',
-               'BookSourceList' => 'includes/SpecialBooksources.php',
+               'SpecialBookSources' => 'includes/SpecialBooksources.php',
                'BrokenRedirectsPage' => 'includes/SpecialBrokenRedirects.php',
                'CategoriesPage' => 'includes/SpecialCategories.php',
                'EmailConfirmation' => 'includes/SpecialConfirmemail.php',
index 40b5a10..85b34ca 100644 (file)
 <?php
-/**
- * ISBNs in wiki pages will create links to this page, with the ISBN passed
- * in via the query string.
- *
- * @package MediaWiki
- * @subpackage SpecialPage
- */
-
-/**
- * Constructor
- */
-function wfSpecialBooksources( $par ) {
-       global $wgRequest;
-
-       $isbn = $par;
-       if( empty( $par ) ) {
-               $isbn = $wgRequest->getVal( 'isbn' );
-       }
-       $isbn = preg_replace( '/[^0-9X]/', '', $isbn );
-
-       $bsl = new BookSourceList( $isbn );
-       $bsl->show();
-}
 
 /**
+ * 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
  *
  * @package MediaWiki
- * @subpackage SpecialPage
+ * @subpackage Special pages
+ * @author Rob Church <robchur@gmail.com>
+ * @todo Validate ISBNs using the standard check-digit method
  */
-class BookSourceList {
-       var $mIsbn;
+class SpecialBookSources extends SpecialPage {
 
-       function BookSourceList( $isbn ) {
-               $this->mIsbn = $isbn;
+       /**
+        * ISBN passed to the page, if any
+        */
+       private $isbn = '';
+       
+       /**
+        * Constructor
+        */
+       public function __construct() {
+               parent::__construct( 'Booksources' );
        }
-
-       function show() {
-               global $wgOut;
-
-               $wgOut->setPagetitle( wfMsg( "booksources" ) );
-               if( $this->mIsbn == '' ) {
-                       $this->askForm();
-               } else {
-                       $this->showList();
-               }
+       
+       /**
+        * Show the special page
+        *
+        * @param $isbn ISBN passed as a subpage parameter
+        */
+       public function execute( $isbn = false ) {
+               global $wgOut, $wgRequest;
+               $this->setHeaders();
+               $this->isbn = $this->cleanIsbn( $isbn ? $isbn : $wgRequest->getText( 'isbn' ) );
+               $wgOut->addHtml( $this->makeForm() );
+               if( strlen( $this->isbn) > 0 )
+                       $wgOut->addHtml( $this->makeList() );
        }
-
-       function showList() {
+       
+       /**
+        * Trim ISBN and remove characters which aren't required
+        *
+        * @param $isbn Unclean ISBN
+        * @return string
+        */
+       private function cleanIsbn( $isbn ) {
+               return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
+       }
+       
+       /**
+        * Generate a form to allow users to enter an ISBN
+        *
+        * @return string
+        */
+       private function makeForm() {
+               global $wgScript;
+               $title = self::getTitleFor( 'Booksources' );
+               $form  = '<fieldset><legend>' . wfMsgHtml( 'booksources-search-legend' ) . '</legend>';
+               $form .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
+               $form .= Xml::hidden( 'title', $title->getPrefixedText() );
+               $form .= '<p>' . Xml::inputLabel( wfMsg( 'booksources-isbn' ), 'isbn', 'isbn', false, $this->isbn );
+               $form .= '&nbsp;' . Xml::submitButton( wfMsg( 'booksources-go' ) ) . '</p>';
+               $form .= '</fieldset>';
+               $form .= Xml::closeElement( 'form' );
+               return $form;
+       }
+       
+       /**
+        * Generate the list of book sources
+        *
+        * @return string
+        */
+       private function makeList() {
                global $wgOut, $wgContLang;
-
-               # First, see if we have a custom list setup in
-               # [[Wikipedia:Book sources]] or equivalent.
-               $bstitle = Title::makeTitleSafe( NS_PROJECT, wfMsg( "booksources" ) );
-               if( $bstitle ) {
-                       $revision = Revision::newFromTitle( $bstitle );
-                       if( $revision ) {
-                               $bstext = $revision->getText();
-                               if( $bstext ) {
-                                       $bstext = str_replace( "MAGICNUMBER", $this->mIsbn, $bstext );
-                                       $wgOut->addWikiText( $bstext );
-                                       return;
-                               }
-                       }
+               
+               # Check for a local page such as Project:Book_sources and use that if available
+               $title = Title::makeTitleSafe( NS_PROJECT, wfMsg( 'booksources' ) ); # Should this be wfMsgForContent()? -- RC
+               if( is_object( $title ) && $title->exists() ) {
+                       $rev = Revision::newFromTitle( $title );
+                       return $wgOut->parse( str_replace( 'MAGICNUMBER', $this->isbn, $rev->getText() ) );
                }
-
-               # Otherwise, use the list of links in the default Language.php file.
-               $s = wfMsgWikiHtml( 'booksourcetext' ) . "<ul>\n";
-               $bs = $wgContLang->getBookstoreList() ;
-               $bsn = array_keys ( $bs ) ;
-               foreach ( $bsn as $name ) {
-                       $adr = $bs[$name] ;
-                       if ( ! $this->mIsbn ) {
-                               $adr = explode( ":" , $adr , 2 );
-                               $adr = explode( "/" , $adr[1] );
-                               $a = "";
-                               while ( $a == "" ) {
-                                       $a = array_shift( $adr );
-                               }
-                               $adr = "http://".$a ;
-                       } else {
-                               $adr = str_replace ( "$1" , $this->mIsbn , $adr ) ;
-                       }
-                       $name = htmlspecialchars( $name );
-                       $adr = htmlspecialchars( $adr );
-                       $s .= "<li><a href=\"{$adr}\" class=\"external\">{$name}</a></li>\n" ;
-               }
-               $s .= "</ul>\n";
-
-               $wgOut->addHTML( $s );
+               
+               # Fall back to the defaults given in the language file
+               $html  = $wgOut->parse( wfMsg( 'booksources-text' ) );
+               $html .= '<ul>';
+               $items = $wgContLang->getBookstoreList();
+               foreach( $items as $label => $url )
+                       $html .= $this->makeListItem( $label, $url );
+               $html .= '</ul>';
+               return $html;           
        }
-
-       function askForm() {
-               global $wgOut, $wgTitle;
-
-               $action = $wgTitle->escapeLocalUrl();
-               $isbn = htmlspecialchars( wfMsg( "isbn" ) );
-               $go = htmlspecialchars( wfMsg( "go" ) );
-               $out = "<form action=\"$action\" method='post'>
-                       $isbn: <input name='isbn' id='isbn' />
-                       <input type='submit' value=\"$go\" />
-               </form>";
-               $wgOut->addHTML( $out );
+       
+       /**
+        * Format a book source list item
+        *
+        * @param $label Book source label
+        * @param $url Book source URL
+        * @return string
+        */
+       private function makeListItem( $label, $url ) {
+               $url = str_replace( '$1', $this->isbn, $url );
+               return '<li><a href="' . htmlspecialchars( $url ) . '">' . htmlspecialchars( $label ) . '</a></li>';
        }
+
 }
 
 ?>
index 50a8ae6..8643875 100644 (file)
@@ -120,7 +120,7 @@ class SpecialPage
                'Movepage'                  => array( 'UnlistedSpecialPage', 'Movepage' ),
                'Blockme'                   => array( 'UnlistedSpecialPage', 'Blockme' ),
                'Resetpass'                 => array( 'UnlistedSpecialPage', 'Resetpass' ),
-               'Booksources'               => array( 'SpecialPage', 'Booksources' ),
+               'Booksources'               => 'SpecialBookSources',
                'Categories'                => array( 'SpecialPage', 'Categories' ),
                'Export'                    => array( 'SpecialPage', 'Export' ),
                'Version'                   => array( 'SpecialPage', 'Version' ),
index 822f03a..79185c9 100644 (file)
@@ -1515,17 +1515,21 @@ a direct URL, and so may still be listed here despite being
 in active use.</p>',
 'unusedcategoriestext' => 'The following category pages exist although no other article or category make use of them.',
 
+# Book sources
 'booksources'  => 'Book sources',
 'booksources-summary'  => '',
+'booksources-search-legend' => 'Search for book sources',
+'booksources-isbn' => 'ISBN:',
+'booksources-go' => 'Go',
+'booksources-text' => 'Below is a list of links to other sites that sell new and used books, and may also have
+further information about books you are looking for:',
+
 'categoriespagetext' => 'The following categories exist in the wiki.',
 'data' => 'Data',
 'userrights' => 'User rights management',
 'userrights-summary' => '',
 'groups' => 'User groups',
 
-'booksourcetext' => "Below is a list of links to other sites that
-sell new and used books, and may also have further information
-about books you are looking for.",
 'isbn' => 'ISBN',
 'rfcurl' =>  'http://www.ietf.org/rfc/rfc$1.txt',
 'pubmedurl' =>  'http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?cmd=Retrieve&db=pubmed&dopt=Abstract&list_uids=$1',