MediaWiki  master
SpecialBookSources.php
Go to the documentation of this file.
1 <?php
26 
35 
37  private $revisionLookup;
38 
41 
46  public function __construct(
49  ) {
50  parent::__construct( 'Booksources' );
51  $this->revisionLookup = $revisionLookup;
52  $this->contentLanguage = $contentLanguage;
53  }
54 
58  public function execute( $isbn ) {
59  $out = $this->getOutput();
60 
61  $this->setHeaders();
62  $this->outputHeader();
63 
64  // User provided ISBN
65  $isbn = $isbn ?: $this->getRequest()->getText( 'isbn' );
66  $isbn = trim( $isbn );
67 
68  $this->buildForm( $isbn );
69 
70  if ( $isbn !== '' ) {
71  if ( !self::isValidISBN( $isbn ) ) {
72  $out->wrapWikiMsg(
73  "<div class=\"error\">\n$1\n</div>",
74  'booksources-invalid-isbn'
75  );
76  }
77 
78  $this->showList( $isbn );
79  }
80  }
81 
88  public static function isValidISBN( $isbn ) {
89  $isbn = self::cleanIsbn( $isbn );
90  $sum = 0;
91  if ( strlen( $isbn ) == 13 ) {
92  for ( $i = 0; $i < 12; $i++ ) {
93  if ( $isbn[$i] === 'X' ) {
94  return false;
95  } elseif ( $i % 2 == 0 ) {
96  $sum += $isbn[$i];
97  } else {
98  $sum += 3 * $isbn[$i];
99  }
100  }
101 
102  $check = ( 10 - ( $sum % 10 ) ) % 10;
103  if ( (string)$check === $isbn[12] ) {
104  return true;
105  }
106  } elseif ( strlen( $isbn ) == 10 ) {
107  for ( $i = 0; $i < 9; $i++ ) {
108  if ( $isbn[$i] === 'X' ) {
109  return false;
110  }
111  $sum += $isbn[$i] * ( $i + 1 );
112  }
113 
114  $check = $sum % 11;
115  if ( $check == 10 ) {
116  $check = "X";
117  }
118  if ( (string)$check === $isbn[9] ) {
119  return true;
120  }
121  }
122 
123  return false;
124  }
125 
132  private static function cleanIsbn( $isbn ) {
133  return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
134  }
135 
141  private function buildForm( $isbn ) {
142  $formDescriptor = [
143  'isbn' => [
144  'type' => 'text',
145  'name' => 'isbn',
146  'label-message' => 'booksources-isbn',
147  'default' => $isbn,
148  'autofocus' => true,
149  'required' => true,
150  ],
151  ];
152 
153  $context = new DerivativeContext( $this->getContext() );
154  $context->setTitle( $this->getPageTitle() );
155  HTMLForm::factory( 'ooui', $formDescriptor, $context )
156  ->setWrapperLegendMsg( 'booksources-search-legend' )
157  ->setSubmitTextMsg( 'booksources-search' )
158  ->setMethod( 'get' )
159  ->prepareForm()
160  ->displayForm( false );
161  }
162 
171  private function showList( $isbn ) {
172  $out = $this->getOutput();
173 
174  $isbn = self::cleanIsbn( $isbn );
175  # Hook to allow extensions to insert additional HTML,
176  # e.g. for API-interacting plugins and so on
177  $this->getHookRunner()->onBookInformation( $isbn, $out );
178 
179  # Check for a local page such as Project:Book_sources and use that if available
180  $page = $this->msg( 'booksources' )->inContentLanguage()->text();
181  $title = Title::makeTitleSafe( NS_PROJECT, $page ); # Show list in content language
182  if ( is_object( $title ) && $title->exists() ) {
183  $rev = $this->revisionLookup->getRevisionByTitle( $title );
184  $content = $rev->getContent( SlotRecord::MAIN );
185 
186  if ( $content instanceof TextContent ) {
187  // XXX: in the future, this could be stored as structured data, defining a list of book sources
188 
189  $text = $content->getText();
190  $out->addWikiTextAsInterface( str_replace( 'MAGICNUMBER', $isbn, $text ) );
191 
192  return true;
193  } else {
194  throw new MWException( "Unexpected content type for book sources: " . $content->getModel() );
195  }
196  }
197 
198  # Fall back to the defaults given in the language file
199  $out->addWikiMsg( 'booksources-text' );
200  $out->addHTML( '<ul>' );
201  $items = $this->contentLanguage->getBookstoreList();
202  foreach ( $items as $label => $url ) {
203  $out->addHTML( $this->makeListItem( $isbn, $label, $url ) );
204  }
205  $out->addHTML( '</ul>' );
206 
207  return true;
208  }
209 
218  private function makeListItem( $isbn, $label, $url ) {
219  $url = str_replace( '$1', $isbn, $url );
220 
221  return Html::rawElement( 'li', [],
222  Html::element( 'a', [ 'href' => $url, 'class' => 'external' ], $label )
223  );
224  }
225 
226  protected function getGroupName() {
227  return 'wiki';
228  }
229 }
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:827
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:743
SpecialBookSources\execute
execute( $isbn)
Definition: SpecialBookSources.php:58
SpecialBookSources\$revisionLookup
RevisionLookup $revisionLookup
Definition: SpecialBookSources.php:37
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:226
SpecialBookSources\showList
showList( $isbn)
Determine where to get the list of book sources from, format and output them.
Definition: SpecialBookSources.php:171
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:88
$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:617
$content
$content
Definition: router.php:76
SpecialBookSources\buildForm
buildForm( $isbn)
Generate a form to allow users to enter an ISBN.
Definition: SpecialBookSources.php:141
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:733
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\$contentLanguage
Language $contentLanguage
Definition: SpecialBookSources.php:40
SpecialBookSources\__construct
__construct(RevisionLookup $revisionLookup, Language $contentLanguage)
Definition: SpecialBookSources.php:46
SpecialBookSources\cleanIsbn
static cleanIsbn( $isbn)
Trim ISBN and remove characters which aren't required.
Definition: SpecialBookSources.php:132
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:218
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
Language
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition: Language.php:41
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40