MediaWiki  1.33.0
Category.php
Go to the documentation of this file.
1 <?php
31 class Category {
33  private $mName = null;
34  private $mID = null;
39  private $mTitle = null;
41  private $mPages = null, $mSubcats = null, $mFiles = null;
42 
43  const LOAD_ONLY = 0;
44  const LAZY_INIT_ROW = 1;
45 
46  private function __construct() {
47  }
48 
55  protected function initialize( $mode = self::LOAD_ONLY ) {
56  if ( $this->mName === null && $this->mID === null ) {
57  throw new MWException( __METHOD__ . ' has both names and IDs null' );
58  } elseif ( $this->mID === null ) {
59  $where = [ 'cat_title' => $this->mName ];
60  } elseif ( $this->mName === null ) {
61  $where = [ 'cat_id' => $this->mID ];
62  } else {
63  # Already initialized
64  return true;
65  }
66 
67  $dbr = wfGetDB( DB_REPLICA );
68  $row = $dbr->selectRow(
69  'category',
70  [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ],
71  $where,
72  __METHOD__
73  );
74 
75  if ( !$row ) {
76  # Okay, there were no contents. Nothing to initialize.
77  if ( $this->mTitle ) {
78  # If there is a title object but no record in the category table,
79  # treat this as an empty category.
80  $this->mID = false;
81  $this->mName = $this->mTitle->getDBkey();
82  $this->mPages = 0;
83  $this->mSubcats = 0;
84  $this->mFiles = 0;
85 
86  # If the title exists, call refreshCounts to add a row for it.
87  if ( $mode === self::LAZY_INIT_ROW && $this->mTitle->exists() ) {
88  DeferredUpdates::addCallableUpdate( [ $this, 'refreshCounts' ] );
89  }
90 
91  return true;
92  } else {
93  return false; # Fail
94  }
95  }
96 
97  $this->mID = $row->cat_id;
98  $this->mName = $row->cat_title;
99  $this->mPages = $row->cat_pages;
100  $this->mSubcats = $row->cat_subcats;
101  $this->mFiles = $row->cat_files;
102 
103  # (T15683) If the count is negative, then 1) it's obviously wrong
104  # and should not be kept, and 2) we *probably* don't have to scan many
105  # rows to obtain the correct figure, so let's risk a one-time recount.
106  if ( $this->mPages < 0 || $this->mSubcats < 0 || $this->mFiles < 0 ) {
107  $this->mPages = max( $this->mPages, 0 );
108  $this->mSubcats = max( $this->mSubcats, 0 );
109  $this->mFiles = max( $this->mFiles, 0 );
110 
111  if ( $mode === self::LAZY_INIT_ROW ) {
112  DeferredUpdates::addCallableUpdate( [ $this, 'refreshCounts' ] );
113  }
114  }
115 
116  return true;
117  }
118 
126  public static function newFromName( $name ) {
127  $cat = new self();
129 
130  if ( !is_object( $title ) ) {
131  return false;
132  }
133 
134  $cat->mTitle = $title;
135  $cat->mName = $title->getDBkey();
136 
137  return $cat;
138  }
139 
146  public static function newFromTitle( $title ) {
147  $cat = new self();
148 
149  $cat->mTitle = $title;
150  $cat->mName = $title->getDBkey();
151 
152  return $cat;
153  }
154 
161  public static function newFromID( $id ) {
162  $cat = new self();
163  $cat->mID = intval( $id );
164  return $cat;
165  }
166 
179  public static function newFromRow( $row, $title = null ) {
180  $cat = new self();
181  $cat->mTitle = $title;
182 
183  # NOTE: the row often results from a LEFT JOIN on categorylinks. This may result in
184  # all the cat_xxx fields being null, if the category page exists, but nothing
185  # was ever added to the category. This case should be treated link an empty
186  # category, if possible.
187 
188  if ( $row->cat_title === null ) {
189  if ( $title === null ) {
190  # the name is probably somewhere in the row, for example as page_title,
191  # but we can't know that here...
192  return false;
193  } else {
194  # if we have a title object, fetch the category name from there
195  $cat->mName = $title->getDBkey();
196  }
197 
198  $cat->mID = false;
199  $cat->mSubcats = 0;
200  $cat->mPages = 0;
201  $cat->mFiles = 0;
202  } else {
203  $cat->mName = $row->cat_title;
204  $cat->mID = $row->cat_id;
205  $cat->mSubcats = $row->cat_subcats;
206  $cat->mPages = $row->cat_pages;
207  $cat->mFiles = $row->cat_files;
208  }
209 
210  return $cat;
211  }
212 
216  public function getName() {
217  return $this->getX( 'mName' );
218  }
219 
223  public function getID() {
224  return $this->getX( 'mID' );
225  }
226 
230  public function getPageCount() {
231  return $this->getX( 'mPages' );
232  }
233 
237  public function getSubcatCount() {
238  return $this->getX( 'mSubcats' );
239  }
240 
244  public function getFileCount() {
245  return $this->getX( 'mFiles' );
246  }
247 
251  public function getTitle() {
252  if ( $this->mTitle ) {
253  return $this->mTitle;
254  }
255 
256  if ( !$this->initialize( self::LAZY_INIT_ROW ) ) {
257  return false;
258  }
259 
260  $this->mTitle = Title::makeTitleSafe( NS_CATEGORY, $this->mName );
261  return $this->mTitle;
262  }
263 
271  public function getMembers( $limit = false, $offset = '' ) {
272  $dbr = wfGetDB( DB_REPLICA );
273 
274  $conds = [ 'cl_to' => $this->getName(), 'cl_from = page_id' ];
275  $options = [ 'ORDER BY' => 'cl_sortkey' ];
276 
277  if ( $limit ) {
278  $options['LIMIT'] = $limit;
279  }
280 
281  if ( $offset !== '' ) {
282  $conds[] = 'cl_sortkey > ' . $dbr->addQuotes( $offset );
283  }
284 
286  $dbr->select(
287  [ 'page', 'categorylinks' ],
288  [ 'page_id', 'page_namespace', 'page_title', 'page_len',
289  'page_is_redirect', 'page_latest' ],
290  $conds,
291  __METHOD__,
292  $options
293  )
294  );
295 
296  return $result;
297  }
298 
304  private function getX( $key ) {
305  if ( $this->{$key} === null && !$this->initialize( self::LAZY_INIT_ROW ) ) {
306  return false;
307  }
308  return $this->{$key};
309  }
310 
316  public function refreshCounts() {
317  if ( wfReadOnly() ) {
318  return false;
319  }
320 
321  # If we have just a category name, find out whether there is an
322  # existing row. Or if we have just an ID, get the name, because
323  # that's what categorylinks uses.
324  if ( !$this->initialize( self::LOAD_ONLY ) ) {
325  return false;
326  }
327 
328  $dbw = wfGetDB( DB_MASTER );
329  # Avoid excess contention on the same category (T162121)
330  $name = __METHOD__ . ':' . md5( $this->mName );
331  $scopedLock = $dbw->getScopedLockAndFlush( $name, __METHOD__, 0 );
332  if ( !$scopedLock ) {
333  return false;
334  }
335 
336  $dbw->startAtomic( __METHOD__ );
337 
338  // Lock the `category` row before locking `categorylinks` rows to try
339  // to avoid deadlocks with LinksDeletionUpdate (T195397)
340  $dbw->lockForUpdate( 'category', [ 'cat_title' => $this->mName ], __METHOD__ );
341 
342  // Lock all the `categorylinks` records and gaps for this category;
343  // this is a separate query due to postgres/oracle limitations
344  $dbw->selectRowCount(
345  [ 'categorylinks', 'page' ],
346  '*',
347  [ 'cl_to' => $this->mName, 'page_id = cl_from' ],
348  __METHOD__,
349  [ 'LOCK IN SHARE MODE' ]
350  );
351  // Get the aggregate `categorylinks` row counts for this category
352  $catCond = $dbw->conditional( [ 'page_namespace' => NS_CATEGORY ], 1, 'NULL' );
353  $fileCond = $dbw->conditional( [ 'page_namespace' => NS_FILE ], 1, 'NULL' );
354  $result = $dbw->selectRow(
355  [ 'categorylinks', 'page' ],
356  [
357  'pages' => 'COUNT(*)',
358  'subcats' => "COUNT($catCond)",
359  'files' => "COUNT($fileCond)"
360  ],
361  [ 'cl_to' => $this->mName, 'page_id = cl_from' ],
362  __METHOD__
363  );
364 
365  $shouldExist = $result->pages > 0 || $this->getTitle()->exists();
366 
367  if ( $this->mID ) {
368  if ( $shouldExist ) {
369  # The category row already exists, so do a plain UPDATE instead
370  # of INSERT...ON DUPLICATE KEY UPDATE to avoid creating a gap
371  # in the cat_id sequence. The row may or may not be "affected".
372  $dbw->update(
373  'category',
374  [
375  'cat_pages' => $result->pages,
376  'cat_subcats' => $result->subcats,
377  'cat_files' => $result->files
378  ],
379  [ 'cat_title' => $this->mName ],
380  __METHOD__
381  );
382  } else {
383  # The category is empty and has no description page, delete it
384  $dbw->delete(
385  'category',
386  [ 'cat_title' => $this->mName ],
387  __METHOD__
388  );
389  $this->mID = false;
390  }
391  } elseif ( $shouldExist ) {
392  # The category row doesn't exist but should, so create it. Use
393  # upsert in case of races.
394  $dbw->upsert(
395  'category',
396  [
397  'cat_title' => $this->mName,
398  'cat_pages' => $result->pages,
399  'cat_subcats' => $result->subcats,
400  'cat_files' => $result->files
401  ],
402  [ 'cat_title' ],
403  [
404  'cat_pages' => $result->pages,
405  'cat_subcats' => $result->subcats,
406  'cat_files' => $result->files
407  ],
408  __METHOD__
409  );
410  // @todo: Should we update $this->mID here? Or not since Category
411  // objects tend to be short lived enough to not matter?
412  }
413 
414  $dbw->endAtomic( __METHOD__ );
415 
416  # Now we should update our local counts.
417  $this->mPages = $result->pages;
418  $this->mSubcats = $result->subcats;
419  $this->mFiles = $result->files;
420 
421  return true;
422  }
423 
435  public function refreshCountsIfEmpty() {
436  $dbw = wfGetDB( DB_MASTER );
437 
438  $hasLink = $dbw->selectField(
439  'categorylinks',
440  '1',
441  [ 'cl_to' => $this->getName() ],
442  __METHOD__
443  );
444  if ( !$hasLink ) {
445  $this->refreshCounts(); // delete any category table entry
446 
447  return true;
448  }
449 
450  $hasBadRow = $dbw->selectField(
451  'category',
452  '1',
453  [ 'cat_title' => $this->getName(), 'cat_pages <= 0' ],
454  __METHOD__
455  );
456  if ( $hasBadRow ) {
457  $this->refreshCounts(); // clean up this row
458 
459  return true;
460  }
461 
462  return false;
463  }
464 }
Category\getTitle
getTitle()
Definition: Category.php:251
Category\refreshCountsIfEmpty
refreshCountsIfEmpty()
Call refreshCounts() if there are no entries in the categorylinks table or if the category table has ...
Definition: Category.php:435
Category\getID
getID()
Definition: Category.php:223
TitleArray\newFromResult
static newFromResult( $res)
Definition: TitleArray.php:40
Category\$mName
$mName
Name of the category, normalized to DB-key form.
Definition: Category.php:33
$result
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message. Please note the header message cannot receive/use parameters. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item. Return false to stop further processing of the tag $reader:XMLReader object $logInfo:Array of information 'ImportHandlePageXMLTag':When parsing a XML tag in a page. Return false to stop further processing of the tag $reader:XMLReader object & $pageInfo:Array of information 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision. Return false to stop further processing of the tag $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information 'ImportHandleToplevelXMLTag':When parsing a top level XML tag. Return false to stop further processing of the tag $reader:XMLReader object 'ImportHandleUnknownUser':When a user doesn 't exist locally, this hook is called to give extensions an opportunity to auto-create it. If the auto-creation is successful, return false. $name:User name 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload. Return false to stop further processing of the tag $reader:XMLReader object $revisionInfo:Array of information 'ImportLogInterwikiLink':Hook to change the interwiki link used in log entries and edit summaries for transwiki imports. & $fullInterwikiPrefix:Interwiki prefix, may contain colons. & $pageTitle:String that contains page title. 'ImportSources':Called when reading from the $wgImportSources configuration variable. Can be used to lazy-load the import sources list. & $importSources:The value of $wgImportSources. Modify as necessary. See the comment in DefaultSettings.php for the detail of how to structure this array. 'InfoAction':When building information to display on the action=info page. $context:IContextSource object & $pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect. & $title:Title object for the current page & $request:WebRequest & $ignoreRedirect:boolean to skip redirect check & $target:Title/string of redirect target & $article:Article object 'InternalParseBeforeLinks':during Parser 's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InternalParseBeforeSanitize':during Parser 's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings. Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not. Return true without providing an interwiki to continue interwiki search. $prefix:interwiki prefix we are looking for. & $iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InvalidateEmailComplete':Called after a user 's email has been invalidated successfully. $user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification. Callee may modify $url and $query, URL will be constructed as $url . $query & $url:URL to index.php & $query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) & $article:article(object) being checked 'IsTrustedProxy':Override the result of IP::isTrustedProxy() & $ip:IP being check & $result:Change this value to override the result of IP::isTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from & $allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of Sanitizer::validateEmail(), for instance to return false if the domain name doesn 't match your organization. $addr:The e-mail address entered by the user & $result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user & $result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we 're looking for a messages file for & $file:The messages file path, you can override this to change the location. 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces. Do not use this hook to add namespaces. Use CanonicalNamespaces for that. & $namespaces:Array of namespaces indexed by their numbers 'LanguageGetTranslatedLanguageNames':Provide translated language names. & $names:array of language code=> language name $code:language of the preferred translations 'LanguageLinks':Manipulate a page 's language links. This is called in various places to allow extensions to define the effective language links for a page. $title:The page 's Title. & $links:Array with elements of the form "language:title" in the order that they will be output. & $linkFlags:Associative array mapping prefixed links to arrays of flags. Currently unused, but planned to provide support for marking individual language links in the UI, e.g. for featured articles. 'LanguageSelector':Hook to change the language selector available on a page. $out:The output page. $cssClassName:CSS class name of the language selector. 'LinkBegin':DEPRECATED since 1.28! Use HtmlPageLinkRendererBegin instead. Used when generating internal and interwiki links in Linker::link(), before processing starts. Return false to skip default processing and return $ret. See documentation for Linker::link() for details on the expected meanings of parameters. $skin:the Skin object $target:the Title that the link is pointing to & $html:the contents that the< a > tag should have(raw HTML) $result
Definition: hooks.txt:1983
Category
Category objects are immutable, strictly speaking.
Definition: Category.php:31
NS_FILE
const NS_FILE
Definition: Defines.php:70
wfReadOnly
wfReadOnly()
Check whether the wiki is in read-only mode.
Definition: GlobalFunctions.php:1197
Category\LAZY_INIT_ROW
const LAZY_INIT_ROW
Definition: Category.php:44
Category\initialize
initialize( $mode=self::LOAD_ONLY)
Set up all member variables using a database query.
Definition: Category.php:55
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
$dbr
$dbr
Definition: testCompression.php:50
MWException
MediaWiki exception.
Definition: MWException.php:26
Category\getX
getX( $key)
Generic accessor.
Definition: Category.php:304
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
Category\$mFiles
$mFiles
Definition: Category.php:41
Category\$mID
$mID
Definition: Category.php:34
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2636
Category\refreshCounts
refreshCounts()
Refresh the counts for this category.
Definition: Category.php:316
Category\getName
getName()
Definition: Category.php:216
Category\getSubcatCount
getSubcatCount()
Definition: Category.php:237
Category\getMembers
getMembers( $limit=false, $offset='')
Fetch a TitleArray of up to $limit category members, beginning after the category sort key $offset.
Definition: Category.php:271
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:78
DB_MASTER
const DB_MASTER
Definition: defines.php:26
Category\$mSubcats
$mSubcats
Definition: Category.php:41
Category\newFromRow
static newFromRow( $row, $title=null)
Factory function, for constructing a Category object from a result set.
Definition: Category.php:179
Category\newFromTitle
static newFromTitle( $title)
Factory function.
Definition: Category.php:146
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:604
Category\getPageCount
getPageCount()
Definition: Category.php:230
Category\LOAD_ONLY
const LOAD_ONLY
Definition: Category.php:43
Category\newFromID
static newFromID( $id)
Factory function.
Definition: Category.php:161
Title
Represents a title within MediaWiki.
Definition: Title.php:40
Category\getFileCount
getFileCount()
Definition: Category.php:244
$options
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 & $options
Definition: hooks.txt:1985
Category\$mTitle
Title $mTitle
Category page title.
Definition: Category.php:39
Category\__construct
__construct()
Definition: Category.php:46
Category\newFromName
static newFromName( $name)
Factory function.
Definition: Category.php:126
DeferredUpdates\addCallableUpdate
static addCallableUpdate( $callable, $stage=self::POSTSEND, $dbw=null)
Add a callable update.
Definition: DeferredUpdates.php:118
Category\$mPages
$mPages
Counts of membership (cat_pages, cat_subcats, cat_files)
Definition: Category.php:41