Fix when nothing is passed to listToText (eg: no author)
[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 $this->showList();
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 * Determine where to get the list of book sources from,
71 * format and output them
72 *
73 * @return string
74 */
75 private function showList() {
76 global $wgOut, $wgContLang;
77
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() ) );
83 return true;
84 }
85
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>' );
93 return true;
94 }
95
96 /**
97 * Format a book source list item
98 *
99 * @param $label Book source label
100 * @param $url Book source URL
101 * @return string
102 */
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>';
106 }
107
108 }
109
110 ?>