summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/AbuseFilter/includes/AbuseFilterVariableHolder.php
blob: dd6f0be6c2468441604521dad8a4fa547be25abb (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php

class AbuseFilterVariableHolder {
	public $mVars = [];

	public static $varBlacklist = [ 'context' ];

	public function __construct() {
		// Backwards-compatibility (unused now)
		$this->setVar( 'minor_edit', false );
	}

	/**
	 * @param string $variable
	 * @param mixed $datum
	 */
	function setVar( $variable, $datum ) {
		$variable = strtolower( $variable );
		if ( !( $datum instanceof AFPData || $datum instanceof AFComputedVariable ) ) {
			$datum = AFPData::newFromPHPVar( $datum );
		}

		$this->mVars[$variable] = $datum;
	}

	/**
	 * @param string $variable
	 * @param string $method
	 * @param array $parameters
	 */
	function setLazyLoadVar( $variable, $method, $parameters ) {
		$placeholder = new AFComputedVariable( $method, $parameters );
		$this->setVar( $variable, $placeholder );
	}

	/**
	 * Get a variable from the current object
	 *
	 * @param string $variable
	 * @return AFPData
	 */
	function getVar( $variable ) {
		$variable = strtolower( $variable );
		if ( isset( $this->mVars[$variable] ) ) {
			if ( $this->mVars[$variable] instanceof AFComputedVariable ) {
				$value = $this->mVars[$variable]->compute( $this );
				$this->setVar( $variable, $value );
				return $value;
			} elseif ( $this->mVars[$variable] instanceof AFPData ) {
				return $this->mVars[$variable];
			}
		}
		return new AFPData();
	}

	/**
	 * @return AbuseFilterVariableHolder
	 */
	public static function merge() {
		$newHolder = new AbuseFilterVariableHolder;
		call_user_func_array( [ $newHolder, "addHolders" ], func_get_args() );

		return $newHolder;
	}

	/**
	 * @param self $addHolder
	 * @throws MWException
	 * @deprecated use addHolders() instead
	 */
	public function addHolder( $addHolder ) {
		$this->addHolders( $addHolder );
	}

	/**
	 * Merge any number of holders given as arguments into this holder.
	 *
	 * @throws MWException
	 */
	public function addHolders() {
		$holders = func_get_args();

		foreach ( $holders as $addHolder ) {
			if ( !is_object( $addHolder ) ) {
				throw new MWException( 'Invalid argument to AbuseFilterVariableHolder::addHolders' );
			}
			$this->mVars = array_merge( $this->mVars, $addHolder->mVars );
		}
	}

	function __wakeup() {
		// Reset the context.
		$this->setVar( 'context', 'stored' );
	}

	/**
	 * Export all variables stored in this object as string
	 *
	 * @return string[]
	 */
	function exportAllVars() {
		$exported = [];
		foreach ( array_keys( $this->mVars ) as $varName ) {
			if ( !in_array( $varName, self::$varBlacklist ) ) {
				$exported[$varName] = $this->getVar( $varName )->toString();
			}
		}

		return $exported;
	}

	/**
	 * Export all non-lazy variables stored in this object as string
	 *
	 * @return string[]
	 */
	function exportNonLazyVars() {
		$exported = [];
		foreach ( $this->mVars as $varName => $data ) {
			if (
				!( $data instanceof AFComputedVariable )
				&& !in_array( $varName, self::$varBlacklist )
			) {
				$exported[$varName] = $this->getVar( $varName )->toString();
			}
		}

		return $exported;
	}

	/**
	 * Dump all variables stored in this object in their native types.
	 * If you want a not yet set variable to be included in the results you can
	 * either set $compute to an array with the name of the variable or set
	 * $compute to true to compute all not yet set variables.
	 *
	 * @param array|bool $compute Variables we should copute if not yet set
	 * @param bool $includeUserVars Include user set variables
	 * @return array
	 */
	public function dumpAllVars( $compute = [], $includeUserVars = false ) {
		$allVarNames = array_keys( $this->mVars );
		$exported = [];
		$coreVariables = [];

		if ( !$includeUserVars ) {
			// Compile a list of all variables set by the extension to be able
			// to filter user set ones by name
			global $wgRestrictionTypes;

			$coreVariables = AbuseFilter::getBuilderValues();
			$coreVariables = array_keys( $coreVariables['vars'] );

			// Title vars can have several prefixes
			$prefixes = [ 'ARTICLE', 'MOVED_FROM', 'MOVED_TO' ];
			$titleVars = [
				'_ARTICLEID',
				'_NAMESPACE',
				'_TEXT',
				'_PREFIXEDTEXT',
				'_recent_contributors'
			];
			foreach ( $wgRestrictionTypes as $action ) {
				$titleVars[] = "_restrictions_$action";
			}

			foreach ( $titleVars as $var ) {
				foreach ( $prefixes as $prefix ) {
					$coreVariables[] = $prefix . $var;
				}
			}
			$coreVariables = array_map( 'strtolower', $coreVariables );
		}

		foreach ( $allVarNames as $varName ) {
			if (
				( $includeUserVars || in_array( strtolower( $varName ), $coreVariables ) ) &&
				// Only include variables set in the extension in case $includeUserVars is false
				!in_array( $varName, self::$varBlacklist ) &&
				( $compute === true ||
					( is_array( $compute ) && in_array( $varName, $compute ) ) ||
					$this->mVars[$varName] instanceof AFPData
				)
			) {
				$exported[$varName] = $this->getVar( $varName )->toNative();
			}
		}

		return $exported;
	}

	/**
	 * @param string $var
	 * @return bool
	 */
	function varIsSet( $var ) {
		return array_key_exists( $var, $this->mVars );
	}

	/**
	 * Compute all vars which need DB access. Useful for vars which are going to be saved
	 * cross-wiki or used for offline analysis.
	 */
	function computeDBVars() {
		static $dbTypes = [
			'links-from-wikitext-or-database',
			'load-recent-authors',
			'get-page-restrictions',
			'simple-user-accessor',
			'user-age',
			'user-groups',
			'user-rights',
			'revision-text-by-id',
			'revision-text-by-timestamp'
		];

		foreach ( $this->mVars as $name => $value ) {
			if ( $value instanceof AFComputedVariable &&
				in_array( $value->mMethod, $dbTypes )
			) {
				$value = $value->compute( $this );
				$this->setVar( $name, $value );
			}
		}
	}
}