MediaWiki  master
SpecialBookSources.php
Go to the documentation of this file.
1 <?php
26 
35 
37  private $revisionLookup;
38 
42  public function __construct(
44  ) {
45  parent::__construct( 'Booksources' );
46  $this->revisionLookup = $revisionLookup;
47  }
48 
52  public function execute( $isbn ) {
53  $out = $this->getOutput();
54 
55  $this->setHeaders();
56  $this->outputHeader();
57 
58  // User provided ISBN
59  $isbn = $isbn ?: $this->getRequest()->getText( 'isbn' );
60  $isbn = trim( $isbn );
61 
62  $this->buildForm( $isbn );
63 
64  if ( $isbn !== '' ) {
65  if ( !self::isValidISBN( $isbn ) ) {
66  $out->wrapWikiMsg(
67  "<div class=\"error\">\n$1\n</div>",
68  'booksources-invalid-isbn'
69  );
70  }
71 
72  $this->showList( $isbn );
73  }
74  }
75 
82  public static function isValidISBN( $isbn ) {
83  $isbn = self::cleanIsbn( $isbn );
84  $sum = 0;
85  if ( strlen( $isbn ) == 13 ) {
86  for ( $i = 0; $i < 12; $i++ ) {
87  if ( $isbn[$i] === 'X' ) {
88  return false;
89  } elseif ( $i % 2 == 0 ) {
90  $sum += $isbn[$i];
91  } else {
92  $sum += 3 * $isbn[$i];
93  }
94  }
95 
96  $check = ( 10 - ( $sum % 10 ) ) % 10;
97  if ( (string)$check === $isbn[12] ) {
98  return true;
99  }
100  } elseif ( strlen( $isbn ) == 10 ) {
101  for ( $i = 0; $i < 9; $i++ ) {
102  if ( $isbn[$i] === 'X' ) {
103  return false;
104  }
105  $sum += $isbn[$i] * ( $i + 1 );
106  }
107 
108  $check = $sum % 11;
109  if ( $check == 10 ) {
110  $check = "X";
111  }
112  if ( (string)$check === $isbn[9] ) {
113  return true;
114  }
115  }
116 
117  return false;
118  }
119 
126  private static function cleanIsbn( $isbn ) {
127  return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
128  }
129 
135  private function buildForm( $isbn ) {
136  $formDescriptor = [
137  'isbn' => [
138  'type' => 'text',
139  'name' => 'isbn',
140  'label-message' => 'booksources-isbn',
141  'default' => $isbn,
142  'autofocus' => true,
143  'required' => true,
144  ],
145  ];
146 
147  $context = new DerivativeContext( $this->getContext() );
148  $context->setTitle( $this->getPageTitle() );
149  HTMLForm::factory( 'ooui', $formDescriptor, $context )
150  ->setWrapperLegendMsg( 'booksources-search-legend' )
151  ->setSubmitTextMsg( 'booksources-search' )
152  ->setMethod( 'get' )
153  ->prepareForm()
154  ->displayForm( false );
155  }
156 
165  private function showList( $isbn ) {
166  $out = $this->getOutput();
167 
168  $isbn = self::cleanIsbn( $isbn );
169  # Hook to allow extensions to insert additional HTML,
170  # e.g. for API-interacting plugins and so on
171  $this->getHookRunner()->onBookInformation( $isbn, $out );
172 
173  # Check for a local page such as Project:Book_sources and use that if available
174  $page = $this->msg( 'booksources' )->inContentLanguage()->text();
175  $title = Title::makeTitleSafe( NS_PROJECT, $page ); # Show list in content language
176  if ( is_object( $title ) && $title->exists() ) {
177  $rev = $this->revisionLookup->getRevisionByTitle( $title );
178  $content = $rev->getContent( SlotRecord::MAIN );
179 
180  if ( $content instanceof TextContent ) {
181  // XXX: in the future, this could be stored as structured data, defining a list of book sources
182 
183  $text = $content->getText();
184  $out->addWikiTextAsInterface( str_replace( 'MAGICNUMBER', $isbn, $text ) );
185 
186  return true;
187  } else {
188  throw new MWException( "Unexpected content type for book sources: " . $content->getModel() );
189  }
190  }
191 
192  # Fall back to the defaults given in the language file
193  $out->addWikiMsg( 'booksources-text' );
194  $out->addHTML( '<ul>' );
195  $items = $this->getContentLanguage()->getBookstoreList();
196  foreach ( $items as $label => $url ) {
197  $out->addHTML( $this->makeListItem( $isbn, $label, $url ) );
198  }
199  $out->addHTML( '</ul>' );
200 
201  return true;
202  }
203 
212  private function makeListItem( $isbn, $label, $url ) {
213  $url = str_replace( '$1', $isbn, $url );
214 
215  return Html::rawElement( 'li', [],
216  Html::element( 'a', [ 'href' => $url, 'class' => 'external' ], $label )
217  );
218  }
219 
220  protected function getGroupName() {
221  return 'wiki';
222  }
223 }
SpecialPage\getPageTitle
getPageTitle( $subpage=false)
Get a self-referential title object.
Definition: SpecialPage.php:744
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:912
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:790
SpecialBookSources\execute
execute( $isbn)
Definition: SpecialBookSources.php:52
SpecialBookSources\$revisionLookup
RevisionLookup $revisionLookup
Definition: SpecialBookSources.php:37
MediaWiki\Revision\RevisionLookup
Service for looking up page revisions.
Definition: RevisionLookup.php:38
SpecialBookSources\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialBookSources.php:220
SpecialBookSources\showList
showList( $isbn)
Determine where to get the list of book sources from, format and output them.
Definition: SpecialBookSources.php:165
DerivativeContext
An IContextSource implementation which will inherit context from another source but allow individual ...
Definition: DerivativeContext.php:32
SpecialPage\getHookRunner
getHookRunner()
Definition: SpecialPage.php:1095
MWException
MediaWiki exception.
Definition: MWException.php:29
SpecialBookSources\isValidISBN
static isValidISBN( $isbn)
Return whether a given ISBN (10 or 13) is valid.
Definition: SpecialBookSources.php:82
NS_PROJECT
const NS_PROJECT
Definition: Defines.php:68
$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:618
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:764
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:677
$content
$content
Definition: router.php:76
SpecialBookSources\buildForm
buildForm( $isbn)
Generate a form to allow users to enter an ISBN.
Definition: SpecialBookSources.php:135
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:43
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:780
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:210
SpecialPage\getContentLanguage
getContentLanguage()
Shortcut to get content language.
Definition: SpecialPage.php:840
SpecialBookSources\__construct
__construct(RevisionLookup $revisionLookup)
Definition: SpecialBookSources.php:42
SpecialBookSources\cleanIsbn
static cleanIsbn( $isbn)
Trim ISBN and remove characters which aren't required.
Definition: SpecialBookSources.php:126
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:232
SpecialBookSources\makeListItem
makeListItem( $isbn, $label, $url)
Format a book source list item.
Definition: SpecialBookSources.php:212
HTMLForm\factory
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:326
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:709
MediaWiki\Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40