-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkAutocomplete.php
146 lines (136 loc) · 4.31 KB
/
LinkAutocomplete.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
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
<?php
/**
* Link autocompleter for MediaWiki
*
* Copyright 2013 Vitaliy Filippov <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @file
* @ingroup Extensions
* @author Vitaliy Filippov <[email protected]>
*/
# TODO: Autocomplete template and parser function parameters
# TODO: Autocomplete semantic properties [[Property::Value]]
# TODO: Maybe cache loaded items?
$wgHooks['BeforePageDisplay'][] = 'efLinkAutocomplete_BPD';
$wgExtensionFunctions[] = 'efLinkAutocomplete';
$wgAjaxExportList[] = 'efLinkAutocomplete_ParserFunctions';
$wgResourceModules['LinkAutocomplete'] = array(
'localBasePath' => __DIR__,
'remoteExtPath' => basename(__DIR__),
'scripts' => array('hinter.js', 'linkautocomplete.js'),
'styles' => array('hinter.css'),
);
$wgExtensionCredits['other'][] = array(
'name' => 'LinkAutocomplete',
'version' => '2013-10-21',
'author' => 'Vitaliy Filippov',
'url' => 'http://wiki.4intra.net/LinkAutocomplete',
'description' => 'Link autocompleter for MediaWiki editbox',
);
function efLinkAutocomplete()
{
global $wgResourceModules;
if (isset($wgResourceModules['ext.wikiEditor']))
{
$a = &$wgResourceModules['ext.wikiEditor']['dependencies'];
$a = (array)$a;
$a[] = 'LinkAutocomplete';
}
}
function efLinkAutocomplete_BPD($out)
{
global $wgRequest;
$action = $wgRequest->getVal('action');
if ($action == 'edit' || $action == 'submit')
{
$out->addModules('LinkAutocomplete');
}
return true;
}
class TrackingParser extends Parser
{
var $setters = array();
// Track setFunctionHook() calls to find out the extension which has registered it
function setFunctionHook($id, $callback, $flags = 0)
{
$setter = 'core';
foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) as $frame)
{
if (isset($frame['file']) &&
preg_match('#[/\\\\]extensions[/\\\\]([^/\\\\]*)[/\\\\]#is', $frame['file'], $m) &&
$m[1] != 'LinkAutocomplete')
{
$setter = $m[1];
break;
}
}
$this->setters[$id] = $setter;
return parent::setFunctionHook($id, $callback, $flags);
}
}
function efLinkAutocomplete_ParserFunctions()
{
$parser = new TrackingParser();
$parser->firstCallInit();
$result = array();
foreach ($parser->mFunctionHooks as $f => $settings)
{
$mag = MagicWord::get($f);
$setter = $parser->setters[$f];
foreach ($mag->mSynonyms as $f)
{
if (!$mag->mCaseSensitive)
{
$f = mb_strtolower($f);
}
$f = rtrim($f, ':');
if (preg_match('/^[\x20-\x7F]+$/', $f))
{
// Prefer english synonym
break;
}
}
if (!($settings[1] & Parser::SFH_NO_HASH))
{
$f = "#$f";
}
$result[] = array($f, $setter);
}
usort($result, function($a, $b)
{
// first list extension functions
if ($a[1] == 'core' && $b[1] != 'core')
{
return 1;
}
elseif ($a[1] != 'core' && $b[1] == 'core')
{
return -1;
}
// first list lowercase functions
elseif (mb_strtolower($a[0]{0}) == $a[0]{0} && mb_strtolower($b[0]{0}) != $b[0]{0})
{
return -1;
}
elseif (mb_strtolower($a[0]{0}) != $a[0]{0} && mb_strtolower($b[0]{0}) == $b[0]{0})
{
return 1;
}
return strcmp($a[1].'-'.$a[0], $b[1].'-'.$b[0]);
});
return json_encode($result);
}