Passed
Branch master (e0a0b4)
by Sam
01:19 queued 16s
created

main.inc.php (13 issues)

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 15 and the first side effect is on line 12.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/*
3
Plugin Name: Piwigo to MediaWiki
4
Version: 0.1.0
5
Description: A Piwigo plugin for exporting photos to MediaWiki wikis.
6
Plugin URI: auto
7
Author: Sam Wilson
8
Author URI: https://samwilson.id.au
9
*/
10
11
// Make sure we're already in Piwigo.
12
defined('PHPWG_ROOT_PATH') or exit(1);
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
13
14
// Define plugin's paths etc.
15
define('PIWIGO2MEDIAWIKI_ID', 'piwigo2mediawiki');
16
define('PIWIGO2MEDIAWIKI_PATH', PHPWG_PLUGINS_PATH.PIWIGO2MEDIAWIKI_ID.'/');
0 ignored issues
show
The constant PHPWG_PLUGINS_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
17
define('PIWIGO2MEDIAWIKI_PAGE', 'plugin-'.PIWIGO2MEDIAWIKI_ID);
18
define(
19
  'PIWIGO2MEDIAWIKI_ADMIN',
20
  get_absolute_root_url().'admin.php?page=plugin-'.PIWIGO2MEDIAWIKI_ID
0 ignored issues
show
The function get_absolute_root_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
  /** @scrutinizer ignore-call */ 
21
  get_absolute_root_url().'admin.php?page=plugin-'.PIWIGO2MEDIAWIKI_ID
Loading history...
21
);
22
define(
23
  'PIWIGO2MEDIAWIKI_DIR',
24
  realpath(PHPWG_PLUGINS_PATH.PIWIGO2MEDIAWIKI_ID).'/'
25
);
26
27
// Complain if our plugin directory is not named correctly.
28
if (basename(dirname(__FILE__)) != PIWIGO2MEDIAWIKI_ID) {
29
  add_event_handler('init', function () {
0 ignored issues
show
The function add_event_handler was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
  /** @scrutinizer ignore-call */ 
30
  add_event_handler('init', function () {
Loading history...
30
    global $page;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
31
    $page['errors'][] = l10n(
0 ignored issues
show
The function l10n was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
    $page['errors'][] = /** @scrutinizer ignore-call */ l10n(
Loading history...
32
      'Plugin folder name is incorrect, please rename %s to %s',
33
      basename(dirname(__FILE__)),
34
      PIWIGO2MEDIAWIKI_ID
35
    );
36
  });
37
  return;
38
}
39
40
//Initialise the plugin.
41
add_event_handler('init', function(){
42
  global $conf;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
43
  load_language('plugin.lang', PIWIGO2MEDIAWIKI_PATH);
0 ignored issues
show
The function load_language was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
  /** @scrutinizer ignore-call */ 
44
  load_language('plugin.lang', PIWIGO2MEDIAWIKI_PATH);
Loading history...
44
  if (isset($conf['piwigo2mediawiki'])) {
45
    $conf['piwigo2mediawiki'] = safe_unserialize($conf['piwigo2mediawiki']);
0 ignored issues
show
The function safe_unserialize was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
    $conf['piwigo2mediawiki'] = /** @scrutinizer ignore-call */ safe_unserialize($conf['piwigo2mediawiki']);
Loading history...
46
  }
47
});
48
49
// Add event handlers.
50
if (defined('IN_ADMIN')) {
51
52
  // Add the admin menu item.
53
  add_event_handler('get_admin_plugin_menu_links', function($menu) {
54
    $menu[] = array(
55
      'NAME' => l10n('Piwigo to MediaWiki'),
0 ignored issues
show
The function l10n was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
      'NAME' => /** @scrutinizer ignore-call */ l10n('Piwigo to MediaWiki'),
Loading history...
56
      'URL' => PIWIGO2MEDIAWIKI_ADMIN,
57
    );
58
    return $menu;
59
  });
60
61
  // Add the send-to-MediaWiki global action.
62
  add_event_handler('loc_end_element_set_global',
63
    function () {
64
      global $template, $conf;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
65
      $content = $template->assign( PIWIGO2MEDIAWIKI_DIR.'action.tpl' );
66
      $template->append('element_set_global_plugins_actions',
67
        array(
68
          'ID' => PIWIGO2MEDIAWIKI_ID,
69
          'NAME' => l10n('Copy to MediaWiki'), 'CONTENT' => $content,
0 ignored issues
show
The function l10n was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
          'NAME' => /** @scrutinizer ignore-call */ l10n('Copy to MediaWiki'), 'CONTENT' => $content,
Loading history...
70
        )
71
      );
72
    }
73
  );
74
75
}
76