| Total Complexity | 46 |
| Total Lines | 460 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Edit 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 Edit, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Edit extends Model |
||
| 17 | { |
||
| 18 | /** @var int ID of the revision */ |
||
| 19 | protected $id; |
||
| 20 | |||
| 21 | /** @var DateTime Timestamp of the revision */ |
||
| 22 | protected $timestamp; |
||
| 23 | |||
| 24 | /** @var bool Whether or not this edit was a minor edit */ |
||
| 25 | protected $minor; |
||
| 26 | |||
| 27 | /** @var int|string|null Length of the page as of this edit, in bytes */ |
||
| 28 | protected $length; |
||
| 29 | |||
| 30 | /** @var int|string|null The diff size of this edit */ |
||
| 31 | protected $lengthChange; |
||
| 32 | |||
| 33 | /** @var User - User object of who made the edit */ |
||
| 34 | protected $user; |
||
| 35 | |||
| 36 | /** @var string The edit summary */ |
||
| 37 | protected $comment; |
||
| 38 | |||
| 39 | /** @var string The SHA-1 of the wikitext as of the revision. */ |
||
| 40 | protected $sha; |
||
| 41 | |||
| 42 | /** @var bool Whether this edit was later reverted. */ |
||
| 43 | protected $reverted; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Edit constructor. |
||
| 47 | * @param Page $page |
||
| 48 | * @param string[] $attrs Attributes, as retrieved by PageRepository::getRevisions() |
||
| 49 | */ |
||
| 50 | public function __construct(Page $page, array $attrs = []) |
||
| 51 | { |
||
| 52 | $this->page = $page; |
||
| 53 | |||
| 54 | // Copy over supported attributes |
||
| 55 | $this->id = isset($attrs['id']) ? (int)$attrs['id'] : (int)$attrs['rev_id']; |
||
| 56 | |||
| 57 | // Allow DateTime or string (latter assumed to be of format YmdHis) |
||
| 58 | if ($attrs['timestamp'] instanceof DateTime) { |
||
| 59 | $this->timestamp = $attrs['timestamp']; |
||
| 60 | } else { |
||
| 61 | $this->timestamp = DateTime::createFromFormat('YmdHis', $attrs['timestamp']); |
||
| 62 | } |
||
| 63 | |||
| 64 | $this->minor = '1' === $attrs['minor']; |
||
| 65 | $this->length = (int)$attrs['length']; |
||
| 66 | $this->lengthChange = (int)$attrs['length_change']; |
||
| 67 | $this->user = $attrs['user'] ?? ($attrs['username'] ? new User($attrs['username']) : null); |
||
| 68 | $this->comment = $attrs['comment']; |
||
| 69 | |||
| 70 | if (isset($attrs['rev_sha1']) || isset($attrs['sha'])) { |
||
| 71 | $this->sha = $attrs['rev_sha1'] ?? $attrs['sha']; |
||
| 72 | } |
||
| 73 | |||
| 74 | // This can be passed in to save as a property on the Edit instance. |
||
| 75 | // Note that the Edit class knows nothing about it's value, and |
||
| 76 | // is not capable of detecting whether the given edit was reverted. |
||
| 77 | $this->reverted = isset($attrs['reverted']) ? (bool)$attrs['reverted'] : null; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Get Edits given revision rows (JOINed on the page table). |
||
| 82 | * @param Project $project |
||
| 83 | * @param User $user |
||
| 84 | * @param array $revs Each must contain 'page_title' and 'page_namespace'. |
||
| 85 | * @return Edit[] |
||
| 86 | */ |
||
| 87 | public static function getEditsFromRevs(Project $project, User $user, array $revs): array |
||
| 88 | { |
||
| 89 | return array_map(function ($rev) use ($project, $user) { |
||
| 90 | /** @var Page $page Page object to be passed to the Edit constructor. */ |
||
| 91 | $page = Page::newFromRow($project, $rev); |
||
| 92 | $rev['user'] = $user; |
||
| 93 | |||
| 94 | return new self($page, $rev); |
||
| 95 | }, $revs); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Unique identifier for this Edit, to be used in cache keys. |
||
| 100 | * @see Repository::getCacheKey() |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | public function getCacheKey(): string |
||
| 104 | { |
||
| 105 | return (string)$this->id; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * ID of the edit. |
||
| 110 | * @return int |
||
| 111 | */ |
||
| 112 | public function getId(): int |
||
| 113 | { |
||
| 114 | return $this->id; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Get the edit's timestamp. |
||
| 119 | * @return DateTime |
||
| 120 | */ |
||
| 121 | public function getTimestamp(): DateTime |
||
| 122 | { |
||
| 123 | return $this->timestamp; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Get the edit's timestamp as a UTC string, as with YYYY-MM-DDTHH:MM:SS |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | public function getUTCTimestamp(): string |
||
| 131 | { |
||
| 132 | return $this->getTimestamp()->format('Y-m-d\TH:i:s'); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Year the revision was made. |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | public function getYear(): string |
||
| 140 | { |
||
| 141 | return $this->timestamp->format('Y'); |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Get the numeric representation of the month the revision was made, with leading zeros. |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | public function getMonth(): string |
||
| 149 | { |
||
| 150 | return $this->timestamp->format('m'); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Whether or not this edit was a minor edit. |
||
| 155 | * @return bool |
||
| 156 | */ |
||
| 157 | public function getMinor(): bool |
||
| 158 | { |
||
| 159 | return $this->minor; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Alias of getMinor() |
||
| 164 | * @return bool Whether or not this edit was a minor edit |
||
| 165 | */ |
||
| 166 | public function isMinor(): bool |
||
| 167 | { |
||
| 168 | return $this->getMinor(); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Length of the page as of this edit, in bytes. |
||
| 173 | * @see Edit::getSize() Edit::getSize() for the size <em>change</em>. |
||
| 174 | * @return int |
||
| 175 | */ |
||
| 176 | public function getLength(): int |
||
| 177 | { |
||
| 178 | return $this->length; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * The diff size of this edit. |
||
| 183 | * @return int Signed length change in bytes. |
||
| 184 | */ |
||
| 185 | public function getSize(): int |
||
| 186 | { |
||
| 187 | return $this->lengthChange; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Alias of getSize() |
||
| 192 | * @return int The diff size of this edit |
||
| 193 | */ |
||
| 194 | public function getLengthChange(): int |
||
| 195 | { |
||
| 196 | return $this->getSize(); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get the user who made the edit. |
||
| 201 | * @return User|null null can happen for instance if the username was suppressed. |
||
| 202 | */ |
||
| 203 | public function getUser(): ?User |
||
| 204 | { |
||
| 205 | return $this->user; |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Set the User. |
||
| 210 | * @param User $user |
||
| 211 | */ |
||
| 212 | public function setUser(User $user): void |
||
| 213 | { |
||
| 214 | $this->user = $user; |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Get the edit summary. |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | public function getComment(): string |
||
| 222 | { |
||
| 223 | return (string)$this->comment; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Get the edit summary (alias of Edit::getComment()). |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | public function getSummary(): string |
||
| 231 | { |
||
| 232 | return $this->getComment(); |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Get the SHA-1 of the revision. |
||
| 237 | * @return string|null |
||
| 238 | */ |
||
| 239 | public function getSha(): ?string |
||
| 240 | { |
||
| 241 | return $this->sha; |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Was this edit reported as having been reverted? |
||
| 246 | * The value for this is merely passed in from precomputed data. |
||
| 247 | * @return bool|null |
||
| 248 | */ |
||
| 249 | public function isReverted(): ?bool |
||
| 250 | { |
||
| 251 | return $this->reverted; |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Set the reverted property. |
||
| 256 | * @param bool $revert |
||
| 257 | */ |
||
| 258 | public function setReverted(bool $revert): void |
||
| 259 | { |
||
| 260 | $this->reverted = $revert; |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Get edit summary as 'wikified' HTML markup |
||
| 265 | * @param bool $useUnnormalizedPageTitle Use the unnormalized page title to avoid |
||
| 266 | * an API call. This should be used only if you fetched the page title via other |
||
| 267 | * means (SQL query), and is not from user input alone. |
||
| 268 | * @return string Safe HTML |
||
| 269 | */ |
||
| 270 | public function getWikifiedComment(bool $useUnnormalizedPageTitle = false): string |
||
| 271 | { |
||
| 272 | return self::wikifyString( |
||
| 273 | $this->getSummary(), |
||
| 274 | $this->getProject(), |
||
| 275 | $this->page, |
||
| 276 | $useUnnormalizedPageTitle |
||
| 277 | ); |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Public static method to wikify a summary, can be used on any arbitrary string. |
||
| 282 | * Does NOT support section links unless you specify a page. |
||
| 283 | * @param string $summary |
||
| 284 | * @param Project $project |
||
| 285 | * @param Page $page |
||
| 286 | * @param bool $useUnnormalizedPageTitle Use the unnormalized page title to avoid |
||
| 287 | * an API call. This should be used only if you fetched the page title via other |
||
| 288 | * means (SQL query), and is not from user input alone. |
||
| 289 | * @static |
||
| 290 | * @return string |
||
| 291 | */ |
||
| 292 | public static function wikifyString( |
||
| 293 | string $summary, |
||
| 294 | Project $project, |
||
| 295 | ?Page $page = null, |
||
| 296 | bool $useUnnormalizedPageTitle = false |
||
| 297 | ): string { |
||
| 298 | $summary = htmlspecialchars(html_entity_decode($summary), ENT_NOQUOTES); |
||
| 299 | |||
| 300 | // First link raw URLs. Courtesy of https://stackoverflow.com/a/11641499/604142 |
||
| 301 | $summary = preg_replace( |
||
| 302 | '%\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))%s', |
||
| 303 | '<a target="_blank" href="$1">$1</a>', |
||
| 304 | $summary |
||
| 305 | ); |
||
| 306 | |||
| 307 | $sectionMatch = null; |
||
| 308 | $isSection = preg_match_all("/^\/\* (.*?) \*\//", $summary, $sectionMatch); |
||
| 309 | |||
| 310 | if ($isSection && isset($page)) { |
||
|
|
|||
| 311 | $pageUrl = $project->getUrl(false) . str_replace( |
||
| 312 | '$1', |
||
| 313 | $page->getTitle($useUnnormalizedPageTitle), |
||
| 314 | $project->getArticlePath() |
||
| 315 | ); |
||
| 316 | $sectionTitle = $sectionMatch[1][0]; |
||
| 317 | |||
| 318 | // Must have underscores for the link to properly go to the section. |
||
| 319 | $sectionTitleLink = htmlspecialchars(str_replace(' ', '_', $sectionTitle)); |
||
| 320 | |||
| 321 | $sectionWikitext = "<a target='_blank' href='$pageUrl#$sectionTitleLink'>→</a>" . |
||
| 322 | "<em class='text-muted'>" . htmlspecialchars($sectionTitle) . ":</em> "; |
||
| 323 | $summary = str_replace($sectionMatch[0][0], $sectionWikitext, $summary); |
||
| 324 | } |
||
| 325 | |||
| 326 | $linkMatch = null; |
||
| 327 | |||
| 328 | while (preg_match_all("/\[\[:?(.*?)]]/", $summary, $linkMatch)) { |
||
| 329 | $wikiLinkParts = explode('|', $linkMatch[1][0]); |
||
| 330 | $wikiLinkPath = htmlspecialchars($wikiLinkParts[0]); |
||
| 331 | $wikiLinkText = htmlspecialchars( |
||
| 332 | $wikiLinkParts[1] ?? $wikiLinkPath |
||
| 333 | ); |
||
| 334 | |||
| 335 | // Use normalized page title (underscored, capitalized). |
||
| 336 | $pageUrl = $project->getUrl(false) . str_replace( |
||
| 337 | '$1', |
||
| 338 | ucfirst(str_replace(' ', '_', $wikiLinkPath)), |
||
| 339 | $project->getArticlePath() |
||
| 340 | ); |
||
| 341 | |||
| 342 | $link = "<a target='_blank' href='$pageUrl'>$wikiLinkText</a>"; |
||
| 343 | $summary = str_replace($linkMatch[0][0], $link, $summary); |
||
| 344 | } |
||
| 345 | |||
| 346 | return $summary; |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Get edit summary as 'wikified' HTML markup (alias of Edit::getWikifiedSummary()). |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | public function getWikifiedSummary(): string |
||
| 354 | { |
||
| 355 | return $this->getWikifiedComment(); |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Get the project this edit was made on |
||
| 360 | * @return Project |
||
| 361 | */ |
||
| 362 | public function getProject(): Project |
||
| 363 | { |
||
| 364 | return $this->getPage()->getProject(); |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Get the full URL to the diff of the edit |
||
| 369 | * @return string |
||
| 370 | */ |
||
| 371 | public function getDiffUrl(): string |
||
| 372 | { |
||
| 373 | $project = $this->getProject(); |
||
| 374 | $path = str_replace('$1', 'Special:Diff/' . $this->id, $project->getArticlePath()); |
||
| 375 | return rtrim($project->getUrl(), '/') . $path; |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Get the full permanent URL to the page at the time of the edit |
||
| 380 | * @return string |
||
| 381 | */ |
||
| 382 | public function getPermaUrl(): string |
||
| 383 | { |
||
| 384 | $project = $this->getProject(); |
||
| 385 | $path = str_replace('$1', 'Special:PermaLink/' . $this->id, $project->getArticlePath()); |
||
| 386 | return rtrim($project->getUrl(), '/') . $path; |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Was the edit a revert, based on the edit summary? |
||
| 391 | * @param ContainerInterface $container The DI container. |
||
| 392 | * @return bool |
||
| 393 | */ |
||
| 394 | public function isRevert(ContainerInterface $container): bool |
||
| 395 | { |
||
| 396 | $automatedEditsHelper = $container->get('app.automated_edits_helper'); |
||
| 397 | return $automatedEditsHelper->isRevert($this->comment, $this->getProject()); |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Get the name of the tool that was used to make this edit. |
||
| 402 | * @param ContainerInterface $container The DI container. |
||
| 403 | * @return array|false The name of the tool that was used to make the edit |
||
| 404 | */ |
||
| 405 | public function getTool(ContainerInterface $container) |
||
| 406 | { |
||
| 407 | $automatedEditsHelper = $container->get('app.automated_edits_helper'); |
||
| 408 | return $automatedEditsHelper->getTool((string)$this->comment, $this->getProject()); |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Was the edit (semi-)automated, based on the edit summary? |
||
| 413 | * @param ContainerInterface $container |
||
| 414 | * @return bool |
||
| 415 | */ |
||
| 416 | public function isAutomated(ContainerInterface $container): bool |
||
| 417 | { |
||
| 418 | return (bool)$this->getTool($container); |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Was the edit made by a logged out user? |
||
| 423 | * @return bool|null |
||
| 424 | */ |
||
| 425 | public function isAnon(): ?bool |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Get HTML for the diff of this Edit. |
||
| 432 | * @return string|null Raw HTML, must be wrapped in a <table> tag. Null if no comparison could be made. |
||
| 433 | */ |
||
| 434 | public function getDiffHtml(): ?string |
||
| 435 | { |
||
| 436 | return $this->getRepository()->getDiffHtml($this); |
||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Formats the data as an array for use in JSON APIs. |
||
| 441 | * @param bool $includeUsername False for most tools such as Global Contribs, AutoEdits, etc. |
||
| 442 | * @return array |
||
| 443 | * @internal This method assumes the Edit was constructed with data already filled in from a database query. |
||
| 444 | */ |
||
| 445 | public function getForJson(bool $includeUsername = false, bool $includeProject = false): array |
||
| 446 | { |
||
| 447 | $nsId = $this->getPage()->getNamespace(); |
||
| 448 | $pageTitle = $this->getPage()->getTitle(true); |
||
| 449 | |||
| 450 | if ($nsId > 0) { |
||
| 451 | $nsName = $this->getProject()->getNamespaces()[$nsId]; |
||
| 452 | $pageTitle = preg_replace("/^$nsName:/", '', $pageTitle); |
||
| 453 | } |
||
| 454 | |||
| 455 | $ret = [ |
||
| 456 | 'page_title' => $pageTitle, |
||
| 457 | 'page_namespace' => $nsId, |
||
| 458 | 'rev_id' => $this->id, |
||
| 478 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: