ApiMergeHistory::getHelpUrls()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 *
5
 * Created on Dec 29, 2015
6
 *
7
 * Copyright © 2015 Geoffrey Mon <[email protected]>
8
 *
9
 * This program is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 2 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License along
20
 * with this program; if not, write to the Free Software Foundation, Inc.,
21
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22
 * http://www.gnu.org/copyleft/gpl.html
23
 *
24
 * @file
25
 */
26
27
/**
28
 * API Module to merge page histories
29
 * @ingroup API
30
 */
31
class ApiMergeHistory extends ApiBase {
32
33
	public function execute() {
34
		$this->useTransactionalTimeLimit();
35
36
		$params = $this->extractRequestParams();
37
38
		$this->requireOnlyOneParameter( $params, 'from', 'fromid' );
39
		$this->requireOnlyOneParameter( $params, 'to', 'toid' );
40
41
		// Get page objects (nonexistant pages get caught in MergeHistory::isValidMerge())
42 View Code Duplication
		if ( isset( $params['from'] ) ) {
43
			$fromTitle = Title::newFromText( $params['from'] );
44
			if ( !$fromTitle || $fromTitle->isExternal() ) {
45
				$this->dieUsageMsg( [ 'invalidtitle', $params['from'] ] );
46
			}
47
		} elseif ( isset( $params['fromid'] ) ) {
48
			$fromTitle = Title::newFromID( $params['fromid'] );
49
			if ( !$fromTitle ) {
50
				$this->dieUsageMsg( [ 'nosuchpageid', $params['fromid'] ] );
51
			}
52
		}
53
54 View Code Duplication
		if ( isset( $params['to'] ) ) {
55
			$toTitle = Title::newFromText( $params['to'] );
56
			if ( !$toTitle || $toTitle->isExternal() ) {
57
				$this->dieUsageMsg( [ 'invalidtitle', $params['to'] ] );
58
			}
59
		} elseif ( isset( $params['toid'] ) ) {
60
			$toTitle = Title::newFromID( $params['toid'] );
61
			if ( !$toTitle ) {
62
				$this->dieUsageMsg( [ 'nosuchpageid', $params['toid'] ] );
63
			}
64
		}
65
66
		$reason = $params['reason'];
67
		$timestamp = $params['timestamp'];
68
69
		// Merge!
70
		$status = $this->merge( $fromTitle, $toTitle, $timestamp, $reason );
0 ignored issues
show
Bug introduced by
The variable $fromTitle 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...
Bug introduced by
The variable $toTitle 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...
Bug introduced by
It seems like $fromTitle can be null; however, merge() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
Bug introduced by
It seems like $toTitle can be null; however, merge() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
71
		if ( !$status->isOK() ) {
72
			$this->dieStatus( $status );
73
		}
74
75
		$r = [
76
			'from' => $fromTitle->getPrefixedText(),
77
			'to' => $toTitle->getPrefixedText(),
78
			'timestamp' => wfTimestamp( TS_ISO_8601, $params['timestamp'] ),
79
			'reason' => $params['reason']
80
		];
81
		$result = $this->getResult();
82
83
		$result->addValue( null, $this->getModuleName(), $r );
84
	}
85
86
	/**
87
	 * @param Title $from
88
	 * @param Title $to
89
	 * @param string $timestamp
90
	 * @param string $reason
91
	 * @return Status
92
	 */
93
	protected function merge( Title $from, Title $to, $timestamp, $reason ) {
94
		$mh = new MergeHistory( $from, $to, $timestamp );
95
96
		return $mh->merge( $this->getUser(), $reason );
97
	}
98
99
	public function mustBePosted() {
100
		return true;
101
	}
102
103
	public function isWriteMode() {
104
		return true;
105
	}
106
107 View Code Duplication
	public function getAllowedParams() {
108
		return [
109
			'from' => null,
110
			'fromid' => [
111
				ApiBase::PARAM_TYPE => 'integer'
112
			],
113
			'to' => null,
114
			'toid' => [
115
				ApiBase::PARAM_TYPE => 'integer'
116
			],
117
			'timestamp' => [
118
				ApiBase::PARAM_TYPE => 'timestamp'
119
			],
120
			'reason' => '',
121
		];
122
	}
123
124
	public function needsToken() {
125
		return 'csrf';
126
	}
127
128
	protected function getExamplesMessages() {
129
		return [
130
			'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
131
			'reason=Reason'
132
			=> 'apihelp-mergehistory-example-merge',
133
			'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
134
			'reason=Reason&timestamp=2015-12-31T04%3A37%3A41Z' // TODO
135
			=> 'apihelp-mergehistory-example-merge-timestamp',
136
		];
137
	}
138
139
	public function getHelpUrls() {
140
		return 'https://www.mediawiki.org/wiki/API:Mergehistory';
141
	}
142
}
143