Fix regression in even sizing of diff columns; forgot to restore a couple bits I...
[mediawiki.git] / includes / SpecialBooksources.php
blob1903a8a5dc7fc6d71f7dc3bc91b50f74cf78b325
1 <?php
3 /**
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
7 * @addtogroup SpecialPage
8 * @author Rob Church <robchur@gmail.com>
9 * @todo Validate ISBNs using the standard check-digit method
11 class SpecialBookSources extends SpecialPage {
13 /**
14 * ISBN passed to the page, if any
16 private $isbn = '';
18 /**
19 * Constructor
21 public function __construct() {
22 parent::__construct( 'Booksources' );
25 /**
26 * Show the special page
28 * @param $isbn ISBN passed as a subpage parameter
30 public function execute( $isbn ) {
31 global $wgOut, $wgRequest;
32 $this->setHeaders();
33 $this->isbn = $this->cleanIsbn( $isbn ? $isbn : $wgRequest->getText( 'isbn' ) );
34 $wgOut->addWikiText( wfMsgNoTrans( 'booksources-summary' ) );
35 $wgOut->addHtml( $this->makeForm() );
36 if( strlen( $this->isbn ) > 0 )
37 $this->showList();
40 /**
41 * Trim ISBN and remove characters which aren't required
43 * @param $isbn Unclean ISBN
44 * @return string
46 private function cleanIsbn( $isbn ) {
47 return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
50 /**
51 * Generate a form to allow users to enter an ISBN
53 * @return string
55 private function makeForm() {
56 global $wgScript;
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 .= '&nbsp;' . Xml::submitButton( wfMsg( 'booksources-go' ) ) . '</p>';
63 $form .= Xml::closeElement( 'form' );
64 $form .= '</fieldset>';
65 return $form;
68 /**
69 * Determine where to get the list of book sources from,
70 * format and output them
72 * @return string
74 private function showList() {
75 global $wgOut, $wgContLang;
77 # Hook to allow extensions to insert additional HTML,
78 # e.g. for API-interacting plugins and so on
79 wfRunHooks( 'BookInformation', array( $this->isbn, &$wgOut ) );
81 # Check for a local page such as Project:Book_sources and use that if available
82 $title = Title::makeTitleSafe( NS_PROJECT, wfMsg( 'booksources' ) ); # Should this be wfMsgForContent()? -- RC
83 if( is_object( $title ) && $title->exists() ) {
84 $rev = Revision::newFromTitle( $title );
85 $wgOut->addWikiText( str_replace( 'MAGICNUMBER', $this->isbn, $rev->getText() ) );
86 return true;
89 # Fall back to the defaults given in the language file
90 $wgOut->addWikiText( wfMsgNoTrans( 'booksources-text' ) );
91 $wgOut->addHtml( '<ul>' );
92 $items = $wgContLang->getBookstoreList();
93 foreach( $items as $label => $url )
94 $wgOut->addHtml( $this->makeListItem( $label, $url ) );
95 $wgOut->addHtml( '</ul>' );
96 return true;
99 /**
100 * Format a book source list item
102 * @param $label Book source label
103 * @param $url Book source URL
104 * @return string
106 private function makeListItem( $label, $url ) {
107 $url = str_replace( '$1', $this->isbn, $url );
108 return '<li><a href="' . htmlspecialchars( $url ) . '">' . htmlspecialchars( $label ) . '</a></li>';