* prevent minor W3C validator/tidy error on Special:Booksources about nesting of...
[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->addHtml( $this->makeForm() );
36 if( strlen( $this->isbn) > 0 )
37 $wgOut->addHtml( $this->makeList() );
38 }
39
40 /**
41 * Trim ISBN and remove characters which aren't required
42 *
43 * @param $isbn Unclean ISBN
44 * @return string
45 */
46 private function cleanIsbn( $isbn ) {
47 return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
48 }
49
50 /**
51 * Generate a form to allow users to enter an ISBN
52 *
53 * @return string
54 */
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;
66 }
67
68 /**
69 * Generate the list of book sources
70 *
71 * @return string
72 */
73 private function makeList() {
74 global $wgOut, $wgContLang;
75
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() ) );
81 }
82
83 # Fall back to the defaults given in the language file
84 $html = $wgOut->parse( wfMsg( 'booksources-text' ) );
85 $html .= '<ul>';
86 $items = $wgContLang->getBookstoreList();
87 foreach( $items as $label => $url )
88 $html .= $this->makeListItem( $label, $url );
89 $html .= '</ul>';
90 return $html;
91 }
92
93 /**
94 * Format a book source list item
95 *
96 * @param $label Book source label
97 * @param $url Book source URL
98 * @return string
99 */
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>';
103 }
104
105 }
106
107 ?>