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