MediaWiki  master
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 
66  public function getTrackingCategories() {
67  $categories = array_merge(
68  self::$coreTrackingCategories,
69  ExtensionRegistry::getInstance()->getAttribute( 'TrackingCategories' ),
70  $this->config->get( 'TrackingCategories' ) // deprecated
71  );
72 
73  // Only show magic link tracking categories if they are enabled
74  $enableMagicLinks = $this->config->get( 'EnableMagicLinks' );
75  if ( $enableMagicLinks['ISBN'] ) {
76  $categories[] = 'magiclink-tracking-isbn';
77  }
78  if ( $enableMagicLinks['RFC'] ) {
79  $categories[] = 'magiclink-tracking-rfc';
80  }
81  if ( $enableMagicLinks['PMID'] ) {
82  $categories[] = 'magiclink-tracking-pmid';
83  }
84 
85  $trackingCategories = [];
86  $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
87  foreach ( $categories as $catMsg ) {
88  /*
89  * Check if the tracking category varies by namespace
90  * Otherwise only pages in the current namespace will be displayed
91  * If it does vary, show pages considering all namespaces
92  */
93  $msgObj = wfMessage( $catMsg )->inContentLanguage();
94  $allCats = [];
95  $catMsgTitle = Title::makeTitleSafe( NS_MEDIAWIKI, $catMsg );
96  if ( !$catMsgTitle ) {
97  continue;
98  }
99 
100  // Match things like {{NAMESPACE}} and {{NAMESPACENUMBER}}.
101  // False positives are ok, this is just an efficiency shortcut
102  if ( strpos( $msgObj->plain(), '{{' ) !== false ) {
103  $ns = $nsInfo->getValidNamespaces();
104  foreach ( $ns as $namesp ) {
105  $tempTitle = Title::makeTitleSafe( $namesp, $catMsg );
106  if ( !$tempTitle ) {
107  continue;
108  }
109  $catName = $msgObj->title( $tempTitle )->text();
110  # Allow tracking categories to be disabled by setting them to "-"
111  if ( $catName !== '-' ) {
112  $catTitle = Title::makeTitleSafe( NS_CATEGORY, $catName );
113  if ( $catTitle ) {
114  $allCats[] = $catTitle;
115  }
116  }
117  }
118  } else {
119  $catName = $msgObj->text();
120  # Allow tracking categories to be disabled by setting them to "-"
121  if ( $catName !== '-' ) {
122  $catTitle = Title::makeTitleSafe( NS_CATEGORY, $catName );
123  if ( $catTitle ) {
124  $allCats[] = $catTitle;
125  }
126  }
127  }
128  $trackingCategories[$catMsg] = [
129  'cats' => $allCats,
130  'msg' => $catMsgTitle,
131  ];
132  }
133 
134  return $trackingCategories;
135  }
136 }
Interface for configuration instances.
Definition: Config.php:28
const NS_CATEGORY
Definition: Defines.php:74
static array $coreTrackingCategories
Tracking categories that exist in core.
__construct(Config $config)
const NS_MEDIAWIKI
Definition: Defines.php:68
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:612
getTrackingCategories()
Read the global and extract title objects from the corresponding messages.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
This class performs some operations related to tracking categories, such as creating a list of all su...