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 * @ingroup SpecialPage
31 class SpecialBookSources
extends SpecialPage
{
33 * ISBN passed to the page, if any
40 public function __construct() {
41 parent
::__construct( 'Booksources' );
45 * Show the special page
47 * @param string $isbn ISBN passed as a subpage parameter
49 public function execute( $isbn ) {
51 $this->outputHeader();
52 $this->isbn
= self
::cleanIsbn( $isbn ?
$isbn : $this->getRequest()->getText( 'isbn' ) );
53 $this->getOutput()->addHTML( $this->makeForm() );
54 if ( strlen( $this->isbn
) > 0 ) {
55 if ( !self
::isValidISBN( $this->isbn
) ) {
56 $this->getOutput()->wrapWikiMsg(
57 "<div class=\"error\">\n$1\n</div>",
58 'booksources-invalid-isbn'
66 * Returns whether a given ISBN (10 or 13) is valid. True indicates validity.
67 * @param string $isbn ISBN passed for check
70 public static function isValidISBN( $isbn ) {
71 $isbn = self
::cleanIsbn( $isbn );
73 if ( strlen( $isbn ) == 13 ) {
74 for ( $i = 0; $i < 12; $i++
) {
75 if ( $isbn[$i] === 'X' ) {
77 } elseif ( $i %
2 == 0 ) {
80 $sum +
= 3 * $isbn[$i];
84 $check = ( 10 - ( $sum %
10 ) ) %
10;
85 if ( (string)$check === $isbn[12] ) {
88 } elseif ( strlen( $isbn ) == 10 ) {
89 for ( $i = 0; $i < 9; $i++
) {
90 if ( $isbn[$i] === 'X' ) {
93 $sum +
= $isbn[$i] * ( $i +
1 );
100 if ( (string)$check === $isbn[9] ) {
109 * Trim ISBN and remove characters which aren't required
111 * @param string $isbn Unclean ISBN
114 private static function cleanIsbn( $isbn ) {
115 return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
119 * Generate a form to allow users to enter an ISBN
123 private function makeForm() {
124 $form = Html
::openElement( 'fieldset' ) . "\n";
125 $form .= Html
::element(
128 $this->msg( 'booksources-search-legend' )->text()
130 $form .= Html
::openElement( 'form', array( 'method' => 'get', 'action' => wfScript() ) ) . "\n";
131 $form .= Html
::hidden( 'title', $this->getPageTitle()->getPrefixedText() ) . "\n";
132 $form .= '<p>' . Xml
::inputLabel(
133 $this->msg( 'booksources-isbn' )->text(),
138 array( 'autofocus' => true, 'class' => 'mw-ui-input-inline' )
141 $form .= ' ' . Html
::submitButton(
142 $this->msg( 'booksources-search' )->text(),
143 array(), array( 'mw-ui-progressive' )
146 $form .= Html
::closeElement( 'form' ) . "\n";
147 $form .= Html
::closeElement( 'fieldset' ) . "\n";
153 * Determine where to get the list of book sources from,
154 * format and output them
156 * @throws MWException
159 private function showList() {
162 # Hook to allow extensions to insert additional HTML,
163 # e.g. for API-interacting plugins and so on
164 wfRunHooks( 'BookInformation', array( $this->isbn
, $this->getOutput() ) );
166 # Check for a local page such as Project:Book_sources and use that if available
167 $page = $this->msg( 'booksources' )->inContentLanguage()->text();
168 $title = Title
::makeTitleSafe( NS_PROJECT
, $page ); # Show list in content language
169 if ( is_object( $title ) && $title->exists() ) {
170 $rev = Revision
::newFromTitle( $title, false, Revision
::READ_NORMAL
);
171 $content = $rev->getContent();
173 if ( $content instanceof TextContent
) {
174 //XXX: in the future, this could be stored as structured data, defining a list of book sources
176 $text = $content->getNativeData();
177 $this->getOutput()->addWikiText( str_replace( 'MAGICNUMBER', $this->isbn
, $text ) );
181 throw new MWException( "Unexpected content type for book sources: " . $content->getModel() );
185 # Fall back to the defaults given in the language file
186 $this->getOutput()->addWikiMsg( 'booksources-text' );
187 $this->getOutput()->addHTML( '<ul>' );
188 $items = $wgContLang->getBookstoreList();
189 foreach ( $items as $label => $url ) {
190 $this->getOutput()->addHTML( $this->makeListItem( $label, $url ) );
192 $this->getOutput()->addHTML( '</ul>' );
198 * Format a book source list item
200 * @param string $label Book source label
201 * @param string $url Book source URL
204 private function makeListItem( $label, $url ) {
205 $url = str_replace( '$1', $this->isbn
, $url );
207 return Html
::rawElement( 'li', array(),
208 Html
::element( 'a', array( 'href' => $url, 'class' => 'external' ), $label ) );
211 protected function getGroupName() {