forked from joomla/joomla-cms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpTOC.php
222 lines (181 loc) · 5.53 KB
/
helpTOC.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
<?php
/**
* @package Joomla.Build
*
* @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// Set flag that this is a parent file.
const _JEXEC = 1;
// Load system defines
if (file_exists(dirname(__DIR__) . '/defines.php'))
{
require_once dirname(__DIR__) . '/defines.php';
}
if (!defined('_JDEFINES'))
{
define('JPATH_BASE', dirname(__DIR__));
require_once JPATH_BASE . '/includes/defines.php';
}
// Get the framework.
require_once JPATH_LIBRARIES . '/bootstrap.php';
// Configure error reporting to maximum for CLI output.
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Load the admin en-GB.ini language file to get the JHELP language keys
JFactory::getLanguage()->load('joomla', JPATH_ADMINISTRATOR, null, false, false);
/**
* Utility CLI to retrieve the list of help screens from the docs wiki and create an index for the admin help view.
*
* @since 3.0
*/
class MediawikiCli extends \Joomla\CMS\Application\CliApplication
{
/**
* Entry point for CLI script
*
* @return void
*
* @since 3.0
*/
public function doExecute()
{
// Get the version data for the script
$version = new \Joomla\CMS\Version();
$helpVersion = str_replace('.', '', $version::RELEASE);
$namespace = 'Help' . $helpVersion . ':';
// Set up HTTP driver for MediaWiki
$http = new \Joomla\Mediawiki\Http([], JHttpFactory::getAvailableDriver());
// Set up options for JMediawiki
$options = new Joomla\Registry\Registry;
$options->set('api.url', 'https://docs.joomla.org');
$mediawiki = new Joomla\Mediawiki\Mediawiki($options, $http);
// Get the category members (local hack)
$this->out('Fetching data from docs wiki', true);
$categoryMembers = $mediawiki->categories->getCategoryMembers('Category:Help_screen_' . $version::RELEASE, null, 'max');
$members = array();
// Loop through the result objects to get every document
foreach ($categoryMembers->query->categorymembers as $catmembers)
{
foreach ($catmembers as $member)
{
$members[] = (string) $member['title'];
}
}
// Get the language object
$language = JFactory::getLanguage();
// Get the language strings via Reflection as the property is protected
$refl = new ReflectionClass($language);
$property = $refl->getProperty('strings');
$property->setAccessible(true);
$strings = $property->getValue($language);
/*
* Now we start fancy processing so we can get the language key for the titles
*/
$cleanMembers = array();
// Strip the namespace prefix off the titles and replace spaces with underscores
foreach ($members as $member)
{
$cleanMembers[] = str_replace(array($namespace, ' '), array('', '_'), $member);
}
// Make sure we only have an array of unique values before continuing
$cleanMembers = array_unique($cleanMembers);
/*
* Loop through the cleaned up title array and the language strings array to match things up
*/
$matchedMembers = array();
foreach ($cleanMembers as $member)
{
foreach ($strings as $k => $v)
{
if ($member === $v)
{
$matchedMembers[] = $k;
continue;
}
}
}
// Alpha sort the array
asort($matchedMembers);
// Now we strip off the JHELP_ prefix from the strings to get usable strings for both COM_ADMIN and JHELP
$stripped = array();
foreach ($matchedMembers as $member)
{
$stripped[] = str_replace('JHELP_', '', $member);
}
/*
* Check to make sure a COM_ADMIN_HELP string exists, don't include in the TOC if not
*/
// Load the admin com_admin language file
$language->load('com_admin', JPATH_ADMINISTRATOR);
$toc = array();
foreach ($stripped as $string)
{
// Validate the key exists
$this->out('Validating key COM_ADMIN_HELP_' . $string, true);
if ($language->hasKey('COM_ADMIN_HELP_' . $string))
{
$this->out('Adding ' . $string, true);
$toc[$string] = $string;
}
// We check if the string for words in singular/plural form and check again
else
{
$this->out('Inflecting ' . $string, true);
if (strpos($string, '_CATEGORIES') !== false)
{
$inflected = str_replace('_CATEGORIES', '_CATEGORY', $string);
}
elseif (strpos($string, '_USERS') !== false)
{
$inflected = str_replace('_USERS', '_USER', $string);
}
elseif (strpos($string, '_CATEGORY') !== false)
{
$inflected = str_replace('_CATEGORY', '_CATEGORIES', $string);
}
elseif (strpos($string, '_USER') !== false)
{
$inflected = str_replace('_USER', '_USERS', $string);
}
else
{
$inflected = '';
}
// Now try to validate the key
if ($inflected !== '')
{
$this->out('Validating key COM_ADMIN_HELP_' . $inflected, true);
if ($language->hasKey('COM_ADMIN_HELP_' . $inflected))
{
$this->out('Adding ' . $inflected, true);
$toc[$string] = $inflected;
}
}
}
}
$this->out('Number of strings: ' . count($toc), true);
// JSON encode the file and write it to JPATH_ADMINISTRATOR/help/en-GB/toc.json
file_put_contents(JPATH_ADMINISTRATOR . '/help/en-GB/toc.json', json_encode($toc));
$this->out('Help Screen TOC written', true);
}
}
// Set up the container
JFactory::getContainer()->share(
'MediawikiCli',
function (\Joomla\DI\Container $container)
{
return new MediawikiCli(
null,
null,
null,
null,
$container->get(\Joomla\Event\DispatcherInterface::class),
$container
);
},
true
);
$app = JFactory::getContainer()->get('MediawikiCli');
JFactory::$application = $app;
$app->execute();