Add/remove/tweak method documentation
[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 var $dbr; # Read-DB slave
33
34 /**
35 * Constructor (currently empty).
36 */
37 function __construct() {
38 }
39
40 /**
41 * Initializes the instance. Do this prior to calling run().
42 * @param $article_ids Array of article IDs
43 * @param $categories FIXME
44 * @param $mode String: FIXME, default 'AND'.
45 * @todo FIXME: $categories/$mode
46 */
47 function seed( $article_ids, $categories, $mode = 'AND' ) {
48 $this->articles = $article_ids;
49 $this->next = $article_ids;
50 $this->mode = $mode;
51
52 # Set the list of target categories; convert them to DBKEY form first
53 $this->targets = array();
54 foreach ( $categories as $c ) {
55 $ct = Title::makeTitleSafe( NS_CATEGORY, $c );
56 if ( $ct ) {
57 $c = $ct->getDBkey();
58 $this->targets[$c] = $c;
59 }
60 }
61 }
62
63 /**
64 * Iterates through the parent tree starting with the seed values,
65 * then checks the articles if they match the conditions
66 * @return array of page_ids (those given to seed() that match the conditions)
67 */
68 function run() {
69 $this->dbr = wfGetDB( DB_SLAVE );
70 while ( count( $this->next ) > 0 ) {
71 $this->scan_next_layer();
72 }
73
74 # Now check if this applies to the individual articles
75 $ret = array();
76
77 foreach ( $this->articles as $article ) {
78 $conds = $this->targets;
79 if ( $this->check( $article, $conds ) ) {
80 # Matches the conditions
81 $ret[] = $article;
82 }
83 }
84 return $ret;
85 }
86
87 /**
88 * This functions recurses through the parent representation, trying to match the conditions
89 * @param $id int The article/category to check
90 * @param $conds array The array of categories to match
91 * @param $path array used to check for recursion loops
92 * @return bool Does this match the conditions?
93 */
94 function check( $id, &$conds, $path = array() ) {
95 // Check for loops and stop!
96 if ( in_array( $id, $path ) ) {
97 return false;
98 }
99
100 $path[] = $id;
101
102 # Shortcut (runtime paranoia): No contitions=all matched
103 if ( count( $conds ) == 0 ) {
104 return true;
105 }
106
107 if ( !isset( $this->parents[$id] ) ) {
108 return false;
109 }
110
111 # iterate through the parents
112 foreach ( $this->parents[$id] as $p ) {
113 $pname = $p->cl_to ;
114
115 # Is this a condition?
116 if ( isset( $conds[$pname] ) ) {
117 # This key is in the category list!
118 if ( $this->mode == 'OR' ) {
119 # One found, that's enough!
120 $conds = array();
121 return true;
122 } else {
123 # Assuming "AND" as default
124 unset( $conds[$pname] );
125 if ( count( $conds ) == 0 ) {
126 # All conditions met, done
127 return true;
128 }
129 }
130 }
131
132 # Not done yet, try sub-parents
133 if ( !isset( $this->name2id[$pname] ) ) {
134 # No sub-parent
135 continue;
136 }
137 $done = $this->check( $this->name2id[$pname], $conds, $path );
138 if ( $done || count( $conds ) == 0 ) {
139 # Subparents have done it!
140 return true;
141 }
142 }
143 return false;
144 }
145
146 /**
147 * Scans a "parent layer" of the articles/categories in $this->next
148 */
149 function scan_next_layer() {
150 # Find all parents of the article currently in $this->next
151 $layer = array();
152 $res = $this->dbr->select(
153 /* FROM */ 'categorylinks',
154 /* SELECT */ '*',
155 /* WHERE */ array( 'cl_from' => $this->next ),
156 __METHOD__ . '-1'
157 );
158 foreach ( $res as $o ) {
159 $k = $o->cl_to;
160
161 # Update parent tree
162 if ( !isset( $this->parents[$o->cl_from] ) ) {
163 $this->parents[$o->cl_from] = array();
164 }
165 $this->parents[$o->cl_from][$k] = $o;
166
167 # Ignore those we already have
168 if ( in_array( $k, $this->deadend ) ) {
169 continue;
170 }
171
172 if ( isset( $this->name2id[$k] ) ) {
173 continue;
174 }
175
176 # Hey, new category!
177 $layer[$k] = $k;
178 }
179
180 $this->next = array();
181
182 # Find the IDs of all category pages in $layer, if they exist
183 if ( count( $layer ) > 0 ) {
184 $res = $this->dbr->select(
185 /* FROM */ 'page',
186 /* SELECT */ array( 'page_id', 'page_title' ),
187 /* WHERE */ array( 'page_namespace' => NS_CATEGORY , 'page_title' => $layer ),
188 __METHOD__ . '-2'
189 );
190 foreach ( $res as $o ) {
191 $id = $o->page_id;
192 $name = $o->page_title;
193 $this->name2id[$name] = $id;
194 $this->next[] = $id;
195 unset( $layer[$name] );
196 }
197 }
198
199 # Mark dead ends
200 foreach ( $layer as $v ) {
201 $this->deadend[$v] = $v;
202 }
203 }
204
205 }