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->addHtml( $this->makeForm() );
36 if( strlen( $this->isbn
) > 0 )
37 $wgOut->addHtml( $this->makeList() );
41 * Trim ISBN and remove characters which aren't required
43 * @param $isbn Unclean ISBN
46 private function cleanIsbn( $isbn ) {
47 return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
51 * Generate a form to allow users to enter an ISBN
55 private function makeForm() {
57 $title = self
::getTitleFor( 'Booksources' );
58 $form = '<fieldset><legend>' . wfMsgHtml( 'booksources-search-legend' ) . '</legend>';
59 $form .= Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
60 $form .= Xml
::hidden( 'title', $title->getPrefixedText() );
61 $form .= '<p>' . Xml
::inputLabel( wfMsg( 'booksources-isbn' ), 'isbn', 'isbn', 20, $this->isbn
);
62 $form .= ' ' . Xml
::submitButton( wfMsg( 'booksources-go' ) ) . '</p>';
63 $form .= Xml
::closeElement( 'form' );
64 $form .= '</fieldset>';
69 * Generate the list of book sources
73 private function makeList() {
74 global $wgOut, $wgContLang;
76 # Check for a local page such as Project:Book_sources and use that if available
77 $title = Title
::makeTitleSafe( NS_PROJECT
, wfMsg( 'booksources' ) ); # Should this be wfMsgForContent()? -- RC
78 if( is_object( $title ) && $title->exists() ) {
79 $rev = Revision
::newFromTitle( $title );
80 return $wgOut->parse( str_replace( 'MAGICNUMBER', $this->isbn
, $rev->getText() ) );
83 # Fall back to the defaults given in the language file
84 $html = $wgOut->parse( wfMsg( 'booksources-text' ) );
86 $items = $wgContLang->getBookstoreList();
87 foreach( $items as $label => $url )
88 $html .= $this->makeListItem( $label, $url );
94 * Format a book source list item
96 * @param $label Book source label
97 * @param $url Book source URL
100 private function makeListItem( $label, $url ) {
101 $url = str_replace( '$1', $this->isbn
, $url );
102 return '<li><a href="' . htmlspecialchars( $url ) . '">' . htmlspecialchars( $label ) . '</a></li>';