summaryrefslogtreecommitdiff
path: root/includes/SpecialPrefixindex.php
blob: bfab21b6ac056db942adc38da763999ce21e89db (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/**
 * @addtogroup SpecialPage
 */

/**
 * Entry point : initialise variables and call subfunctions.
 * @param $par String: becomes "FOO" when called like Special:Prefixindex/FOO (default NULL)
 * @param $specialPage SpecialPage object.
 */
function wfSpecialPrefixIndex( $par=NULL, $specialPage ) {
	global $wgRequest, $wgOut, $wgContLang;

	# GET values
	$from = $wgRequest->getVal( 'from' );
	$prefix = $wgRequest->getVal( 'prefix' );
	$namespace = $wgRequest->getInt( 'namespace' );
	$namespaces = $wgContLang->getNamespaces();

	$indexPage = new SpecialPrefixIndex();

	$wgOut->setPagetitle( ( $namespace > 0 && in_array( $namespace, array_keys( $namespaces ) ) )
		? wfMsg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) )
		: wfMsg( 'allarticles' )
	);

	if ( isset($par) ) {
		$indexPage->showChunk( $namespace, $par, $specialPage->including(), $from );
	} elseif ( isset($prefix) ) {
		$indexPage->showChunk( $namespace, $prefix, $specialPage->including(), $from );
	} elseif ( isset($from) ) {
		$indexPage->showChunk( $namespace, $from, $specialPage->including(), $from );
	} else {
		$wgOut->addHtml($indexPage->namespaceForm ( $namespace, null ));
	}
}

/**
 * implements Special:Prefixindex 
 * @addtogroup SpecialPage
 */
class SpecialPrefixindex extends SpecialAllpages {
	// Inherit $maxPerPage

	// Define other properties
	protected $name = 'Prefixindex';
	protected $nsfromMsg = 'allpagesprefix';

/**
 * @param integer $namespace (Default NS_MAIN)
 * @param string $from list all pages from this name (default FALSE)
 */
function showChunk( $namespace = NS_MAIN, $prefix, $including = false, $from = null ) {
	global $wgOut, $wgUser, $wgContLang;

	$fname = 'indexShowChunk';

	$sk = $wgUser->getSkin();

	if (!isset($from)) $from = $prefix;

	$fromList = $this->getNamespaceKeyAndText($namespace, $from);
	$prefixList = $this->getNamespaceKeyAndText($namespace, $prefix);
	$namespaces = $wgContLang->getNamespaces();
	$align = $wgContLang->isRtl() ? 'left' : 'right';

	if ( !$prefixList || !$fromList ) {
		$out = wfMsgWikiHtml( 'allpagesbadtitle' );
	} elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
		// Show errormessage and reset to NS_MAIN
		$out = wfMsgExt( 'allpages-bad-ns', array( 'parseinline' ), $namespace );
		$namespace = NS_MAIN;
	} else {
		list( $namespace, $prefixKey, $prefix ) = $prefixList;
		list( /* $fromNs */, $fromKey, $from ) = $fromList;

		### FIXME: should complain if $fromNs != $namespace

		$dbr = wfGetDB( DB_SLAVE );

		$res = $dbr->select( 'page',
			array( 'page_namespace', 'page_title', 'page_is_redirect' ),
			array(
				'page_namespace' => $namespace,
				'page_title LIKE \'' . $dbr->escapeLike( $prefixKey ) .'%\'',
				'page_title >= ' . $dbr->addQuotes( $fromKey ),
			),
			$fname,
			array(
				'ORDER BY'  => 'page_title',
				'LIMIT'     => $this->maxPerPage + 1,
				'USE INDEX' => 'name_title',
			)
		);

		### FIXME: side link to previous

		$n = 0;
		$out = '<table style="background: inherit;" border="0" width="100%">';

		while( ($n < $this->maxPerPage) && ($s = $dbr->fetchObject( $res )) ) {
			$t = Title::makeTitle( $s->page_namespace, $s->page_title );
			if( $t ) {
				$link = ($s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
					$sk->makeKnownLinkObj( $t, htmlspecialchars( $t->getText() ), false, false ) .
					($s->page_is_redirect ? '</div>' : '' );
			} else {
				$link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
			}
			if( $n % 3 == 0 ) {
				$out .= '<tr>';
			}
			$out .= "<td>$link</td>";
			$n++;
			if( $n % 3 == 0 ) {
				$out .= '</tr>';
			}
		}
		if( ($n % 3) != 0 ) {
			$out .= '</tr>';
		}
		$out .= '</table>';
	}

	if ( $including ) {
		$out2 = '';
	} else {
		$nsForm = $this->namespaceForm ( $namespace, $prefix );
		$out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
		$out2 .= '<tr valign="top"><td>' . $nsForm;
		$out2 .= '</td><td align="' . $align . '" style="font-size: smaller; margin-bottom: 1em;">' .
				$sk->makeKnownLink( $wgContLang->specialPage( $this->name ),
					wfMsg ( 'allpages' ) );
		if ( isset($dbr) && $dbr && ($n == $this->maxPerPage) && ($s = $dbr->fetchObject( $res )) ) {
			$namespaceparam = $namespace ? "&namespace=$namespace" : "";
			$out2 .= " | " . $sk->makeKnownLink(
				$wgContLang->specialPage( $this->name ),
				wfMsg ( 'nextpage', $s->page_title ),
				"from=" . wfUrlEncode ( $s->page_title ) .
				"&prefix=" . wfUrlEncode ( $prefix ) . $namespaceparam );
		}
		$out2 .= "</td></tr></table><hr />";
	}

	$wgOut->addHtml( $out2 . $out );
}
}