forked from ProfessionalWiki/Maps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maps.hooks.php
219 lines (187 loc) · 5.39 KB
/
Maps.hooks.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
<?php
/**
* Static class for hooks handled by the Maps extension.
*
* @since 0.7
*
* @licence GNU GPL v2+
* @author Jeroen De Dauw < [email protected] >
* @author Daniel Werner
*/
final class MapsHooks {
/**
* Helper flag indicating whether the page has been purged.
* @var bool
*
* TODO: Figure out a better way to do this, not requiring this flag and make sure it works with
* later MW versions (purging mechanism got changed somewhat around 1.18).
*/
static $purgedBeforeStore = false;
/**
* Adds a link to Admin Links page.
*
* @since 0.7
*
* @param ALTree $admin_links_tree
*
* @return boolean
*/
public static function addToAdminLinks( ALTree &$admin_links_tree ) {
$displaying_data_section = $admin_links_tree->getSection( wfMessage( 'smw_adminlinks_displayingdata' )->text() );
// Escape if SMW hasn't added links.
if ( is_null( $displaying_data_section ) ) {
return true;
}
$smw_docu_row = $displaying_data_section->getRow( 'smw' );
$maps_docu_label = wfMessage( 'adminlinks_documentation', 'Maps' )->text();
$smw_docu_row->addItem( AlItem::newFromExternalLink( 'https://semantic-mediawiki.org/wiki/Maps', $maps_docu_label ) );
return true;
}
/**
* Intercept pages in the Layer namespace to handle them correctly.
*
* @param $title: Title
* @param $article: Article or null
*
* @return boolean
*/
public static function onArticleFromTitle( Title &$title, /* Article */ &$article ) {
if ( $title->getNamespace() == Maps_NS_LAYER ) {
$article = new MapsLayerPage( $title );
}
return true;
}
/**
* Adds global JavaScript variables.
*
* @since 1.0
* @see http://www.mediawiki.org/wiki/Manual:Hooks/MakeGlobalVariablesScript
* @param array &$vars Variables to be added into the output
* @param OutputPage $outputPage OutputPage instance calling the hook
* @return boolean true in all cases
*/
public static function onMakeGlobalVariablesScript( array &$vars, OutputPage $outputPage ) {
global $egMapsGlobalJSVars;
$vars['egMapsDebugJS'] = $GLOBALS['egMapsDebugJS'];
$vars[ 'egMapsAvailableServices' ] = $GLOBALS['egMapsAvailableServices'];
$vars += $egMapsGlobalJSVars;
return true;
}
/**
* @since 0.7
*
* @param array $list
*
* @return boolean
*/
public static function onCanonicalNamespaces( array &$list ) {
$list[Maps_NS_LAYER] = 'Layer';
$list[Maps_NS_LAYER_TALK] = 'Layer_talk';
return true;
}
/**
* This will setup database tables for layer functionality.
*
* @since 3.0
*
* @param DatabaseUpdater $updater
*
* @return true
*/
public static function onLoadExtensionSchemaUpdates( DatabaseUpdater $updater ) {
switch( $GLOBALS['wgDBtype'] ) {
case 'mysql':
case 'sqlite':
$updater->addExtensionTable( 'maps_layers', __DIR__ . '/schema/MapsLayers.sql' );
break;
case 'postgres':
$updater->addExtensionTable( 'maps_layers', __DIR__ . '/schema/MapsLayers-postgres.sql' );
break;
}
return true;
}
/**
* Make sure layer data will be stored into database when purging the page
*
* @since 3.0
*
* @param $article WikiPage|Article (depending on MW version, WikiPage in 1.18+)
* @return type
*/
public static function onArticlePurge( &$article ) {
self::$purgedBeforeStore = true;
return true;
}
/**
* At the end of article parsing, in case of layer page, save layers to database
*
* @since 3.0
*
* @param Parser &$parser
* @param string &$text
*
* @return true
*/
public static function onParserAfterTidy( Parser &$parser, &$text ) {
$title = $parser->getTitle();
if( $title === null
|| self::$purgedBeforeStore !== true
) {
// just preprocessing some stuff or no purge
return true;
}
self::processLayersStoreCandidate( $parser->getOutput(), $title );
// Set helper to false immediately so we won't run into job-processing weirdness:
self::$purgedBeforeStore = false;
return true;
}
/**
* After article was edited and parsed, in case of layer page, save layers to database
*
* @since 3.0
*
* @param LinksUpdate &$linksUpdate
*
* @return true
*/
public static function onLinksUpdateConstructed( LinksUpdate &$linksUpdate ) {
$title = $linksUpdate->getTitle();
self::processLayersStoreCandidate( $linksUpdate->mParserOutput, $title );
return true;
}
/**
* Checks whether the parser output has some layer data which should be stored of the
* given title and performs the task.
*
* @since 3.0
*
* @param ParserOutput $parserOutput
* @param Title $title
*/
protected static function processLayersStoreCandidate( ParserOutput $parserOutput, Title $title ) {
// if site which is being parsed is in maps namespace:
if( $title->getNamespace() === Maps_NS_LAYER ) {
if( ! isset( $parserOutput->mExtMapsLayers ) ) {
$parserOutput->mExtMapsLayers = new MapsLayerGroup();
}
// get MapsLayerGroup object with layers to be stored:
$mapsForStore = $parserOutput->mExtMapsLayers;
// store layers in database (also deletes previous definitions still in db):
MapsLayers::storeLayers( $mapsForStore, $title );
}
}
/**
* If a new parser process is getting started, clear collected layer data of the
* previous one.
*
* @since 3.0
*
* @param Parser $parser
*
* @return true
*/
public static function onParserClearState( Parser &$parser ) {
$parser->getOutput()->mExtMapsLayers = null;
return true;
}
}