FU r106752: added b/c code to FSRepo to make things easy for extensions (like Confirm...
[lhc/web/wiklou.git] / includes / Categoryfinder.php
1 <?php
2 /**
3 * The "Categoryfinder" class takes a list of articles, creates an internal
4 * representation of all their parent categories (as well as parents of
5 * parents etc.). From this representation, it determines which of these
6 * articles are in one or all of a given subset of categories.
7 *
8 * Example use :
9 * <code>
10 * # Determines whether the article with the page_id 12345 is in both
11 * # "Category 1" and "Category 2" or their subcategories, respectively
12 *
13 * $cf = new Categoryfinder;
14 * $cf->seed(
15 * array( 12345 ),
16 * array( 'Category 1', 'Category 2' ),
17 * 'AND'
18 * );
19 * $a = $cf->run();
20 * print implode( ',' , $a );
21 * </code>
22 *
23 */
24 class Categoryfinder {
25 var $articles = array(); # The original article IDs passed to the seed function
26 var $deadend = array(); # Array of DBKEY category names for categories that don't have a page
27 var $parents = array(); # Array of [ID => array()]
28 var $next = array(); # Array of article/category IDs
29 var $targets = array(); # Array of DBKEY category names
30 var $name2id = array();
31 var $mode; # "AND" or "OR"
32
33 /**
34 * @var DatabaseBase
35 */
36 var $dbr; # Read-DB slave
37
38 /**
39 * Constructor (currently empty).
40 */
41 function __construct() {
42 }
43
44 /**
45 * Initializes the instance. Do this prior to calling run().
46 * @param $article_ids Array of article IDs
47 * @param $categories FIXME
48 * @param $mode String: FIXME, default 'AND'.
49 * @todo FIXME: $categories/$mode
50 */
51 function seed( $article_ids, $categories, $mode = 'AND' ) {
52 $this->articles = $article_ids;
53 $this->next = $article_ids;
54 $this->mode = $mode;
55
56 # Set the list of target categories; convert them to DBKEY form first
57 $this->targets = array();
58 foreach ( $categories as $c ) {
59 $ct = Title::makeTitleSafe( NS_CATEGORY, $c );
60 if ( $ct ) {
61 $c = $ct->getDBkey();
62 $this->targets[$c] = $c;
63 }
64 }
65 }
66
67 /**
68 * Iterates through the parent tree starting with the seed values,
69 * then checks the articles if they match the conditions
70 * @return array of page_ids (those given to seed() that match the conditions)
71 */
72 function run() {
73 $this->dbr = wfGetDB( DB_SLAVE );
74 while ( count( $this->next ) > 0 ) {
75 $this->scan_next_layer();
76 }
77
78 # Now check if this applies to the individual articles
79 $ret = array();
80
81 foreach ( $this->articles as $article ) {
82 $conds = $this->targets;
83 if ( $this->check( $article, $conds ) ) {
84 # Matches the conditions
85 $ret[] = $article;
86 }
87 }
88 return $ret;
89 }
90
91 /**
92 * This functions recurses through the parent representation, trying to match the conditions
93 * @param $id int The article/category to check
94 * @param $conds array The array of categories to match
95 * @param $path array used to check for recursion loops
96 * @return bool Does this match the conditions?
97 */
98 function check( $id, &$conds, $path = array() ) {
99 // Check for loops and stop!
100 if ( in_array( $id, $path ) ) {
101 return false;
102 }
103
104 $path[] = $id;
105
106 # Shortcut (runtime paranoia): No contitions=all matched
107 if ( count( $conds ) == 0 ) {
108 return true;
109 }
110
111 if ( !isset( $this->parents[$id] ) ) {
112 return false;
113 }
114
115 # iterate through the parents
116 foreach ( $this->parents[$id] as $p ) {
117 $pname = $p->cl_to ;
118
119 # Is this a condition?
120 if ( isset( $conds[$pname] ) ) {
121 # This key is in the category list!
122 if ( $this->mode == 'OR' ) {
123 # One found, that's enough!
124 $conds = array();
125 return true;
126 } else {
127 # Assuming "AND" as default
128 unset( $conds[$pname] );
129 if ( count( $conds ) == 0 ) {
130 # All conditions met, done
131 return true;
132 }
133 }
134 }
135
136 # Not done yet, try sub-parents
137 if ( !isset( $this->name2id[$pname] ) ) {
138 # No sub-parent
139 continue;
140 }
141 $done = $this->check( $this->name2id[$pname], $conds, $path );
142 if ( $done || count( $conds ) == 0 ) {
143 # Subparents have done it!
144 return true;
145 }
146 }
147 return false;
148 }
149
150 /**
151 * Scans a "parent layer" of the articles/categories in $this->next
152 */
153 function scan_next_layer() {
154 # Find all parents of the article currently in $this->next
155 $layer = array();
156 $res = $this->dbr->select(
157 /* FROM */ 'categorylinks',
158 /* SELECT */ '*',
159 /* WHERE */ array( 'cl_from' => $this->next ),
160 __METHOD__ . '-1'
161 );
162 foreach ( $res as $o ) {
163 $k = $o->cl_to;
164
165 # Update parent tree
166 if ( !isset( $this->parents[$o->cl_from] ) ) {
167 $this->parents[$o->cl_from] = array();
168 }
169 $this->parents[$o->cl_from][$k] = $o;
170
171 # Ignore those we already have
172 if ( in_array( $k, $this->deadend ) ) {
173 continue;
174 }
175
176 if ( isset( $this->name2id[$k] ) ) {
177 continue;
178 }
179
180 # Hey, new category!
181 $layer[$k] = $k;
182 }
183
184 $this->next = array();
185
186 # Find the IDs of all category pages in $layer, if they exist
187 if ( count( $layer ) > 0 ) {
188 $res = $this->dbr->select(
189 /* FROM */ 'page',
190 /* SELECT */ array( 'page_id', 'page_title' ),
191 /* WHERE */ array( 'page_namespace' => NS_CATEGORY , 'page_title' => $layer ),
192 __METHOD__ . '-2'
193 );
194 foreach ( $res as $o ) {
195 $id = $o->page_id;
196 $name = $o->page_title;
197 $this->name2id[$name] = $id;
198 $this->next[] = $id;
199 unset( $layer[$name] );
200 }
201 }
202
203 # Mark dead ends
204 foreach ( $layer as $v ) {
205 $this->deadend[$v] = $v;
206 }
207 }
208
209 }