MediaWiki REL1_35
SpecialBookSources.php
Go to the documentation of this file.
1<?php
26
35 public function __construct() {
36 parent::__construct( 'Booksources' );
37 }
38
42 public function execute( $isbn ) {
43 $out = $this->getOutput();
44
45 $this->setHeaders();
46 $this->outputHeader();
47
48 // User provided ISBN
49 $isbn = $isbn ?: $this->getRequest()->getText( 'isbn' );
50 $isbn = trim( $isbn );
51
52 $this->buildForm( $isbn );
53
54 if ( $isbn !== '' ) {
55 if ( !self::isValidISBN( $isbn ) ) {
56 $out->wrapWikiMsg(
57 "<div class=\"error\">\n$1\n</div>",
58 'booksources-invalid-isbn'
59 );
60 }
61
62 $this->showList( $isbn );
63 }
64 }
65
72 public static function isValidISBN( $isbn ) {
73 $isbn = self::cleanIsbn( $isbn );
74 $sum = 0;
75 if ( strlen( $isbn ) == 13 ) {
76 for ( $i = 0; $i < 12; $i++ ) {
77 if ( $isbn[$i] === 'X' ) {
78 return false;
79 } elseif ( $i % 2 == 0 ) {
80 $sum += $isbn[$i];
81 } else {
82 $sum += 3 * $isbn[$i];
83 }
84 }
85
86 $check = ( 10 - ( $sum % 10 ) ) % 10;
87 if ( (string)$check === $isbn[12] ) {
88 return true;
89 }
90 } elseif ( strlen( $isbn ) == 10 ) {
91 for ( $i = 0; $i < 9; $i++ ) {
92 if ( $isbn[$i] === 'X' ) {
93 return false;
94 }
95 $sum += $isbn[$i] * ( $i + 1 );
96 }
97
98 $check = $sum % 11;
99 if ( $check == 10 ) {
100 $check = "X";
101 }
102 if ( (string)$check === $isbn[9] ) {
103 return true;
104 }
105 }
106
107 return false;
108 }
109
116 private static function cleanIsbn( $isbn ) {
117 return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
118 }
119
125 private function buildForm( $isbn ) {
126 $formDescriptor = [
127 'isbn' => [
128 'type' => 'text',
129 'name' => 'isbn',
130 'label-message' => 'booksources-isbn',
131 'default' => $isbn,
132 'autofocus' => true,
133 'required' => true,
134 ],
135 ];
136
137 $context = new DerivativeContext( $this->getContext() );
138 $context->setTitle( $this->getPageTitle() );
139 HTMLForm::factory( 'ooui', $formDescriptor, $context )
140 ->setWrapperLegendMsg( 'booksources-search-legend' )
141 ->setSubmitTextMsg( 'booksources-search' )
142 ->setMethod( 'get' )
143 ->prepareForm()
144 ->displayForm( false );
145 }
146
155 private function showList( $isbn ) {
156 $out = $this->getOutput();
157
158 $isbn = self::cleanIsbn( $isbn );
159 # Hook to allow extensions to insert additional HTML,
160 # e.g. for API-interacting plugins and so on
161 $this->getHookRunner()->onBookInformation( $isbn, $out );
162
163 # Check for a local page such as Project:Book_sources and use that if available
164 $page = $this->msg( 'booksources' )->inContentLanguage()->text();
165 $title = Title::makeTitleSafe( NS_PROJECT, $page ); # Show list in content language
166 if ( is_object( $title ) && $title->exists() ) {
167 $rev = MediaWikiServices::getInstance()
168 ->getRevisionLookup()
169 ->getRevisionByTitle( $title );
170 $content = $rev->getContent( SlotRecord::MAIN );
171
172 if ( $content instanceof TextContent ) {
173 // XXX: in the future, this could be stored as structured data, defining a list of book sources
174
175 $text = $content->getText();
176 $out->addWikiTextAsInterface( str_replace( 'MAGICNUMBER', $isbn, $text ) );
177
178 return true;
179 } else {
180 throw new MWException( "Unexpected content type for book sources: " . $content->getModel() );
181 }
182 }
183
184 # Fall back to the defaults given in the language file
185 $out->addWikiMsg( 'booksources-text' );
186 $out->addHTML( '<ul>' );
187 $items = MediaWikiServices::getInstance()->getContentLanguage()->getBookstoreList();
188 foreach ( $items as $label => $url ) {
189 $out->addHTML( $this->makeListItem( $isbn, $label, $url ) );
190 }
191 $out->addHTML( '</ul>' );
192
193 return true;
194 }
195
204 private function makeListItem( $isbn, $label, $url ) {
205 $url = str_replace( '$1', $isbn, $url );
206
207 return Html::rawElement( 'li', [],
208 Html::element( 'a', [ 'href' => $url, 'class' => 'external' ], $label )
209 );
210 }
211
212 protected function getGroupName() {
213 return 'wiki';
214 }
215}
An IContextSource implementation which will inherit context from another source but allow individual ...
MediaWiki exception.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Value object representing a content slot associated with a page revision.
Special page outputs information on sourcing a book with a particular ISBN The parser creates links t...
static cleanIsbn( $isbn)
Trim ISBN and remove characters which aren't required.
buildForm( $isbn)
Generate a form to allow users to enter an ISBN.
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...
showList( $isbn)
Determine where to get the list of book sources from, format and output them.
makeListItem( $isbn, $label, $url)
Format a book source list item.
Parent class for all special pages.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!...
getOutput()
Get the OutputPage being used for this instance.
getContext()
Gets the context this SpecialPage is executed in.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getRequest()
Get the WebRequest being used for this instance.
getPageTitle( $subpage=false)
Get a self-referential title object.
Content object implementation for representing flat text.
const NS_PROJECT
Definition Defines.php:74
$content
Definition router.php:76