MediaWiki master
SpecialBookSources.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Specials;
25
32use TextContent;
33use UnexpectedValueException;
34
43
44 private RevisionLookup $revisionLookup;
45 private TitleFactory $titleFactory;
46
51 public function __construct(
52 RevisionLookup $revisionLookup,
53 TitleFactory $titleFactory
54 ) {
55 parent::__construct( 'Booksources' );
56 $this->revisionLookup = $revisionLookup;
57 $this->titleFactory = $titleFactory;
58 }
59
63 public function execute( $isbn ) {
64 $out = $this->getOutput();
65
66 $this->setHeaders();
67 $this->outputHeader();
68
69 // User provided ISBN
70 $isbn = $isbn ?: $this->getRequest()->getText( 'isbn' );
71 $isbn = trim( $isbn );
72
73 $this->buildForm( $isbn );
74
75 if ( $isbn !== '' ) {
76 if ( !self::isValidISBN( $isbn ) ) {
77 $out->wrapWikiMsg(
78 "<div class=\"error\">\n$1\n</div>",
79 'booksources-invalid-isbn'
80 );
81 }
82
83 $this->showList( $isbn );
84 }
85 }
86
93 public static function isValidISBN( $isbn ) {
94 $isbn = self::cleanIsbn( $isbn );
95 $sum = 0;
96 if ( strlen( $isbn ) == 13 ) {
97 for ( $i = 0; $i < 12; $i++ ) {
98 if ( $isbn[$i] === 'X' ) {
99 return false;
100 } elseif ( $i % 2 == 0 ) {
101 $sum += (int)$isbn[$i];
102 } else {
103 $sum += 3 * (int)$isbn[$i];
104 }
105 }
106
107 $check = ( 10 - ( $sum % 10 ) ) % 10;
108 if ( (string)$check === $isbn[12] ) {
109 return true;
110 }
111 } elseif ( strlen( $isbn ) == 10 ) {
112 for ( $i = 0; $i < 9; $i++ ) {
113 if ( $isbn[$i] === 'X' ) {
114 return false;
115 }
116 $sum += (int)$isbn[$i] * ( $i + 1 );
117 }
118
119 $check = $sum % 11;
120 if ( $check == 10 ) {
121 $check = "X";
122 }
123 if ( (string)$check === $isbn[9] ) {
124 return true;
125 }
126 }
127
128 return false;
129 }
130
137 private static function cleanIsbn( $isbn ) {
138 return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
139 }
140
146 private function buildForm( $isbn ) {
147 $formDescriptor = [
148 'isbn' => [
149 'type' => 'text',
150 'name' => 'isbn',
151 'label-message' => 'booksources-isbn',
152 'default' => $isbn,
153 'autofocus' => true,
154 'required' => true,
155 ],
156 ];
157
158 HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
159 ->setTitle( $this->getPageTitle() )
160 ->setWrapperLegendMsg( 'booksources-search-legend' )
161 ->setSubmitTextMsg( 'booksources-search' )
162 ->setMethod( 'get' )
163 ->prepareForm()
164 ->displayForm( false );
165 }
166
174 private function showList( $isbn ) {
175 $out = $this->getOutput();
176
177 $isbn = self::cleanIsbn( $isbn );
178 # Hook to allow extensions to insert additional HTML,
179 # e.g. for API-interacting plugins and so on
180 $this->getHookRunner()->onBookInformation( $isbn, $out );
181
182 # Check for a local page such as Project:Book_sources and use that if available
183 $page = $this->msg( 'booksources' )->inContentLanguage()->text();
184 // Show list in content language
185 $title = $this->titleFactory->makeTitleSafe( NS_PROJECT, $page );
186 if ( is_object( $title ) && $title->exists() ) {
187 $rev = $this->revisionLookup->getRevisionByTitle( $title );
188 $content = $rev->getContent( SlotRecord::MAIN );
189
190 if ( $content instanceof TextContent ) {
191 // XXX: in the future, this could be stored as structured data, defining a list of book sources
192
193 $text = $content->getText();
194 $out->addWikiTextAsInterface( str_replace( 'MAGICNUMBER', $isbn, $text ) );
195
196 return true;
197 } else {
198 throw new UnexpectedValueException(
199 "Unexpected content type for book sources: " . $content->getModel()
200 );
201 }
202 }
203
204 # Fall back to the defaults given in the language file
205 $out->addWikiMsg( 'booksources-text' );
206 $out->addHTML( '<ul>' );
207 $items = $this->getContentLanguage()->getBookstoreList();
208 foreach ( $items as $label => $url ) {
209 $out->addHTML( $this->makeListItem( $isbn, $label, $url ) );
210 }
211 $out->addHTML( '</ul>' );
212
213 return true;
214 }
215
224 private function makeListItem( $isbn, $label, $url ) {
225 $url = str_replace( '$1', $isbn, $url );
226
227 return Html::rawElement( 'li', [],
228 Html::element( 'a', [ 'href' => $url, 'class' => 'external' ], $label )
229 );
230 }
231
232 protected function getGroupName() {
233 return 'wiki';
234 }
235}
236
238class_alias( SpecialBookSources::class, 'SpecialBookSources' );
const NS_PROJECT
Definition Defines.php:68
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:206
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
Value object representing a content slot associated with a page revision.
Parent class for all special pages.
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getPageTitle( $subpage=false)
Get a self-referential title object.
getContext()
Gets the context this SpecialPage is executed in.
getRequest()
Get the WebRequest being used for this instance.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
getContentLanguage()
Shortcut to get content language.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
Special page outputs information on sourcing a book with a particular ISBN The parser creates links t...
__construct(RevisionLookup $revisionLookup, TitleFactory $titleFactory)
static isValidISBN( $isbn)
Return whether a given ISBN (10 or 13) is valid.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Creates Title objects.
Content object implementation for representing flat text.
Service for looking up page revisions.
element(SerializerNode $parent, SerializerNode $node, $contents)
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...