LinkTitles extension for MediaWiki
Automatically add links to existing pages.
Targets.php
1 <?php
24 namespace LinkTitles;
25 
29 class Targets {
30  private static $instance;
31 
43  public static function singleton( \Title $title, Config $config ) {
44  if ( ( self::$instance === null ) || ( self::$instance->sourceNamespace != $title->getNamespace() ) ) {
45  self::$instance = new Targets( $title, $config );
46  }
47  return self::$instance;
48  }
49 
57  public static function invalidate() {
58  self::$instance = null;
59  }
60 
66  public $queryResult;
67 
74 
75  private $config;
76 
82 
87  private function __construct( \Title $title, Config $config) {
88  $this->config = $config;
89  $this->sourceNamespace = $title->getNamespace();
90  $this->fetch();
91  }
92 
93  //
97  private function fetch() {
98  ( $this->config->preferShortTitles ) ? $sortOrder = 'ASC' : $sortOrder = 'DESC';
99 
100  // Build a blacklist of pages that are not supposed to be link
101  // targets. This includes the current page.
102  if ( $this->config->blackList ) {
103  $blackList = 'page_title NOT IN ' .
104  str_replace( ' ', '_', '("' . implode( '","', str_replace( '"', '\"', $this->config->blackList ) ) . '")' );
105  } else {
106  $blackList = null;
107  }
108 
109  if ( $this->config->sameNamespace ) {
110  // Build our weight list. Make sure current namespace is first element
111  $namespaces = array_diff( $this->config->targetNamespaces, [ $this->sourceNamespace ] );
112  array_unshift( $namespaces, $this->sourceNamespace );
113  } else {
114  $namespaces = $this->config->targetNamespaces;
115  }
116 
117  if ( !$namespaces) {
118  // If there are absolutely no target namespaces (not even the one of the
119  // source page), we can just return.
120  return;
121  }
122 
123  // No need for sanitiy check. we are sure that we have at least one element in the array
124  $weightSelect = "CASE page_namespace ";
125  $currentWeight = 0;
126  foreach ($namespaces as &$namespaceValue) {
127  $currentWeight = $currentWeight + 100;
128  $weightSelect = $weightSelect . " WHEN " . $namespaceValue . " THEN " . $currentWeight . PHP_EOL;
129  }
130  $weightSelect = $weightSelect . " END ";
131  $namespacesClause = '(' . implode( ', ', $namespaces ) . ')';
132 
133  // Build an SQL query and fetch all page titles ordered by length from
134  // shortest to longest. Only titles from 'normal' pages (namespace uid
135  // = 0) are returned. Since the db may be sqlite, we need a try..catch
136  // structure because sqlite does not support the CHAR_LENGTH function.
137  $dbr = wfGetDB( DB_SLAVE );
138  $this->queryResult = $dbr->select(
139  'page',
140  array( 'page_title', 'page_namespace' , "weight" => $weightSelect),
141  array_filter(
142  array(
143  'page_namespace IN ' . $namespacesClause,
144  $this->charLength() . '(page_title) >= ' . $this->config->minimumTitleLength,
145  $blackList,
146  )
147  ),
148  __METHOD__,
149  array( 'ORDER BY' => 'weight ASC, ' . $this->charLength() . '(page_title) ' . $sortOrder )
150  );
151  }
152 
153  private function charLength() {
154  if ($this->charLengthFunction === null) {
155  $this->charLengthFunction = $this->config->sqliteDatabase() ? 'LENGTH' : 'CHAR_LENGTH';
156  }
157  return $this->charLengthFunction;
158  }
159 }
fetch()
Fetches the page titles from the database.
Definition: Targets.php:97
$charLengthFunction
Stores the CHAR_LENGTH function to be used with the database connection.
Definition: Targets.php:81
Fetches potential target page titles from the database.
Definition: Targets.php:29
static singleton(\Title $title, Config $config)
Singleton factory that returns a (cached) database query results with potential target page titles...
Definition: Targets.php:43
static invalidate()
Invalidates the cache; the next call of Targets::singleton() will trigger a database query...
Definition: Targets.php:57
__construct(\Title $title, Config $config)
The constructor is private to enforce using the singleton pattern.
Definition: Targets.php:87
$queryResult
Holds the results of a database query for target page titles, filtered and sorted.
Definition: Targets.php:66
$sourceNamespace
Holds the source page&#39;s namespace (integer) for which the list of target pages was built...
Definition: Targets.php:73
Holds LinkTitles configuration.
Definition: Config.php:37
The LinkTitles class holds configuration for the LinkTitles extension.
Definition: Config.php:23