-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamicSidebarMenu.php
executable file
·223 lines (195 loc) · 5.92 KB
/
DynamicSidebarMenu.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
if( !defined( 'MEDIAWIKI' ) )
{
echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );
die( 1 );
}
class DynamicSidebarMenu
{
private $debug = false; ///< Debug option - true to show debug statements, false to not
private $title; ///< Title of the current page (Title object)
private $namespace = ''; ///< Title current namespace
/**
* Options array
*/
private $options = array();
/**
* Hook function to draw the wenu
*/
static function createMenu( Skin $skin, &$bar )
{
global $dsmDebug;
$menu = new DynamicSidebarMenu( $skin->getRelevantTitle() );
$options = array( "parent" => "/");
$menu->options( $options );
$text = $menu->render();
$bar["DynamicSidebarMenu"]="<div id=\"DSM\">$text</div>";
return true;
}
/**
* Called when the extension is first loaded
*/
public static function onRegistration()
{
global $wgExtensionFunctions;
$wgExtensionFunctions[] = 'DynamicSidebarMenu::setup';
}
/**
* Called at extension setup time, install hooks and module resources
*/
public static function setup()
{
global $wgOut, $wgExtensionAssetsPath, $wgAutoloadClasses, $IP, $wgResourceModules, $dsmExpandSidebar;
// This gets the remote path even if it's a symlink (MW1.25+)
$path = str_replace( "$IP/extensions", '', __DIR__ );
$path = str_replace( "$IP\\extensions", '', $path ); // Windows uses forward slash
// Fancytree script and styles
$wgResourceModules['ext.fancytree']['localBasePath'] = __DIR__ . '/fancytree';
$wgResourceModules['ext.fancytree']['remoteExtPath'] = "$path/fancytree";
$wgOut->addModules( 'ext.fancytree' );
$wgOut->addStyle( "$wgExtensionAssetsPath/$path/fancytree/fancytree.css" );
$wgOut->addStyle( "$wgExtensionAssetsPath/$path/fancytree/dynamicsidebarmenu.css" );
if($dsmExpandSidebar)
$wgOut->addStyle( "$wgExtensionAssetsPath/$path/fancytree/dynamicsidebarmenu-expandsidebar.css" );
$wgOut->addJsConfigVars( 'fancytree_path', "$wgExtensionAssetsPath/$path/fancytree" );
}
function __construct( $title )
{
global $wgContLang;
$this->title = $title;
if (!is_null($title))
$this->namespace = $title->getNamespace();
else
$this->namespace = 0;
self::log("Created a DynamicSideMenu with title => '$title' and namespace => '".$this->namespace."'");
}
function options( $options )
{
self::log("Setting options: ", $options);
if( isset( $options['debug'] ) )
if( $options['debug'] == 'true' || intval( $options['debug'] ) == 1 )
$this->debug = true;
}
function render()
{
$pages = $this->queryDatabase();
if( $pages != null && count( $pages ) > 0 )
{
$html = $this->menufy( $pages );
} else {
$html = "''" . $this->title->getText() . "''\n";
}
return $html;
}
/**
* Query the database to obtain all titles.
* @return array all titles
*/
function queryDatabase()
{
self::log("Querying the database");
$dbr = wfGetDB( DB_SLAVE );
$conditions = array();
$options = array();
$options['ORDER BY'] = 'page_title ASC';
$conditions['page_namespace'] = $this->namespace; // don't let list the streams(namespaces) cross
$conditions['page_is_redirect'] = 0; // do not list redirects (for now)
$fields = array('page_title', 'page_namespace');
$res = $dbr->select( 'page', $fields, $conditions, __METHOD__, $options );
$titles = array();
$i = 0;
foreach ( $res as $row )
{
$i++;
$title = Title::makeTitle( $row->page_namespace, $row->page_title );
if( $title )
$titles[] = $title;
}
self::log("Processed ".count($titles)." out of $i results");
return $titles;
}
/**
* Create a link item (anchor link) of given title
* @param $title Title the title of a page
* @return string the prepared string
*/
function createItemLink($title)
{
$a = '<a href="'.$title->getFullURL().'">'.$title->getSubpageText().'</a>';
self::log("a =".$a);
return '<a href="'.$title->getFullURL().'">'.$title->getSubpageText().'</a>';
}
/**
* Make the given list of titles into a menu
* @param $titles Array all page titles
* @return string the whole list
*/
function menufy( $titles )
{
$menuList = array(); // list in Namespace:Path/to/Title (string)
$menu = array();
$existingTitles = array(); // array of existing titles
// creates a list of all the menu levels we need making sure all items can be connected
self::log("Sorting array");
asort($titles);
self::log("Creating menu structure");
foreach( $titles as $title )
{
$existingTitles[] = $title->getFullText();
$levels = explode('/', $title->getFullText());
$titlePath="";
foreach ($levels as $level)
{
$titlePath = trim("$titlePath/$level", '/');
if(!in_array($titlePath, $menuList) && strlen($titlePath) > 0)
{
$menuList[] = $titlePath;
}
}
}
self::log("Creating menu");
$prevLevel = 0;
$level = 0;
$out = '<ul id="DSMData">';
foreach( $menuList as $menuTitle )
{
self::log("Processing: $menuTitle");
$level = substr_count($menuTitle, '/');
if($level < $prevLevel)
{
$out .= '</ul></li>';
}
else if ($level > $prevLevel)
{
$out .= '<ul>';
}
if(!in_array($menuTitle, $existingTitles))
$out .= '<li class="DSMRed">'.$this->createItemLink(Title::newFromText($menuTitle));
else
$out .= '<li>'.$this->createItemLink(Title::newFromText($menuTitle)); // closing is done by the next item
$prevLevel = $level;
}
return $out.'</li></ul>';
}
/**
* Prints debugging information.
* @param string $debugText Text to print
* @param array $debugArgs Argument to show. Will be expanded into argItem::argItem2::argItem3::...
*/
static function log( $debugText, $debugArgs = null )
{
global $dsmDebug;
if($dsmDebug)
{
if ( isset( $debugArgs ) )
{
$text = $debugText . " " . implode( "::", $debugArgs );
wfDebugLog( 'dynamicsidebarmenu', $text );
}
else
{
wfDebugLog( 'dynamicsidebarmenu', $debugText );
}
}
}
}