Completed
Push — master ( af61ba...20f2b6 )
by Toni Hermoso
20:25
created

SDImportJob::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Background job to import a page into the wiki, adapted from Data Transfer
5
 *
6
 * @author Yaron Koren
7
 * @author Toni Hermoso
8
 */
9
class SDImportJob extends Job {
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...
10
11
   function __construct( $title, $params = '', $id = 0 ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
12
       parent::__construct( 'dtImport', $title, $params, $id );
13
   }
14
15
   /**
16
    * Run a dtImport job
17
    * @return boolean success
18
    */
19
   function run() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
20
       wfProfileIn( __METHOD__ );
21
22
       if ( is_null( $this->title ) ) {
23
           $this->error = "dtImport: Invalid title";
24
           wfProfileOut( __METHOD__ );
25
           return false;
26
       }
27
28
       if ( method_exists( 'WikiPage', 'getContent' ) ) {
29
           $wikiPage = new WikiPage( $this->title );
30
           if ( !$wikiPage ) {
31
               $this->error = 'dtImport: Wiki page not found "' . $this->title->getPrefixedDBkey() . '"';
32
               wfProfileOut( __METHOD__ );
33
               return false;
34
           }
35
       } else {
36
         // Remove old support
37
         return false;
38
       }
39
       $for_pages_that_exist = $this->params['for_pages_that_exist'];
40
       if ( $for_pages_that_exist == 'skip' && $this->title->exists() ) {
41
           return true;
42
       }
43
44
       // Change global $wgUser variable to the one specified by
45
       // the job only for the extent of this import.
46
       global $wgUser;
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...
47
       $actual_user = $wgUser;
48
       $wgUser = User::newFromId( $this->params['user_id'] );
49
       $text = $this->params['text'];
50
       $edit_summary = $this->params['edit_summary'];
51
52
       // Act according to storage here
53
       if ( $this->params['storage'] == "json" ) {
54
          // TODO: to evaluate if check destination page here or before
55
56
          // Handle JSON new content
57
    			$contentModel = $wikipage->getContentModel();
0 ignored issues
show
Bug introduced by
The variable $wikipage does not exist. Did you mean $wikiPage?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
58
    			if ( $contentModel === "json" || ! $wikipage->exists() ) {
0 ignored issues
show
Bug introduced by
The variable $wikipage does not exist. Did you mean $wikiPage?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
59
    				$new_content = new JSONContent( $text );
60
    			}
61
62
       } else {
63
64
         if ( $this->title->exists() ) {
65
             if ( $for_pages_that_exist == 'append' ) {
66
                     // MW >= 1.21
67
               $existingText = $wikiPage->getContent()->getNativeData();
68
               $text = $existingText . "\n" . $text;
69
70
             }
71
         }
72
73
         $new_content = new WikitextContent( $text );
74
75
       }
76
77
       $wikiPage->doEditContent( $new_content, $edit_summary );
0 ignored issues
show
Bug introduced by
The variable $new_content does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
78
79
       $wgUser = $actual_user;
80
       wfProfileOut( __METHOD__ );
81
       return true;
82
   }
83
}
84