summaryrefslogtreecommitdiff
path: root/extensions/Nuke/Nuke_body.php
blob: d493bb8b563913381791b999764fe205fef77377 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php

if( !defined( 'MEDIAWIKI' ) )
	die( 'Not an entry point.' );

class SpecialNuke extends SpecialPage {
	function __construct() {
		wfLoadExtensionMessages( 'Nuke' );
		parent::__construct( 'Nuke', 'nuke' );
	}

	function execute( $par ){
		global $wgUser, $wgRequest;

		if( !$this->userCanExecute( $wgUser ) ){
			$this->displayRestrictionError();
			return;
		}

		$this->setHeaders();
		$this->outputHeader();

		$target = $wgRequest->getText( 'target', $par );

		// Normalise name
		if ( $target !== '' ) {
			$user = User::newFromName( $target );
			if ( $user ) $target = $user->getName();
		}

		$reason = $wgRequest->getText( 'wpReason',
			wfMsgForContent( 'nuke-defaultreason', $target ) );
		$posted = $wgRequest->wasPosted() &&
			$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) );
		if( $posted ) {
			$pages = $wgRequest->getArray( 'pages' );
			if( $pages ) {
				return $this->doDelete( $pages, $reason );
			}
		}
		if( $target != '' ) {
			$this->listForm( $target, $reason );
		} else {
			$this->promptForm();
		}
	}

	function promptForm() {
		global $wgOut;

		$input = Xml::input( 'target', 40 );
		$submit = Xml::submitButton( wfMsg( 'nuke-submit-user' ) );

		$wgOut->addWikiMsg( 'nuke-tools' );
		$wgOut->addHTML(
			Xml::openElement( 'form', array(
				'action' => $this->getTitle()->getLocalURL( 'action=submit' ),
				'method' => 'post' )
			) . "$input\n$submit\n"
		);

		$wgOut->addHTML( "</form>" );
	}

	function listForm( $username, $reason ) {
		global $wgUser, $wgOut, $wgLang;

		$pages = $this->getNewPages( $username );

		if( count( $pages ) == 0 ) {
			$wgOut->addWikiMsg( 'nuke-nopages', $username );
			return $this->promptForm();
		}
		$wgOut->addWikiMsg( 'nuke-list', $username );

		$nuke = $this->getTitle();
		$submit = Xml::submitButton( wfMsg( 'nuke-submit-delete' ) );

		$wgOut->addHTML(
			Xml::openElement( 'form', array(
				'action' => $nuke->getLocalURL( 'action=delete' ),
				'method' => 'post' )
			) .
			Xml::hidden( 'wpEditToken', $wgUser->editToken() ) .
			Xml::inputLabel(
				wfMsg( 'deletecomment' ), 'wpReason', 'wpReason', 60, $reason
			) . '<br /><br />' .
			Xml::submitButton( wfMsg( 'nuke-submit-delete' ) )
		);

		$wgOut->addHTML( '<ul>' );

		$sk = $wgUser->getSkin();
		foreach( $pages as $info ) {
			list( $title, $edits ) = $info;
			$image = $title->getNamespace() == NS_IMAGE ? wfLocalFile( $title ) : false;
			$thumb = $image && $image->exists() ? $image->getThumbnail( 120, 120 ) : false;

			$changes = wfMsgExt( 'nchanges', 'parsemag', $wgLang->formatNum( $edits ) );
			
			$wgOut->addHTML( '<li>' .
				Xml::check( 'pages[]', true,
					array( 'value' =>  $title->getPrefixedDbKey() )
				) .
				'&#160;' .
				( $thumb ? $thumb->toHtml( array( 'desc-link' => true ) ) : '' ) .
				$sk->makeKnownLinkObj( $title ) .
				'&#160;(' .
				$sk->makeKnownLinkObj( $title, $changes, 'action=history' ) .
				")</li>\n" );
		}
		$wgOut->addHTML(
			"</ul>\n" .
			Xml::submitButton( wfMsg( 'nuke-submit-delete' ) ) .
			"</form>"
		);
	}

	function getNewPages( $username ) {
		$dbr = wfGetDB( DB_SLAVE );
		$result = $dbr->select( 'recentchanges',
			array( 'rc_namespace', 'rc_title', 'rc_timestamp', 'COUNT(*) AS edits' ),
			array(
				'rc_user_text' => $username,
				"(rc_new = 1) OR (rc_log_type = 'import' AND rc_log_action = 'upload')"
			),
			__METHOD__,
			array(
				'ORDER BY' => 'rc_timestamp DESC',
				'GROUP BY' => 'rc_namespace, rc_title'
			)
		);
		$pages = array();
		while( $row = $dbr->fetchObject( $result ) ) {
			$pages[] = array( Title::makeTitle( $row->rc_namespace, $row->rc_title ), $row->edits );
		}
		$dbr->freeResult( $result );
		return $pages;
	}

	function doDelete( $pages, $reason ) {
		foreach( $pages as $page ) {
			$title = Title::newFromURL( $page );
			$file = $title->getNamespace() == NS_IMAGE ? wfLocalFile( $title ) : false;
			if ( $file ) {
				$oldimage = null; // Must be passed by reference
				FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, false );								
			} else {
				$article = new Article( $title );
				$article->doDelete( $reason );
			}
		}
	}
}