]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/SpecialBooksources.php
MediaWiki 1.11.0
[autoinstallsdev/mediawiki.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  * @addtogroup SpecialPage
8  * @author Rob Church <robchur@gmail.com>
9  * @todo Validate ISBNs using the standard check-digit method
10  */
11 class SpecialBookSources extends SpecialPage {
12
13         /**
14          * ISBN passed to the page, if any
15          */
16         private $isbn = '';
17
18         /**
19          * Constructor
20          */
21         public function __construct() {
22                 parent::__construct( 'Booksources' );
23         }
24
25         /**
26          * Show the special page
27          *
28          * @param $isbn ISBN passed as a subpage parameter
29          */
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();
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          * Determine where to get the list of book sources from,
70          * format and output them
71          *
72          * @return string
73          */
74         private function showList() {
75                 global $wgOut, $wgContLang;
76
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 ) );
80
81                 # Check for a local page such as Project:Book_sources and use that if available
82                 $title = Title::makeTitleSafe( NS_PROJECT, wfMsgForContent( 'booksources' ) ); # Show list in content language
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;
87                 }
88
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;
97         }
98         
99         /**
100          * Format a book source list item
101          *
102          * @param $label Book source label
103          * @param $url Book source URL
104          * @return string
105          */
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>';
109         }
110
111 }
112
113