Merge "Tidy Message::parseAsBlock() by enabling tidy in MessageCache"
[lhc/web/wiklou.git] / includes / specials / SpecialBooksources.php
1 <?php
2 /**
3 * Implements Special:Booksources
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 use MediaWiki\MediaWikiServices;
25
26 /**
27 * Special page outputs information on sourcing a book with a particular ISBN
28 * The parser creates links to this page when dealing with ISBNs in wikitext
29 *
30 * @author Rob Church <robchur@gmail.com>
31 * @ingroup SpecialPage
32 */
33 class SpecialBookSources extends SpecialPage {
34 public function __construct() {
35 parent::__construct( 'Booksources' );
36 }
37
38 /**
39 * Show the special page
40 *
41 * @param string $isbn ISBN passed as a subpage parameter
42 */
43 public function execute( $isbn ) {
44 $out = $this->getOutput();
45
46 $this->setHeaders();
47 $this->outputHeader();
48
49 // User provided ISBN
50 $isbn = $isbn ?: $this->getRequest()->getText( 'isbn' );
51 $isbn = trim( $isbn );
52
53 $this->buildForm( $isbn );
54
55 if ( $isbn !== '' ) {
56 if ( !self::isValidISBN( $isbn ) ) {
57 $out->wrapWikiMsg(
58 "<div class=\"error\">\n$1\n</div>",
59 'booksources-invalid-isbn'
60 );
61 }
62
63 $this->showList( $isbn );
64 }
65 }
66
67 /**
68 * Return whether a given ISBN (10 or 13) is valid.
69 *
70 * @param string $isbn ISBN passed for check
71 * @return bool
72 */
73 public static function isValidISBN( $isbn ) {
74 $isbn = self::cleanIsbn( $isbn );
75 $sum = 0;
76 if ( strlen( $isbn ) == 13 ) {
77 for ( $i = 0; $i < 12; $i++ ) {
78 if ( $isbn[$i] === 'X' ) {
79 return false;
80 } elseif ( $i % 2 == 0 ) {
81 $sum += $isbn[$i];
82 } else {
83 $sum += 3 * $isbn[$i];
84 }
85 }
86
87 $check = ( 10 - ( $sum % 10 ) ) % 10;
88 if ( (string)$check === $isbn[12] ) {
89 return true;
90 }
91 } elseif ( strlen( $isbn ) == 10 ) {
92 for ( $i = 0; $i < 9; $i++ ) {
93 if ( $isbn[$i] === 'X' ) {
94 return false;
95 }
96 $sum += $isbn[$i] * ( $i + 1 );
97 }
98
99 $check = $sum % 11;
100 if ( $check == 10 ) {
101 $check = "X";
102 }
103 if ( (string)$check === $isbn[9] ) {
104 return true;
105 }
106 }
107
108 return false;
109 }
110
111 /**
112 * Trim ISBN and remove characters which aren't required
113 *
114 * @param string $isbn Unclean ISBN
115 * @return string
116 */
117 private static function cleanIsbn( $isbn ) {
118 return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
119 }
120
121 /**
122 * Generate a form to allow users to enter an ISBN
123 *
124 * @param string $isbn
125 */
126 private function buildForm( $isbn ) {
127 $formDescriptor = [
128 'isbn' => [
129 'type' => 'text',
130 'name' => 'isbn',
131 'label-message' => 'booksources-isbn',
132 'default' => $isbn,
133 'autofocus' => true,
134 'required' => true,
135 ],
136 ];
137
138 $context = new DerivativeContext( $this->getContext() );
139 $context->setTitle( $this->getPageTitle() );
140 HTMLForm::factory( 'ooui', $formDescriptor, $context )
141 ->setWrapperLegendMsg( 'booksources-search-legend' )
142 ->setSubmitTextMsg( 'booksources-search' )
143 ->setMethod( 'get' )
144 ->prepareForm()
145 ->displayForm( false );
146 }
147
148 /**
149 * Determine where to get the list of book sources from,
150 * format and output them
151 *
152 * @param string $isbn
153 * @throws MWException
154 * @return bool
155 */
156 private function showList( $isbn ) {
157 $out = $this->getOutput();
158
159 $isbn = self::cleanIsbn( $isbn );
160 # Hook to allow extensions to insert additional HTML,
161 # e.g. for API-interacting plugins and so on
162 Hooks::run( 'BookInformation', [ $isbn, $out ] );
163
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();
170
171 if ( $content instanceof TextContent ) {
172 // XXX: in the future, this could be stored as structured data, defining a list of book sources
173
174 $text = $content->getNativeData();
175 $out->addWikiTextAsInterface( str_replace( 'MAGICNUMBER', $isbn, $text ) );
176
177 return true;
178 } else {
179 throw new MWException( "Unexpected content type for book sources: " . $content->getModel() );
180 }
181 }
182
183 # Fall back to the defaults given in the language file
184 $out->addWikiMsg( 'booksources-text' );
185 $out->addHTML( '<ul>' );
186 $items = MediaWikiServices::getInstance()->getContentLanguage()->getBookstoreList();
187 foreach ( $items as $label => $url ) {
188 $out->addHTML( $this->makeListItem( $isbn, $label, $url ) );
189 }
190 $out->addHTML( '</ul>' );
191
192 return true;
193 }
194
195 /**
196 * Format a book source list item
197 *
198 * @param string $isbn
199 * @param string $label Book source label
200 * @param string $url Book source URL
201 * @return string
202 */
203 private function makeListItem( $isbn, $label, $url ) {
204 $url = str_replace( '$1', $isbn, $url );
205
206 return Html::rawElement( 'li', [],
207 Html::element( 'a', [ 'href' => $url, 'class' => 'external' ], $label )
208 );
209 }
210
211 protected function getGroupName() {
212 return 'wiki';
213 }
214 }