3 * Recent changes filtering by category.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 * The "CategoryFinder" class takes a list of articles, creates an internal
25 * representation of all their parent categories (as well as parents of
26 * parents etc.). From this representation, it determines which of these
27 * articles are in one or all of a given subset of categories.
31 * # Determines whether the article with the page_id 12345 is in both
32 * # "Category 1" and "Category 2" or their subcategories, respectively
34 * $cf = new CategoryFinder;
37 * [ 'Category 1', 'Category 2' ],
41 * print implode( ',' , $a );
44 class CategoryFinder
{
45 /** @var int[] The original article IDs passed to the seed function */
46 protected $articles = [];
48 /** @var array Array of DBKEY category names for categories that don't have a page */
49 protected $deadend = [];
51 /** @var array Array of [ ID => [] ] */
52 protected $parents = [];
54 /** @var array Array of article/category IDs */
57 /** @var array Array of DBKEY category names */
58 protected $targets = [];
61 protected $name2id = [];
63 /** @var string "AND" or "OR" */
66 /** @var IDatabase Read-DB replica DB */
70 * Initializes the instance. Do this prior to calling run().
71 * @param array $articleIds Array of article IDs
72 * @param array $categories FIXME
73 * @param string $mode FIXME, default 'AND'.
74 * @todo FIXME: $categories/$mode
76 public function seed( $articleIds, $categories, $mode = 'AND' ) {
77 $this->articles
= $articleIds;
78 $this->next
= $articleIds;
81 # Set the list of target categories; convert them to DBKEY form first
83 foreach ( $categories as $c ) {
84 $ct = Title
::makeTitleSafe( NS_CATEGORY
, $c );
87 $this->targets
[$c] = $c;
93 * Iterates through the parent tree starting with the seed values,
94 * then checks the articles if they match the conditions
95 * @return array Array of page_ids (those given to seed() that match the conditions)
97 public function run() {
98 $this->dbr
= wfGetDB( DB_REPLICA
);
99 while ( count( $this->next
) > 0 ) {
100 $this->scanNextLayer();
103 # Now check if this applies to the individual articles
106 foreach ( $this->articles
as $article ) {
107 $conds = $this->targets
;
108 if ( $this->check( $article, $conds ) ) {
109 # Matches the conditions
117 * Get the parents. Only really useful if run() has been called already
120 public function getParents() {
121 return $this->parents
;
125 * This functions recurses through the parent representation, trying to match the conditions
126 * @param int $id The article/category to check
127 * @param array $conds The array of categories to match
128 * @param array $path Used to check for recursion loops
129 * @return bool Does this match the conditions?
131 private function check( $id, &$conds, $path = [] ) {
132 // Check for loops and stop!
133 if ( in_array( $id, $path ) ) {
139 # Shortcut (runtime paranoia): No conditions=all matched
140 if ( count( $conds ) == 0 ) {
144 if ( !isset( $this->parents
[$id] ) ) {
148 # iterate through the parents
149 foreach ( $this->parents
[$id] as $p ) {
152 # Is this a condition?
153 if ( isset( $conds[$pname] ) ) {
154 # This key is in the category list!
155 if ( $this->mode
== 'OR' ) {
156 # One found, that's enough!
160 # Assuming "AND" as default
161 unset( $conds[$pname] );
162 if ( count( $conds ) == 0 ) {
163 # All conditions met, done
169 # Not done yet, try sub-parents
170 if ( !isset( $this->name2id
[$pname] ) ) {
174 $done = $this->check( $this->name2id
[$pname], $conds, $path );
175 if ( $done ||
count( $conds ) == 0 ) {
176 # Subparents have done it!
184 * Scans a "parent layer" of the articles/categories in $this->next
186 private function scanNextLayer() {
188 # Find all parents of the article currently in $this->next
190 $res = $this->dbr
->select(
191 /* FROM */ 'categorylinks',
193 /* WHERE */ [ 'cl_from' => $this->next
],
196 foreach ( $res as $o ) {
200 if ( !isset( $this->parents
[$o->cl_from
] ) ) {
201 $this->parents
[$o->cl_from
] = [];
203 $this->parents
[$o->cl_from
][$k] = $o;
205 # Ignore those we already have
206 if ( in_array( $k, $this->deadend
) ) {
210 if ( isset( $this->name2id
[$k] ) ) {
220 # Find the IDs of all category pages in $layer, if they exist
221 if ( count( $layer ) > 0 ) {
222 $res = $this->dbr
->select(
224 /* SELECT */ [ 'page_id', 'page_title' ],
225 /* WHERE */ [ 'page_namespace' => NS_CATEGORY
, 'page_title' => $layer ],
228 foreach ( $res as $o ) {
230 $name = $o->page_title
;
231 $this->name2id
[$name] = $id;
233 unset( $layer[$name] );
238 foreach ( $layer as $v ) {
239 $this->deadend
[$v] = $v;