MediaWiki REL1_32
SpecialBooksources.php
Go to the documentation of this file.
1<?php
25
34 public function __construct() {
35 parent::__construct( 'Booksources' );
36 }
37
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
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
117 private static function cleanIsbn( $isbn ) {
118 return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
119 }
120
126 private function buildForm( $isbn ) {
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
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->addWikiText( 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
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}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
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.
static newFromTitle(LinkTarget $linkTarget, $id=0, $flags=0)
Load either the current, or a specified, revision that's attached to a given link target.
Definition Revision.php:133
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...
execute( $isbn)
Show the special page.
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)
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.
per default it will return the text for text based content
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
const NS_PROJECT
Definition Defines.php:68
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead & $formDescriptor
Definition hooks.txt:2208
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition hooks.txt:2885
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:994
null for the local wiki Added in
Definition hooks.txt:1627
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition hooks.txt:894
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition hooks.txt:1818
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
$content