Completed
Branch master (939199)
by
unknown
39:35
created

includes/export/DumpNamespaceFilter.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Dump output filter to include or exclude pages in a given set of namespaces.
4
 *
5
 * Copyright © 2003, 2005, 2006 Brion Vibber <[email protected]>
6
 * https://www.mediawiki.org/
7
 *
8
 * This program is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 2 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License along
19
 * with this program; if not, write to the Free Software Foundation, Inc.,
20
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
 * http://www.gnu.org/copyleft/gpl.html
22
 *
23
 * @file
24
 */
25
26
/**
27
 * @ingroup Dump
28
 */
29
class DumpNamespaceFilter extends DumpFilter {
30
	/** @var bool */
31
	public $invert = false;
32
33
	/** @var array */
34
	public $namespaces = [];
35
36
	/**
37
	 * @param DumpOutput $sink
38
	 * @param array $param
39
	 * @throws MWException
40
	 */
41
	function __construct( &$sink, $param ) {
42
		parent::__construct( $sink );
43
44
		$constants = [
45
			"NS_MAIN"           => NS_MAIN,
46
			"NS_TALK"           => NS_TALK,
47
			"NS_USER"           => NS_USER,
48
			"NS_USER_TALK"      => NS_USER_TALK,
49
			"NS_PROJECT"        => NS_PROJECT,
50
			"NS_PROJECT_TALK"   => NS_PROJECT_TALK,
51
			"NS_FILE"           => NS_FILE,
52
			"NS_FILE_TALK"      => NS_FILE_TALK,
53
			"NS_IMAGE"          => NS_IMAGE, // NS_IMAGE is an alias for NS_FILE
0 ignored issues
show
Deprecated Code introduced by
The constant NS_IMAGE has been deprecated with message: since 1.14

This class constant 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 constant will be removed from the class and what other constant to use instead.

Loading history...
54
			"NS_IMAGE_TALK"     => NS_IMAGE_TALK,
0 ignored issues
show
Deprecated Code introduced by
The constant NS_IMAGE_TALK has been deprecated with message: since 1.14

This class constant 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 constant will be removed from the class and what other constant to use instead.

Loading history...
55
			"NS_MEDIAWIKI"      => NS_MEDIAWIKI,
56
			"NS_MEDIAWIKI_TALK" => NS_MEDIAWIKI_TALK,
57
			"NS_TEMPLATE"       => NS_TEMPLATE,
58
			"NS_TEMPLATE_TALK"  => NS_TEMPLATE_TALK,
59
			"NS_HELP"           => NS_HELP,
60
			"NS_HELP_TALK"      => NS_HELP_TALK,
61
			"NS_CATEGORY"       => NS_CATEGORY,
62
			"NS_CATEGORY_TALK"  => NS_CATEGORY_TALK ];
63
64
		if ( $param { 0 } == '!' ) {
65
			$this->invert = true;
66
			$param = substr( $param, 1 );
67
		}
68
69
		foreach ( explode( ',', $param ) as $key ) {
70
			$key = trim( $key );
71
			if ( isset( $constants[$key] ) ) {
72
				$ns = $constants[$key];
73
				$this->namespaces[$ns] = true;
74
			} elseif ( is_numeric( $key ) ) {
75
				$ns = intval( $key );
76
				$this->namespaces[$ns] = true;
77
			} else {
78
				throw new MWException( "Unrecognized namespace key '$key'\n" );
79
			}
80
		}
81
	}
82
83
	/**
84
	 * @param object $page
85
	 * @return bool
86
	 */
87
	function pass( $page ) {
88
		$match = isset( $this->namespaces[$page->page_namespace] );
89
		return $this->invert xor $match;
90
	}
91
}
92