summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Gadgets/includes/GadgetRepo.php
blob: 11996f8e6ead147dbbaffdbd04faeb6efedc5314 (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
<?php

use MediaWiki\Linker\LinkTarget;

abstract class GadgetRepo {

	/**
	 * @var GadgetRepo|null
	 */
	private static $instance;

	/**
	 * Get the ids of the gadgets provided by this repository
	 *
	 * It's possible this could be out of sync with what
	 * getGadget() will return due to caching
	 *
	 * @return string[]
	 */
	abstract public function getGadgetIds();

	/**
	 * Get the Gadget object for a given gadget id
	 *
	 * @param string $id
	 * @throws InvalidArgumentException
	 * @return Gadget
	 */
	abstract public function getGadget( $id );

	/**
	 * Given that the provided page was updated, invalidate
	 * caches if necessary
	 *
	 * @param LinkTarget $target
	 *
	 * @return void
	 */
	public function handlePageUpdate( LinkTarget $target ) {
	}

	/**
	 * Given that the provided page was created, invalidate
	 * caches if necessary
	 *
	 * @param LinkTarget $target
	 *
	 * @return void
	 */
	public function handlePageCreation( LinkTarget $target ) {
	}

	/**
	 * Given that the provided page was updated, invalidate
	 * caches if necessary
	 *
	 * @param LinkTarget $target
	 *
	 * @return void
	 */
	public function handlePageDeletion( LinkTarget $target ) {
	}

	/**
	 * Get a list of gadgets sorted by category
	 *
	 * @return array [ 'category' => [ 'name' => $gadget ] ]
	 */
	public function getStructuredList() {
		$list = [];
		foreach ( $this->getGadgetIds() as $id ) {
			try {
				$gadget = $this->getGadget( $id );
			} catch ( InvalidArgumentException $e ) {
				continue;
			}
			$list[$gadget->getCategory()][$gadget->getName()] = $gadget;
		}

		return $list;
	}

	/**
	 * Get the configured default GadgetRepo.
	 *
	 * @return GadgetRepo
	 */
	public static function singleton() {
		if ( self::$instance === null ) {
			global $wgGadgetsRepoClass; // @todo use Config here
			self::$instance = new $wgGadgetsRepoClass();
		}
		return self::$instance;
	}

	/**
	 * Should only be used by unit tests
	 *
	 * @param GadgetRepo|null $repo
	 */
	public static function setSingleton( $repo = null ) {
		self::$instance = $repo;
	}
}