Blame view

libraries/joomla/mediawiki/categories.php 9.3 KB
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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
<?php
/**
 * @package     Joomla.Platform
 * @subpackage  MediaWiki
 *
 * @copyright   Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE
 */

defined('JPATH_PLATFORM') or die;

/**
 * MediaWiki API Categories class for the Joomla Platform.
 *
 * @since  3.1.4
 */
class JMediawikiCategories extends JMediawikiObject
{
	/**
	 * Method to list all categories the page(s) belong to.
	 *
	 * @param   array    $titles        Page titles to retrieve categories.
	 * @param   array    $clprop        List of additional properties to get.
	 * @param   array    $clshow        Type of categories to show.
	 * @param   integer  $cllimit       Number of categories to return.
	 * @param   boolean  $clcontinue    Continue when more results are available.
	 * @param   array    $clcategories  Only list these categories.
	 * @param   string   $cldir         Direction of listing.
	 *
	 * @return  object
	 *
	 * @since   3.0.0
	 */
	public function getCategories(array $titles, array $clprop = null, array $clshow = null, $cllimit = null, $clcontinue = false,
		array $clcategories = null, $cldir = null)
	{
		// Build the request.
		$path = '?action=query&prop=categories';

		// Append titles to the request.
		$path .= '&titles=' . $this->buildParameter($titles);

		if (isset($clprop))
		{
			$path .= '&clprop=' . $this->buildParameter($clprop);
		}

		if (isset($clshow))
		{
			$path .= '&$clshow=' . $this->buildParameter($clshow);
		}

		if (isset($cllimit))
		{
			$path .= '&cllimit=' . $cllimit;
		}

		if ($clcontinue)
		{
			$path .= '&clcontinue=';
		}

		if (isset($clcategories))
		{
			$path .= '&clcategories=' . $this->buildParameter($clcategories);
		}

		if (isset($cldir))
		{
			$path .= '&cldir=' . $cldir;
		}

		// Send the request.
		$response = $this->client->get($this->fetchUrl($path));

		return $this->validateResponse($response);
	}

	/**
	 * Method to get information about all categories used.
	 *
	 * @param   array  $titles  Page titles to retrieve categories.
	 *
	 * @return  object
	 *
	 * @since   3.1.4
	 */
	public function getCategoriesUsed(array $titles)
	{
		// Build the request
		$path = '?action=query&generator=categories&prop=info';

		// Append titles to the request
		$path .= '&titles=' . $this->buildParameter($titles);

		// Send the request.
		$response = $this->client->get($this->fetchUrl($path));

		return $this->validateResponse($response);
	}

	/**
	 * Method to get information about the given categories.
	 *
	 * @param   array    $titles      Page titles to retrieve categories.
	 * @param   boolean  $clcontinue  Continue when more results are available.
	 *
	 * @return  object
	 *
	 * @since   3.1.4
	 */
	public function getCategoriesInfo(array $titles, $clcontinue = false)
	{
		// Build the request.
		$path = '?action=query&prop=categoryinfo';

		// Append titles to the request
		$path .= '&titles=' . $this->buildParameter($titles);

		if ($clcontinue)
		{
			$path .= '&clcontinue=';
		}

		// Send the request.
		$response = $this->client->get($this->fetchUrl($path));

		return $this->validateResponse($response);
	}

