MediaWiki  1.30.2
CategoryFinder.php
Go to the documentation of this file.
1 <?php
24 
48  protected $articles = [];
49 
51  protected $deadend = [];
52 
54  protected $parents = [];
55 
57  protected $next = [];
58 
60  protected $targets = [];
61 
63  protected $name2id = [];
64 
66  protected $mode;
67 
69  protected $dbr;
70 
78  public function seed( $articleIds, $categories, $mode = 'AND' ) {
79  $this->articles = $articleIds;
80  $this->next = $articleIds;
81  $this->mode = $mode;
82 
83  # Set the list of target categories; convert them to DBKEY form first
84  $this->targets = [];
85  foreach ( $categories as $c ) {
86  $ct = Title::makeTitleSafe( NS_CATEGORY, $c );
87  if ( $ct ) {
88  $c = $ct->getDBkey();
89  $this->targets[$c] = $c;
90  }
91  }
92  }
93 
99  public function run() {
100  $this->dbr = wfGetDB( DB_REPLICA );
101  while ( count( $this->next ) > 0 ) {
102  $this->scanNextLayer();
103  }
104 
105  # Now check if this applies to the individual articles
106  $ret = [];
107 
108  foreach ( $this->articles as $article ) {
109  $conds = $this->targets;
110  if ( $this->check( $article, $conds ) ) {
111  # Matches the conditions
112  $ret[] = $article;
113  }
114  }
115  return $ret;
116  }
117 
122  public function getParents() {
123  return $this->parents;
124  }
125 
133  private function check( $id, &$conds, $path = [] ) {
134  // Check for loops and stop!
135  if ( in_array( $id, $path ) ) {
136  return false;
137  }
138 
139  $path[] = $id;
140 
141  # Shortcut (runtime paranoia): No conditions=all matched
142  if ( count( $conds ) == 0 ) {
143  return true;
144  }
145 
146  if ( !isset( $this->parents[$id] ) ) {
147  return false;
148  }
149 
150  # iterate through the parents
151  foreach ( $this->parents[$id] as $p ) {
152  $pname = $p->cl_to;
153 
154  # Is this a condition?
155  if ( isset( $conds[$pname] ) ) {
156  # This key is in the category list!
157  if ( $this->mode == 'OR' ) {
158  # One found, that's enough!
159  $conds = [];
160  return true;
161  } else {
162  # Assuming "AND" as default
163  unset( $conds[$pname] );
164  if ( count( $conds ) == 0 ) {
165  # All conditions met, done
166  return true;
167  }
168  }
169  }
170 
171  # Not done yet, try sub-parents
172  if ( !isset( $this->name2id[$pname] ) ) {
173  # No sub-parent
174  continue;
175  }
176  $done = $this->check( $this->name2id[$pname], $conds, $path );
177  if ( $done || count( $conds ) == 0 ) {
178  # Subparents have done it!
179  return true;
180  }
181  }
182  return false;
183  }
184 
188  private function scanNextLayer() {
189  # Find all parents of the article currently in $this->next
190  $layer = [];
191  $res = $this->dbr->select(
192  /* FROM */ 'categorylinks',
193  /* SELECT */ '*',
194  /* WHERE */ [ 'cl_from' => $this->next ],
195  __METHOD__ . '-1'
196  );
197  foreach ( $res as $o ) {
198  $k = $o->cl_to;
199 
200  # Update parent tree
201  if ( !isset( $this->parents[$o->cl_from] ) ) {
202  $this->parents[$o->cl_from] = [];
203  }
204  $this->parents[$o->cl_from][$k] = $o;
205 
206  # Ignore those we already have
207  if ( in_array( $k, $this->deadend ) ) {
208  continue;
209  }
210 
211  if ( isset( $this->name2id[$k] ) ) {
212  continue;
213  }
214 
215  # Hey, new category!
216  $layer[$k] = $k;
217  }
218 
219  $this->next = [];
220 
221  # Find the IDs of all category pages in $layer, if they exist
222  if ( count( $layer ) > 0 ) {
223  $res = $this->dbr->select(
224  /* FROM */ 'page',
225  /* SELECT */ [ 'page_id', 'page_title' ],
226  /* WHERE */ [ 'page_namespace' => NS_CATEGORY, 'page_title' => $layer ],
227  __METHOD__ . '-2'
228  );
229  foreach ( $res as $o ) {
230  $id = $o->page_id;
231  $name = $o->page_title;
232  $this->name2id[$name] = $id;
233  $this->next[] = $id;
234  unset( $layer[$name] );
235  }
236  }
237 
238  # Mark dead ends
239  foreach ( $layer as $v ) {
240  $this->deadend[$v] = $v;
241  }
242  }
243 }
CategoryFinder\$parents
array $parents
Array of [ ID => [] ].
Definition: CategoryFinder.php:54
CategoryFinder\$articles
int[] $articles
The original article IDs passed to the seed function.
Definition: CategoryFinder.php:48
CategoryFinder\$mode
string $mode
"AND" or "OR"
Definition: CategoryFinder.php:66
captcha-old.count
count
Definition: captcha-old.py:249
CategoryFinder\$targets
array $targets
Array of DBKEY category names.
Definition: CategoryFinder.php:60
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$res
$res
Definition: database.txt:21
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
CategoryFinder\scanNextLayer
scanNextLayer()
Scans a "parent layer" of the articles/categories in $this->next.
Definition: CategoryFinder.php:188
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:40
CategoryFinder\check
check( $id, &$conds, $path=[])
This functions recurses through the parent representation, trying to match the conditions.
Definition: CategoryFinder.php:133
CategoryFinder\run
run()
Iterates through the parent tree starting with the seed values, then checks the articles if they matc...
Definition: CategoryFinder.php:99
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2856
$article
Using a hook running we can avoid having all this option specific stuff in our mainline code Using the function array $article
Definition: hooks.txt:77
mode
if write to the Free Software Franklin Fifth MA USA Also add information on how to contact you by electronic and paper mail If the program is make it output a short notice like this when it starts in an interactive mode
Definition: COPYING.txt:307
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
CategoryFinder\seed
seed( $articleIds, $categories, $mode='AND')
Initializes the instance.
Definition: CategoryFinder.php:78
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:79
CategoryFinder\$next
array $next
Array of article/category IDs.
Definition: CategoryFinder.php:57
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:557
CategoryFinder
The "CategoryFinder" class takes a list of articles, creates an internal representation of all their ...
Definition: CategoryFinder.php:46
$ret
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:1965
CategoryFinder\$name2id
array $name2id
Definition: CategoryFinder.php:63
$path
$path
Definition: NoLocalSettings.php:26
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
CategoryFinder\getParents
getParents()
Get the parents.
Definition: CategoryFinder.php:122
CategoryFinder\$dbr
IDatabase $dbr
Read-DB replica DB.
Definition: CategoryFinder.php:69
array
the array() calling protocol came about after MediaWiki 1.4rc1.
CategoryFinder\$deadend
array $deadend
Array of DBKEY category names for categories that don't have a page.
Definition: CategoryFinder.php:51