1 | <?php |
||
19 | class CategoryTraverser extends Service { |
||
20 | |||
21 | public const CALLBACK_CATEGORY = 10; |
||
22 | public const CALLBACK_PAGE = 20; |
||
23 | |||
24 | /** |
||
25 | * @var string[] |
||
26 | */ |
||
27 | protected $namespaces; |
||
28 | |||
29 | /** |
||
30 | * @var callable[] |
||
31 | */ |
||
32 | protected $callbacks; |
||
33 | |||
34 | /** |
||
35 | * Used to remember the previously-visited categories when traversing. |
||
36 | * @var string[] |
||
37 | */ |
||
38 | protected $alreadyVisited; |
||
39 | |||
40 | /** |
||
41 | * @param MediawikiApi $api The API to connect to. |
||
42 | */ |
||
43 | 4 | public function __construct( MediawikiApi $api ) { |
|
47 | |||
48 | /** |
||
49 | * Query the remote site for the list of namespaces in use, so that later we can tell what's a |
||
50 | * category and what's not. This populates $this->namespaces, and will not re-request on |
||
51 | * repeated invocations. |
||
52 | * @return void |
||
53 | */ |
||
54 | 4 | protected function retrieveNamespaces() { |
|
64 | |||
65 | /** |
||
66 | * Register a callback that will be called for each page or category visited during the |
||
67 | * traversal. |
||
68 | * @param int $type One of the 'CALLBACK_' constants of this class. |
||
69 | * @param callable $callback A callable that takes two \Mediawiki\DataModel\Page parameters. |
||
70 | */ |
||
71 | 2 | public function addCallback( $type, $callback ) { |
|
77 | |||
78 | /** |
||
79 | * Visit every descendant page of $rootCategoryName (which will be a Category |
||
80 | * page, because there are no desecendants of any other pages). |
||
81 | * @param Page $rootCat The full name of the page to start at. |
||
82 | * @param Page[]|null $currentPath Used only when recursing into this method, to track each path |
||
83 | * through the category hierarchy in case of loops. |
||
84 | * @return Pages All descendants of the given category. |
||
85 | * @throws CategoryLoopException If a category loop is detected. |
||
86 | */ |
||
87 | 4 | public function descend( Page $rootCat, $currentPath = null ) { |
|
144 | |||
145 | /** |
||
146 | * Call all the registered callbacks of a particular type. |
||
147 | * @param int $type The callback type; should match one of the 'CALLBACK_' constants. |
||
148 | * @param mixed[] $params The parameters to pass to the callback function. |
||
149 | */ |
||
150 | 4 | protected function call( $type, $params ) { |
|
160 | |||
161 | } |
||
162 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: