| Total Complexity | 42 |
| Total Lines | 358 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Project often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Project, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Project extends Model |
||
| 11 | { |
||
| 12 | protected PageAssessments $pageAssessments; |
||
| 13 | |||
| 14 | /** @var string The project name as supplied by the user. */ |
||
| 15 | protected string $nameUnnormalized; |
||
| 16 | |||
| 17 | /** @var string[]|null Basic metadata about the project */ |
||
| 18 | protected ?array $metadata; |
||
| 19 | |||
| 20 | /** @var string[]|null Project's 'dbName', 'url' and 'lang'. */ |
||
| 21 | protected ?array $basicInfo; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Whether the user being queried for in this session has opted in to restricted statistics. |
||
| 25 | * @var bool |
||
| 26 | */ |
||
| 27 | protected bool $userOptedIn; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Create a new Project. |
||
| 31 | * @param string $nameOrUrl The project's database name or URL. |
||
| 32 | */ |
||
| 33 | public function __construct(string $nameOrUrl) |
||
| 34 | { |
||
| 35 | $this->nameUnnormalized = $nameOrUrl; |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Get the associated PageAssessments model. |
||
| 40 | * @return PageAssessments |
||
| 41 | */ |
||
| 42 | public function getPageAssessments(): PageAssessments |
||
| 43 | { |
||
| 44 | return $this->pageAssessments; |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param PageAssessments $pageAssessments |
||
| 49 | * @return Project |
||
| 50 | */ |
||
| 51 | public function setPageAssessments(PageAssessments $pageAssessments): Project |
||
| 52 | { |
||
| 53 | $this->pageAssessments = $pageAssessments; |
||
| 54 | return $this; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Whether or not this project supports page assessments, or if they exist for the given namespace. |
||
| 59 | * @param int|string|null $nsId Namespace ID, null if checking if project has page assessments for any namespace. |
||
| 60 | * @return bool |
||
| 61 | */ |
||
| 62 | public function hasPageAssessments($nsId = null): bool |
||
| 63 | { |
||
| 64 | if (null !== $nsId && (int)$nsId > 0) { |
||
| 65 | return $this->pageAssessments->isSupportedNamespace((int)$nsId); |
||
| 66 | } else { |
||
| 67 | return $this->pageAssessments->isEnabled(); |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Unique identifier this Project, to be used in cache keys. |
||
| 73 | * @see Repository::getCacheKey() |
||
| 74 | * @return string |
||
| 75 | */ |
||
| 76 | public function getCacheKey(): string |
||
| 77 | { |
||
| 78 | return $this->getDatabaseName(); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Get 'dbName', 'url' and 'lang' of the project, the relevant basic info we can get from the meta database. |
||
| 83 | * This is all you need to make database queries. More comprehensive metadata can be fetched with getMetadata() |
||
| 84 | * at the expense of an API call, which may be cached. |
||
| 85 | * @return string[]|null null if not found. |
||
| 86 | */ |
||
| 87 | protected function getBasicInfo(): ?array |
||
| 88 | { |
||
| 89 | if (!isset($this->basicInfo)) { |
||
| 90 | $this->basicInfo = $this->repository->getOne($this->nameUnnormalized); |
||
|
|
|||
| 91 | } |
||
| 92 | return $this->basicInfo; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get full metadata about the project. See ProjectRepository::getMetadata() for more information. |
||
| 97 | * @return array|null null if project not found. |
||
| 98 | */ |
||
| 99 | protected function getMetadata(): ?array |
||
| 100 | { |
||
| 101 | if (!isset($this->metadata)) { |
||
| 102 | $info = $this->getBasicInfo(); |
||
| 103 | if (!isset($info['url'])) { |
||
| 104 | // Project is probably not replicated. |
||
| 105 | return null; |
||
| 106 | } |
||
| 107 | $url = $this->getBasicInfo()['url']; |
||
| 108 | $this->metadata = $this->getRepository()->getMetadata($url); |
||
| 109 | } |
||
| 110 | return $this->metadata; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Does this project exist? |
||
| 115 | * @return bool |
||
| 116 | */ |
||
| 117 | public function exists(): bool |
||
| 118 | { |
||
| 119 | return !empty($this->getDomain()); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * The unique domain name of this project, without protocol or path components. |
||
| 124 | * This should be used as the canonical project identifier. |
||
| 125 | * @return string|null null if nonexistent. |
||
| 126 | */ |
||
| 127 | public function getDomain(): ?string |
||
| 128 | { |
||
| 129 | $url = $this->getBasicInfo()['url'] ?? ''; |
||
| 130 | return parse_url($url, PHP_URL_HOST); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * The name of the database for this project. |
||
| 135 | * @return string |
||
| 136 | */ |
||
| 137 | public function getDatabaseName(): string |
||
| 138 | { |
||
| 139 | return $this->getBasicInfo()['dbName'] ?? ''; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * The language for this project. |
||
| 144 | * @return string |
||
| 145 | */ |
||
| 146 | public function getLang(): string |
||
| 147 | { |
||
| 148 | return $this->getBasicInfo()['lang'] ?? ''; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * The project URL is the fully-qualified domain name, with protocol and trailing slash. |
||
| 153 | * @param bool $withTrailingSlash Whether to append a slash. |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | public function getUrl(bool $withTrailingSlash = true): string |
||
| 157 | { |
||
| 158 | return rtrim($this->getBasicInfo()['url'], '/') . ($withTrailingSlash ? '/' : ''); |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param Page|string $page Full page title including namespace, or a Page object. |
||
| 163 | * @param bool $useUnnormalizedPageTitle Use the unnormalized page title to avoid |
||
| 164 | * an API call. This should be used only if you fetched the page title via other |
||
| 165 | * means (SQL query), and is not from user input alone. Only applicable if $page |
||
| 166 | * is a Page object. |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | public function getUrlForPage($page, bool $useUnnormalizedPageTitle = false): string |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * The base URL path of this project (that page titles are appended to). |
||
| 179 | * For some wikis the title (apparently) may not be at the end. |
||
| 180 | * Replace $1 with the article name. |
||
| 181 | * @link https://www.mediawiki.org/wiki/Manual:$wgArticlePath |
||
| 182 | * @return string |
||
| 183 | */ |
||
| 184 | public function getArticlePath(): string |
||
| 185 | { |
||
| 186 | $metadata = $this->getMetadata(); |
||
| 187 | return $metadata['general']['articlePath'] ?? '/wiki/$1'; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * The URL path of the directory that contains index.php, with no trailing slash. |
||
| 192 | * Defaults to '/w' which is the same as the normal WMF set-up. |
||
| 193 | * @link https://www.mediawiki.org/wiki/Manual:$wgScriptPath |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | public function getScriptPath(): string |
||
| 197 | { |
||
| 198 | $metadata = $this->getMetadata(); |
||
| 199 | return $metadata['general']['scriptPath'] ?? '/w'; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * The URL path to index.php |
||
| 204 | * Defaults to '/w/index.php' which is the same as the normal WMF set-up. |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | public function getScript(): string |
||
| 208 | { |
||
| 209 | $metadata = $this->getMetadata(); |
||
| 210 | return $metadata['general']['script'] ?? $this->getScriptPath() . '/index.php'; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * The full URL to api.php. |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | public function getApiUrl(): string |
||
| 218 | { |
||
| 219 | return rtrim($this->getUrl(), '/') . $this->getRepository()->getApiPath(); |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Get the project's title, the human-language full title of the wiki (e.g. "English Wikipedia (en.wikipedia.org)"). |
||
| 224 | */ |
||
| 225 | public function getTitle(): string |
||
| 226 | { |
||
| 227 | $metadata = $this->getMetadata(); |
||
| 228 | return $metadata['general']['wikiName'].' ('.$this->getDomain().')'; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Get an array of this project's namespaces and their IDs. |
||
| 233 | * @return string[] Keys are IDs, values are names. |
||
| 234 | */ |
||
| 235 | public function getNamespaces(): array |
||
| 236 | { |
||
| 237 | $metadata = $this->getMetadata(); |
||
| 238 | return $metadata['namespaces']; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Get the title of the Main Page. |
||
| 243 | * @return string |
||
| 244 | */ |
||
| 245 | public function getMainPage(): string |
||
| 246 | { |
||
| 247 | $metadata = $this->getMetadata(); |
||
| 248 | return $metadata['general']['mainpage'] ?? ''; |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * List of extensions that are installed on the wiki. |
||
| 253 | * @return string[] |
||
| 254 | */ |
||
| 255 | public function getInstalledExtensions(): array |
||
| 256 | { |
||
| 257 | // Quick cache, valid only for the same request. |
||
| 258 | static $installedExtensions = null; |
||
| 259 | if (is_array($installedExtensions)) { |
||
| 260 | return $installedExtensions; |
||
| 261 | } |
||
| 262 | |||
| 263 | return $installedExtensions = $this->getRepository()->getInstalledExtensions($this); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Whether the project has temporary accounts enabled. |
||
| 268 | * @return bool |
||
| 269 | */ |
||
| 270 | public function hasTempAccounts(): bool |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Get the patterns that match temporary accounts. |
||
| 278 | * @return string[] |
||
| 279 | */ |
||
| 280 | public function getTempAccountPatterns(): array |
||
| 281 | { |
||
| 282 | $metadata = $this->getMetadata(); |
||
| 283 | return $metadata['tempAccountPatterns'] ?? []; |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Get a list of users who are in one of the given user groups. |
||
| 288 | * @param string[] $groups User groups to search for. |
||
| 289 | * @param string[] $globalGroups Global groups to search for. |
||
| 290 | * @return string[] User groups keyed by user name. |
||
| 291 | */ |
||
| 292 | public function getUsersInGroups(array $groups, array $globalGroups): array |
||
| 293 | { |
||
| 294 | $users = []; |
||
| 295 | $usersAndGroups = $this->getRepository()->getUsersInGroups($this, $groups, $globalGroups); |
||
| 296 | foreach ($usersAndGroups as $userAndGroup) { |
||
| 297 | $username = $userAndGroup['user_name']; |
||
| 298 | if (isset($users[$username])) { |
||
| 299 | $users[$username][] = $userAndGroup['user_group']; |
||
| 300 | } else { |
||
| 301 | $users[$username] = [$userAndGroup['user_group']]; |
||
| 302 | } |
||
| 303 | } |
||
| 304 | return $users; |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Get the name of the page on this project that the user must create in order to opt in for restricted statistics. |
||
| 309 | * @param User $user |
||
| 310 | * @return string |
||
| 311 | */ |
||
| 312 | public function userOptInPage(User $user): string |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Has a user opted in to having their restricted statistics displayed to anyone? |
||
| 319 | * @param User $user |
||
| 320 | * @return bool |
||
| 321 | */ |
||
| 322 | public function userHasOptedIn(User $user): bool |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Normalize and quote a table name for use in SQL. |
||
| 361 | * @param string $tableName |
||
| 362 | * @param string|null $tableExtension Optional table extension, which will only get used if we're on Labs. |
||
| 363 | * @return string Fully-qualified and quoted table name. |
||
| 364 | */ |
||
| 365 | public function getTableName(string $tableName, ?string $tableExtension = null): string |
||
| 368 | } |
||
| 369 | } |
||
| 370 |