MediaWiki master
SpecialBookSources.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
30use UnexpectedValueException;
31
42
43 private RevisionLookup $revisionLookup;
44 private TitleFactory $titleFactory;
45
46 public function __construct(
47 RevisionLookup $revisionLookup,
48 TitleFactory $titleFactory
49 ) {
50 parent::__construct( 'Booksources' );
51 $this->revisionLookup = $revisionLookup;
52 $this->titleFactory = $titleFactory;
53 }
54
58 public function execute( $isbn ) {
59 $out = $this->getOutput();
60
61 $this->setHeaders();
62 $this->outputHeader();
63
64 // User provided ISBN
65 $isbn = $isbn ?: $this->getRequest()->getText( 'isbn' );
66 $isbn = trim( $isbn );
67
68 $this->buildForm( $isbn );
69
70 if ( $isbn !== '' ) {
71 if ( !self::isValidISBN( $isbn ) ) {
72 $out->wrapWikiMsg(
73 "<div class=\"error\">\n$1\n</div>",
74 'booksources-invalid-isbn'
75 );
76 }
77
78 $this->showList( $isbn );
79 }
80 }
81
88 public static function isValidISBN( $isbn ) {
89 $isbn = self::cleanIsbn( $isbn );
90 $sum = 0;
91 if ( strlen( $isbn ) == 13 ) {
92 for ( $i = 0; $i < 12; $i++ ) {
93 if ( $isbn[$i] === 'X' ) {
94 return false;
95 } elseif ( $i % 2 == 0 ) {
96 $sum += (int)$isbn[$i];
97 } else {
98 $sum += 3 * (int)$isbn[$i];
99 }
100 }
101
102 $check = ( 10 - ( $sum % 10 ) ) % 10;
103 if ( (string)$check === $isbn[12] ) {
104 return true;
105 }
106 } elseif ( strlen( $isbn ) == 10 ) {
107 for ( $i = 0; $i < 9; $i++ ) {
108 if ( $isbn[$i] === 'X' ) {
109 return false;
110 }
111 $sum += (int)$isbn[$i] * ( $i + 1 );
112 }
113
114 $check = $sum % 11;
115 if ( $check == 10 ) {
116 $check = "X";
117 }
118 if ( (string)$check === $isbn[9] ) {
119 return true;
120 }
121 }
122
123 return false;
124 }
125
132 private static function cleanIsbn( $isbn ) {
133 return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
134 }
135
141 private function buildForm( $isbn ) {
142 $formDescriptor = [
143 'isbn' => [
144 'type' => 'text',
145 'name' => 'isbn',
146 'label-message' => 'booksources-isbn',
147 'default' => $isbn,
148 'autofocus' => true,
149 'required' => true,
150 ],
151 ];
152
153 HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
154 ->setTitle( $this->getPageTitle() )
155 ->setWrapperLegendMsg( 'booksources-search-legend' )
156 ->setSubmitTextMsg( 'booksources-search' )
157 ->setMethod( 'get' )
158 ->prepareForm()
159 ->displayForm( false );
160 }
161
169 private function showList( $isbn ) {
170 $out = $this->getOutput();
171
172 $isbn = self::cleanIsbn( $isbn );
173 # Hook to allow extensions to insert additional HTML,
174 # e.g. for API-interacting plugins and so on
175 $this->getHookRunner()->onBookInformation( $isbn, $out );
176
177 # Check for a local page such as Project:Book_sources and use that if available
178 $page = $this->msg( 'booksources' )->inContentLanguage()->text();
179 // Show list in content language
180 $title = $this->titleFactory->makeTitleSafe( NS_PROJECT, $page );
181 if ( is_object( $title ) && $title->exists() ) {
182 $rev = $this->revisionLookup->getRevisionByTitle( $title );
183 $content = $rev->getContent( SlotRecord::MAIN );
184
185 if ( $content instanceof TextContent ) {
186 // XXX: in the future, this could be stored as structured data, defining a list of book sources
187
188 $text = $content->getText();
189 $out->addWikiTextAsInterface( str_replace( 'MAGICNUMBER', $isbn, $text ) );
190
191 return true;
192 } else {
193 throw new UnexpectedValueException(
194 "Unexpected content type for book sources: " . $content->getModel()
195 );
196 }
197 }
198
199 # Fall back to the defaults given in the language file
200 $out->addWikiMsg( 'booksources-text' );
201 $out->addHTML( '<ul>' );
202 $items = $this->getContentLanguage()->getBookstoreList();
203 foreach ( $items as $label => $url ) {
204 $out->addHTML( $this->makeListItem( $isbn, $label, $url ) );
205 }
206 $out->addHTML( '</ul>' );
207
208 return true;
209 }
210
219 private function makeListItem( $isbn, $label, $url ) {
220 $url = str_replace( '$1', $isbn, $url );
221
222 return Html::rawElement( 'li', [],
223 Html::element( 'a', [ 'href' => $url, 'class' => 'external' ], $label )
224 );
225 }
226
227 protected function getGroupName() {
228 return 'wiki';
229 }
230}
231
233class_alias( SpecialBookSources::class, 'SpecialBookSources' );
const NS_PROJECT
Definition Defines.php:69
Content object implementation for representing flat text.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:209
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 By default the message key is the canonical name of...
Information on citing a book with a particular ISBN.
__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.
Service for looking up page revisions.
element(SerializerNode $parent, SerializerNode $node, $contents)