-
Notifications
You must be signed in to change notification settings - Fork 0
/
DIQAimport.php
271 lines (219 loc) · 7.53 KB
/
DIQAimport.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
use DIQA\Import\Models\CrawlerConfig;
use Illuminate\Database\QueryException;
use DIQA\Import\Specials\TaggingSpecialPage;
/**
* The main file of the DIQA Import extension
*
* @file
* @ingroup DIQA
*/
if ( !defined( 'MEDIAWIKI' ) ) {
die( 'This file is part of a MediaWiki extension, it is not a valid entry point.' );
}
define( 'DIQA_IMPORT_VERSION', '0.1' );
########################################
# Default settings
########################################
global $wgDIQAImportUseAllMetadata;
$wgDIQAImportUseAllMetadata = false;
########################################
global $wgVersion;
global $wgExtensionCredits;
global $wgExtensionMessagesFiles;
global $wgHooks;
global $wgResourceModules;
global $wgExtensionFunctions;
global $wgActions;
// register extension
$wgExtensionCredits[ 'diqa' ][] = array(
'path' => __FILE__,
'name' => 'Import',
'author' => array( 'DIQA Projektmanagement GmbH' ),
'license-name' => 'GPL-2.0+',
'url' => 'http://www.diqa-pm.com',
'descriptionmsg' => 'diqa-import-desc',
'version' => DIQA_IMPORT_VERSION,
);
$dir = dirname( __FILE__ );
$wgExtensionMessagesFiles['DIQAimport'] = $dir . '/DIQAimport.i18n.php';
$wgExtensionFunctions[] = 'wfDIQAimportSetup';
// allow sysops to crawl/tag by default. can be revoked in LocalSettings
global $wgGroupPermissions;
$wgGroupPermissions['sysop']['diqa-crawl'] = true;
$wgGroupPermissions['sysop']['diqa-tag'] = true;
global $wgJobClasses;
$wgJobClasses['ImportDocumentJob'] = 'DIQA\Import\ImportDocumentJob';
$wgJobClasses['ImportImageJob'] = 'DIQA\Import\ImportImageJob';
$wgJobClasses['CrawlDirectoryJob'] = 'DIQA\Import\CrawlDirectoryJob';
$wgJobClasses['RefreshDocumentsJob'] = 'DIQA\Import\RefreshDocumentsJob';
$GLOBALS['wgAPIModules']['diqa_import'] = 'DIQA\Import\Api\DIQAImportAPI';
$GLOBALS['wgAPIModules']['diqa_import_log'] = 'DIQA\Import\Api\DIQAImportLoggingAPI';
$wgActions['directannotation'] = 'DIQA\Import\Search\FacetedSearchExtensions';
$wgResourceModules['ext.diqaimport.core'] = array(
'localBasePath' => $dir,
'remoteExtPath' => 'Import',
'scripts' => array(
'libs/fancytree/jquery.fancytree-all.js',
'libs/jquery-combobox.js',
'libs/import-page-crawler.js',
'libs/import-page-tagging-folderpicker.js',
'libs/import-page-tagging.js',
),
'styles' => [
'skins/diqa-import.css',
'libs/fancytree/skin-win8/ui.fancytree.min.css'
],
'dependencies' => [ 'jquery.ui.autocomplete', 'jquery.ui.datepicker', 'jquery.tablesorter', 'jquery.ui.core', 'jquery.effects.core', 'jquery.ui.slider' ],
'messages' => array(
'diqa-import-no-file-selected',
'diqa-import-regex-label',
'diqa-import-regex-path-label'
),
);
$wgResourceModules['ext.diqaimport.enhancedretrieval'] = array(
'localBasePath' => $dir,
'remoteExtPath' => 'Import',
'scripts' => array(
'libs/xfs_facetedsearch.js',
),
'styles' => [],
'dependencies' => [],
'messages' => array(
'diqa-import-open-document',
'diqa-import-open-document-page',
'diqa-import-open-document-dir',
),
);
$wgHooks['ParserAfterStrip'][] = 'DIQA\Import\TaggingRuleParserFunction::parserAfterStrip';
$wgHooks['ParserFirstCallInit'][] = 'wfDIQAimportRegisterParserHooks';
$wgHooks['fs_saveArticle'][] = 'DIQA\Import\MetadataIndexer::addExtractedMetadata';
$wgHooks['LoadExtensionSchemaUpdates'][] = 'wfDIQAimportDBUpdate';
function wfDIQAimportRegisterParserHooks(Parser $parser)
{
// Create a function hook associating the name of the parser function with the method to call
$parser->setFunctionHook('chooseTaggingValue', 'DIQA\Import\TaggingRuleParserFunction::chooseTaggingValue');
// add JS modules
global $wgOut, $wgTitle;
$wgOut->addModules('ext.diqaimport.enhancedretrieval');
if (!is_null($wgTitle) && ($wgTitle->getNamespace() == NS_SPECIAL &&
(strtolower($wgTitle->getText()) == 'diqaimport') || strtolower($wgTitle->getText()) == 'diqatagging')) {
$wgOut->addModules('ext.diqaimport.core');
TaggingSpecialPage::addJSData();
}
}
/**
* Updates DB schema (if necessary)
*/
function wfDIQAimportDBUpdate() {
require_once('maintenance/Setup.php');
$setup = new SetupDIQAImport();
$setup->execute();
}
/**
* Setup import extension
*/
function wfDIQAimportSetup() {
global $wgOut;
global $fsgExtraPropertiesToRequest;
$fsgExtraPropertiesToRequest[] = 'smwh_diqa_import_fullpath_xsdvalue_t';
$fsgExtraPropertiesToRequest[] = 'smwh_DIQAFileLocation_xsdvalue_t';
$fsgExtraPropertiesToRequest[] = 'smwh_DIQAFilename_xsdvalue_t';
$fsgExtraPropertiesToRequest[] = 'smwh_DIQAFilesuffix_xsdvalue_t';
global $wgSpecialPages;
$wgSpecialPages['DIQAImport'] = array('DIQA\Import\Specials\ImportSpecialPage');
$wgSpecialPages['DIQATagging'] = array('DIQA\Import\Specials\TaggingSpecialPage');
// temporarily deactivate assistent
//$wgSpecialPages['DIQAImportAssistent'] = array('DIQA\Import\Specials\ImportAssistentSpecialPage');
wfDIQAInitializeEloquent();
global $fsgTitleProperty;
$fsgTitleProperty = isset($fsgTitleProperty) ? $fsgTitleProperty : '';
// serialize crawler config to JS
$script = "";
$script .= "\nvar DIQA = DIQA || {};";
$script .= "\nDIQA.IMPORT = DIQA.IMPORT || {};";
$script .= "\nDIQA.IMPORT.crawlerConfig = [];";
$script .= "\nDIQA.IMPORT.fsgTitleProperty = '{$fsgTitleProperty}';";
$extractedMetadata = json_encode(wfDIQAGetMetadata());
$script .= "\nDIQA.IMPORT.extractedMetadata = {$extractedMetadata};";
try {
$entries = CrawlerConfig::all ();
$mappings = [];
foreach($entries as $e) {
$mappings[] = [
'root_path' => $e->getRootPath(),
'url_prefix' => $e->getURLPrefix()
];
}
usort($mappings, function($e, $f) {
return strlen($f['root_path']) - strlen($e['root_path']);
});
$script .= "\nDIQA.IMPORT.crawlerConfig = ".json_encode($mappings).";";
} catch(QueryException $e) {
// ignore. table may not exist before setup
}
$wgOut->addScript(
'<script type="text/javascript">'.$script.'</script>'
);
return true;
}
function wfDIQAGetMetadata() {
$cache = ObjectCache::getInstance(CACHE_DB);
$list = $cache->get('DIQA.Import.metadataProperties');
if ($list === false) {
return [];
}
usort($list, function($e, $f) {
return strcmp(strtolower($e), strtolower($f));
});
return $list;
}
/**
* Setups Eloquent ORM-Mapper
*/
function wfDIQAInitializeEloquent() {
$capsule = new Capsule ();
global $wgDBname, $wgDBuser, $wgDBpassword;
$capsule->addConnection ( [
'driver' => 'mysql',
'host' => 'localhost',
'database' => $wgDBname,
'username' => $wgDBuser,
'password' => $wgDBpassword,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => ''
] );
// Set the event dispatcher used by Eloquent models... (optional)
$capsule->setEventDispatcher ( new Dispatcher ( new Container () ) );
// Make this Capsule instance available globally via static methods... (optional)
$capsule->setAsGlobal ();
// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$capsule->bootEloquent ();
}
/**
* Shortens a string if it is longer than 55 chars.
*
* @param string $str
* @return unknown|string
*/
function wfDIQAShorten($str) {
if (strlen($str) < 55) {
return $str;
}
return substr($str, 0, 5) . " ... " . substr($str, -50);
}
/**
* Returns absolute Wiki-URL from relative path.
*
* @param string $url relative URL
* @return string
*/
function wfDIQAURL($url) {
global $wgServer, $wgScriptPath;
$url = ltrim($url, '/');
return $wgServer.$wgScriptPath.'/index.php/'.$url;
}