MediaWiki  master
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 }
SpecialPage\getPageTitle
getPageTitle( $subpage=false)
Get a self-referential title object.
Definition: SpecialPage.php:697
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:828
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:744
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:154
SpecialBookSources\execute
execute( $isbn)
Definition: SpecialBookSources.php:42
SpecialBookSources\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialBookSources.php:212
SpecialBookSources\showList
showList( $isbn)
Determine where to get the list of book sources from, format and output them.
Definition: SpecialBookSources.php:155
DerivativeContext
An IContextSource implementation which will inherit context from another source but allow individual ...
Definition: DerivativeContext.php:31
SpecialPage\getHookRunner
getHookRunner()
Definition: SpecialPage.php:1010
MWException
MediaWiki exception.
Definition: MWException.php:29
NS_PROJECT
const NS_PROJECT
Definition: Defines.php:73
SpecialBookSources\isValidISBN
static isValidISBN( $isbn)
Return whether a given ISBN (10 or 13) is valid.
Definition: SpecialBookSources.php:72
$title
$title
Definition: testCompression.php:38
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!...
Definition: SpecialPage.php:571
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:717
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:618
$content
$content
Definition: router.php:76
SpecialBookSources\__construct
__construct()
Definition: SpecialBookSources.php:35
SpecialBookSources\buildForm
buildForm( $isbn)
Generate a form to allow users to enter an ISBN.
Definition: SpecialBookSources.php:125
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:41
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:734
SpecialBookSources
Special page outputs information on sourcing a book with a particular ISBN The parser creates links t...
Definition: SpecialBookSources.php:34
TextContent
Content object implementation for representing flat text.
Definition: TextContent.php:39
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:209
SpecialBookSources\cleanIsbn
static cleanIsbn( $isbn)
Trim ISBN and remove characters which aren't required.
Definition: SpecialBookSources.php:116
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:231
SpecialBookSources\makeListItem
makeListItem( $isbn, $label, $url)
Format a book source list item.
Definition: SpecialBookSources.php:204
HTMLForm\factory
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:315
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:662
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:39