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