Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / specials / SpecialBooksources.php
blob91a148dec08dfd3fd62a740c5c6c1a64d12d148c
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 * @ingroup SpecialPage
31 class SpecialBookSources extends SpecialPage {
32 /**
33 * ISBN passed to the page, if any
35 private $isbn = '';
37 /**
38 * Constructor
40 public function __construct() {
41 parent::__construct( 'Booksources' );
44 /**
45 * Show the special page
47 * @param string $isbn ISBN passed as a subpage parameter
49 public function execute( $isbn ) {
50 $this->setHeaders();
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'
61 $this->showList();
65 /**
66 * Returns whether a given ISBN (10 or 13) is valid. True indicates validity.
67 * @param string $isbn ISBN passed for check
68 * @return bool
70 public static function isValidISBN( $isbn ) {
71 $isbn = self::cleanIsbn( $isbn );
72 $sum = 0;
73 if ( strlen( $isbn ) == 13 ) {
74 for ( $i = 0; $i < 12; $i++ ) {
75 if ( $isbn[$i] === 'X' ) {
76 return false;
77 } elseif ( $i % 2 == 0 ) {
78 $sum += $isbn[$i];
79 } else {
80 $sum += 3 * $isbn[$i];
84 $check = ( 10 - ( $sum % 10 ) ) % 10;
85 if ( (string)$check === $isbn[12] ) {
86 return true;
88 } elseif ( strlen( $isbn ) == 10 ) {
89 for ( $i = 0; $i < 9; $i++ ) {
90 if ( $isbn[$i] === 'X' ) {
91 return false;
93 $sum += $isbn[$i] * ( $i + 1 );
96 $check = $sum % 11;
97 if ( $check == 10 ) {
98 $check = "X";
100 if ( (string)$check === $isbn[9] ) {
101 return true;
105 return false;
109 * Trim ISBN and remove characters which aren't required
111 * @param string $isbn Unclean ISBN
112 * @return string
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
121 * @return string
123 private function makeForm() {
124 $form = Html::openElement( 'fieldset' ) . "\n";
125 $form .= Html::element(
126 'legend',
127 array(),
128 $this->msg( 'booksources-search-legend' )->text()
129 ) . "\n";
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(),
134 'isbn',
135 'isbn',
137 $this->isbn,
138 array( 'autofocus' => true, 'class' => 'mw-ui-input-inline' )
141 $form .= '&#160;' . Html::submitButton(
142 $this->msg( 'booksources-search' )->text(),
143 array(), array( 'mw-ui-progressive' )
144 ) . "</p>\n";
146 $form .= Html::closeElement( 'form' ) . "\n";
147 $form .= Html::closeElement( 'fieldset' ) . "\n";
149 return $form;
153 * Determine where to get the list of book sources from,
154 * format and output them
156 * @throws MWException
157 * @return string
159 private function showList() {
160 global $wgContLang;
162 # Hook to allow extensions to insert additional HTML,
163 # e.g. for API-interacting plugins and so on
164 Hooks::run( '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 ) );
179 return true;
180 } else {
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>' );
194 return true;
198 * Format a book source list item
200 * @param string $label Book source label
201 * @param string $url Book source URL
202 * @return string
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() {
212 return 'wiki';