From ca7c098134b5a7663ce9fc2487a8bab0a3e6ec2b Mon Sep 17 00:00:00 2001 From: umherirrender Date: Thu, 13 Feb 2014 20:48:53 +0100 Subject: [PATCH] Use TablePager on Special:ProtectedPages (using log_search) Adding the pr_id to log_search table and use a join to get the user, timestamp and comment from the logging table. For logs without the pr_id a placeholder text is shown. The code for page protection is changed from a replace to delete/insert to have for each pr_id a log_id and not reusing pr_ids which was used for the first protection on a protection change. The log_deleted field is checked to avoid showing revision deleted data from the logging table. This is a alternative patch for Ie40057398a7829c698df3d68813006798a72dccb and Iade82653999ca770586762b3c1383f73dfe3a5ee to avoid a schema change on table page_restriction. Idea by Aaron Schulz. Change-Id: Ia0dc5895ec672896fe4b75c2e0f76da2474bd6e4 --- RELEASE-NOTES-1.23 | 2 + includes/Title.php | 15 +- includes/WikiPage.php | 29 +- includes/specials/SpecialProtectedpages.php | 325 ++++++++++++------ languages/messages/MessagesEn.php | 9 + languages/messages/MessagesQqq.php | 11 + maintenance/language/messageTypes.inc | 1 + maintenance/language/messages.inc | 9 + .../mediawiki.special/mediawiki.special.css | 12 + 9 files changed, 296 insertions(+), 117 deletions(-) diff --git a/RELEASE-NOTES-1.23 b/RELEASE-NOTES-1.23 index 660869c259..18de6867d6 100644 --- a/RELEASE-NOTES-1.23 +++ b/RELEASE-NOTES-1.23 @@ -34,6 +34,8 @@ production. * The 'max threads' setting was removed from $wgDBservers. * Support for AdminSettings.php has been completely removed. All configuration belongs in LocalSettings.php. +* Special:ProtectedPages shows now a table. The timestamp, the reason and + the protecting user is also shown. === New features in 1.23 === * ResourceLoader can utilize the Web Storage API to cache modules client-side. diff --git a/includes/Title.php b/includes/Title.php index dc9822a54c..ab79d6dd2b 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -3876,7 +3876,20 @@ class Title { $comment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $reason; } // @todo FIXME: $params? - $log->addEntry( 'move_prot', $nt, $comment, array( $this->getPrefixedText() ), $wgUser ); + $logId = $log->addEntry( 'move_prot', $nt, $comment, array( $this->getPrefixedText() ), $wgUser ); + + // reread inserted pr_ids for log relation + $insertedPrIds = $dbw->select( + 'page_restrictions', + 'pr_id', + array( 'pr_page' => $redirid ), + __METHOD__ + ); + $logRelationsValues = array(); + foreach ( $insertedPrIds as $prid ) { + $logRelationsValues[] = $prid->pr_id; + } + $log->addRelations( 'pr_id', $logRelationsValues, $logId ); } # Update watchlists diff --git a/includes/WikiPage.php b/includes/WikiPage.php index a191983d42..7e13136469 100644 --- a/includes/WikiPage.php +++ b/includes/WikiPage.php @@ -2350,6 +2350,9 @@ class WikiPage implements Page, IDBAccessObject { // Truncate for whole multibyte characters $reason = $wgContLang->truncate( $reason, 255 ); + $logRelationsValues = array(); + $logRelationsField = null; + if ( $id ) { // Protection of existing page if ( !wfRunHooks( 'ArticleProtect', array( &$this, &$user, $limit, $reason ) ) ) { return Status::newGood(); @@ -2384,11 +2387,24 @@ class WikiPage implements Page, IDBAccessObject { return Status::newFatal( 'no-null-revision', $this->mTitle->getPrefixedText() ); } + $logRelationsField = 'pr_id'; + // Update restrictions table foreach ( $limit as $action => $restrictions ) { + $dbw->delete( + 'page_restrictions', + array( + 'pr_page' => $id, + 'pr_type' => $action + ), + __METHOD__ + ); if ( $restrictions != '' ) { - $dbw->replace( 'page_restrictions', array( array( 'pr_page', 'pr_type' ) ), - array( 'pr_page' => $id, + $dbw->insert( + 'page_restrictions', + array( + 'pr_id' => $dbw->nextSequenceValue( 'page_restrictions_pr_id_seq' ), + 'pr_page' => $id, 'pr_type' => $action, 'pr_level' => $restrictions, 'pr_cascade' => ( $cascade && $action == 'edit' ) ? 1 : 0, @@ -2396,9 +2412,7 @@ class WikiPage implements Page, IDBAccessObject { ), __METHOD__ ); - } else { - $dbw->delete( 'page_restrictions', array( 'pr_page' => $id, - 'pr_type' => $action ), __METHOD__ ); + $logRelationsValues[] = $dbw->insertId(); } } @@ -2451,7 +2465,10 @@ class WikiPage implements Page, IDBAccessObject { // Update the protection log $log = new LogPage( 'protect' ); - $log->addEntry( $logAction, $this->mTitle, $reason, $params, $user ); + $logId = $log->addEntry( $logAction, $this->mTitle, $reason, $params, $user ); + if ( $logRelationsField !== null && count( $logRelationsValues ) ) { + $log->addRelations( $logRelationsField, $logRelationsValues, $logId ); + } return Status::newGood(); } diff --git a/includes/specials/SpecialProtectedpages.php b/includes/specials/SpecialProtectedpages.php index 09284d0a9a..e505ecb391 100644 --- a/includes/specials/SpecialProtectedpages.php +++ b/includes/specials/SpecialProtectedpages.php @@ -38,6 +38,7 @@ class SpecialProtectedpages extends SpecialPage { public function execute( $par ) { $this->setHeaders(); $this->outputHeader(); + $this->getOutput()->addModuleStyles( 'mediawiki.special' ); // Purge expired entries on one in every 10 queries if ( !mt_rand( 0, 10 ) ) { @@ -81,7 +82,7 @@ class SpecialProtectedpages extends SpecialPage { if ( $pager->getNumRows() ) { $this->getOutput()->addHTML( $pager->getNavigationBar() . - '' . + $pager->getBody() . $pager->getNavigationBar() ); } else { @@ -89,105 +90,6 @@ class SpecialProtectedpages extends SpecialPage { } } - /** - * Callback function to output a restriction - * @param Title $row Protected title - * @return string Formatted "
  • " element - */ - public function formatRow( $row ) { - wfProfileIn( __METHOD__ ); - - static $infinity = null; - - if ( is_null( $infinity ) ) { - $infinity = wfGetDB( DB_SLAVE )->getInfinity(); - } - - $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title ); - if ( !$title ) { - wfProfileOut( __METHOD__ ); - - return Html::rawElement( - 'li', - array(), - Html::element( - 'span', - array( 'class' => 'mw-invalidtitle' ), - Linker::getInvalidTitleDescription( - $this->getContext(), - $row->page_namespace, - $row->page_title - ) - ) - ) . "\n"; - } - - $link = Linker::link( $title ); - - $description_items = array(); - - // Messages: restriction-level-sysop, restriction-level-autoconfirmed - $protType = $this->msg( 'restriction-level-' . $row->pr_level )->escaped(); - - $description_items[] = $protType; - - if ( $row->pr_cascade ) { - $description_items[] = $this->msg( 'protect-summary-cascade' )->text(); - } - - $stxt = ''; - $lang = $this->getLanguage(); - - $expiry = $lang->formatExpiry( $row->pr_expiry, TS_MW ); - if ( $expiry != $infinity ) { - $user = $this->getUser(); - $description_items[] = $this->msg( - 'protect-expiring-local', - $lang->userTimeAndDate( $expiry, $user ), - $lang->userDate( $expiry, $user ), - $lang->userTime( $expiry, $user ) - )->escaped(); - } - - if ( !is_null( $size = $row->page_len ) ) { - $stxt = $lang->getDirMark() . ' ' . Linker::formatRevisionSize( $size ); - } - - // Show a link to the change protection form for allowed users otherwise - // a link to the protection log - if ( $this->getUser()->isAllowed( 'protect' ) ) { - $changeProtection = Linker::linkKnown( - $title, - $this->msg( 'protect_change' )->escaped(), - array(), - array( 'action' => 'unprotect' ) - ); - } else { - $ltitle = SpecialPage::getTitleFor( 'Log' ); - $changeProtection = Linker::linkKnown( - $ltitle, - $this->msg( 'protectlogpage' )->escaped(), - array(), - array( - 'type' => 'protect', - 'page' => $title->getPrefixedText() - ) - ); - } - - $changeProtection = ' ' . $this->msg( 'parentheses' )->rawParams( $changeProtection ) - ->escaped(); - - wfProfileOut( __METHOD__ ); - - return Html::rawElement( - 'li', - array(), - $lang->specialList( $link . $stxt, $lang->commaList( $description_items ), false ) . - $changeProtection - ) . "\n"; - } - /** * @param int $namespace * @param string $type Restriction type @@ -388,7 +290,7 @@ class SpecialProtectedpages extends SpecialPage { * @todo document * @ingroup Pager */ -class ProtectedPagesPager extends AlphabeticPager { +class ProtectedPagesPager extends TablePager { public $mForm, $mConds; private $type, $level, $namespace, $sizetype, $size, $indefonly, $cascadeonly, $noredirect; @@ -408,19 +310,185 @@ class ProtectedPagesPager extends AlphabeticPager { parent::__construct( $form->getContext() ); } - function getStartBody() { + function preprocessResults( $result ) { # Do a link batch query $lb = new LinkBatch; - foreach ( $this->mResult as $row ) { + $userids = array(); + + foreach ( $result as $row ) { $lb->add( $row->page_namespace, $row->page_title ); + // field is nullable, maybe null on old protections + if ( $row->log_user !== null ) { + $userids[] = $row->log_user; + } + } + + // fill LinkBatch with user page and user talk + if ( count( $userids ) ) { + $userCache = UserCache::singleton(); + $userCache->doQuery( $userids, array(), __METHOD__ ); + foreach ( $userids as $userid ) { + $name = $userCache->getProp( $userid, 'name' ); + if ( $name !== false ) { + $lb->add( NS_USER, $name ); + $lb->add( NS_USER_TALK, $name ); + } + } } + $lb->execute(); + } - return ''; + function getFieldNames() { + static $headers = null; + + if ( $headers == array() ) { + $headers = array( + 'log_timestamp' => 'protectedpages-timestamp', + 'pr_page' => 'protectedpages-page', + 'pr_expiry' => 'protectedpages-expiry', + 'log_user' => 'protectedpages-performer', + 'pr_params' => 'protectedpages-params', + 'log_comment' => 'protectedpages-reason', + ); + foreach ( $headers as $key => $val ) { + $headers[$key] = $this->msg( $val )->text(); + } + } + + return $headers; } - function formatRow( $row ) { - return $this->mForm->formatRow( $row ); + /** + * @param string $field + * @param string $value + * @return string + * @throws MWException + */ + function formatValue( $field, $value ) { + /** @var $row object */ + $row = $this->mCurrentRow; + + $formatted = ''; + + switch ( $field ) { + case 'log_timestamp': + // when timestamp is null, this is a old protection row + if ( $value === null ) { + $formatted = Html::rawElement( + 'span', + array( 'class' => 'mw-protectedpages-unknown' ), + $this->msg( 'protectedpages-unknown-timestamp' )->escaped() + ); + } else { + $formatted = $this->getLanguage()->userTimeAndDate( $value, $this->getUser() ); + } + break; + + case 'pr_page': + $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title ); + if ( !$title ) { + $formatted = Html::element( + 'span', + array( 'class' => 'mw-invalidtitle' ), + Linker::getInvalidTitleDescription( + $this->getContext(), + $row->page_namespace, + $row->page_title + ) + ); + } else { + $formatted = Linker::link( $title ); + } + if ( !is_null( $row->page_len ) ) { + $formatted .= $this->getLanguage()->getDirMark() . + ' ' . Html::rawElement( + 'span', + array( 'class' => 'mw-protectedpages-length' ), + Linker::formatRevisionSize( $row->page_len ) + ); + } + break; + + case 'pr_expiry': + $formatted = $this->getLanguage()->formatExpiry( $value, /* User preference timezone */true ); + $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title ); + if ( $this->getUser()->isAllowed( 'protect' ) && $title ) { + $changeProtection = Linker::linkKnown( + $title, + $this->msg( 'protect_change' )->escaped(), + array(), + array( 'action' => 'unprotect' ) + ); + $formatted .= ' ' . Html::rawElement( + 'span', + array( 'class' => 'mw-protectedpages-actions' ), + $this->msg( 'parentheses' )->rawParams( $changeProtection )->escaped() + ); + } + break; + + case 'log_user': + // when timestamp is null, this is a old protection row + if ( $row->log_timestamp === null ) { + $formatted = Html::rawElement( + 'span', + array( 'class' => 'mw-protectedpages-unknown' ), + $this->msg( 'protectedpages-unknown-performer' )->escaped() + ); + } else { + $username = UserCache::singleton()->getProp( $value, 'name' ); + if ( LogEventsList::userCanBitfield( $row->log_deleted, LogPage::DELETED_USER, $this->getUser() ) ) { + if ( $username === false ) { + $formatted = htmlspecialchars( $value ); + } else { + $formatted = Linker::userLink( $value, $username ) + . Linker::userToolLinks( $value, $username ); + } + } else { + $formatted = $this->msg( 'rev-deleted-user' )->escaped(); + } + if ( LogEventsList::isDeleted( $row, LogPage::DELETED_USER ) ) { + $formatted = '' . $formatted . ''; + } + } + break; + + case 'pr_params': + $params = array(); + // Messages: restriction-level-sysop, restriction-level-autoconfirmed + $params[] = $this->msg( 'restriction-level-' . $row->pr_level )->escaped(); + if ( $row->pr_cascade ) { + $params[] = $this->msg( 'protect-summary-cascade' )->text(); + } + $formatted = $this->getLanguage()->commaList( $params ); + break; + + case 'log_comment': + // when timestamp is null, this is a old protection row + if ( $row->log_timestamp === null ) { + $formatted = Html::rawElement( + 'span', + array( 'class' => 'mw-protectedpages-unknown' ), + $this->msg( 'protectedpages-unknown-reason' )->escaped() + ); + } else { + if ( LogEventsList::userCanBitfield( $row->log_deleted, LogPage::DELETED_COMMENT, $this->getUser() ) ) { + $formatted = Linker::formatComment( $value !== null ? $value : '' ); + } else { + $formatted = $this->msg( 'rev-deleted-comment' )->escaped(); + } + if ( LogEventsList::isDeleted( $row, LogPage::DELETED_COMMENT ) ) { + $formatted = '' . $formatted . ''; + } + } + break; + + default: + throw new MWException( "Unknown field '$field'" ); + } + + return $formatted; } function getQueryInfo() { @@ -455,14 +523,51 @@ class ProtectedPagesPager extends AlphabeticPager { } return array( - 'tables' => array( 'page_restrictions', 'page' ), - 'fields' => array( 'pr_id', 'page_namespace', 'page_title', 'page_len', - 'pr_type', 'pr_level', 'pr_expiry', 'pr_cascade' ), - 'conds' => $conds + 'tables' => array( 'page', 'page_restrictions', 'log_search', 'logging' ), + 'fields' => array( + 'pr_id', + 'page_namespace', + 'page_title', + 'page_len', + 'pr_type', + 'pr_level', + 'pr_expiry', + 'pr_cascade', + 'log_timestamp', + 'log_user', + 'log_comment', + 'log_deleted', + ), + 'conds' => $conds, + 'join_conds' => array( + 'log_search' => array( + 'LEFT JOIN', array( + 'ls_field' => 'pr_id', 'ls_value = pr_id' + ) + ), + 'logging' => array( + 'LEFT JOIN', array( + 'ls_log_id = log_id' + ) + ) + ) ); } + public function getTableClass() { + return 'TablePager mw-protectedpages'; + } + function getIndexField() { return 'pr_id'; } + + function getDefaultSort() { + return 'pr_id'; + } + + function isFieldSortable( $field ) { + // no index for sorting exists + return false; + } } diff --git a/languages/messages/MessagesEn.php b/languages/messages/MessagesEn.php index 88b729d75f..290cf63112 100644 --- a/languages/messages/MessagesEn.php +++ b/languages/messages/MessagesEn.php @@ -2767,6 +2767,15 @@ It now redirects to [[$2]].', 'protectedpages-cascade' => 'Cascading protections only', 'protectedpages-noredirect' => 'Hide redirects', 'protectedpagesempty' => 'No pages are currently protected with these parameters.', +'protectedpages-timestamp' => 'Timestamp', +'protectedpages-page' => 'Page', +'protectedpages-expiry' => 'Expires', +'protectedpages-performer' => 'Protecting user', +'protectedpages-params' => 'Protection parameters', +'protectedpages-reason' => 'Reason', +'protectedpages-unknown-timestamp' => 'Unknown', +'protectedpages-unknown-performer' => 'Unknown user', +'protectedpages-unknown-reason' => '—', # do not translate or duplicate this message to other languages 'protectedtitles' => 'Protected titles', 'protectedtitles-summary' => '', # do not translate or duplicate this message to other languages 'protectedtitlesempty' => 'No titles are currently protected with these parameters.', diff --git a/languages/messages/MessagesQqq.php b/languages/messages/MessagesQqq.php index 221d67e5b8..b1ddd206af 100644 --- a/languages/messages/MessagesQqq.php +++ b/languages/messages/MessagesQqq.php @@ -5013,6 +5013,17 @@ See the following search results: 'protectedpages-noredirect' => 'Option in [[Special:ProtectedPages]]. {{Identical|Hide redirect}}', 'protectedpagesempty' => 'Used in [[Special:ProtectedPages]], when there are no protected pages with the specified parameters.', +'protectedpages-timestamp' => 'This is a column header for dates and times in the table on the page [[Special:ProtectedPages]]. +{{Identical|Timestamp}}', +'protectedpages-page' => 'This is a column header in the table on the page [[Special:ProtectedPages]].', +'protectedpages-expiry' => 'This is a column header in the table on the page [[Special:ProtectedPages]]. +{{Identical|Expire}}', +'protectedpages-performer' => 'This is a column header in the table on the page [[Special:ProtectedPages]].', +'protectedpages-params' => 'This is a column header in the table on the page [[Special:ProtectedPages]].', +'protectedpages-reason' => 'This is a column header in the table on the page [[Special:ProtectedPages]]. +{{Identical|Reason}}', +'protectedpages-unknown-timestamp' => 'This is shown, when the date and time is unknown for a protection on the page [[Special:ProtectedPages]].', +'protectedpages-unknown-performer' => 'This is shown, when the protecting user is unknown for a protection on the page [[Special:ProtectedPages]].', 'protectedtitles' => '{{doc-special|ProtectedTitles}}', 'protectedtitlesempty' => 'Used on [[Special:ProtectedTitles]]. This text appears if the list of protected titles is empty. See the [[mw:Project:Protected_titles|help page on MediaWiki]] for more information.', 'listusers' => '{{doc-special|ListUsers}}', diff --git a/maintenance/language/messageTypes.inc b/maintenance/language/messageTypes.inc index 97a00a9bf2..1c398da5be 100644 --- a/maintenance/language/messageTypes.inc +++ b/maintenance/language/messageTypes.inc @@ -180,6 +180,7 @@ $wgIgnoredMessages = array( 'brokenredirects-summary', 'deadendpages-summary', 'protectedpages-summary', + 'protectedpages-unknown-reason', 'disambiguations-summary', 'pageswithprop-summary', 'doubleredirects-summary', diff --git a/maintenance/language/messages.inc b/maintenance/language/messages.inc index ed85223c37..6c9b86ff2f 100644 --- a/maintenance/language/messages.inc +++ b/maintenance/language/messages.inc @@ -1816,6 +1816,15 @@ $wgMessageStructure = array( 'protectedpages-cascade', 'protectedpages-noredirect', 'protectedpagesempty', + 'protectedpages-timestamp', + 'protectedpages-page', + 'protectedpages-expiry', + 'protectedpages-performer', + 'protectedpages-params', + 'protectedpages-reason', + 'protectedpages-unknown-timestamp', + 'protectedpages-unknown-performer', + 'protectedpages-unknown-reason', 'protectedtitles', 'protectedtitles-summary', 'protectedtitlesempty', diff --git a/resources/mediawiki.special/mediawiki.special.css b/resources/mediawiki.special/mediawiki.special.css index b02e8f21e4..ee4fc06aad 100644 --- a/resources/mediawiki.special/mediawiki.special.css +++ b/resources/mediawiki.special/mediawiki.special.css @@ -264,6 +264,18 @@ td.mw-statistics-numbers { text-align: right; } +/**** Special:ProtectedPages ****/ +table.mw-protectedpages span.mw-usertoollinks, +span.mw-protectedpages-length, +span.mw-protectedpages-actions { + white-space: nowrap; + font-size: 90%; +} +span.mw-protectedpages-unknown { + color: grey; + font-size: 90%; +} + /**** Special:UserRights ****/ .mw-userrights-disabled { color: #888; -- 2.20.1