4 * Special page outputs information on sourcing a book with a particular ISBN
5 * The parser creates links to this page when dealing with ISBNs in wikitext
8 * @subpackage Special pages
9 * @author Rob Church <robchur@gmail.com>
10 * @todo Validate ISBNs using the standard check-digit method
12 class SpecialBookSources
extends SpecialPage
{
15 * ISBN passed to the page, if any
22 public function __construct() {
23 parent
::__construct( 'Booksources' );
27 * Show the special page
29 * @param $isbn ISBN passed as a subpage parameter
31 public function execute( $isbn = false ) {
32 global $wgOut, $wgRequest;
34 $this->isbn
= $this->cleanIsbn( $isbn ?
$isbn : $wgRequest->getText( 'isbn' ) );
35 $wgOut->addWikiText( wfMsgNoTrans( 'booksources-summary' ) );
36 $wgOut->addHtml( $this->makeForm() );
37 if( strlen( $this->isbn
) > 0 )
42 * Trim ISBN and remove characters which aren't required
44 * @param $isbn Unclean ISBN
47 private function cleanIsbn( $isbn ) {
48 return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
52 * Generate a form to allow users to enter an ISBN
56 private function makeForm() {
58 $title = self
::getTitleFor( 'Booksources' );
59 $form = '<fieldset><legend>' . wfMsgHtml( 'booksources-search-legend' ) . '</legend>';
60 $form .= Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
61 $form .= Xml
::hidden( 'title', $title->getPrefixedText() );
62 $form .= '<p>' . Xml
::inputLabel( wfMsg( 'booksources-isbn' ), 'isbn', 'isbn', 20, $this->isbn
);
63 $form .= ' ' . Xml
::submitButton( wfMsg( 'booksources-go' ) ) . '</p>';
64 $form .= Xml
::closeElement( 'form' );
65 $form .= '</fieldset>';
70 * Determine where to get the list of book sources from,
71 * format and output them
75 private function showList() {
76 global $wgOut, $wgContLang;
78 # Check for a local page such as Project:Book_sources and use that if available
79 $title = Title
::makeTitleSafe( NS_PROJECT
, wfMsg( 'booksources' ) ); # Should this be wfMsgForContent()? -- RC
80 if( is_object( $title ) && $title->exists() ) {
81 $rev = Revision
::newFromTitle( $title );
82 $wgOut->addWikiText( str_replace( 'MAGICNUMBER', $this->isbn
, $rev->getText() ) );
86 # Fall back to the defaults given in the language file
87 $wgOut->addWikiText( wfMsgNoTrans( 'booksources-text' ) );
88 $wgOut->addHtml( '<ul>' );
89 $items = $wgContLang->getBookstoreList();
90 foreach( $items as $label => $url )
91 $wgOut->addHtml( $this->makeListItem( $label, $url ) );
92 $wgOut->addHtml( '</ul>' );
97 * Format a book source list item
99 * @param $label Book source label
100 * @param $url Book source URL
103 private function makeListItem( $label, $url ) {
104 $url = str_replace( '$1', $this->isbn
, $url );
105 return '<li><a href="' . htmlspecialchars( $url ) . '">' . htmlspecialchars( $label ) . '</a></li>';