Bring back "booksources-summary", displayed above the search form on Special:Booksour...
[lhc/web/wiklou.git] / includes / SpecialBooksources.php
1 <?php
2
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
6 *
7 * @package MediaWiki
8 * @subpackage Special pages
9 * @author Rob Church <robchur@gmail.com>
10 * @todo Validate ISBNs using the standard check-digit method
11 */
12 class SpecialBookSources extends SpecialPage {
13
14 /**
15 * ISBN passed to the page, if any
16 */
17 private $isbn = '';
18
19 /**
20 * Constructor
21 */
22 public function __construct() {
23 parent::__construct( 'Booksources' );
24 }
25
26 /**
27 * Show the special page
28 *
29 * @param $isbn ISBN passed as a subpage parameter
30 */
31 public function execute( $isbn = false ) {
32 global $wgOut, $wgRequest;
33 $this->setHeaders();
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 )
38 $wgOut->addHtml( $this->makeList() );
39 }
40
41 /**
42 * Trim ISBN and remove characters which aren't required
43 *
44 * @param $isbn Unclean ISBN
45 * @return string
46 */
47 private function cleanIsbn( $isbn ) {
48 return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
49 }
50
51 /**
52 * Generate a form to allow users to enter an ISBN
53 *
54 * @return string
55 */
56 private function makeForm() {
57 global $wgScript;
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 .= '&nbsp;' . Xml::submitButton( wfMsg( 'booksources-go' ) ) . '</p>';
64 $form .= Xml::closeElement( 'form' );
65 $form .= '</fieldset>';
66 return $form;
67 }
68
69 /**
70 * Generate the list of book sources
71 *
72 * @return string
73 */
74 private function makeList() {
75 global $wgOut, $wgContLang;
76
77 # Check for a local page such as Project:Book_sources and use that if available
78 $title = Title::makeTitleSafe( NS_PROJECT, wfMsg( 'booksources' ) ); # Should this be wfMsgForContent()? -- RC
79 if( is_object( $title ) && $title->exists() ) {
80 $rev = Revision::newFromTitle( $title );
81 return $wgOut->parse( str_replace( 'MAGICNUMBER', $this->isbn, $rev->getText() ) );
82 }
83
84 # Fall back to the defaults given in the language file
85 $html = $wgOut->parse( wfMsg( 'booksources-text' ) );
86 $html .= '<ul>';
87 $items = $wgContLang->getBookstoreList();
88 foreach( $items as $label => $url )
89 $html .= $this->makeListItem( $label, $url );
90 $html .= '</ul>';
91 return $html;
92 }
93
94 /**
95 * Format a book source list item
96 *
97 * @param $label Book source label
98 * @param $url Book source URL
99 * @return string
100 */
101 private function makeListItem( $label, $url ) {
102 $url = str_replace( '$1', $this->isbn, $url );
103 return '<li><a href="' . htmlspecialchars( $url ) . '">' . htmlspecialchars( $label ) . '</a></li>';
104 }
105
106 }
107
108 ?>