RebuildJSONData::execute()   B
last analyzed

Complexity

Conditions 9
Paths 31

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 31
nop 0
dl 0
loc 53
rs 7.4698
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
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 {
13
	
14
	public function __construct() {
15
		parent::__construct();
16
		$this->addDescription( "\n" .
17
			"Script for rebuilding data stored in JSON stores\n"
18
		);
19
		$this->addDefaultParams();
20
	}
21
22
	/**
23
	 * @see Maintenance::addDefaultParams
24
	 */
25
	protected function addDefaultParams() {
26
		$this->addOption( 'namespace', '<namespace> Namespace index number to be refreshed.', true, true, "ns" );
27
		$this->addOption( 'dryrun', '<dryRun> If you don\'t really want to refresh information', false, false, "dr" );
28
		$this->addOption( 'u', 'User to run the script', false, true );
29
	}
30
	
31
	/**
32
	 * @see Maintenance::execute
33
	 */
34
	public function execute() {
35
		
36
		if ( !defined( 'SMW_VERSION' ) || !$GLOBALS['smwgSemanticsEnabled'] ) {
37
			
38
			$this->reportMessage( "\nYou need to have SMW enabled in order to run this maintenance script!\n" );
39
			return false;
40
		}
41
		
42
		$ns = $this->getOption( "namespace", null ); 
43
		$dryRun = $this->getOption( "dryrun", false); 
44
		$u = $this->getOption( 'u', false );
45
46
		$reportingInterval = 100;
47
		$dbr = wfGetDB( DB_SLAVE );
48
		$ns_restrict = "page_namespace > -1";
49
		$tables = array('page');
50
		
51
		$seltables = array( 'page_id' );
52
		// Need to do for NS
53
		if ( $ns > -1 ) {
54
			if ( is_numeric( $ns ) ) {
55
				$ns_restrict = "page_namespace = $ns";
56
			}
57
		}
58
59
		// Default, no user
60
		$user = null;
61
		if ( $u ) {
62
			// $user = User::newSystemUser("SDImport");
63
			$user = User::newFromName( $u );
64
		}
65
		
66
67
		$res = $dbr->select( $tables,
68
			$seltables,
69
			array(
70
				$ns_restrict ),
71
			__METHOD__
72
		);
73
		$num = $dbr->numRows( $res );
74
		$this->output( "$num articles...\n" );
75
		$i = 0;
76
		foreach ( $res as $row ) {
77
			if ( !( ++$i % $reportingInterval ) ) {
78
				$this->output( "$i\n" );
79
				wfWaitForSlaves(); // Doubt if necessary
80
			}
81
			if ( ! $dryRun ) {
82
				self::refreshArticle( $row->page_id, $user );
83
			}
84
		}
85
86
	}
87
	
88
	
89
	/**
90
	 * Run fixEditFromArticle for all links on a given page_id (and a user)
91
	 * @param $id int The page_id
92
	 */
93
	public static function refreshArticle( $pageid, $user ) {
94
95
		$wikipage = WikiPage::newFromID( $pageid );
96
	
97
		if ( $wikipage === null ) {
98
			return;
99
		}
100
101
		// Check compatibility. Only if newer versions of MW
102
		if ( method_exists ( $wikipage, "getContent" ) ) {
103
			$contentModel = $wikipage->getContentModel();
104
			if ( $contentModel === "json" || ! $wikipage->exists() ) {
105
106
				// Retrigger import
107
				$statusValue = new StatusValue();
108
				$statusValue->setOK(true);
109
				$status = new Status();
110
				$status->wrap( $statusValue );
111
112
                // TODO: To be fixed	
113
				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...
114
				// $status = $wikipage->doEditContent( $wikipage->getContent(), "Rebuild", EDIT_FORCE_BOT, false, $user );
115
				// $status = SDImportData::importJSON( $wikipage->getContent()->getNativeData(), $wikipage->getTitle()->getPrefixedText(), true );
116
			}
117
		}
118
		
119
	}
120
	
121
}
122
123
124
$maintClass = 'RebuildJSONData';
125
require_once( DO_MAINTENANCE );
126