-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSpecialFlexiblePrefix.php
93 lines (80 loc) · 2.47 KB
/
SpecialFlexiblePrefix.php
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
<?php
class SpecialFlexiblePrefix extends SpecialPage {
function __construct() {
parent::__construct( 'FlexiblePrefix' );
}
function getTitles($prefix){
global $wgFlexiblePrefixNamespaces;
$title = Title::newFromTextThrow($prefix);
if ($prefix[0] != ':' && $title->inNamespace(NS_MAIN) && $wgFlexiblePrefixNamespaces){
$ns = $wgFlexiblePrefixNamespaces;
} else {
$ns = $title->getNamespace();
}
$dbr = wfGetDB(DB_REPLICA);
return TitleArray::newFromResult($dbr->select(
'page',
['page_namespace', 'page_title'],
[
'page_namespace' => $ns,
'page_title'.$dbr->buildLike($title->getDBkey(), $dbr->anyString()),
'page_title NOT LIKE "%/%"', # exclude subpages
'page_is_redirect=0'
],
__METHOD__,
['ORDER BY' => ['page_title', 'page_namespace']]
));
}
function addDetails($titles){
$items = [];
foreach ($titles as $title){
$details = [];
if ($title->getNamespace() != 0)
$details['ns'] = str_replace('_', ' ', $title->getNsText());
Hooks::run('FlexiblePrefixDetails', [clone $title, &$details, $this->getContext()]);
$items[] = ['title'=>$title, 'details'=>$details];
}
Hooks::run('FlexiblePrefixBeforeDisplay', [&$items, $this->getContext()]);
return $items;
}
function makeList($titlesWithDetails, $currentTitle=null){
$html = '<ul>';
foreach ($titlesWithDetails as $item){
$html .= '<li>';
if ($currentTitle && $item['title']->equals($currentTitle))
$html .= Linker::makeSelfLinkObj($item['title'], $item['title']->getText());
else
$html .= Linker::linkKnown($item['title'], $item['title']->getText());
if ($item['details'])
$html .= ' (' . implode(array_values($item['details']), ', ') . ')';
$html .= '</li>';
}
return $html . '</ul>';
}
function execute( $par ) {
global $wgFlexiblePrefixNamespaces;
$this->setHeaders();
if ($wgFlexiblePrefixNamespaces == null){
$out->addHTML('$wgFlexiblePrefixNamespaces not set.');
return;
}
$out = $this->getOutput();
if (empty($par)){
$out->addWikiText(wfMessage('notargettext'));
$out->setPageTitle(wfMessage('notargettitle'));
return;
}
try {
$titles = $this->getTitles($par);
} catch (MalformedTitleException $e){
$out->setPageTitle(wfMessage('invalidtitle'));
return;
}
if ($titles->count() == 0)
$out->addHTML('No results found.');
elseif ($titles->count() == 1)
$out->redirect($titles->current()->getFullURL());
else
$out->addHTML($this->makeList($this->addDetails($titles)));
}
}