Completed
Push — master ( 2ed9e0...86f4fe )
by Toni Hermoso
38:59 queued 18:56
created

RebuildJSONData   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 111
rs 10
c 0
b 0
f 0
wmc 16
lcom 0
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A addDefaultParams() 0 5 1
B execute() 0 53 9
A refreshArticle() 0 29 5
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 12 and the first side effect is on line 3.

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
$basePath = getenv( 'MW_INSTALL_PATH' ) !== false ? getenv( 'MW_INSTALL_PATH' ) : __DIR__ . '/../../..';
4
require_once $basePath . '/maintenance/Maintenance.php';
5
6
7
/**
8
 * Class for handling the rebuilding process of JSON namespaces
9
 * @author Toni Hermoso
10
 */
11
12
class RebuildJSONData extends Maintenance {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
13
		public function __construct() {
14
		parent::__construct();
15
		$this->addDescription( "\n" .
16
			"Script for rebuilding data stored in JSON stores\n"
17
		);
18
		$this->addDefaultParams();
19
	}
20
21
	/**
22
	 * @see Maintenance::addDefaultParams
23
	 */
24
	protected function addDefaultParams() {
25
		$this->addOption( 'namespace', '<namespace> Namespace index number to be refreshed.', true, true, "ns" );
26
		$this->addOption( 'dryrun', '<dryRun> If you don\'t really want to refresh information', false, false, "dr" );
27
		$this->addOption( 'u', 'User to run the script', false, true );
28
	}
29
	
30
	/**
31
	 * @see Maintenance::execute
32
	 */
33
	public function execute() {
0 ignored issues
show
Coding Style introduced by
execute 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...
34
		
35
		if ( !defined( 'SMW_VERSION' ) || !$GLOBALS['smwgSemanticsEnabled'] ) {
36
			
37
			$this->reportMessage( "\nYou need to have SMW enabled in order to run this maintenance script!\n" );
38
			return false;
39
		}
40
		
41
		$ns = $this->getOption( "namespace", null ); 
42
		$dryRun = $this->getOption( "dryrun", false); 
43
		$u = $this->getOption( 'u', false );
44
45
		$reportingInterval = 100;
46
		$dbr = wfGetDB( DB_SLAVE );
47
		$ns_restrict = "page_namespace > -1";
48
		$tables = array('page');
49
		
50
		$seltables = array( 'page_id' );
51
		// Need to do for NS
52
		if ( $ns > -1 ) {
53
			if ( is_numeric( $ns ) ) {
54
				$ns_restrict = "page_namespace = $ns";
55
			}
56
		}
57
58
		// Default, no user
59
		$user = null;
60
		if ( $u ) {
61
			// $user = User::newSystemUser("SDImport");
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
62
			$user = User::newFromName( $u );
63
		}
64
		
65
66
		$res = $dbr->select( $tables,
67
			$seltables,
68
			array(
69
				$ns_restrict ),
70
			__METHOD__
71
		);
72
		$num = $dbr->numRows( $res );
73
		$this->output( "$num articles...\n" );
74
		$i = 0;
75
		foreach ( $res as $row ) {
76
			if ( !( ++$i % $reportingInterval ) ) {
77
				$this->output( "$i\n" );
78
				wfWaitForSlaves(); // Doubt if necessary
79
			}
80
			if ( ! $dryRun ) {
81
				self::refreshArticle( $row->page_id, $user );
82
			}
83
		}
84
85
	}
86
	
87
	
88
	/**
89
	 * Run fixEditFromArticle for all links on a given page_id (and a user)
90
	 * @param $id int The page_id
91
	 */
92
	public static function refreshArticle( $pageid, $user ) {
93
94
		$wikipage = WikiPage::newFromID( $pageid );
95
	
96
		if ( $wikipage === null ) {
97
			return;
98
		}
99
100
		// Check compatibility. Only if newer versions of MW
101
		if ( method_exists ( $wikipage, "getContent" ) ) {
102
			$contentModel = $wikipage->getContentModel();
103
			if ( $contentModel === "json" || ! $wikipage->exists() ) {
104
105
				// Retrigger import
106
				$statusValue = new StatusValue();
107
				$statusValue->setOK(true);
108
				$status = new Status();
109
				$status->wrap( $statusValue );
110
	
111
                // TODO: To be fixed	
112
				SDImportData::saveJSONData( $wikipage, $user, $wikipage->getContent(), "Rebuild JSON", 0, null, null, 2, $wikipage->getRevision(), $status, false );
0 ignored issues
show
Documentation introduced by
0 is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
null is of type null, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
false is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
113
				// $status = $wikipage->doEditContent( $wikipage, "Rebuild", EDIT_FORCE_BOT, false, $user );
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
114
				// var_dump( $status );
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
115
				// SDImportData::importJSON( $wikipage->getContent()->getNativeData(), $wikipage->getTitle()->getPrefixedText(), true );
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
116
117
			}
118
		}
119
		
120
	}
121
	
122
}
123
124
125
$maintClass = 'RebuildJSONData';
126
require_once( DO_MAINTENANCE );
127