Completed
Push — address-as-title ( feca68...38b79f )
by Peter
04:24
created

Maps.hooks.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Static class for hooks handled by the Maps extension.
5
 *
6
 * @since 0.7
7
 *
8
 * @licence GNU GPL v2+
9
 * @author Jeroen De Dauw < [email protected] >
10
 * @author Daniel Werner
11
 */
12
final class MapsHooks {
13
	/**
14
	 * Helper flag indicating whether the page has been purged.
15
	 * @var bool
16
	 *
17
	 * TODO: Figure out a better way to do this, not requiring this flag and make sure it works with
18
	 *       later MW versions (purging mechanism got changed somewhat around 1.18).
19
	 */
20
	static $purgedBeforeStore = false;
21
22
	/**
23
	 * Adds a link to Admin Links page.
24
	 *
25
	 * @since 0.7
26
	 *
27
	 * @param ALTree $admin_links_tree
28
	 *
29
	 * @return boolean
30
	 */
31
	public static function addToAdminLinks( ALTree &$admin_links_tree ) {
32
		$displaying_data_section = $admin_links_tree->getSection( wfMessage( 'smw_adminlinks_displayingdata' )->text() );
33
34
		// Escape if SMW hasn't added links.
35
		if ( is_null( $displaying_data_section ) ) {
36
			return true;
37
		}
38
39
		$smw_docu_row = $displaying_data_section->getRow( 'smw' );
40
41
		$maps_docu_label = wfMessage( 'adminlinks_documentation', 'Maps' )->text();
42
		$smw_docu_row->addItem( AlItem::newFromExternalLink( 'https://semantic-mediawiki.org/wiki/Maps', $maps_docu_label ) );
43
44
		return true;
45
	}
46
47
	/**
48
	 * Intercept pages in the Layer namespace to handle them correctly.
49
	 *
50
	 * @param $title: Title
51
	 * @param $article: Article or null
52
	 *
53
	 * @return boolean
54
	 */
55
	public static function onArticleFromTitle( Title &$title, /* Article */ &$article ) {
56
		if ( $title->getNamespace() == Maps_NS_LAYER ) {
57
			$article = new MapsLayerPage( $title );
58
		}
59
60
		return true;
61
	}
62
63
	/**
64
	 * Adds global JavaScript variables.
65
	 *
66
	 * @since 1.0
67
         * @see http://www.mediawiki.org/wiki/Manual:Hooks/MakeGlobalVariablesScript
68
         * @param array &$vars Variables to be added into the output
69
         * @param OutputPage $outputPage OutputPage instance calling the hook
70
         * @return boolean true in all cases
71
	 */
72
	public static function onMakeGlobalVariablesScript( array &$vars, OutputPage $outputPage ) {
0 ignored issues
show
The parameter $outputPage is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
onMakeGlobalVariablesScript uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
73
		global $egMapsGlobalJSVars;
74
75
		$vars['egMapsDebugJS'] = $GLOBALS['egMapsDebugJS'];
76
                $vars[ 'egMapsAvailableServices' ] = $GLOBALS['egMapsAvailableServices'];
77
78
		$vars += $egMapsGlobalJSVars;
79
80
		return true;
81
	}
82
83
	/**
84
	 * @since 0.7
85
	 *
86
	 * @param array $list
87
	 *
88
	 * @return boolean
89
	 */
90
	public static function onCanonicalNamespaces( array &$list ) {
91
		$list[Maps_NS_LAYER] = 'Layer';
92
		$list[Maps_NS_LAYER_TALK] = 'Layer_talk';
93
		return true;
94
	}
95
96
	/**
97
	 * This will setup database tables for layer functionality.
98
	 *
99
	 * @since 3.0
100
	 *
101
	 * @param DatabaseUpdater $updater
102
	 *
103
	 * @return true
104
	 */
105
	public static function onLoadExtensionSchemaUpdates( DatabaseUpdater $updater ) {
0 ignored issues
show
onLoadExtensionSchemaUpdates uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
106
		switch( $GLOBALS['wgDBtype'] ) {
107
			case 'mysql':
108
			case 'sqlite':
109
				$updater->addExtensionTable( 'maps_layers', __DIR__ . '/schema/MapsLayers.sql' );
110
				break;
111
			case 'postgres':
112
				$updater->addExtensionTable( 'maps_layers', __DIR__ . '/schema/MapsLayers-postgres.sql' );
113
				break;
114
		}
115
116
		return true;
117
	}
118
119
	/**
120
	 * Make sure layer data will be stored into database when purging the page
121
	 *
122
	 * @since 3.0
123
	 *
124
	 * @param $article WikiPage|Article (depending on MW version, WikiPage in 1.18+)
125
	 * @return type
126
	 */
127
	public static function onArticlePurge( &$article ) {
0 ignored issues
show
The parameter $article is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
128
		self::$purgedBeforeStore = true;
129
		return true;
130
	}
131
132
	/**
133
	 * At the end of article parsing, in case of layer page, save layers to database
134
	 *
135
	 * @since 3.0
136
	 *
137
	 * @param Parser &$parser
138
	 * @param string &$text
139
	 *
140
	 * @return true
141
	 */
142
	public static function onParserAfterTidy( Parser &$parser, &$text ) {
0 ignored issues
show
The parameter $text is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
143
144
		$title = $parser->getTitle();
145
146
		if( $title === null
147
			|| self::$purgedBeforeStore !== true
148
		) {
149
			// just preprocessing some stuff or no purge
150
			return true;
151
		}
152
153
		self::processLayersStoreCandidate( $parser->getOutput(), $title );
154
155
		// Set helper to false immediately so we won't run into job-processing weirdness:
156
		self::$purgedBeforeStore = false;
157
158
		return true;
159
	}
160
161
	/**
162
	 * After article was edited and parsed, in case of layer page, save layers to database
163
	 *
164
	 * @since 3.0
165
	 *
166
	 * @param LinksUpdate &$linksUpdate
167
	 *
168
	 * @return true
169
	 */
170
	public static function onLinksUpdateConstructed( LinksUpdate &$linksUpdate ) {
171
		$title = $linksUpdate->getTitle();
172
173
		self::processLayersStoreCandidate( $linksUpdate->mParserOutput, $title );
174
175
		return true;
176
	}
177
178
	/**
179
	 * Checks whether the parser output has some layer data which should be stored of the
180
	 * given title and performs the task.
181
	 *
182
	 * @since 3.0
183
	 *
184
	 * @param ParserOutput $parserOutput
185
	 * @param Title $title
186
	 */
187
	protected static function processLayersStoreCandidate( ParserOutput $parserOutput, Title $title ) {
188
189
		// if site which is being parsed is in maps namespace:
190
		if( $title->getNamespace() === Maps_NS_LAYER ) {
191
192
			if( ! isset( $parserOutput->mExtMapsLayers ) ) {
193
				$parserOutput->mExtMapsLayers = new MapsLayerGroup();
194
			}
195
196
			// get MapsLayerGroup object with layers to be stored:
197
			$mapsForStore = $parserOutput->mExtMapsLayers;
198
199
			// store layers in database (also deletes previous definitions still in db):
200
			MapsLayers::storeLayers( $mapsForStore, $title );
201
		}
202
	}
203
204
	/**
205
	 * If a new parser process is getting started, clear collected layer data of the
206
	 * previous one.
207
	 *
208
	 * @since 3.0
209
	 *
210
	 * @param Parser $parser
211
	 *
212
	 * @return true
213
	 */
214
	public static function onParserClearState( Parser &$parser ) {
215
		$parser->getOutput()->mExtMapsLayers = null;
216
		return true;
217
	}
218
}
219
220