MediaWiki  1.23.0
SpecialBooksources.php
Go to the documentation of this file.
1 <?php
33 
37  private $isbn = '';
38 
42  public function __construct() {
43  parent::__construct( 'Booksources' );
44  }
45 
51  public function execute( $isbn ) {
52  $this->setHeaders();
53  $this->outputHeader();
54  $this->isbn = self::cleanIsbn( $isbn ? $isbn : $this->getRequest()->getText( 'isbn' ) );
55  $this->getOutput()->addHTML( $this->makeForm() );
56  if ( strlen( $this->isbn ) > 0 ) {
57  if ( !self::isValidISBN( $this->isbn ) ) {
58  $this->getOutput()->wrapWikiMsg( "<div class=\"error\">\n$1\n</div>", 'booksources-invalid-isbn' );
59  }
60  $this->showList();
61  }
62  }
63 
69  public static function isValidISBN( $isbn ) {
71  $sum = 0;
72  if ( strlen( $isbn ) == 13 ) {
73  for ( $i = 0; $i < 12; $i++ ) {
74  if ( $i % 2 == 0 ) {
75  $sum += $isbn[$i];
76  } else {
77  $sum += 3 * $isbn[$i];
78  }
79  }
80 
81  $check = ( 10 - ( $sum % 10 ) ) % 10;
82  if ( $check == $isbn[12] ) {
83  return true;
84  }
85  } elseif ( strlen( $isbn ) == 10 ) {
86  for ( $i = 0; $i < 9; $i++ ) {
87  $sum += $isbn[$i] * ( $i + 1 );
88  }
89 
90  $check = $sum % 11;
91  if ( $check == 10 ) {
92  $check = "X";
93  }
94  if ( $check == $isbn[9] ) {
95  return true;
96  }
97  }
98 
99  return false;
100  }
101 
108  private static function cleanIsbn( $isbn ) {
109  return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
110  }
111 
117  private function makeForm() {
118  global $wgScript;
119 
120  $form = Html::openElement( 'fieldset' ) . "\n";
121  $form .= Html::element( 'legend', array(), $this->msg( 'booksources-search-legend' )->text() ) . "\n";
122  $form .= Html::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) . "\n";
123  $form .= Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() ) . "\n";
124  $form .= '<p>' . Xml::inputLabel( $this->msg( 'booksources-isbn' )->text(), 'isbn', 'isbn', 20, $this->isbn, array( 'autofocus' => true ) );
125  $form .= '&#160;' . Xml::submitButton( $this->msg( 'booksources-go' )->text() ) . "</p>\n";
126  $form .= Html::closeElement( 'form' ) . "\n";
127  $form .= Html::closeElement( 'fieldset' ) . "\n";
128 
129  return $form;
130  }
131 
139  private function showList() {
141 
142  # Hook to allow extensions to insert additional HTML,
143  # e.g. for API-interacting plugins and so on
144  wfRunHooks( 'BookInformation', array( $this->isbn, $this->getOutput() ) );
145 
146  # Check for a local page such as Project:Book_sources and use that if available
147  $page = $this->msg( 'booksources' )->inContentLanguage()->text();
148  $title = Title::makeTitleSafe( NS_PROJECT, $page ); # Show list in content language
149  if ( is_object( $title ) && $title->exists() ) {
151  $content = $rev->getContent();
152 
153  if ( $content instanceof TextContent ) {
154  //XXX: in the future, this could be stored as structured data, defining a list of book sources
155 
156  $text = $content->getNativeData();
157  $this->getOutput()->addWikiText( str_replace( 'MAGICNUMBER', $this->isbn, $text ) );
158 
159  return true;
160  } else {
161  throw new MWException( "Unexpected content type for book sources: " . $content->getModel() );
162  }
163  }
164 
165  # Fall back to the defaults given in the language file
166  $this->getOutput()->addWikiMsg( 'booksources-text' );
167  $this->getOutput()->addHTML( '<ul>' );
168  $items = $wgContLang->getBookstoreList();
169  foreach ( $items as $label => $url ) {
170  $this->getOutput()->addHTML( $this->makeListItem( $label, $url ) );
171  }
172  $this->getOutput()->addHTML( '</ul>' );
173 
174  return true;
175  }
176 
184  private function makeListItem( $label, $url ) {
185  $url = str_replace( '$1', $this->isbn, $url );
186 
187  return Html::rawElement( 'li', array(),
188  Html::element( 'a', array( 'href' => $url, 'class' => 'external' ), $label ) );
189  }
190 
191  protected function getGroupName() {
192  return 'wiki';
193  }
194 }
SpecialPage\getPageTitle
getPageTitle( $subpage=false)
Get a self-referential title object.
Definition: SpecialPage.php:488
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
content
per default it will return the text for text based content
Definition: contenthandler.txt:107
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:535
text
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add text
Definition: design.txt:12
SpecialBookSources\execute
execute( $isbn)
Show the special page.
Definition: SpecialBooksources.php:51
$form
usually copyright or history_copyright This message must be in HTML not wikitext $subpages will be ignored and the rest of subPageSubtitle() will run. 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink' whether MediaWiki currently thinks this is a CSS JS page Hooks may change this value to override the return value of Title::isCssOrJsPage(). 'TitleIsAlwaysKnown' whether MediaWiki currently thinks this page is known isMovable() always returns false. $title whether MediaWiki currently thinks this page is movable Hooks may change this value to override the return value of Title::isMovable(). 'TitleIsWikitextPage' whether MediaWiki currently thinks this is a wikitext page Hooks may change this value to override the return value of Title::isWikitextPage() 'TitleMove' use UploadVerification and UploadVerifyFile instead $form
Definition: hooks.txt:2573
Html\hidden
static hidden( $name, $value, $attribs=array())
Convenience function to produce an input element with type=hidden.
Definition: Html.php:662
$wgContLang
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the content language as $wgContLang
Definition: design.txt:56
SpecialBookSources\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialBooksources.php:191
SpecialBookSources\$isbn
$isbn
ISBN passed to the page, if any.
Definition: SpecialBooksources.php:37
Html\closeElement
static closeElement( $element)
Returns "</$element>", except if $wgWellFormedXml is off, in which case it returns the empty string w...
Definition: Html.php:235
IDBAccessObject\READ_NORMAL
const READ_NORMAL
Definition: IDBAccessObject.php:53
Html\openElement
static openElement( $element, $attribs=array())
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing '/'...
Definition: Html.php:166
SpecialBookSources\showList
showList()
Determine where to get the list of book sources from, format and output them.
Definition: SpecialBooksources.php:139
MWException
MediaWiki exception.
Definition: MWException.php:26
NS_PROJECT
const NS_PROJECT
Definition: Defines.php:83
Html\element
static element( $element, $attribs=array(), $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:148
SpecialBookSources\isValidISBN
static isValidISBN( $isbn)
Returns whether a given ISBN (10 or 13) is valid.
Definition: SpecialBooksources.php:69
wfRunHooks
wfRunHooks( $event, array $args=array(), $deprecatedVersion=null)
Call hook functions defined in $wgHooks.
Definition: GlobalFunctions.php:4001
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:352
Xml\inputLabel
static inputLabel( $label, $name, $id, $size=false, $value=false, $attribs=array())
Convenience function to build an HTML text input field with a label.
Definition: Xml.php:398
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
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
SpecialBookSources\makeListItem
makeListItem( $label, $url)
Format a book source list item.
Definition: SpecialBooksources.php:184
Revision\newFromTitle
static newFromTitle( $title, $id=0, $flags=0)
Load either the current, or a specified, revision that's attached to a given title.
Definition: Revision.php:106
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:422
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
SpecialBookSources\__construct
__construct()
Constructor.
Definition: SpecialBooksources.php:42
SpecialPage\msg
msg()
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:609
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:33
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:525
SpecialBookSources\makeForm
makeForm()
Generate a form to allow users to enter an ISBN.
Definition: SpecialBooksources.php:117
SpecialBookSources
Special page outputs information on sourcing a book with a particular ISBN The parser creates links t...
Definition: SpecialBooksources.php:32
TextContent
Content object implementation for representing flat text.
Definition: TextContent.php:35
$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:1337
in
Prior to maintenance scripts were a hodgepodge of code that had no cohesion or formal method of action Beginning in
Definition: maintenance.txt:1
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
Xml\submitButton
static submitButton( $value, $attribs=array())
Convenience function to build an HTML submit button.
Definition: Xml.php:463
SpecialBookSources\cleanIsbn
static cleanIsbn( $isbn)
Trim ISBN and remove characters which aren't required.
Definition: SpecialBooksources.php:108
Html\rawElement
static rawElement( $element, $attribs=array(), $contents='')
Returns an HTML element in a string.
Definition: Html.php:124
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:443