<?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 .= ' ' . 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>';
}
+
}
?>
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',