MediaWiki  1.33.0
SpecialBooksources.php
Go to the documentation of this file.
1 <?php
25 
34  public function __construct() {
35  parent::__construct( 'Booksources' );
36  }
37 
41  public function execute( $isbn ) {
42  $out = $this->getOutput();
43 
44  $this->setHeaders();
45  $this->outputHeader();
46 
47  // User provided ISBN
48  $isbn = $isbn ?: $this->getRequest()->getText( 'isbn' );
49  $isbn = trim( $isbn );
50 
51  $this->buildForm( $isbn );
52 
53  if ( $isbn !== '' ) {
54  if ( !self::isValidISBN( $isbn ) ) {
55  $out->wrapWikiMsg(
56  "<div class=\"error\">\n$1\n</div>",
57  'booksources-invalid-isbn'
58  );
59  }
60 
61  $this->showList( $isbn );
62  }
63  }
64 
71  public static function isValidISBN( $isbn ) {
72  $isbn = self::cleanIsbn( $isbn );
73  $sum = 0;
74  if ( strlen( $isbn ) == 13 ) {
75  for ( $i = 0; $i < 12; $i++ ) {
76  if ( $isbn[$i] === 'X' ) {
77  return false;
78  } elseif ( $i % 2 == 0 ) {
79  $sum += $isbn[$i];
80  } else {
81  $sum += 3 * $isbn[$i];
82  }
83  }
84 
85  $check = ( 10 - ( $sum % 10 ) ) % 10;
86  if ( (string)$check === $isbn[12] ) {
87  return true;
88  }
89  } elseif ( strlen( $isbn ) == 10 ) {
90  for ( $i = 0; $i < 9; $i++ ) {
91  if ( $isbn[$i] === 'X' ) {
92  return false;
93  }
94  $sum += $isbn[$i] * ( $i + 1 );
95  }
96 
97  $check = $sum % 11;
98  if ( $check == 10 ) {
99  $check = "X";
100  }
101  if ( (string)$check === $isbn[9] ) {
102  return true;
103  }
104  }
105 
106  return false;
107  }
108 
115  private static function cleanIsbn( $isbn ) {
116  return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
117  }
118 
124  private function buildForm( $isbn ) {
125  $formDescriptor = [
126  'isbn' => [
127  'type' => 'text',
128  'name' => 'isbn',
129  'label-message' => 'booksources-isbn',
130  'default' => $isbn,
131  'autofocus' => true,
132  'required' => true,
133  ],
134  ];
135 
136  $context = new DerivativeContext( $this->getContext() );
137  $context->setTitle( $this->getPageTitle() );
139  ->setWrapperLegendMsg( 'booksources-search-legend' )
140  ->setSubmitTextMsg( 'booksources-search' )
141  ->setMethod( 'get' )
142  ->prepareForm()
143  ->displayForm( false );
144  }
145 
154  private function showList( $isbn ) {
155  $out = $this->getOutput();
156 
157  $isbn = self::cleanIsbn( $isbn );
158  # Hook to allow extensions to insert additional HTML,
159  # e.g. for API-interacting plugins and so on
160  Hooks::run( 'BookInformation', [ $isbn, $out ] );
161 
162  # Check for a local page such as Project:Book_sources and use that if available
163  $page = $this->msg( 'booksources' )->inContentLanguage()->text();
165  if ( is_object( $title ) && $title->exists() ) {
166  $rev = Revision::newFromTitle( $title, false, Revision::READ_NORMAL );
167  $content = $rev->getContent();
168 
169  if ( $content instanceof TextContent ) {
170  // XXX: in the future, this could be stored as structured data, defining a list of book sources
171 
172  $text = $content->getText();
173  $out->addWikiTextAsInterface( str_replace( 'MAGICNUMBER', $isbn, $text ) );
174 
175  return true;
176  } else {
177  throw new MWException( "Unexpected content type for book sources: " . $content->getModel() );
178  }
179  }
180 
181  # Fall back to the defaults given in the language file
182  $out->addWikiMsg( 'booksources-text' );
183  $out->addHTML( '<ul>' );
184  $items = MediaWikiServices::getInstance()->getContentLanguage()->getBookstoreList();
185  foreach ( $items as $label => $url ) {
186  $out->addHTML( $this->makeListItem( $isbn, $label, $url ) );
187  }
188  $out->addHTML( '</ul>' );
189 
190  return true;
191  }
192 
201  private function makeListItem( $isbn, $label, $url ) {
202  $url = str_replace( '$1', $isbn, $url );
203 
204  return Html::rawElement( 'li', [],
205  Html::element( 'a', [ 'href' => $url, 'class' => 'external' ], $label )
206  );
207  }
208 
209  protected function getGroupName() {
210  return 'wiki';
211  }
212 }
SpecialPage\getPageTitle
getPageTitle( $subpage=false)
Get a self-referential title object.
Definition: SpecialPage.php:678
SpecialPage\msg
msg( $key)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:796
content
per default it will return the text for text based content
Definition: contenthandler.txt:104
$context
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:2636
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:725
SpecialBookSources\execute
execute( $isbn)
Definition: SpecialBooksources.php:41
$out
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password 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:780
$formDescriptor
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:2064
php
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:35
SpecialBookSources\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialBooksources.php:209
SpecialBookSources\showList
showList( $isbn)
Determine where to get the list of book sources from, format and output them.
Definition: SpecialBooksources.php:154
Revision\newFromTitle
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:137
DerivativeContext
An IContextSource implementation which will inherit context from another source but allow individual ...
Definition: DerivativeContext.php:30
MWException
MediaWiki exception.
Definition: MWException.php:26
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
NS_PROJECT
const NS_PROJECT
Definition: Defines.php:68
in
null for the wiki Added in
Definition: hooks.txt:1588
HTMLForm\factory
static factory( $displayFormat)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:286
SpecialBookSources\isValidISBN
static isValidISBN( $isbn)
Return whether a given ISBN (10 or 13) is valid.
Definition: SpecialBooksources.php:71
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:531
list
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
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:698
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:604
SpecialBookSources\__construct
__construct()
Definition: SpecialBooksources.php:34
SpecialBookSources\buildForm
buildForm( $isbn)
Generate a form to allow users to enter an ISBN.
Definition: SpecialBooksources.php:124
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:36
language
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two distribute and or modify the software for each author s protection and we want to make certain that everyone understands that there is no warranty for this free software If the software is modified by someone else and passed we want its recipients to know that what they have is not the so that any problems introduced by others will not reflect on the original authors reputations any free program is threatened constantly by software patents We wish to avoid the danger that redistributors of a free program will individually obtain patent in effect making the program proprietary To prevent we have made it clear that any patent must be licensed for everyone s free use or not licensed at all The precise terms and conditions for distribution and modification follow GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR DISTRIBUTION AND MODIFICATION This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License The refers to any such program or and a work based on the Program means either the Program or any derivative work under copyright a work containing the Program or a portion of either verbatim or with modifications and or translated into another language(Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:715
SpecialBookSources
Special page outputs information on sourcing a book with a particular ISBN The parser creates links t...
Definition: SpecialBooksources.php:33
TextContent
Content object implementation for representing flat text.
Definition: TextContent.php:37
$rev
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:1769
as
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
Definition: distributors.txt:9
$content
$content
Definition: pageupdater.txt:72
SpecialBookSources\cleanIsbn
static cleanIsbn( $isbn)
Trim ISBN and remove characters which aren't required.
Definition: SpecialBooksources.php:115
MediaWikiServices
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 MediaWikiServices
Definition: injection.txt:23
SpecialBookSources\makeListItem
makeListItem( $isbn, $label, $url)
Format a book source list item.
Definition: SpecialBooksources.php:201
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
SpecialPage\outputHeader
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
Definition: SpecialPage.php:633