Conditions | 53 |
Paths | > 20000 |
Total Lines | 486 |
Code Lines | 279 |
Lines | 16 |
Ratio | 3.29 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
197 | protected function pageInfo() { |
||
198 | global $wgContLang; |
||
199 | |||
200 | $user = $this->getUser(); |
||
201 | $lang = $this->getLanguage(); |
||
202 | $title = $this->getTitle(); |
||
203 | $id = $title->getArticleID(); |
||
204 | $config = $this->context->getConfig(); |
||
205 | $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer(); |
||
206 | |||
207 | $pageCounts = $this->pageCounts( $this->page ); |
||
208 | |||
209 | $pageProperties = []; |
||
210 | $props = PageProps::getInstance()->getAllProperties( $title ); |
||
211 | if ( isset( $props[$id] ) ) { |
||
212 | $pageProperties = $props[$id]; |
||
213 | } |
||
214 | |||
215 | // Basic information |
||
216 | $pageInfo = []; |
||
217 | $pageInfo['header-basic'] = []; |
||
218 | |||
219 | // Display title |
||
220 | $displayTitle = $title->getPrefixedText(); |
||
221 | if ( isset( $pageProperties['displaytitle'] ) ) { |
||
222 | $displayTitle = $pageProperties['displaytitle']; |
||
223 | } |
||
224 | |||
225 | $pageInfo['header-basic'][] = [ |
||
226 | $this->msg( 'pageinfo-display-title' ), $displayTitle |
||
227 | ]; |
||
228 | |||
229 | // Is it a redirect? If so, where to? |
||
230 | if ( $title->isRedirect() ) { |
||
231 | $pageInfo['header-basic'][] = [ |
||
232 | $this->msg( 'pageinfo-redirectsto' ), |
||
233 | Linker::link( $this->page->getRedirectTarget() ) . |
||
234 | $this->msg( 'word-separator' )->escaped() . |
||
235 | $this->msg( 'parentheses' )->rawParams( Linker::link( |
||
236 | $this->page->getRedirectTarget(), |
||
237 | $this->msg( 'pageinfo-redirectsto-info' )->escaped(), |
||
238 | [], |
||
239 | [ 'action' => 'info' ] |
||
240 | ) )->escaped() |
||
241 | ]; |
||
242 | } |
||
243 | |||
244 | // Default sort key |
||
245 | $sortKey = $title->getCategorySortkey(); |
||
246 | if ( isset( $pageProperties['defaultsort'] ) ) { |
||
247 | $sortKey = $pageProperties['defaultsort']; |
||
248 | } |
||
249 | |||
250 | $sortKey = htmlspecialchars( $sortKey ); |
||
251 | $pageInfo['header-basic'][] = [ $this->msg( 'pageinfo-default-sort' ), $sortKey ]; |
||
252 | |||
253 | // Page length (in bytes) |
||
254 | $pageInfo['header-basic'][] = [ |
||
255 | $this->msg( 'pageinfo-length' ), $lang->formatNum( $title->getLength() ) |
||
256 | ]; |
||
257 | |||
258 | // Page ID (number not localised, as it's a database ID) |
||
259 | $pageInfo['header-basic'][] = [ $this->msg( 'pageinfo-article-id' ), $id ]; |
||
260 | |||
261 | // Language in which the page content is (supposed to be) written |
||
262 | $pageLang = $title->getPageLanguage()->getCode(); |
||
263 | |||
264 | if ( $config->get( 'PageLanguageUseDB' ) |
||
265 | && $this->getTitle()->userCan( 'pagelang', $this->getUser() ) |
||
266 | ) { |
||
267 | // Link to Special:PageLanguage with pre-filled page title if user has permissions |
||
268 | $titleObj = SpecialPage::getTitleFor( 'PageLanguage', $title->getPrefixedText() ); |
||
269 | $langDisp = Linker::link( |
||
270 | $titleObj, |
||
271 | $this->msg( 'pageinfo-language' )->escaped() |
||
272 | ); |
||
273 | } else { |
||
274 | // Display just the message |
||
275 | $langDisp = $this->msg( 'pageinfo-language' )->escaped(); |
||
276 | } |
||
277 | |||
278 | $pageInfo['header-basic'][] = [ $langDisp, |
||
279 | Language::fetchLanguageName( $pageLang, $lang->getCode() ) |
||
280 | . ' ' . $this->msg( 'parentheses', $pageLang )->escaped() ]; |
||
281 | |||
282 | // Content model of the page |
||
283 | $modelHtml = htmlspecialchars( ContentHandler::getLocalizedName( $title->getContentModel() ) ); |
||
284 | // If the user can change it, add a link to Special:ChangeContentModel |
||
285 | if ( $title->quickUserCan( 'editcontentmodel' ) ) { |
||
286 | $modelHtml .= ' ' . $this->msg( 'parentheses' )->rawParams( $linkRenderer->makeLink( |
||
287 | SpecialPage::getTitleValueFor( 'ChangeContentModel', $title->getPrefixedText() ), |
||
288 | $this->msg( 'pageinfo-content-model-change' )->text() |
||
289 | ) )->escaped(); |
||
290 | } |
||
291 | |||
292 | $pageInfo['header-basic'][] = [ |
||
293 | $this->msg( 'pageinfo-content-model' ), |
||
294 | $modelHtml |
||
295 | ]; |
||
296 | |||
297 | if ( $title->inNamespace( NS_USER ) ) { |
||
298 | $pageUser = User::newFromName( $title->getRootText() ); |
||
299 | if ( $pageUser && $pageUser->getId() && !$pageUser->isHidden() ) { |
||
300 | $pageInfo['header-basic'][] = [ |
||
301 | $this->msg( 'pageinfo-user-id' ), |
||
302 | $pageUser->getId() |
||
303 | ]; |
||
304 | } |
||
305 | } |
||
306 | |||
307 | // Search engine status |
||
308 | $pOutput = new ParserOutput(); |
||
309 | if ( isset( $pageProperties['noindex'] ) ) { |
||
310 | $pOutput->setIndexPolicy( 'noindex' ); |
||
311 | } |
||
312 | if ( isset( $pageProperties['index'] ) ) { |
||
313 | $pOutput->setIndexPolicy( 'index' ); |
||
314 | } |
||
315 | |||
316 | // Use robot policy logic |
||
317 | $policy = $this->page->getRobotPolicy( 'view', $pOutput ); |
||
318 | $pageInfo['header-basic'][] = [ |
||
319 | // Messages: pageinfo-robot-index, pageinfo-robot-noindex |
||
320 | $this->msg( 'pageinfo-robot-policy' ), |
||
321 | $this->msg( "pageinfo-robot-${policy['index']}" ) |
||
322 | ]; |
||
323 | |||
324 | $unwatchedPageThreshold = $config->get( 'UnwatchedPageThreshold' ); |
||
325 | if ( |
||
326 | $user->isAllowed( 'unwatchedpages' ) || |
||
327 | ( $unwatchedPageThreshold !== false && |
||
328 | $pageCounts['watchers'] >= $unwatchedPageThreshold ) |
||
329 | ) { |
||
330 | // Number of page watchers |
||
331 | $pageInfo['header-basic'][] = [ |
||
332 | $this->msg( 'pageinfo-watchers' ), |
||
333 | $lang->formatNum( $pageCounts['watchers'] ) |
||
334 | ]; |
||
335 | if ( |
||
336 | $config->get( 'ShowUpdatedMarker' ) && |
||
337 | isset( $pageCounts['visitingWatchers'] ) |
||
338 | ) { |
||
339 | $minToDisclose = $config->get( 'UnwatchedPageSecret' ); |
||
340 | if ( $pageCounts['visitingWatchers'] > $minToDisclose || |
||
341 | $user->isAllowed( 'unwatchedpages' ) ) { |
||
342 | $pageInfo['header-basic'][] = [ |
||
343 | $this->msg( 'pageinfo-visiting-watchers' ), |
||
344 | $lang->formatNum( $pageCounts['visitingWatchers'] ) |
||
345 | ]; |
||
346 | } else { |
||
347 | $pageInfo['header-basic'][] = [ |
||
348 | $this->msg( 'pageinfo-visiting-watchers' ), |
||
349 | $this->msg( 'pageinfo-few-visiting-watchers' ) |
||
350 | ]; |
||
351 | } |
||
352 | } |
||
353 | } elseif ( $unwatchedPageThreshold !== false ) { |
||
354 | $pageInfo['header-basic'][] = [ |
||
355 | $this->msg( 'pageinfo-watchers' ), |
||
356 | $this->msg( 'pageinfo-few-watchers' )->numParams( $unwatchedPageThreshold ) |
||
357 | ]; |
||
358 | } |
||
359 | |||
360 | // Redirects to this page |
||
361 | $whatLinksHere = SpecialPage::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() ); |
||
362 | $pageInfo['header-basic'][] = [ |
||
363 | Linker::link( |
||
364 | $whatLinksHere, |
||
365 | $this->msg( 'pageinfo-redirects-name' )->escaped(), |
||
366 | [], |
||
367 | [ |
||
368 | 'hidelinks' => 1, |
||
369 | 'hidetrans' => 1, |
||
370 | 'hideimages' => $title->getNamespace() == NS_FILE |
||
371 | ] |
||
372 | ), |
||
373 | $this->msg( 'pageinfo-redirects-value' ) |
||
374 | ->numParams( count( $title->getRedirectsHere() ) ) |
||
375 | ]; |
||
376 | |||
377 | // Is it counted as a content page? |
||
378 | if ( $this->page->isCountable() ) { |
||
379 | $pageInfo['header-basic'][] = [ |
||
380 | $this->msg( 'pageinfo-contentpage' ), |
||
381 | $this->msg( 'pageinfo-contentpage-yes' ) |
||
382 | ]; |
||
383 | } |
||
384 | |||
385 | // Subpages of this page, if subpages are enabled for the current NS |
||
386 | if ( MWNamespace::hasSubpages( $title->getNamespace() ) ) { |
||
387 | $prefixIndex = SpecialPage::getTitleFor( |
||
388 | 'Prefixindex', $title->getPrefixedText() . '/' ); |
||
389 | $pageInfo['header-basic'][] = [ |
||
390 | Linker::link( $prefixIndex, $this->msg( 'pageinfo-subpages-name' )->escaped() ), |
||
391 | $this->msg( 'pageinfo-subpages-value' ) |
||
392 | ->numParams( |
||
393 | $pageCounts['subpages']['total'], |
||
394 | $pageCounts['subpages']['redirects'], |
||
395 | $pageCounts['subpages']['nonredirects'] ) |
||
396 | ]; |
||
397 | } |
||
398 | |||
399 | if ( $title->inNamespace( NS_CATEGORY ) ) { |
||
400 | $category = Category::newFromTitle( $title ); |
||
401 | |||
402 | // $allCount is the total number of cat members, |
||
403 | // not the count of how many members are normal pages. |
||
404 | $allCount = (int)$category->getPageCount(); |
||
405 | $subcatCount = (int)$category->getSubcatCount(); |
||
406 | $fileCount = (int)$category->getFileCount(); |
||
407 | $pagesCount = $allCount - $subcatCount - $fileCount; |
||
408 | |||
409 | $pageInfo['category-info'] = [ |
||
410 | [ |
||
411 | $this->msg( 'pageinfo-category-total' ), |
||
412 | $lang->formatNum( $allCount ) |
||
413 | ], |
||
414 | [ |
||
415 | $this->msg( 'pageinfo-category-pages' ), |
||
416 | $lang->formatNum( $pagesCount ) |
||
417 | ], |
||
418 | [ |
||
419 | $this->msg( 'pageinfo-category-subcats' ), |
||
420 | $lang->formatNum( $subcatCount ) |
||
421 | ], |
||
422 | [ |
||
423 | $this->msg( 'pageinfo-category-files' ), |
||
424 | $lang->formatNum( $fileCount ) |
||
425 | ] |
||
426 | ]; |
||
427 | } |
||
428 | |||
429 | // Page protection |
||
430 | $pageInfo['header-restrictions'] = []; |
||
431 | |||
432 | // Is this page affected by the cascading protection of something which includes it? |
||
433 | if ( $title->isCascadeProtected() ) { |
||
434 | $cascadingFrom = ''; |
||
435 | $sources = $title->getCascadeProtectionSources()[0]; |
||
436 | |||
437 | foreach ( $sources as $sourceTitle ) { |
||
438 | $cascadingFrom .= Html::rawElement( |
||
439 | 'li', [], Linker::linkKnown( $sourceTitle ) ); |
||
440 | } |
||
441 | |||
442 | $cascadingFrom = Html::rawElement( 'ul', [], $cascadingFrom ); |
||
443 | $pageInfo['header-restrictions'][] = [ |
||
444 | $this->msg( 'pageinfo-protect-cascading-from' ), |
||
445 | $cascadingFrom |
||
446 | ]; |
||
447 | } |
||
448 | |||
449 | // Is out protection set to cascade to other pages? |
||
450 | if ( $title->areRestrictionsCascading() ) { |
||
451 | $pageInfo['header-restrictions'][] = [ |
||
452 | $this->msg( 'pageinfo-protect-cascading' ), |
||
453 | $this->msg( 'pageinfo-protect-cascading-yes' ) |
||
454 | ]; |
||
455 | } |
||
456 | |||
457 | // Page protection |
||
458 | foreach ( $title->getRestrictionTypes() as $restrictionType ) { |
||
459 | $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) ); |
||
460 | |||
461 | if ( $protectionLevel == '' ) { |
||
462 | // Allow all users |
||
463 | $message = $this->msg( 'protect-default' )->escaped(); |
||
464 | } else { |
||
465 | // Administrators only |
||
466 | // Messages: protect-level-autoconfirmed, protect-level-sysop |
||
467 | $message = $this->msg( "protect-level-$protectionLevel" ); |
||
468 | if ( $message->isDisabled() ) { |
||
469 | // Require "$1" permission |
||
470 | $message = $this->msg( "protect-fallback", $protectionLevel )->parse(); |
||
471 | } else { |
||
472 | $message = $message->escaped(); |
||
473 | } |
||
474 | } |
||
475 | $expiry = $title->getRestrictionExpiry( $restrictionType ); |
||
476 | $formattedexpiry = $this->msg( 'parentheses', |
||
477 | $this->getLanguage()->formatExpiry( $expiry ) )->escaped(); |
||
478 | $message .= $this->msg( 'word-separator' )->escaped() . $formattedexpiry; |
||
479 | |||
480 | // Messages: restriction-edit, restriction-move, restriction-create, |
||
481 | // restriction-upload |
||
482 | $pageInfo['header-restrictions'][] = [ |
||
483 | $this->msg( "restriction-$restrictionType" ), $message |
||
484 | ]; |
||
485 | } |
||
486 | |||
487 | if ( !$this->page->exists() ) { |
||
488 | return $pageInfo; |
||
489 | } |
||
490 | |||
491 | // Edit history |
||
492 | $pageInfo['header-edits'] = []; |
||
493 | |||
494 | $firstRev = $this->page->getOldestRevision(); |
||
495 | $lastRev = $this->page->getRevision(); |
||
496 | $batch = new LinkBatch; |
||
497 | |||
498 | View Code Duplication | if ( $firstRev ) { |
|
499 | $firstRevUser = $firstRev->getUserText( Revision::FOR_THIS_USER ); |
||
500 | if ( $firstRevUser !== '' ) { |
||
501 | $firstRevUserTitle = Title::makeTitle( NS_USER, $firstRevUser ); |
||
502 | $batch->addObj( $firstRevUserTitle ); |
||
503 | $batch->addObj( $firstRevUserTitle->getTalkPage() ); |
||
504 | } |
||
505 | } |
||
506 | |||
507 | View Code Duplication | if ( $lastRev ) { |
|
508 | $lastRevUser = $lastRev->getUserText( Revision::FOR_THIS_USER ); |
||
509 | if ( $lastRevUser !== '' ) { |
||
510 | $lastRevUserTitle = Title::makeTitle( NS_USER, $lastRevUser ); |
||
511 | $batch->addObj( $lastRevUserTitle ); |
||
512 | $batch->addObj( $lastRevUserTitle->getTalkPage() ); |
||
513 | } |
||
514 | } |
||
515 | |||
516 | $batch->execute(); |
||
517 | |||
518 | if ( $firstRev ) { |
||
519 | // Page creator |
||
520 | $pageInfo['header-edits'][] = [ |
||
521 | $this->msg( 'pageinfo-firstuser' ), |
||
522 | Linker::revUserTools( $firstRev ) |
||
523 | ]; |
||
524 | |||
525 | // Date of page creation |
||
526 | $pageInfo['header-edits'][] = [ |
||
527 | $this->msg( 'pageinfo-firsttime' ), |
||
528 | Linker::linkKnown( |
||
529 | $title, |
||
530 | htmlspecialchars( $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ) ), |
||
531 | [], |
||
532 | [ 'oldid' => $firstRev->getId() ] |
||
533 | ) |
||
534 | ]; |
||
535 | } |
||
536 | |||
537 | if ( $lastRev ) { |
||
538 | // Latest editor |
||
539 | $pageInfo['header-edits'][] = [ |
||
540 | $this->msg( 'pageinfo-lastuser' ), |
||
541 | Linker::revUserTools( $lastRev ) |
||
542 | ]; |
||
543 | |||
544 | // Date of latest edit |
||
545 | $pageInfo['header-edits'][] = [ |
||
546 | $this->msg( 'pageinfo-lasttime' ), |
||
547 | Linker::linkKnown( |
||
548 | $title, |
||
549 | htmlspecialchars( |
||
550 | $lang->userTimeAndDate( $this->page->getTimestamp(), $user ) |
||
551 | ), |
||
552 | [], |
||
553 | [ 'oldid' => $this->page->getLatest() ] |
||
554 | ) |
||
555 | ]; |
||
556 | } |
||
557 | |||
558 | // Total number of edits |
||
559 | $pageInfo['header-edits'][] = [ |
||
560 | $this->msg( 'pageinfo-edits' ), $lang->formatNum( $pageCounts['edits'] ) |
||
561 | ]; |
||
562 | |||
563 | // Total number of distinct authors |
||
564 | if ( $pageCounts['authors'] > 0 ) { |
||
565 | $pageInfo['header-edits'][] = [ |
||
566 | $this->msg( 'pageinfo-authors' ), $lang->formatNum( $pageCounts['authors'] ) |
||
567 | ]; |
||
568 | } |
||
569 | |||
570 | // Recent number of edits (within past 30 days) |
||
571 | $pageInfo['header-edits'][] = [ |
||
572 | $this->msg( 'pageinfo-recent-edits', |
||
573 | $lang->formatDuration( $config->get( 'RCMaxAge' ) ) ), |
||
574 | $lang->formatNum( $pageCounts['recent_edits'] ) |
||
575 | ]; |
||
576 | |||
577 | // Recent number of distinct authors |
||
578 | $pageInfo['header-edits'][] = [ |
||
579 | $this->msg( 'pageinfo-recent-authors' ), |
||
580 | $lang->formatNum( $pageCounts['recent_authors'] ) |
||
581 | ]; |
||
582 | |||
583 | // Array of MagicWord objects |
||
584 | $magicWords = MagicWord::getDoubleUnderscoreArray(); |
||
585 | |||
586 | // Array of magic word IDs |
||
587 | $wordIDs = $magicWords->names; |
||
588 | |||
589 | // Array of IDs => localized magic words |
||
590 | $localizedWords = $wgContLang->getMagicWords(); |
||
591 | |||
592 | $listItems = []; |
||
593 | foreach ( $pageProperties as $property => $value ) { |
||
594 | if ( in_array( $property, $wordIDs ) ) { |
||
595 | $listItems[] = Html::element( 'li', [], $localizedWords[$property][1] ); |
||
596 | } |
||
597 | } |
||
598 | |||
599 | $localizedList = Html::rawElement( 'ul', [], implode( '', $listItems ) ); |
||
600 | $hiddenCategories = $this->page->getHiddenCategories(); |
||
601 | |||
602 | if ( |
||
603 | count( $listItems ) > 0 || |
||
604 | count( $hiddenCategories ) > 0 || |
||
605 | $pageCounts['transclusion']['from'] > 0 || |
||
606 | $pageCounts['transclusion']['to'] > 0 |
||
607 | ) { |
||
608 | $options = [ 'LIMIT' => $config->get( 'PageInfoTransclusionLimit' ) ]; |
||
609 | $transcludedTemplates = $title->getTemplateLinksFrom( $options ); |
||
610 | if ( $config->get( 'MiserMode' ) ) { |
||
611 | $transcludedTargets = []; |
||
612 | } else { |
||
613 | $transcludedTargets = $title->getTemplateLinksTo( $options ); |
||
614 | } |
||
615 | |||
616 | // Page properties |
||
617 | $pageInfo['header-properties'] = []; |
||
618 | |||
619 | // Magic words |
||
620 | if ( count( $listItems ) > 0 ) { |
||
621 | $pageInfo['header-properties'][] = [ |
||
622 | $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) ), |
||
623 | $localizedList |
||
624 | ]; |
||
625 | } |
||
626 | |||
627 | // Hidden categories |
||
628 | if ( count( $hiddenCategories ) > 0 ) { |
||
629 | $pageInfo['header-properties'][] = [ |
||
630 | $this->msg( 'pageinfo-hidden-categories' ) |
||
631 | ->numParams( count( $hiddenCategories ) ), |
||
632 | Linker::formatHiddenCategories( $hiddenCategories ) |
||
633 | ]; |
||
634 | } |
||
635 | |||
636 | // Transcluded templates |
||
637 | if ( $pageCounts['transclusion']['from'] > 0 ) { |
||
638 | if ( $pageCounts['transclusion']['from'] > count( $transcludedTemplates ) ) { |
||
639 | $more = $this->msg( 'morenotlisted' )->escaped(); |
||
640 | } else { |
||
641 | $more = null; |
||
642 | } |
||
643 | |||
644 | $templateListFormatter = new TemplatesOnThisPageFormatter( |
||
645 | $this->getContext(), |
||
646 | $linkRenderer |
||
647 | ); |
||
648 | |||
649 | $pageInfo['header-properties'][] = [ |
||
650 | $this->msg( 'pageinfo-templates' ) |
||
651 | ->numParams( $pageCounts['transclusion']['from'] ), |
||
652 | $templateListFormatter->format( $transcludedTemplates, false, $more ) |
||
653 | ]; |
||
654 | } |
||
655 | |||
656 | if ( !$config->get( 'MiserMode' ) && $pageCounts['transclusion']['to'] > 0 ) { |
||
657 | if ( $pageCounts['transclusion']['to'] > count( $transcludedTargets ) ) { |
||
658 | $more = Linker::link( |
||
659 | $whatLinksHere, |
||
660 | $this->msg( 'moredotdotdot' )->escaped(), |
||
661 | [], |
||
662 | [ 'hidelinks' => 1, 'hideredirs' => 1 ] |
||
663 | ); |
||
664 | } else { |
||
665 | $more = null; |
||
666 | } |
||
667 | |||
668 | $templateListFormatter = new TemplatesOnThisPageFormatter( |
||
669 | $this->getContext(), |
||
670 | $linkRenderer |
||
671 | ); |
||
672 | |||
673 | $pageInfo['header-properties'][] = [ |
||
674 | $this->msg( 'pageinfo-transclusions' ) |
||
675 | ->numParams( $pageCounts['transclusion']['to'] ), |
||
676 | $templateListFormatter->format( $transcludedTargets, false, $more ) |
||
677 | ]; |
||
678 | } |
||
679 | } |
||
680 | |||
681 | return $pageInfo; |
||
682 | } |
||
683 | |||
913 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: