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 );
45 class CategoryFinder
{
46 /** @var int[] The original article IDs passed to the seed function */
47 protected $articles = [];
49 /** @var array Array of DBKEY category names for categories that don't have a page */
50 protected $deadend = [];
52 /** @var array Array of [ ID => [] ] */
53 protected $parents = [];
55 /** @var array Array of article/category IDs */
58 /** @var array Array of DBKEY category names */
59 protected $targets = [];
62 protected $name2id = [];
64 /** @var string "AND" or "OR" */
67 /** @var IDatabase Read-DB replica DB */
71 * Initializes the instance. Do this prior to calling run().
72 * @param array $articleIds Array of article IDs
73 * @param array $categories FIXME
74 * @param string $mode FIXME, default 'AND'.
75 * @todo FIXME: $categories/$mode
77 public function seed( $articleIds, $categories, $mode = 'AND' ) {
78 $this->articles
= $articleIds;
79 $this->next
= $articleIds;
82 # Set the list of target categories; convert them to DBKEY form first
84 foreach ( $categories as $c ) {
85 $ct = Title
::makeTitleSafe( NS_CATEGORY
, $c );
88 $this->targets
[$c] = $c;
94 * Iterates through the parent tree starting with the seed values,
95 * then checks the articles if they match the conditions
96 * @return array Array of page_ids (those given to seed() that match the conditions)
98 public function run() {
99 $this->dbr
= wfGetDB( DB_REPLICA
);
100 while ( count( $this->next
) > 0 ) {
101 $this->scanNextLayer();
104 # Now check if this applies to the individual articles
107 foreach ( $this->articles
as $article ) {
108 $conds = $this->targets
;
109 if ( $this->check( $article, $conds ) ) {
110 # Matches the conditions
118 * Get the parents. Only really useful if run() has been called already
121 public function getParents() {
122 return $this->parents
;
126 * This functions recurses through the parent representation, trying to match the conditions
127 * @param int $id The article/category to check
128 * @param array $conds The array of categories to match
129 * @param array $path Used to check for recursion loops
130 * @return bool Does this match the conditions?
132 private function check( $id, &$conds, $path = [] ) {
133 // Check for loops and stop!
134 if ( in_array( $id, $path ) ) {
140 # Shortcut (runtime paranoia): No conditions=all matched
141 if ( count( $conds ) == 0 ) {
145 if ( !isset( $this->parents
[$id] ) ) {
149 # iterate through the parents
150 foreach ( $this->parents
[$id] as $p ) {
153 # Is this a condition?
154 if ( isset( $conds[$pname] ) ) {
155 # This key is in the category list!
156 if ( $this->mode
== 'OR' ) {
157 # One found, that's enough!
161 # Assuming "AND" as default
162 unset( $conds[$pname] );
163 if ( count( $conds ) == 0 ) {
164 # All conditions met, done
170 # Not done yet, try sub-parents
171 if ( !isset( $this->name2id
[$pname] ) ) {
175 $done = $this->check( $this->name2id
[$pname], $conds, $path );
176 if ( $done ||
count( $conds ) == 0 ) {
177 # Subparents have done it!
185 * Scans a "parent layer" of the articles/categories in $this->next
187 private function scanNextLayer() {
189 # Find all parents of the article currently in $this->next
191 $res = $this->dbr
->select(
192 /* FROM */ 'categorylinks',
194 /* WHERE */ [ 'cl_from' => $this->next
],
197 foreach ( $res as $o ) {
201 if ( !isset( $this->parents
[$o->cl_from
] ) ) {
202 $this->parents
[$o->cl_from
] = [];
204 $this->parents
[$o->cl_from
][$k] = $o;
206 # Ignore those we already have
207 if ( in_array( $k, $this->deadend
) ) {
211 if ( isset( $this->name2id
[$k] ) ) {
221 # Find the IDs of all category pages in $layer, if they exist
222 if ( count( $layer ) > 0 ) {
223 $res = $this->dbr
->select(
225 /* SELECT */ [ 'page_id', 'page_title' ],
226 /* WHERE */ [ 'page_namespace' => NS_CATEGORY
, 'page_title' => $layer ],
229 foreach ( $res as $o ) {
231 $name = $o->page_title
;
232 $this->name2id
[$name] = $id;
234 unset( $layer[$name] );
239 foreach ( $layer as $v ) {
240 $this->deadend
[$v] = $v;