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