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
{
34 * ISBN passed to the page, if any
41 public function __construct() {
42 parent
::__construct( 'Booksources' );
46 * Show the special page
48 * @param string $isbn ISBN passed as a subpage parameter
50 public function execute( $isbn ) {
52 $this->outputHeader();
53 $this->isbn
= self
::cleanIsbn( $isbn ?
$isbn : $this->getRequest()->getText( 'isbn' ) );
54 $this->getOutput()->addHTML( $this->makeForm() );
55 if ( strlen( $this->isbn
) > 0 ) {
56 if ( !self
::isValidISBN( $this->isbn
) ) {
57 $this->getOutput()->wrapWikiMsg(
58 "<div class=\"error\">\n$1\n</div>",
59 'booksources-invalid-isbn'
67 * Returns whether a given ISBN (10 or 13) is valid. True indicates validity.
68 * @param string $isbn ISBN passed for check
71 public static function isValidISBN( $isbn ) {
72 $isbn = self
::cleanIsbn( $isbn );
74 if ( strlen( $isbn ) == 13 ) {
75 for ( $i = 0; $i < 12; $i++
) {
79 $sum +
= 3 * $isbn[$i];
83 $check = ( 10 - ( $sum %
10 ) ) %
10;
84 if ( $check == $isbn[12] ) {
87 } elseif ( strlen( $isbn ) == 10 ) {
88 for ( $i = 0; $i < 9; $i++
) {
89 $sum +
= $isbn[$i] * ( $i +
1 );
96 if ( $check == $isbn[9] ) {
105 * Trim ISBN and remove characters which aren't required
107 * @param string $isbn Unclean ISBN
110 private static function cleanIsbn( $isbn ) {
111 return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
115 * Generate a form to allow users to enter an ISBN
119 private function makeForm() {
120 $form = Html
::openElement( 'fieldset' ) . "\n";
121 $form .= Html
::element(
124 $this->msg( 'booksources-search-legend' )->text()
126 $form .= Html
::openElement( 'form', array( 'method' => 'get', 'action' => wfScript() ) ) . "\n";
127 $form .= Html
::hidden( 'title', $this->getPageTitle()->getPrefixedText() ) . "\n";
128 $form .= '<p>' . Xml
::inputLabel(
129 $this->msg( 'booksources-isbn' )->text(),
134 array( 'autofocus' => true )
136 $form .= ' ' . Xml
::submitButton( $this->msg( 'booksources-go' )->text() ) . "</p>\n";
137 $form .= Html
::closeElement( 'form' ) . "\n";
138 $form .= Html
::closeElement( 'fieldset' ) . "\n";
144 * Determine where to get the list of book sources from,
145 * format and output them
147 * @throws MWException
150 private function showList() {
153 # Hook to allow extensions to insert additional HTML,
154 # e.g. for API-interacting plugins and so on
155 wfRunHooks( 'BookInformation', array( $this->isbn
, $this->getOutput() ) );
157 # Check for a local page such as Project:Book_sources and use that if available
158 $page = $this->msg( 'booksources' )->inContentLanguage()->text();
159 $title = Title
::makeTitleSafe( NS_PROJECT
, $page ); # Show list in content language
160 if ( is_object( $title ) && $title->exists() ) {
161 $rev = Revision
::newFromTitle( $title, false, Revision
::READ_NORMAL
);
162 $content = $rev->getContent();
164 if ( $content instanceof TextContent
) {
165 //XXX: in the future, this could be stored as structured data, defining a list of book sources
167 $text = $content->getNativeData();
168 $this->getOutput()->addWikiText( str_replace( 'MAGICNUMBER', $this->isbn
, $text ) );
172 throw new MWException( "Unexpected content type for book sources: " . $content->getModel() );
176 # Fall back to the defaults given in the language file
177 $this->getOutput()->addWikiMsg( 'booksources-text' );
178 $this->getOutput()->addHTML( '<ul>' );
179 $items = $wgContLang->getBookstoreList();
180 foreach ( $items as $label => $url ) {
181 $this->getOutput()->addHTML( $this->makeListItem( $label, $url ) );
183 $this->getOutput()->addHTML( '</ul>' );
189 * Format a book source list item
191 * @param string $label Book source label
192 * @param string $url Book source URL
195 private function makeListItem( $label, $url ) {
196 $url = str_replace( '$1', $this->isbn
, $url );
198 return Html
::rawElement( 'li', array(),
199 Html
::element( 'a', array( 'href' => $url, 'class' => 'external' ), $label ) );
202 protected function getGroupName() {