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