Completed
Pull Request — master (#22)
by Karsten
08:45
created

SemanticTasks.php (1 issue)

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
 * @see https://github.com/SemanticMediaWiki/SemanticTasks
5
 *
6
 * @defgroup SemanticTasks Semantic Tasks
7
 */
8
9
use ST\SemanticTasksMailer;
10
11
SemanticTasks::load();
12
13
/**
14
 * @codeCoverageIgnore
15
 */
16
class SemanticTasks {
17
18
	public static function load() {
19
		if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) {
20
			include_once __DIR__ . '/vendor/autoload.php';
21
		}
22
	}
23
24
	/**
25
	 * @global boolean $wgSemanticTasksNotifyIfUnassigned
26
	 * @since 1.0
27
	 * @see https://www.mediawiki.org/wiki/Manual:Extension.json/Schema#callback
28
	 */
29
	public static function initExtension( $credits = [] ) {
30
31
		$version = 'UNKNOWN' ;
32
33
		// See https://phabricator.wikimedia.org/T151136
34
		if ( isset( $credits['version'] ) ) {
35
			$version = $credits['version'];
36
		}
37
38
		define( 'SEMANTIC_TASKS', $version );
39
40
		// Register extension messages and other localisation.
41
		$wgMessagesDirs['SemanticTasks'] = __DIR__ . '/i18n';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$wgMessagesDirs was never initialized. Although not strictly required by PHP, it is generally a good practice to add $wgMessagesDirs = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
42
43
		// Set to true to notify users when they are unassigned from a task
44
		global $wgSemanticTasksNotifyIfUnassigned;
45
		$wgSemanticTasksNotifyIfUnassigned = false;
46
	}
47
48
	/**
49
	 * @since 1.0
50
	 */
51
	public static function onExtensionFunction() {
52
53
		// Check requirements after LocalSetting.php has been processed
54
		if ( !defined( 'SMW_VERSION' ) ) {
55
			if ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' ) {
56
				die( "\nThe 'Semantic Tasks' extension requires the 'Semantic MediaWiki' extension to be installed and enabled.\n" );
57
			} else {
58
				die(
59
					'<b>Error:</b> The <a href="https://github.com/SemanticMediaWiki/SemanticTasks">Semantic Tasks</a> extension' .
60
					' requires the <a href="https://www.semantic-mediawiki.org/wiki/Semantic_MediaWiki">Semantic MediaWiki</a> extension to be installed and enabled.<br />'
61
				);
62
			}
63
		}
64
65
		$assignees = new \ST\Assignees();
66
67
		// Register extension hooks.
68
		global $wgHooks;
69
		$wgHooks['PageContentSave'][] = [ $assignees, 'saveAssignees' ];
70
		$wgHooks['PageContentSaveComplete'][] = function(WikiPage $article, User $current_user, $text,
71
				$summary, $minoredit, $watchthis, $sectionanchor, $flags) use ($assignees) {
72
			SemanticTasksMailer::mailAssigneesUpdatedTask(
73
				$assignees, $article, $current_user, $text,
74
				$summary, $minoredit, $watchthis, $sectionanchor, $flags
75
			);
76
		};
77
	}
78
79
}
80