MediaWiki  1.34.0
TrackingCategories.php
Go to the documentation of this file.
1 <?php
23 
31  private $config;
32 
38  private static $coreTrackingCategories = [
39  'index-category',
40  'noindex-category',
41  'duplicate-args-category',
42  'expensive-parserfunction-category',
43  'post-expand-template-argument-category',
44  'post-expand-template-inclusion-category',
45  'hidden-category-category',
46  'broken-file-category',
47  'node-count-exceeded-category',
48  'expansion-depth-exceeded-category',
49  'restricted-displaytitle-ignored',
50  'deprecated-self-close-category',
51  'template-loop-category',
52  ];
53 
57  public function __construct( Config $config ) {
58  $this->config = $config;
59  }
60 
65  public function getTrackingCategories() {
66  $categories = array_merge(
67  self::$coreTrackingCategories,
68  ExtensionRegistry::getInstance()->getAttribute( 'TrackingCategories' ),
69  $this->config->get( 'TrackingCategories' ) // deprecated
70  );
71 
72  // Only show magic link tracking categories if they are enabled
73  $enableMagicLinks = $this->config->get( 'EnableMagicLinks' );
74  if ( $enableMagicLinks['ISBN'] ) {
75  $categories[] = 'magiclink-tracking-isbn';
76  }
77  if ( $enableMagicLinks['RFC'] ) {
78  $categories[] = 'magiclink-tracking-rfc';
79  }
80  if ( $enableMagicLinks['PMID'] ) {
81  $categories[] = 'magiclink-tracking-pmid';
82  }
83 
84  $trackingCategories = [];
85  $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
86  foreach ( $categories as $catMsg ) {
87  /*
88  * Check if the tracking category varies by namespace
89  * Otherwise only pages in the current namespace will be displayed
90  * If it does vary, show pages considering all namespaces
91  */
92  $msgObj = wfMessage( $catMsg )->inContentLanguage();
93  $allCats = [];
94  $catMsgTitle = Title::makeTitleSafe( NS_MEDIAWIKI, $catMsg );
95  if ( !$catMsgTitle ) {
96  continue;
97  }
98 
99  // Match things like {{NAMESPACE}} and {{NAMESPACENUMBER}}.
100  // False positives are ok, this is just an efficiency shortcut
101  if ( strpos( $msgObj->plain(), '{{' ) !== false ) {
102  $ns = $nsInfo->getValidNamespaces();
103  foreach ( $ns as $namesp ) {
104  $tempTitle = Title::makeTitleSafe( $namesp, $catMsg );
105  if ( !$tempTitle ) {
106  continue;
107  }
108  $catName = $msgObj->title( $tempTitle )->text();
109  # Allow tracking categories to be disabled by setting them to "-"
110  if ( $catName !== '-' ) {
111  $catTitle = Title::makeTitleSafe( NS_CATEGORY, $catName );
112  if ( $catTitle ) {
113  $allCats[] = $catTitle;
114  }
115  }
116  }
117  } else {
118  $catName = $msgObj->text();
119  # Allow tracking categories to be disabled by setting them to "-"
120  if ( $catName !== '-' ) {
121  $catTitle = Title::makeTitleSafe( NS_CATEGORY, $catName );
122  if ( $catTitle ) {
123  $allCats[] = $catTitle;
124  }
125  }
126  }
127  $trackingCategories[$catMsg] = [
128  'cats' => $allCats,
129  'msg' => $catMsgTitle,
130  ];
131  }
132 
133  return $trackingCategories;
134  }
135 }
TrackingCategories\$coreTrackingCategories
static array $coreTrackingCategories
Tracking categories that exist in core.
Definition: TrackingCategories.php:38
TrackingCategories\getTrackingCategories
getTrackingCategories()
Read the global and extract title objects from the corresponding messages.
Definition: TrackingCategories.php:65
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
TrackingCategories\$config
Config $config
Definition: TrackingCategories.php:31
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1264
ExtensionRegistry\getInstance
static getInstance()
Definition: ExtensionRegistry.php:106
Config
Interface for configuration instances.
Definition: Config.php:28
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:74
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:613
TrackingCategories
This class performs some operations related to tracking categories, such as creating a list of all su...
Definition: TrackingCategories.php:29
TrackingCategories\__construct
__construct(Config $config)
Definition: TrackingCategories.php:57
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition: Defines.php:68