3 * Implements Special:Booksources
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
25 * Special page outputs information on sourcing a book with a particular ISBN
26 * The parser creates links to this page when dealing with ISBNs in wikitext
28 * @author Rob Church <robchur@gmail.com>
29 * @todo Validate ISBNs using the standard check-digit method
30 * @ingroup SpecialPage
32 class SpecialBookSources
extends SpecialPage
{
35 * ISBN passed to the page, if any
42 public function __construct() {
43 parent
::__construct( 'Booksources' );
47 * Show the special page
49 * @param $isbn ISBN passed as a subpage parameter
51 public function execute( $isbn ) {
53 $this->outputHeader();
54 $this->isbn
= self
::cleanIsbn( $isbn ?
$isbn : $this->getRequest()->getText( 'isbn' ) );
55 $this->getOutput()->addHTML( $this->makeForm() );
56 if( strlen( $this->isbn
) > 0 ) {
57 if( !self
::isValidISBN( $this->isbn
) ) {
58 $this->getOutput()->wrapWikiMsg( "<div class=\"error\">\n$1\n</div>", 'booksources-invalid-isbn' );
65 * Returns whether a given ISBN (10 or 13) is valid. True indicates validity.
66 * @param isbn ISBN passed for check
68 public static function isValidISBN( $isbn ) {
69 $isbn = self
::cleanIsbn( $isbn );
71 if( strlen( $isbn ) == 13 ) {
72 for( $i = 0; $i < 12; $i++
) {
76 $sum +
= 3 * $isbn[$i];
80 $check = (10 - ($sum %
10)) %
10;
81 if ($check == $isbn[12]) {
84 } elseif( strlen( $isbn ) == 10 ) {
85 for($i = 0; $i < 9; $i++
) {
86 $sum +
= $isbn[$i] * ($i +
1);
93 if($check == $isbn[9]) {
101 * Trim ISBN and remove characters which aren't required
103 * @param $isbn Unclean ISBN
106 private static function cleanIsbn( $isbn ) {
107 return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
111 * Generate a form to allow users to enter an ISBN
115 private function makeForm() {
118 $form = '<fieldset><legend>' . wfMsgHtml( 'booksources-search-legend' ) . '</legend>';
119 $form .= Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
120 $form .= Html
::hidden( 'title', $this->getTitle()->getPrefixedText() );
121 $form .= '<p>' . Xml
::inputLabel( wfMsg( 'booksources-isbn' ), 'isbn', 'isbn', 20, $this->isbn
);
122 $form .= ' ' . Xml
::submitButton( wfMsg( 'booksources-go' ) ) . '</p>';
123 $form .= Xml
::closeElement( 'form' );
124 $form .= '</fieldset>';
129 * Determine where to get the list of book sources from,
130 * format and output them
134 private function showList() {
137 # Hook to allow extensions to insert additional HTML,
138 # e.g. for API-interacting plugins and so on
139 wfRunHooks( 'BookInformation', array( $this->isbn
, $this->getOutput() ) );
141 # Check for a local page such as Project:Book_sources and use that if available
142 $title = Title
::makeTitleSafe( NS_PROJECT
, wfMsgForContent( 'booksources' ) ); # Show list in content language
143 if( is_object( $title ) && $title->exists() ) {
144 $rev = Revision
::newFromTitle( $title );
145 $this->getOutput()->addWikiText( str_replace( 'MAGICNUMBER', $this->isbn
, $rev->getText() ) );
149 # Fall back to the defaults given in the language file
150 $this->getOutput()->addWikiMsg( 'booksources-text' );
151 $this->getOutput()->addHTML( '<ul>' );
152 $items = $wgContLang->getBookstoreList();
153 foreach( $items as $label => $url )
154 $this->getOutput()->addHTML( $this->makeListItem( $label, $url ) );
155 $this->getOutput()->addHTML( '</ul>' );
160 * Format a book source list item
162 * @param $label Book source label
163 * @param $url Book source URL
166 private function makeListItem( $label, $url ) {
167 $url = str_replace( '$1', $this->isbn
, $url );
168 return '<li><a href="' . htmlspecialchars( $url ) . '" class="external">' . htmlspecialchars( $label ) . '</a></li>';