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 * array( 'Category 1', 'Category 2' ),
41 * print implode( ',' , $a );
45 class Categoryfinder
{
46 var $articles = array(); # The original article IDs passed to the seed function
47 var $deadend = array(); # Array of DBKEY category names for categories that don't have a page
48 var $parents = array(); # Array of [ID => array()]
49 var $next = array(); # Array of article/category IDs
50 var $targets = array(); # Array of DBKEY category names
51 var $name2id = array();
52 var $mode; # "AND" or "OR"
57 var $dbr; # Read-DB slave
60 * Constructor (currently empty).
62 function __construct() {
66 * Initializes the instance. Do this prior to calling run().
67 * @param $article_ids Array of article IDs
68 * @param $categories FIXME
69 * @param string $mode FIXME, default 'AND'.
70 * @todo FIXME: $categories/$mode
72 function seed( $article_ids, $categories, $mode = 'AND' ) {
73 $this->articles
= $article_ids;
74 $this->next
= $article_ids;
77 # Set the list of target categories; convert them to DBKEY form first
78 $this->targets
= array();
79 foreach ( $categories as $c ) {
80 $ct = Title
::makeTitleSafe( NS_CATEGORY
, $c );
83 $this->targets
[$c] = $c;
89 * Iterates through the parent tree starting with the seed values,
90 * then checks the articles if they match the conditions
91 * @return array of page_ids (those given to seed() that match the conditions)
94 $this->dbr
= wfGetDB( DB_SLAVE
);
95 while ( count( $this->next
) > 0 ) {
96 $this->scan_next_layer();
99 # Now check if this applies to the individual articles
102 foreach ( $this->articles
as $article ) {
103 $conds = $this->targets
;
104 if ( $this->check( $article, $conds ) ) {
105 # Matches the conditions
113 * This functions recurses through the parent representation, trying to match the conditions
114 * @param int $id The article/category to check
115 * @param array $conds The array of categories to match
116 * @param array $path used to check for recursion loops
117 * @return bool Does this match the conditions?
119 function check( $id, &$conds, $path = array() ) {
120 // Check for loops and stop!
121 if ( in_array( $id, $path ) ) {
127 # Shortcut (runtime paranoia): No conditions=all matched
128 if ( count( $conds ) == 0 ) {
132 if ( !isset( $this->parents
[$id] ) ) {
136 # iterate through the parents
137 foreach ( $this->parents
[$id] as $p ) {
140 # Is this a condition?
141 if ( isset( $conds[$pname] ) ) {
142 # This key is in the category list!
143 if ( $this->mode
== 'OR' ) {
144 # One found, that's enough!
148 # Assuming "AND" as default
149 unset( $conds[$pname] );
150 if ( count( $conds ) == 0 ) {
151 # All conditions met, done
157 # Not done yet, try sub-parents
158 if ( !isset( $this->name2id
[$pname] ) ) {
162 $done = $this->check( $this->name2id
[$pname], $conds, $path );
163 if ( $done ||
count( $conds ) == 0 ) {
164 # Subparents have done it!
172 * Scans a "parent layer" of the articles/categories in $this->next
174 function scan_next_layer() {
175 wfProfileIn( __METHOD__
);
177 # Find all parents of the article currently in $this->next
179 $res = $this->dbr
->select(
180 /* FROM */ 'categorylinks',
182 /* WHERE */ array( 'cl_from' => $this->next
),
185 foreach ( $res as $o ) {
189 if ( !isset( $this->parents
[$o->cl_from
] ) ) {
190 $this->parents
[$o->cl_from
] = array();
192 $this->parents
[$o->cl_from
][$k] = $o;
194 # Ignore those we already have
195 if ( in_array( $k, $this->deadend
) ) {
199 if ( isset( $this->name2id
[$k] ) ) {
207 $this->next
= array();
209 # Find the IDs of all category pages in $layer, if they exist
210 if ( count( $layer ) > 0 ) {
211 $res = $this->dbr
->select(
213 /* SELECT */ array( 'page_id', 'page_title' ),
214 /* WHERE */ array( 'page_namespace' => NS_CATEGORY
, 'page_title' => $layer ),
217 foreach ( $res as $o ) {
219 $name = $o->page_title
;
220 $this->name2id
[$name] = $id;
222 unset( $layer[$name] );
227 foreach ( $layer as $v ) {
228 $this->deadend
[$v] = $v;
231 wfProfileOut( __METHOD__
);