summaryrefslogtreecommitdiff
path: root/includes/SpecialBooksources.php
blob: 960f622469219fa860a1674473fff7442e5c07e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
 * ISBNs in wiki pages will create links to this page, with the ISBN passed
 * in via the query string.
 *
 * @package MediaWiki
 * @subpackage SpecialPage
 */

/**
 * Constructor
 */
function wfSpecialBooksources( $par ) {
	global $wgRequest;

	$isbn = $par;
	if( empty( $par ) ) {
		$isbn = $wgRequest->getVal( 'isbn' );
	}
	$isbn = preg_replace( '/[^0-9X]/', '', $isbn );

	$bsl = new BookSourceList( $isbn );
	$bsl->show();
}

/**
 *
 * @package MediaWiki
 * @subpackage SpecialPage
 */
class BookSourceList {
	var $mIsbn;

	function BookSourceList( $isbn ) {
		$this->mIsbn = $isbn;
	}

	function show() {
		global $wgOut;

		$wgOut->setPagetitle( wfMsg( "booksources" ) );
		if( $this->mIsbn == '' ) {
			$this->askForm();
		} else {
			$this->showList();
		}
	}

	function showList() {
		global $wgOut, $wgContLang;
		$fname = "BookSourceList::showList()";

		# First, see if we have a custom list setup in
		# [[Wikipedia:Book sources]] or equivalent.
		$bstitle = Title::makeTitleSafe( NS_PROJECT, wfMsg( "booksources" ) );
		if( $bstitle ) {
			$revision = Revision::newFromTitle( $bstitle );
			if( $revision ) {
				$bstext = $revision->getText();
				if( $bstext ) {
					$bstext = str_replace( "MAGICNUMBER", $this->mIsbn, $bstext );
					$wgOut->addWikiText( $bstext );
					return;
				}
			}
		}

		# Otherwise, use the list of links in the default Language.php file.
		$s = wfMsgWikiHtml( 'booksourcetext' ) . "<ul>\n";
		$bs = $wgContLang->getBookstoreList() ;
		$bsn = array_keys ( $bs ) ;
		foreach ( $bsn as $name ) {
			$adr = $bs[$name] ;
			if ( ! $this->mIsbn ) {
				$adr = explode( ":" , $adr , 2 );
				$adr = explode( "/" , $adr[1] );
				$a = "";
				while ( $a == "" ) {
					$a = array_shift( $adr );
				}
				$adr = "http://".$a ;
			} else {
				$adr = str_replace ( "$1" , $this->mIsbn , $adr ) ;
			}
			$name = htmlspecialchars( $name );
			$adr = htmlspecialchars( $adr );
			$s .= "<li><a href=\"{$adr}\" class=\"external\">{$name}</a></li>\n" ;
		}
		$s .= "</ul>\n";

		$wgOut->addHTML( $s );
	}

	function askForm() {
		global $wgOut, $wgTitle;
		$fname = "BookSourceList::askForm()";

		$action = $wgTitle->escapeLocalUrl();
		$isbn = htmlspecialchars( wfMsg( "isbn" ) );
		$go = htmlspecialchars( wfMsg( "go" ) );
		$out = "<form action=\"$action\" method='post'>
			$isbn: <input name='isbn' id='isbn' />
			<input type='submit' value=\"$go\" />
		</form>";
		$wgOut->addHTML( $out );
	}
}

?>