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
50 public function __construct(
51 RevisionLookup $revisionLookup,
52 TitleFactory $titleFactory
53 ) {
54 parent::__construct( 'Booksources' );
55 $this->revisionLookup = $revisionLookup;
56 $this->titleFactory = $titleFactory;
57 }
58
62 public function execute( $isbn ) {
63 $out = $this->getOutput();
64
65 $this->setHeaders();
66 $this->outputHeader();
67
68 // User provided ISBN
69 $isbn = $isbn ?: $this->getRequest()->getText( 'isbn' );
70 $isbn = trim( $isbn );
71
72 $this->buildForm( $isbn );
73
74 if ( $isbn !== '' ) {
75 if ( !self::isValidISBN( $isbn ) ) {
76 $out->wrapWikiMsg(
77 "<div class=\"error\">\n$1\n</div>",
78 'booksources-invalid-isbn'
79 );
80 }
81
82 $this->showList( $isbn );
83 }
84 }
85
92 public static function isValidISBN( $isbn ) {
93 $isbn = self::cleanIsbn( $isbn );
94 $sum = 0;
95 if ( strlen( $isbn ) == 13 ) {
96 for ( $i = 0; $i < 12; $i++ ) {
97 if ( $isbn[$i] === 'X' ) {
98 return false;
99 } elseif ( $i % 2 == 0 ) {
100 $sum += (int)$isbn[$i];
101 } else {
102 $sum += 3 * (int)$isbn[$i];
103 }
104 }
105
106 $check = ( 10 - ( $sum % 10 ) ) % 10;
107 if ( (string)$check === $isbn[12] ) {
108 return true;
109 }
110 } elseif ( strlen( $isbn ) == 10 ) {
111 for ( $i = 0; $i < 9; $i++ ) {
112 if ( $isbn[$i] === 'X' ) {
113 return false;
114 }
115 $sum += (int)$isbn[$i] * ( $i + 1 );
116 }
117
118 $check = $sum % 11;
119 if ( $check == 10 ) {
120 $check = "X";
121 }
122 if ( (string)$check === $isbn[9] ) {
123 return true;
124 }
125 }
126
127 return false;
128 }
129
136 private static function cleanIsbn( $isbn ) {
137 return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
138 }
139
145 private function buildForm( $isbn ) {
146 $formDescriptor = [
147 'isbn' => [
148 'type' => 'text',
149 'name' => 'isbn',
150 'label-message' => 'booksources-isbn',
151 'default' => $isbn,
152 'autofocus' => true,
153 'required' => true,
154 ],
155 ];
156
157 HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
158 ->setTitle( $this->getPageTitle() )
159 ->setWrapperLegendMsg( 'booksources-search-legend' )
160 ->setSubmitTextMsg( 'booksources-search' )
161 ->setMethod( 'get' )
162 ->prepareForm()
163 ->displayForm( false );
164 }
165
173 private function showList( $isbn ) {
174 $out = $this->getOutput();
175
176 $isbn = self::cleanIsbn( $isbn );
177 # Hook to allow extensions to insert additional HTML,
178 # e.g. for API-interacting plugins and so on
179 $this->getHookRunner()->onBookInformation( $isbn, $out );
180
181 # Check for a local page such as Project:Book_sources and use that if available
182 $page = $this->msg( 'booksources' )->inContentLanguage()->text();
183 // Show list in content language
184 $title = $this->titleFactory->makeTitleSafe( NS_PROJECT, $page );
185 if ( is_object( $title ) && $title->exists() ) {
186 $rev = $this->revisionLookup->getRevisionByTitle( $title );
187 $content = $rev->getContent( SlotRecord::MAIN );
188
189 if ( $content instanceof TextContent ) {
190 // XXX: in the future, this could be stored as structured data, defining a list of book sources
191
192 $text = $content->getText();
193 $out->addWikiTextAsInterface( str_replace( 'MAGICNUMBER', $isbn, $text ) );
194
195 return true;
196 } else {
197 throw new UnexpectedValueException(
198 "Unexpected content type for book sources: " . $content->getModel()
199 );
200 }
201 }
202
203 # Fall back to the defaults given in the language file
204 $out->addWikiMsg( 'booksources-text' );
205 $out->addHTML( '<ul>' );
206 $items = $this->getContentLanguage()->getBookstoreList();
207 foreach ( $items as $label => $url ) {
208 $out->addHTML( $this->makeListItem( $isbn, $label, $url ) );
209 }
210 $out->addHTML( '</ul>' );
211
212 return true;
213 }
214
223 private function makeListItem( $isbn, $label, $url ) {
224 $url = str_replace( '$1', $isbn, $url );
225
226 return Html::rawElement( 'li', [],
227 Html::element( 'a', [ 'href' => $url, 'class' => 'external' ], $label )
228 );
229 }
230
231 protected function getGroupName() {
232 return 'wiki';
233 }
234}
235
237class_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:208
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)