MediaWiki  master
SpecialBooksources.php
Go to the documentation of this file.
1 <?php
35  private $isbn = '';
36 
40  public function __construct() {
41  parent::__construct( 'Booksources' );
42  }
43 
49  public function execute( $isbn ) {
50  $this->setHeaders();
51  $this->outputHeader();
52  $this->isbn = self::cleanIsbn( $isbn ? $isbn : $this->getRequest()->getText( 'isbn' ) );
53  $this->getOutput()->addHTML( $this->makeForm() );
54  if ( strlen( $this->isbn ) > 0 ) {
55  if ( !self::isValidISBN( $this->isbn ) ) {
56  $this->getOutput()->wrapWikiMsg(
57  "<div class=\"error\">\n$1\n</div>",
58  'booksources-invalid-isbn'
59  );
60  }
61  $this->showList();
62  }
63  }
64 
70  public static function isValidISBN( $isbn ) {
71  $isbn = self::cleanIsbn( $isbn );
72  $sum = 0;
73  if ( strlen( $isbn ) == 13 ) {
74  for ( $i = 0; $i < 12; $i++ ) {
75  if ( $isbn[$i] === 'X' ) {
76  return false;
77  } elseif ( $i % 2 == 0 ) {
78  $sum += $isbn[$i];
79  } else {
80  $sum += 3 * $isbn[$i];
81  }
82  }
83 
84  $check = ( 10 - ( $sum % 10 ) ) % 10;
85  if ( (string)$check === $isbn[12] ) {
86  return true;
87  }
88  } elseif ( strlen( $isbn ) == 10 ) {
89  for ( $i = 0; $i < 9; $i++ ) {
90  if ( $isbn[$i] === 'X' ) {
91  return false;
92  }
93  $sum += $isbn[$i] * ( $i + 1 );
94  }
95 
96  $check = $sum % 11;
97  if ( $check == 10 ) {
98  $check = "X";
99  }
100  if ( (string)$check === $isbn[9] ) {
101  return true;
102  }
103  }
104 
105  return false;
106  }
107 
114  private static function cleanIsbn( $isbn ) {
115  return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
116  }
117 
123  private function makeForm() {
124  $form = Html::openElement( 'fieldset' ) . "\n";
125  $form .= Html::element(
126  'legend',
127  array(),
128  $this->msg( 'booksources-search-legend' )->text()
129  ) . "\n";
130  $form .= Html::openElement( 'form', array( 'method' => 'get', 'action' => wfScript() ) ) . "\n";
131  $form .= Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() ) . "\n";
132  $form .= '<p>' . Xml::inputLabel(
133  $this->msg( 'booksources-isbn' )->text(),
134  'isbn',
135  'isbn',
136  20,
137  $this->isbn,
138  array( 'autofocus' => '', 'class' => 'mw-ui-input-inline' )
139  );
140 
141  $form .= '&#160;' . Html::submitButton(
142  $this->msg( 'booksources-search' )->text(),
143  array(), array( 'mw-ui-progressive' )
144  ) . "</p>\n";
145 
146  $form .= Html::closeElement( 'form' ) . "\n";
147  $form .= Html::closeElement( 'fieldset' ) . "\n";
148 
149  return $form;
150  }
151 
159  private function showList() {
161 
162  # Hook to allow extensions to insert additional HTML,
163  # e.g. for API-interacting plugins and so on
164  Hooks::run( 'BookInformation', array( $this->isbn, $this->getOutput() ) );
165 
166  # Check for a local page such as Project:Book_sources and use that if available
167  $page = $this->msg( 'booksources' )->inContentLanguage()->text();
168  $title = Title::makeTitleSafe( NS_PROJECT, $page ); # Show list in content language
169  if ( is_object( $title ) && $title->exists() ) {
171  $content = $rev->getContent();
172 
173  if ( $content instanceof TextContent ) {
174  //XXX: in the future, this could be stored as structured data, defining a list of book sources
175 
176  $text = $content->getNativeData();
177  $this->getOutput()->addWikiText( str_replace( 'MAGICNUMBER', $this->isbn, $text ) );
178 
179  return true;
180  } else {
181  throw new MWException( "Unexpected content type for book sources: " . $content->getModel() );
182  }
183  }
184 
185  # Fall back to the defaults given in the language file
186  $this->getOutput()->addWikiMsg( 'booksources-text' );
187  $this->getOutput()->addHTML( '<ul>' );
188  $items = $wgContLang->getBookstoreList();
189  foreach ( $items as $label => $url ) {
190  $this->getOutput()->addHTML( $this->makeListItem( $label, $url ) );
191  }
192  $this->getOutput()->addHTML( '</ul>' );
193 
194  return true;
195  }
196 
204  private function makeListItem( $label, $url ) {
205  $url = str_replace( '$1', $this->isbn, $url );
206 
207  return Html::rawElement( 'li', array(),
208  Html::element( 'a', array( 'href' => $url, 'class' => 'external' ), $label ) );
209  }
210 
211  protected function getGroupName() {
212  return 'wiki';
213  }
214 }
static closeElement($element)
Returns "</$element>".
Definition: Html.php:314
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
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:388
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:256
namespace and then decline to actually register it file or subcat img or subcat RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions instead of letting the login form give the generic error message that the account does not exist For when the account has been renamed or deleted or an array to pass a message key and parameters but no entry for that model exists in $wgContentHandlers if desired whether it is OK to use $contentModel on $title Handler functions that modify $ok should generally return false to prevent further hooks from further modifying $ok called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content $content
Definition: hooks.txt:952
wfScript($script= 'index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
static submitButton($contents, array $attrs, array $modifiers=array())
Returns an HTML link element in a string styled as a button (when $wgUseMediaWikiUIEverywhere is enab...
Definition: Html.php:189
per default it will return the text for text based content
msg()
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
static isValidISBN($isbn)
Returns whether a given ISBN (10 or 13) is valid.
makeForm()
Generate a form to allow users to enter an ISBN.
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
outputHeader($summaryMessageKey= '')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
static hidden($name, $value, array $attribs=array())
Convenience function to produce an input element with type=hidden.
Definition: Html.php:792
Parent class for all special pages.
Definition: SpecialPage.php:32
const NS_PROJECT
Definition: Defines.php:73
Content object implementation for representing flat text.
Definition: TextContent.php:35
__construct()
Constructor.
MediaWiki exception.
Definition: MWException.php:26
static run($event, array $args=array(), $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:137
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes! ...
static makeTitleSafe($ns, $title, $fragment= '', $interwiki= '')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:474
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:870
execute($isbn)
Show the special page.
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
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:1511
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
Prior to maintenance scripts were a hodgepodge of code that had no cohesion or formal method of action Beginning in
Definition: maintenance.txt:1
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:104
$isbn
ISBN passed to the page, if any.
static cleanIsbn($isbn)
Trim ISBN and remove characters which aren't required.
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
static element($element, $attribs=array(), $contents= '')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:238
showList()
Determine where to get the list of book sources from, format and output them.
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 local content language as $wgContLang
Definition: design.txt:56
static rawElement($element, $attribs=array(), $contents= '')
Returns an HTML element in a string.
Definition: Html.php:214
getRequest()
Get the WebRequest being used for this instance.
maintenance dev scripts can help quickly setup a local MediaWiki for development purposes Wikis setup in this way are NOT meant to be publicly available They use a development database not acceptible for use in production Place a sqlite database in an unsafe location a real wiki should never place it in And use predictable default logins for the initial administrator user Running maintenance dev install sh will download and install a local copy of php
Definition: README:5
makeListItem($label, $url)
Format a book source list item.
Special page outputs information on sourcing a book with a particular ISBN The parser creates links t...
getPageTitle($subpage=false)
Get a self-referential title object.