Localisation updates from http://translatewiki.net.
[mediawiki.git] / includes / Categoryfinder.php
blob6ef224b607d87058c3d9e73429dbe7b27783cd2e
1 <?php
2 /**
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
20 * @file
23 /**
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.
29 * Example use :
30 * <code>
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;
35 * $cf->seed(
36 * array( 12345 ),
37 * array( 'Category 1', 'Category 2' ),
38 * 'AND'
39 * );
40 * $a = $cf->run();
41 * print implode( ',' , $a );
42 * </code>
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"
54 /**
55 * @var DatabaseBase
57 var $dbr; # Read-DB slave
59 /**
60 * Constructor (currently empty).
62 function __construct() {
65 /**
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;
75 $this->mode = $mode;
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 );
81 if ( $ct ) {
82 $c = $ct->getDBkey();
83 $this->targets[$c] = $c;
88 /**
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)
93 function run() {
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
100 $ret = array();
102 foreach ( $this->articles as $article ) {
103 $conds = $this->targets;
104 if ( $this->check( $article, $conds ) ) {
105 # Matches the conditions
106 $ret[] = $article;
109 return $ret;
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 ) ) {
122 return false;
125 $path[] = $id;
127 # Shortcut (runtime paranoia): No conditions=all matched
128 if ( count( $conds ) == 0 ) {
129 return true;
132 if ( !isset( $this->parents[$id] ) ) {
133 return false;
136 # iterate through the parents
137 foreach ( $this->parents[$id] as $p ) {
138 $pname = $p->cl_to;
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!
145 $conds = array();
146 return true;
147 } else {
148 # Assuming "AND" as default
149 unset( $conds[$pname] );
150 if ( count( $conds ) == 0 ) {
151 # All conditions met, done
152 return true;
157 # Not done yet, try sub-parents
158 if ( !isset( $this->name2id[$pname] ) ) {
159 # No sub-parent
160 continue;
162 $done = $this->check( $this->name2id[$pname], $conds, $path );
163 if ( $done || count( $conds ) == 0 ) {
164 # Subparents have done it!
165 return true;
168 return false;
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
178 $layer = array();
179 $res = $this->dbr->select(
180 /* FROM */ 'categorylinks',
181 /* SELECT */ '*',
182 /* WHERE */ array( 'cl_from' => $this->next ),
183 __METHOD__ . '-1'
185 foreach ( $res as $o ) {
186 $k = $o->cl_to;
188 # Update parent tree
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 ) ) {
196 continue;
199 if ( isset( $this->name2id[$k] ) ) {
200 continue;
203 # Hey, new category!
204 $layer[$k] = $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(
212 /* FROM */ 'page',
213 /* SELECT */ array( 'page_id', 'page_title' ),
214 /* WHERE */ array( 'page_namespace' => NS_CATEGORY, 'page_title' => $layer ),
215 __METHOD__ . '-2'
217 foreach ( $res as $o ) {
218 $id = $o->page_id;
219 $name = $o->page_title;
220 $this->name2id[$name] = $id;
221 $this->next[] = $id;
222 unset( $layer[$name] );
226 # Mark dead ends
227 foreach ( $layer as $v ) {
228 $this->deadend[$v] = $v;
231 wfProfileOut( __METHOD__ );