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.
10 * # Determines whether the article with the page_id 12345 is in both
11 * # "Category 1" and "Category 2" or their subcategories, respectively
13 * $cf = new Categoryfinder;
16 * array( 'Category 1', 'Category 2' ),
20 * print implode( ',' , $a );
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
35 * Constructor (currently empty).
37 function __construct() {
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
47 function seed( $article_ids, $categories, $mode = 'AND' ) {
48 $this->articles
= $article_ids;
49 $this->next
= $article_ids;
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 );
58 $this->targets
[$c] = $c;
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)
69 $this->dbr
= wfGetDB( DB_SLAVE
);
70 while ( count( $this->next
) > 0 ) {
71 $this->scan_next_layer();
74 # Now check if this applies to the individual articles
77 foreach ( $this->articles
as $article ) {
78 $conds = $this->targets
;
79 if ( $this->check( $article, $conds ) ) {
80 # Matches the conditions
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?
94 function check( $id, &$conds, $path = array() ) {
95 // Check for loops and stop!
96 if ( in_array( $id, $path ) ) {
102 # Shortcut (runtime paranoia): No contitions=all matched
103 if ( count( $conds ) == 0 ) {
107 if ( !isset( $this->parents
[$id] ) ) {
111 # iterate through the parents
112 foreach ( $this->parents
[$id] as $p ) {
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!
123 # Assuming "AND" as default
124 unset( $conds[$pname] );
125 if ( count( $conds ) == 0 ) {
126 # All conditions met, done
132 # Not done yet, try sub-parents
133 if ( !isset( $this->name2id
[$pname] ) ) {
137 $done = $this->check( $this->name2id
[$pname], $conds, $path );
138 if ( $done ||
count( $conds ) == 0 ) {
139 # Subparents have done it!
147 * Scans a "parent layer" of the articles/categories in $this->next
149 function scan_next_layer() {
150 # Find all parents of the article currently in $this->next
152 $res = $this->dbr
->select(
153 /* FROM */ 'categorylinks',
155 /* WHERE */ array( 'cl_from' => $this->next
),
158 foreach ( $res as $o ) {
162 if ( !isset( $this->parents
[$o->cl_from
] ) ) {
163 $this->parents
[$o->cl_from
] = array();
165 $this->parents
[$o->cl_from
][$k] = $o;
167 # Ignore those we already have
168 if ( in_array( $k, $this->deadend
) ) {
172 if ( isset( $this->name2id
[$k] ) ) {
180 $this->next
= array();
182 # Find the IDs of all category pages in $layer, if they exist
183 if ( count( $layer ) > 0 ) {
184 $res = $this->dbr
->select(
186 /* SELECT */ array( 'page_id', 'page_title' ),
187 /* WHERE */ array( 'page_namespace' => NS_CATEGORY
, 'page_title' => $layer ),
190 foreach ( $res as $o ) {
192 $name = $o->page_title
;
193 $this->name2id
[$name] = $id;
195 unset( $layer[$name] );
200 foreach ( $layer as $v ) {
201 $this->deadend
[$v] = $v;