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
37 public function __construct() {
38 parent
::__construct( 'Booksources' );
42 * Show the special page
44 * @param string $isbn ISBN passed as a subpage parameter
46 public function execute( $isbn ) {
48 $this->outputHeader();
49 $this->isbn
= self
::cleanIsbn( $isbn ?
: $this->getRequest()->getText( 'isbn' ) );
50 $this->getOutput()->addHTML( $this->makeForm() );
51 if ( $this->isbn
!== '' ) {
52 if ( !self
::isValidISBN( $this->isbn
) ) {
53 $this->getOutput()->wrapWikiMsg(
54 "<div class=\"error\">\n$1\n</div>",
55 'booksources-invalid-isbn'
63 * Return whether a given ISBN (10 or 13) is valid.
65 * @param string $isbn ISBN passed for check
68 public static function isValidISBN( $isbn ) {
69 $isbn = self
::cleanIsbn( $isbn );
71 if ( strlen( $isbn ) == 13 ) {
72 for ( $i = 0; $i < 12; $i++
) {
73 if ( $isbn[$i] === 'X' ) {
75 } elseif ( $i %
2 == 0 ) {
78 $sum +
= 3 * $isbn[$i];
82 $check = ( 10 - ( $sum %
10 ) ) %
10;
83 if ( (string)$check === $isbn[12] ) {
86 } elseif ( strlen( $isbn ) == 10 ) {
87 for ( $i = 0; $i < 9; $i++
) {
88 if ( $isbn[$i] === 'X' ) {
91 $sum +
= $isbn[$i] * ( $i +
1 );
98 if ( (string)$check === $isbn[9] ) {
107 * Trim ISBN and remove characters which aren't required
109 * @param string $isbn Unclean ISBN
112 private static function cleanIsbn( $isbn ) {
113 return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
117 * Generate a form to allow users to enter an ISBN
121 private function makeForm() {
122 $form = Html
::openElement( 'fieldset' ) . "\n";
123 $form .= Html
::element(
126 $this->msg( 'booksources-search-legend' )->text()
128 $form .= Html
::openElement( 'form', [ 'method' => 'get', 'action' => wfScript() ] ) . "\n";
129 $form .= Html
::hidden( 'title', $this->getPageTitle()->getPrefixedText() ) . "\n";
130 $form .= '<p>' . Xml
::inputLabel(
131 $this->msg( 'booksources-isbn' )->text(),
136 [ 'autofocus' => '', 'class' => 'mw-ui-input-inline' ]
139 $form .= ' ' . Html
::submitButton(
140 $this->msg( 'booksources-search' )->text(),
141 [], [ 'mw-ui-progressive' ]
144 $form .= Html
::closeElement( 'form' ) . "\n";
145 $form .= Html
::closeElement( 'fieldset' ) . "\n";
151 * Determine where to get the list of book sources from,
152 * format and output them
154 * @throws MWException
157 private function showList() {
160 # Hook to allow extensions to insert additional HTML,
161 # e.g. for API-interacting plugins and so on
162 Hooks
::run( 'BookInformation', [ $this->isbn
, $this->getOutput() ] );
164 # Check for a local page such as Project:Book_sources and use that if available
165 $page = $this->msg( 'booksources' )->inContentLanguage()->text();
166 $title = Title
::makeTitleSafe( NS_PROJECT
, $page ); # Show list in content language
167 if ( is_object( $title ) && $title->exists() ) {
168 $rev = Revision
::newFromTitle( $title, false, Revision
::READ_NORMAL
);
169 $content = $rev->getContent();
171 if ( $content instanceof TextContent
) {
172 // XXX: in the future, this could be stored as structured data, defining a list of book sources
174 $text = $content->getNativeData();
175 $this->getOutput()->addWikiText( str_replace( 'MAGICNUMBER', $this->isbn
, $text ) );
179 throw new MWException( "Unexpected content type for book sources: " . $content->getModel() );
183 # Fall back to the defaults given in the language file
184 $this->getOutput()->addWikiMsg( 'booksources-text' );
185 $this->getOutput()->addHTML( '<ul>' );
186 $items = $wgContLang->getBookstoreList();
187 foreach ( $items as $label => $url ) {
188 $this->getOutput()->addHTML( $this->makeListItem( $label, $url ) );
190 $this->getOutput()->addHTML( '</ul>' );
196 * Format a book source list item
198 * @param string $label Book source label
199 * @param string $url Book source URL
202 private function makeListItem( $label, $url ) {
203 $url = str_replace( '$1', $this->isbn
, $url );
205 return Html
::rawElement( 'li', [],
206 Html
::element( 'a', [ 'href' => $url, 'class' => 'external' ], $label )
210 protected function getGroupName() {