	/**
	 * Method to get information about the pages within a category
	 *
	 * @param   string  $cmtitle               The category title, must contain 'Category:' prefix, cannot be used together with $cmpageid
	 * @param   string  $cmpageid              The category's page ID, cannot be used together with $cmtitle
	 * @param   string  $cmlimit               Maximum number of pages to retrieve
	 * @param   array   $cmprop                Array of properties to retrieve
	 * @param   array   $cmnamespace           Namespaces to retrieve pages from
	 * @param   array   $cmtype                Array of category members to include, ignored if $cmsort is set to 'timestamp'
	 * @param   string  $cmstart               Timestamp to start listing from, only used if $cmsort is set to 'timestamp'
	 * @param   string  $cmend                 Timestamp to end listing at, only used if $cmsort is set to 'timestamp'
	 * @param   string  $cmstartsortkey        Hexadecimal key to start listing from, only used if $cmsort is set to 'sortkey'
	 * @param   string  $cmendsortkey          Hexadecimal key to end listing at, only used if $cmsort is set to 'sortkey'
	 * @param   string  $cmstartsortkeyprefix  Hexadecimal key prefix to start listing from, only used if $cmsort is set to 'sortkey',
	 *                                         overrides $cmstartsortkey
	 * @param   string  $cmendsortkeyprefix    Hexadecimal key prefix to end listing before, only used if $cmsort is set to 'sortkey',
	 *                                         overrides $cmendsortkey
	 * @param   string  $cmsort                Property to sort by
	 * @param   string  $cmdir                 Direction to sort in
	 * @param   string  $cmcontinue            Used to continue a previous request
	 *
	 * @return  object
	 *
	 * @since   3.2.2 (CMS)
	 * @throws  RuntimeException
	 */
	public function getCategoryMembers($cmtitle = null, $cmpageid = null, $cmlimit = null, array $cmprop = null, array $cmnamespace = null,
		array $cmtype = null, $cmstart = null, $cmend = null, $cmstartsortkey = null, $cmendsortkey = null, $cmstartsortkeyprefix = null,
		$cmendsortkeyprefix = null, $cmsort = null, $cmdir = null, $cmcontinue = null)
	{
		// Build the request.
		$path = '?action=query&list=categorymembers';

		// Make sure both $cmtitle and $cmpageid are not set
		if (isset($cmtitle) && isset($cmpageid))
		{
			throw new RuntimeException('Both the $cmtitle and $cmpageid parameters cannot be set, please only use one of the two.');
		}

		if (isset($cmtitle))
		{
			// Verify that the Category: prefix exists
			if (strpos($cmtitle, 'Category:') !== 0)
			{
				throw new RuntimeException('The $cmtitle parameter must include the Category: prefix.');
			}

			$path .= '&cmtitle=' . $cmtitle;
		}

		if (isset($cmpageid))
		{
			$path .= '&cmpageid=' . $cmpageid;
		}

		if (isset($cmlimit))
		{
			$path .= '&cmlimit=' . $cmlimit;
		}

		if (isset($cmprop))
		{
			$path .= '&cmprop=' . $this->buildParameter($cmprop);
		}

		if (isset($cmnamespace))
		{
			$path .= '&cmnamespace=' . $this->buildParameter($cmnamespace);
		}

		if (isset($cmtype))
		{
			$path .= '&cmtype=' . $this->buildParameter($cmtype);
		}

		if (isset($cmstart))
		{
			$path .= '&cmstart=' . $cmstart;
		}

		if (isset($cmend))
		{
			$path .= '&cmend=' . $cmend;
		}

		if (isset($cmstartsortkey))
		{
			$path .= '&cmstartsortkey=' . $cmstartsortkey;
		}

		if (isset($cmendsortkey))
		{
			$path .= '&cmendsortkey=' . $cmendsortkey;
		}

		if (isset($cmstartsortkeyprefix))
		{
			$path .= '&cmstartsortkeyprefix=' . $cmstartsortkeyprefix;
		}

		if (isset($cmendsortkeyprefix))
		{
			$path .= '&cmendsortkeyprefix=' . $cmendsortkeyprefix;
		}

		if (isset($cmsort))
		{
			$path .= '&cmsort=' . $cmsort;
		}

		if (isset($cmdir))
		{
			$path .= '&cmdir=' . $cmdir;
		}

		if (isset($cmcontinue))
		{
			$path .= '&cmcontinue=' . $cmcontinue;
		}

		// Send the request.
		$response = $this->client->get($this->fetchUrl($path));

		return $this->validateResponse($response);
	}

	/**
	 * Method to enumerate all categories.
	 *
	 * @param   string   $acfrom    The category to start enumerating from.
	 * @param   string   $acto      The category to stop enumerating at.
	 * @param   string   $acprefix  Search for all category titles that begin with this value.
	 * @param   string   $acdir     Direction to sort in.
	 * @param   integer  $acmin     Minimum number of category members.
	 * @param   integer  $acmax     Maximum number of category members.
	 * @param   integer  $aclimit   How many categories to return.
	 * @param   array    $acprop    Which properties to get.
	 *
	 * @return  object
	 *
	 * @since   3.1.4
	 */
	public function enumerateCategories($acfrom = null, $acto = null, $acprefix = null, $acdir = null, $acmin = null,
		$acmax = null, $aclimit = null, array $acprop = null)
	{
		// Build the request.
		$path = '?action=query&list=allcategories';

		if (isset($acfrom))
		{
			$path .= '&acfrom=' . $acfrom;
		}

		if (isset($acto))
		{
			$path .= '&acto=' . $acto;
		}

		if (isset($acprefix))
		{
			$path .= '&acprefix=' . $acprefix;
		}

		if (isset($acdir))
		{
			$path .= '&acdir=' . $acdir;
		}

		if (isset($acfrom))
		{
			$path .= '&acfrom=' . $acfrom;
		}

		if (isset($acmin))
		{
			$path .= '&acmin=' . $acmin;
		}

		if (isset($acmax))
		{
			$path .= '&acmax=' . $acmax;
		}

		if (isset($aclimit))
		{
			$path .= '&aclimit=' . $aclimit;
		}

		if (isset($acprop))
		{
			$path .= '&acprop=' . $this->buildParameter($acprop);
		}

		// Send the request.
		$response = $this->client->get($this->fetchUrl($path));

		return $this->validateResponse($response);
	}

	/**
	 * Method to list change tags.
	 *
	 * @param   array   $tgprop   List of properties to get.
	 * @param   string  $tglimit  The maximum number of tags to limit.
	 *
	 * @return  object
	 *
	 * @since   3.1.4
	 */
	public function getChangeTags(array $tgprop = null, $tglimit = null)
	{
		// Build the request.
		$path = '?action=query&list=tags';

		if (isset($tgprop))
		{
			$path .= '&tgprop=' . $this->buildParameter($tgprop);
		}

		if (isset($tglimit))
		{
			$path .= '&tglimit=' . $tglimit;
		}

		// @TODO add support for $tgcontinue

		// Send the request.
		$response = $this->client->get($this->fetchUrl($path));

		return $this->validateResponse($response);
	}
}