Followup r82727, improve comments, cast return value to bool
[mediawiki.git] / includes / specials / SpecialBooksources.php
blob67fb54048e3bab600ac2b95eb81ea639516ca47a
1 <?php
2 /**
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
20 * @file
21 * @ingroup SpecialPage
24 /**
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 {
34 /**
35 * ISBN passed to the page, if any
37 private $isbn = '';
39 /**
40 * Constructor
42 public function __construct() {
43 parent::__construct( 'Booksources' );
46 /**
47 * Show the special page
49 * @param $isbn ISBN passed as a subpage parameter
51 public function execute( $isbn ) {
52 global $wgOut, $wgRequest;
53 $this->setHeaders();
54 $this->isbn = self::cleanIsbn( $isbn ? $isbn : $wgRequest->getText( 'isbn' ) );
55 $wgOut->addWikiMsg( 'booksources-summary' );
56 $wgOut->addHTML( $this->makeForm() );
57 if( strlen( $this->isbn ) > 0 ) {
58 if( !self::isValidISBN( $this->isbn ) ) {
59 $wgOut->wrapWikiMsg( "<div class=\"error\">\n$1\n</div>", 'booksources-invalid-isbn' );
61 $this->showList();
65 /**
66 * Returns whether a given ISBN (10 or 13) is valid. True indicates validity.
67 * @param isbn ISBN passed for check
69 public static function isValidISBN( $isbn ) {
70 $isbn = self::cleanIsbn( $isbn );
71 $sum = 0;
72 if( strlen( $isbn ) == 13 ) {
73 for( $i = 0; $i < 12; $i++ ) {
74 if($i % 2 == 0) {
75 $sum += $isbn{$i};
76 } else {
77 $sum += 3 * $isbn{$i};
81 $check = (10 - ($sum % 10)) % 10;
82 if ($check == $isbn{12}) {
83 return true;
85 } elseif( strlen( $isbn ) == 10 ) {
86 for($i = 0; $i < 9; $i++) {
87 $sum += $isbn{$i} * ($i + 1);
90 $check = $sum % 11;
91 if($check == 10) {
92 $check = "X";
94 if($check == $isbn{9}) {
95 return true;
98 return false;
102 * Trim ISBN and remove characters which aren't required
104 * @param $isbn Unclean ISBN
105 * @return string
107 private static function cleanIsbn( $isbn ) {
108 return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
112 * Generate a form to allow users to enter an ISBN
114 * @return string
116 private function makeForm() {
117 global $wgScript;
118 $title = self::getTitleFor( 'Booksources' );
119 $form = '<fieldset><legend>' . wfMsgHtml( 'booksources-search-legend' ) . '</legend>';
120 $form .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
121 $form .= Html::hidden( 'title', $title->getPrefixedText() );
122 $form .= '<p>' . Xml::inputLabel( wfMsg( 'booksources-isbn' ), 'isbn', 'isbn', 20, $this->isbn );
123 $form .= '&#160;' . Xml::submitButton( wfMsg( 'booksources-go' ) ) . '</p>';
124 $form .= Xml::closeElement( 'form' );
125 $form .= '</fieldset>';
126 return $form;
130 * Determine where to get the list of book sources from,
131 * format and output them
133 * @return string
135 private function showList() {
136 global $wgOut, $wgContLang;
138 # Hook to allow extensions to insert additional HTML,
139 # e.g. for API-interacting plugins and so on
140 wfRunHooks( 'BookInformation', array( $this->isbn, &$wgOut ) );
142 # Check for a local page such as Project:Book_sources and use that if available
143 $title = Title::makeTitleSafe( NS_PROJECT, wfMsgForContent( 'booksources' ) ); # Show list in content language
144 if( is_object( $title ) && $title->exists() ) {
145 $rev = Revision::newFromTitle( $title );
146 $wgOut->addWikiText( str_replace( 'MAGICNUMBER', $this->isbn, $rev->getText() ) );
147 return true;
150 # Fall back to the defaults given in the language file
151 $wgOut->addWikiMsg( 'booksources-text' );
152 $wgOut->addHTML( '<ul>' );
153 $items = $wgContLang->getBookstoreList();
154 foreach( $items as $label => $url )
155 $wgOut->addHTML( $this->makeListItem( $label, $url ) );
156 $wgOut->addHTML( '</ul>' );
157 return true;
161 * Format a book source list item
163 * @param $label Book source label
164 * @param $url Book source URL
165 * @return string
167 private function makeListItem( $label, $url ) {
168 $url = str_replace( '$1', $this->isbn, $url );
169 return '<li><a href="' . htmlspecialchars( $url ) . '" class="external">' . htmlspecialchars( $label ) . '</a></li>';