DeleteDefaultMessages   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B execute() 0 46 4
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 33 and the first side effect is on line 25.

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
 * Deletes all pages in the MediaWiki namespace which were last edited by
4
 * "MediaWiki default".
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along
17
 * with this program; if not, write to the Free Software Foundation, Inc.,
18
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 * http://www.gnu.org/copyleft/gpl.html
20
 *
21
 * @file
22
 * @ingroup Maintenance
23
 */
24
25
require_once __DIR__ . '/Maintenance.php';
26
27
/**
28
 * Maintenance script that deletes all pages in the MediaWiki namespace
29
 * which were last edited by "MediaWiki default".
30
 *
31
 * @ingroup Maintenance
32
 */
33
class DeleteDefaultMessages extends Maintenance {
34
	public function __construct() {
35
		parent::__construct();
36
		$this->addDescription( 'Deletes all pages in the MediaWiki namespace' .
37
			' which were last edited by "MediaWiki default"' );
38
	}
39
40
	public function execute() {
41
		global $wgUser;
42
43
		$this->output( "Checking existence of old default messages..." );
44
		$dbr = $this->getDB( DB_REPLICA );
45
		$res = $dbr->select( [ 'page', 'revision' ],
46
			[ 'page_namespace', 'page_title' ],
47
			[
48
				'page_namespace' => NS_MEDIAWIKI,
49
				'page_latest=rev_id',
50
				'rev_user_text' => 'MediaWiki default',
51
			]
52
		);
53
54
		if ( $dbr->numRows( $res ) == 0 ) {
55
			# No more messages left
56
			$this->output( "done.\n" );
57
58
			return;
59
		}
60
61
		# Deletions will be made by $user temporarly added to the bot group
62
		# in order to hide it in RecentChanges.
63
		$user = User::newFromName( 'MediaWiki default' );
64
		if ( !$user ) {
65
			$this->error( "Invalid username", true );
66
		}
67
		$user->addGroup( 'bot' );
68
		$wgUser = $user;
69
70
		# Handle deletion
71
		$this->output( "\n...deleting old default messages (this may take a long time!)...", 'msg' );
72
		$dbw = $this->getDB( DB_MASTER );
73
74
		foreach ( $res as $row ) {
75
			wfWaitForSlaves();
0 ignored issues
show
Deprecated Code introduced by
The function wfWaitForSlaves() has been deprecated with message: since 1.27 Use LBFactory::waitForReplication

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
76
			$dbw->ping();
77
			$title = Title::makeTitle( $row->page_namespace, $row->page_title );
78
			$page = WikiPage::factory( $title );
79
			$error = ''; // Passed by ref
80
			// FIXME: Deletion failures should be reported, not silently ignored.
81
			$page->doDeleteArticle( 'No longer required', false, 0, true, $error, $user );
0 ignored issues
show
Security Bug introduced by
It seems like $user defined by \User::newFromName('MediaWiki default') on line 63 can also be of type false; however, WikiPage::doDeleteArticle() does only seem to accept null|object<User>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
82
		}
83
84
		$this->output( "done!\n", 'msg' );
85
	}
86
}
87
88
$maintClass = "DeleteDefaultMessages";
89
require_once RUN_MAINTENANCE_IF_MAIN;
90