Expose $wgMaxArticleSize in siteinfo query api
[mediawiki.git] / includes / specials / SpecialBooksources.php
blob11faa2803dc9248393358d740c855cf7a536eb5c
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 protected $isbn = '';
37 public function __construct() {
38 parent::__construct( 'Booksources' );
41 /**
42 * Show the special page
44 * @param string $isbn ISBN passed as a subpage parameter
46 public function execute( $isbn ) {
47 $out = $this->getOutput();
49 $this->setHeaders();
50 $this->outputHeader();
52 $this->isbn = self::cleanIsbn( $isbn ?: $this->getRequest()->getText( 'isbn' ) );
54 $this->buildForm();
56 if ( $this->isbn !== '' ) {
57 if ( !self::isValidISBN( $this->isbn ) ) {
58 $out->wrapWikiMsg(
59 "<div class=\"error\">\n$1\n</div>",
60 'booksources-invalid-isbn'
64 $this->showList();
68 /**
69 * Return whether a given ISBN (10 or 13) is valid.
71 * @param string $isbn ISBN passed for check
72 * @return bool
74 public static function isValidISBN( $isbn ) {
75 $isbn = self::cleanIsbn( $isbn );
76 $sum = 0;
77 if ( strlen( $isbn ) == 13 ) {
78 for ( $i = 0; $i < 12; $i++ ) {
79 if ( $isbn[$i] === 'X' ) {
80 return false;
81 } elseif ( $i % 2 == 0 ) {
82 $sum += $isbn[$i];
83 } else {
84 $sum += 3 * $isbn[$i];
88 $check = ( 10 - ( $sum % 10 ) ) % 10;
89 if ( (string)$check === $isbn[12] ) {
90 return true;
92 } elseif ( strlen( $isbn ) == 10 ) {
93 for ( $i = 0; $i < 9; $i++ ) {
94 if ( $isbn[$i] === 'X' ) {
95 return false;
97 $sum += $isbn[$i] * ( $i + 1 );
100 $check = $sum % 11;
101 if ( $check == 10 ) {
102 $check = "X";
104 if ( (string)$check === $isbn[9] ) {
105 return true;
109 return false;
113 * Trim ISBN and remove characters which aren't required
115 * @param string $isbn Unclean ISBN
116 * @return string
118 private static function cleanIsbn( $isbn ) {
119 return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
123 * Generate a form to allow users to enter an ISBN
125 private function buildForm() {
126 $formDescriptor = [
127 'isbn' => [
128 'type' => 'text',
129 'name' => 'isbn',
130 'label-message' => 'booksources-isbn',
131 'default' => $this->isbn,
132 'autofocus' => true,
133 'required' => true,
137 $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
138 ->setWrapperLegendMsg( 'booksources-search-legend' )
139 ->setSubmitTextMsg( 'booksources-search' )
140 ->setMethod( 'get' )
141 ->prepareForm()
142 ->displayForm( false );
146 * Determine where to get the list of book sources from,
147 * format and output them
149 * @throws MWException
150 * @return bool
152 private function showList() {
153 $out = $this->getOutput();
155 global $wgContLang;
157 # Hook to allow extensions to insert additional HTML,
158 # e.g. for API-interacting plugins and so on
159 Hooks::run( 'BookInformation', [ $this->isbn, $out ] );
161 # Check for a local page such as Project:Book_sources and use that if available
162 $page = $this->msg( 'booksources' )->inContentLanguage()->text();
163 $title = Title::makeTitleSafe( NS_PROJECT, $page ); # Show list in content language
164 if ( is_object( $title ) && $title->exists() ) {
165 $rev = Revision::newFromTitle( $title, false, Revision::READ_NORMAL );
166 $content = $rev->getContent();
168 if ( $content instanceof TextContent ) {
169 // XXX: in the future, this could be stored as structured data, defining a list of book sources
171 $text = $content->getNativeData();
172 $out->addWikiText( str_replace( 'MAGICNUMBER', $this->isbn, $text ) );
174 return true;
175 } else {
176 throw new MWException( "Unexpected content type for book sources: " . $content->getModel() );
180 # Fall back to the defaults given in the language file
181 $out->addWikiMsg( 'booksources-text' );
182 $out->addHTML( '<ul>' );
183 $items = $wgContLang->getBookstoreList();
184 foreach ( $items as $label => $url ) {
185 $out->addHTML( $this->makeListItem( $label, $url ) );
187 $out->addHTML( '</ul>' );
189 return true;
193 * Format a book source list item
195 * @param string $label Book source label
196 * @param string $url Book source URL
197 * @return string
199 private function makeListItem( $label, $url ) {
200 $url = str_replace( '$1', $this->isbn, $url );
202 return Html::rawElement( 'li', [],
203 Html::element( 'a', [ 'href' => $url, 'class' => 'external' ], $label )
207 protected function getGroupName() {
208 return 'wiki';