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