2004-02-18 02:15:00 +00:00
|
|
|
<?php
|
2003-04-14 23:10:40 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2006-12-16 19:20:44 +00:00
|
|
|
* Special page outputs information on sourcing a book with a particular ISBN
|
|
|
|
|
* The parser creates links to this page when dealing with ISBNs in wikitext
|
2004-09-02 23:28:24 +00:00
|
|
|
*
|
2004-09-03 23:00:01 +00:00
|
|
|
* @package MediaWiki
|
2006-12-16 19:20:44 +00:00
|
|
|
* @subpackage Special pages
|
|
|
|
|
* @author Rob Church <robchur@gmail.com>
|
|
|
|
|
* @todo Validate ISBNs using the standard check-digit method
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2006-12-16 19:20:44 +00:00
|
|
|
class SpecialBookSources extends SpecialPage {
|
2003-04-14 23:10:40 +00:00
|
|
|
|
2006-12-16 19:20:44 +00:00
|
|
|
/**
|
|
|
|
|
* ISBN passed to the page, if any
|
|
|
|
|
*/
|
|
|
|
|
private $isbn = '';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor
|
|
|
|
|
*/
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct( 'Booksources' );
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
2006-12-16 19:20:44 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Show the special page
|
|
|
|
|
*
|
|
|
|
|
* @param $isbn ISBN passed as a subpage parameter
|
|
|
|
|
*/
|
|
|
|
|
public function execute( $isbn = false ) {
|
|
|
|
|
global $wgOut, $wgRequest;
|
|
|
|
|
$this->setHeaders();
|
|
|
|
|
$this->isbn = $this->cleanIsbn( $isbn ? $isbn : $wgRequest->getText( 'isbn' ) );
|
2006-12-19 23:42:00 +00:00
|
|
|
$wgOut->addWikiText( wfMsgNoTrans( 'booksources-summary' ) );
|
2006-12-16 19:20:44 +00:00
|
|
|
$wgOut->addHtml( $this->makeForm() );
|
|
|
|
|
if( strlen( $this->isbn) > 0 )
|
2006-12-20 16:51:26 +00:00
|
|
|
$this->showList();
|
2004-04-09 06:27:18 +00:00
|
|
|
}
|
2006-12-16 19:20:44 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Trim ISBN and remove characters which aren't required
|
|
|
|
|
*
|
|
|
|
|
* @param $isbn Unclean ISBN
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function cleanIsbn( $isbn ) {
|
|
|
|
|
return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate a form to allow users to enter an ISBN
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function makeForm() {
|
|
|
|
|
global $wgScript;
|
|
|
|
|
$title = self::getTitleFor( 'Booksources' );
|
|
|
|
|
$form = '<fieldset><legend>' . wfMsgHtml( 'booksources-search-legend' ) . '</legend>';
|
|
|
|
|
$form .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
|
|
|
|
|
$form .= Xml::hidden( 'title', $title->getPrefixedText() );
|
2006-12-19 05:35:35 +00:00
|
|
|
$form .= '<p>' . Xml::inputLabel( wfMsg( 'booksources-isbn' ), 'isbn', 'isbn', 20, $this->isbn );
|
2006-12-16 19:20:44 +00:00
|
|
|
$form .= ' ' . Xml::submitButton( wfMsg( 'booksources-go' ) ) . '</p>';
|
|
|
|
|
$form .= Xml::closeElement( 'form' );
|
2006-12-19 05:35:35 +00:00
|
|
|
$form .= '</fieldset>';
|
2006-12-16 19:20:44 +00:00
|
|
|
return $form;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2006-12-20 16:51:26 +00:00
|
|
|
* Determine where to get the list of book sources from,
|
|
|
|
|
* format and output them
|
2006-12-16 19:20:44 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2006-12-20 16:51:26 +00:00
|
|
|
private function showList() {
|
2005-12-04 18:27:59 +00:00
|
|
|
global $wgOut, $wgContLang;
|
2006-12-16 19:20:44 +00:00
|
|
|
|
|
|
|
|
# Check for a local page such as Project:Book_sources and use that if available
|
|
|
|
|
$title = Title::makeTitleSafe( NS_PROJECT, wfMsg( 'booksources' ) ); # Should this be wfMsgForContent()? -- RC
|
|
|
|
|
if( is_object( $title ) && $title->exists() ) {
|
|
|
|
|
$rev = Revision::newFromTitle( $title );
|
2006-12-20 16:51:26 +00:00
|
|
|
$wgOut->addWikiText( str_replace( 'MAGICNUMBER', $this->isbn, $rev->getText() ) );
|
|
|
|
|
return true;
|
2004-04-09 06:27:18 +00:00
|
|
|
}
|
2006-12-16 19:20:44 +00:00
|
|
|
|
|
|
|
|
# Fall back to the defaults given in the language file
|
2006-12-20 16:51:26 +00:00
|
|
|
$wgOut->addWikiText( wfMsgNoTrans( 'booksources-text' ) );
|
|
|
|
|
$wgOut->addHtml( '<ul>' );
|
2006-12-16 19:20:44 +00:00
|
|
|
$items = $wgContLang->getBookstoreList();
|
|
|
|
|
foreach( $items as $label => $url )
|
2006-12-20 16:51:26 +00:00
|
|
|
$wgOut->addHtml( $this->makeListItem( $label, $url ) );
|
|
|
|
|
$wgOut->addHtml( '</ul>' );
|
|
|
|
|
return true;
|
2004-04-09 06:27:18 +00:00
|
|
|
}
|
2006-12-16 19:20:44 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Format a book source list item
|
|
|
|
|
*
|
|
|
|
|
* @param $label Book source label
|
|
|
|
|
* @param $url Book source URL
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function makeListItem( $label, $url ) {
|
|
|
|
|
$url = str_replace( '$1', $this->isbn, $url );
|
|
|
|
|
return '<li><a href="' . htmlspecialchars( $url ) . '">' . htmlspecialchars( $label ) . '</a></li>';
|
2003-05-19 21:30:19 +00:00
|
|
|
}
|
2006-12-16 19:20:44 +00:00
|
|
|
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
?>
|