r42571 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r42570‎ | r42571 | r42572 >
Date:20:23, 25 October 2008
Author:vasilievvv
Status:old (Comments)
Tags:
Comment:
* Refactor CloseWikis mostly per brion's review notes
* Add Special:ListClosedWikis
Modified paths:
  • /trunk/extensions/CloseWikis/CloseWikis.i18n.php (modified) (history)
  • /trunk/extensions/CloseWikis/CloseWikis.list.php (added) (history)
  • /trunk/extensions/CloseWikis/CloseWikis.page.php (modified) (history)
  • /trunk/extensions/CloseWikis/CloseWikis.php (modified) (history)
  • /trunk/extensions/CloseWikis/closewikis.sql (added) (history)
  • /trunk/extensions/CloseWikis/tables.sql (deleted) (history)

Diff [purge]

Index: trunk/extensions/CloseWikis/tables.sql
@@ -1,8 +0,0 @@
2 -
3 -CREATE TABLE /*$wgDBprefix*/closedwikis (
4 - cw_wiki varchar(255) NOT NULL,
5 - cw_reason mediumblob NOT NULL,
6 -
7 - UNIQUE INDEX cw_wiki (cw_wiki)
8 -) /*$wgDBTableOptions*/;
Index: trunk/extensions/CloseWikis/CloseWikis.i18n.php
@@ -12,7 +12,6 @@
1313 $messages['en'] = array(
1414 'closewikis-desc' => 'Allows to close wiki sites in wiki farms',
1515 'closewikis-closed' => '$1',
16 - 'closewikis-closed-default' => 'This wiki is closed',
1716 'closewikis-page' => 'Close wiki',
1817
1918 'closewikis-page-close' => 'Close wiki',
@@ -30,11 +29,19 @@
3130 'closewikis-page-err-closed' => 'Wiki is already closed',
3231 'closewikis-page-err-opened' => 'Wiki is not closed',
3332
 33+ 'closewikis-list' => 'Closed wikis list',
 34+ 'closewikis-list-intro' => 'This list contains wikis which were closed by stewards.',
 35+ 'closewikis-list-header-wiki' => 'Wiki',
 36+ 'closewikis-list-header-by' => 'Closer',
 37+ 'closewikis-list-header-timestamp' => 'Closure time',
 38+ 'closewikis-list-header-dispreason' => 'Displayed reason',
 39+
3440 'closewikis-log' => 'Wikis closure log',
3541 'closewikis-log-header' => 'Here is a log of all wiki closures and reopenings made by stewards',
3642 'closewikis-log-close' => 'closed $2',
3743 'closewikis-log-reopen' => 'reopened $2',
3844 'right-editclosedwikis' => 'Edit closed wikis',
 45+ 'right-closewikis' => 'Close wikis',
3946 );
4047
4148 /** Message documentation (Message documentation)
Index: trunk/extensions/CloseWikis/CloseWikis.php
@@ -34,7 +34,7 @@
3535 $dir = dirname( __FILE__ );
3636 $wgExtensionMessagesFiles['CloseWikis'] = "$dir/CloseWikis.i18n.php";
3737 $wgExtensionAliasesFiles['CloseWikis'] = "$dir/CloseWikis.alias.php";
38 -$wgHooks['getUserPermissionsErrors'][] = "CloseWikis::userCan";
 38+$wgHooks['getUserPermissionsErrors'][] = "CloseWikisHooks::userCan";
3939
4040 $wgGroupPermissions['steward']['closewikis'] = true;
4141 $wgAvailableRights[] = 'closewikis';
@@ -42,7 +42,9 @@
4343 $wgAvailableRights[] = 'editclosedwikis';
4444
4545 $wgAutoloadClasses['SpecialCloseWiki'] = "$dir/CloseWikis.page.php";
 46+$wgAutoloadClasses['SpecialListClosedWikis'] = "$dir/CloseWikis.list.php";
4647 $wgSpecialPages['CloseWiki'] = 'SpecialCloseWiki';
 48+$wgSpecialPages['ListClosedWikis'] = 'SpecialListClosedWikis';
4749
4850 $wgCloseWikisDatabase = 'closedwikis';
4951
@@ -52,7 +54,37 @@
5355 $wgLogActions['closewiki/close'] = 'closewikis-log-close';
5456 $wgLogActions['closewiki/reopen'] = 'closewikis-log-reopen';
5557
 58+class CloseWikisRow {
 59+ private $mRow;
 60+
 61+ public function __construct( $row ) {
 62+ $this->mRow = $row;
 63+ }
 64+
 65+ public function isClosed() {
 66+ return (bool)$this->mRow;
 67+ }
 68+
 69+ public function getWiki() {
 70+ return $this->mRow ? $this->mRow->cw_wiki : null;
 71+ }
 72+
 73+ public function getReason() {
 74+ return $this->mRow ? $this->mRow->cw_reason : null;
 75+ }
 76+
 77+ public function getTimestamp() {
 78+ return $this->mRow ? wfTimestamp( TS_MW, $this->mRow->cw_timestamp ) : null;
 79+ }
 80+
 81+ public function getBy() {
 82+ return $this->mRow ? $this->mRow->cw_by : null;
 83+ }
 84+}
 85+
5686 class CloseWikis {
 87+ static $cachedList = null;
 88+
5789 static function getSlaveDB() {
5890 global $wgCloseWikisDatabase;
5991 return wfGetDB( DB_SLAVE, 'closewikis', $wgCloseWikisDatabase );
@@ -63,35 +95,108 @@
6496 return wfGetDB( DB_MASTER, 'closewikis', $wgCloseWikisDatabase );
6597 }
6698
67 - static function getList() {
68 - global $wgMemc;
69 - $cached = $wgMemc->get( 'closedwikis' );
70 - if( is_array( $cached ) )
71 - return $cached;
 99+ /** Returns list of all closed wikis in form of CloseWikisRow array. Not cached */
 100+ static function getAll() {
72101 $list = array();
73102 $dbr = self::getSlaveDB();
74103 $result = $dbr->select( 'closedwikis', '*', false, __METHOD__ );
75104 foreach( $result as $row ) {
76 - $list[$row->cw_wiki] = $row->cw_reason;
 105+ $list[] = new CloseWikisRow( $row );
77106 }
78107 $dbr->freeResult( $result );
79 - $wgMemc->set( 'closedwikis', $list );
80108 return $list;
81109 }
82110
 111+ /** Returns list of closed wikis in form of string array. Cached in CloseWikis::$cachedList */
 112+ static function getList() {
 113+ if( self::$cachedList )
 114+ return self::$cachedList;
 115+ $list = array();
 116+ $dbr = self::getMasterDB(); // Used only on writes
 117+ $result = $dbr->select( 'closedwikis', 'cw_wiki', false, __METHOD__ );
 118+ foreach( $result as $row ) {
 119+ $list[] = $row->cw_wiki;
 120+ }
 121+ $dbr->freeResult( $result );
 122+ self::$cachedList = $list;
 123+ return $list;
 124+ }
 125+
 126+ /** Returns list of unclosed wikis in form of string array. Based on getList() */
83127 static function getUnclosedList() {
84128 global $wgLocalDatabases;
85 - return array_diff( $wgLocalDatabases, array_keys( self::getList() ) );
 129+ return array_diff( $wgLocalDatabases, self::getList() );
86130 }
87131
 132+ /** Returns a CloseWikisRow for specific wiki. Cached in $wgMemc */
 133+ static function getClosedRow( $wiki ) {
 134+ global $wgMemc;
 135+ $memcKey = "closedwikis:{$wiki}";
 136+ $cached = $wgMemc->get( $memcKey );
 137+ if( is_object( $cached ) )
 138+ return $cached;
 139+ $dbr = self::getSlaveDB();
 140+ $result = new CloseWikisRow( $dbr->selectRow( 'closedwikis', '*', array( 'cw_wiki' => $wiki ), __METHOD__ ) );
 141+ $wgMemc->set( $memcKey, $result );
 142+ return $result;
 143+ }
 144+
 145+ /** Closes a wiki */
 146+ static function close( $wiki, $dispreason, $by ) {
 147+ global $wgMemc;
 148+ $dbw = CloseWikis::getMasterDB();
 149+ $dbw->begin();
 150+ $dbw->insert(
 151+ 'closedwikis',
 152+ array(
 153+ 'cw_wiki' => $wiki,
 154+ 'cw_reason' => $dispreason,
 155+ 'cw_timestamp' => $dbw->timestamp( wfTimestampNow() ),
 156+ 'cw_by' => $by->getName(),
 157+ ),
 158+ __METHOD__,
 159+ array( 'IGNORE' ) // Better error handling
 160+ );
 161+ $result = (bool)$dbw->affectedRows();
 162+ $dbw->commit();
 163+ $wgMemc->delete( "closedwikis:{$wiki}" );
 164+ self::$cachedList = null;
 165+ return $result;
 166+ }
 167+
 168+ /** Reopens a wiki */
 169+ static function reopen( $wiki ) {
 170+ global $wgMemc;
 171+ $dbw = self::getMasterDB();
 172+ $dbw->begin();
 173+ $dbw->delete(
 174+ 'closedwikis',
 175+ array( 'cw_wiki' => $wiki ),
 176+ __METHOD__
 177+ );
 178+ $result = (bool)$dbw->affectedRows();
 179+ $dbw->commit();
 180+ $wgMemc->delete( "closedwikis:{$wiki}" );
 181+ self::$cachedList = null;
 182+ return $result;
 183+ }
 184+}
 185+
 186+class CloseWikisHooks {
88187 static function userCan( &$title, &$user, $action, &$result ) {
89 - $list = self::getList();
90 - if( isset( $list[wfWikiID()] ) && !$user->isAllowed( 'editclosedwikis' ) ) {
 188+ static $closed = null;
 189+ global $wgLang;
 190+ if( $action == 'read' )
 191+ return true;
 192+ if( is_null( $closed ) )
 193+ $closed = CloseWikis::getClosedRow( wfWikiID() );
 194+ if( $closed->isClosed() && !$user->isAllowed( 'editclosedwikis' ) ) {
91195 wfLoadExtensionMessages( 'CloseWikis' );
92 - $reason = $list[wfWikiID()];
93 - $result[] = $reason ?
94 - array( 'closewikis-closed', $reason ) :
95 - array( 'closewikis-closed-default' );
 196+ $reason = $closed->getReason();
 197+ $ts = $closed->getTimestamp();
 198+ $by = $closed->getBy();
 199+ $result[] = array( 'closewikis-closed', $reason, $by,
 200+ $wgLang->timeanddate( $ts ), $wgLang->time( $ts ), $wgLang->date( $ts ) );
96201 return false;
97202 }
98203 return true;
Index: trunk/extensions/CloseWikis/closewikis.sql
@@ -0,0 +1,10 @@
 2+-- Tables for CloseWikis extension
 3+
 4+CREATE TABLE /*$wgDBprefix*/closedwikis (
 5+ cw_wiki varchar(255) NOT NULL,
 6+ cw_reason mediumblob NOT NULL,
 7+ cw_timestamp binary(14) NOT NULL,
 8+ cw_by tinyblob NOT NULL,
 9+
 10+ UNIQUE INDEX cw_wiki (cw_wiki)
 11+) /*$wgDBTableOptions*/;
Property changes on: trunk/extensions/CloseWikis/closewikis.sql
___________________________________________________________________
Name: svn:eol-style
112 + native
Index: trunk/extensions/CloseWikis/CloseWikis.list.php
@@ -0,0 +1,54 @@
 2+<?php
 3+/*
 4+ * Copyright (C) 2008 Victor Vasiliev <vasilvv@gmail.com>
 5+ *
 6+ * This program is free software; you can redistribute it and/or modify
 7+ * it under the terms of the GNU General Public License as published by
 8+ * the Free Software Foundation; either version 2 of the License, or
 9+ * (at your option) any later version.
 10+ *
 11+ * This program is distributed in the hope that it will be useful,
 12+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 14+ * GNU General Public License for more details.
 15+ *
 16+ * You should have received a copy of the GNU General Public License
 17+ * along with this program; if not, write to the Free Software
 18+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 19+ * MA 02110-1301, USA.
 20+ */
 21+
 22+if( !defined( 'MEDIAWIKI' ) )
 23+ exit;
 24+
 25+class SpecialListClosedWikis extends SpecialPage {
 26+ public function __construct() {
 27+ wfLoadExtensionMessages( 'CloseWikis' );
 28+ parent::__construct( 'ListClosedWikis' );
 29+ }
 30+
 31+ public function getDescription() {
 32+ return wfMsg( 'closewikis-list' );
 33+ }
 34+
 35+ public function execute( $par ) {
 36+ global $wgUser, $wgOut, $wgLang;
 37+
 38+ $this->setHeaders();
 39+ $wgOut->addWikiMsg( 'closewikis-list-intro' );
 40+ $wgOut->addHtml( '<table class="TablePager" style="width: 100%"><tr>' );
 41+ foreach( array( 'wiki', 'by', 'timestamp', 'dispreason' ) as $column )
 42+ $wgOut->addHtml( '<th>' . wfMsgExt( "closewikis-list-header-{$column}", 'parseinline' ) . '</th>' );
 43+ $wgOut->addHtml( '</tr>' );
 44+ $list = CloseWikis::getAll();
 45+ foreach( $list as $entry ) {
 46+ $wgOut->addHTML( '<tr>' );
 47+ $wgOut->addHTML( '<td>' . $entry->getWiki() . '</td>' );
 48+ $wgOut->addHTML( '<td>' . $entry->getBy() . '</td>' );
 49+ $wgOut->addHTML( '<td>' . $wgLang->timeanddate( $entry->getTimestamp() ) . '</td>' );
 50+ $wgOut->addHTML( '<td>' ); $wgOut->addWikiText( $entry->getReason() ); $wgOut->addHTML( '</td>' );
 51+ $wgOut->addHTML( '</tr>' );
 52+ }
 53+ $wgOut->addHtml( '</table>' );
 54+ }
 55+}
Property changes on: trunk/extensions/CloseWikis/CloseWikis.list.php
___________________________________________________________________
Name: svn:eol-style
156 + native
Index: trunk/extensions/CloseWikis/CloseWikis.page.php
@@ -41,7 +41,8 @@
4242 }
4343
4444 $this->closeForm();
45 - $this->reopenForm();
 45+ if( CloseWikis::getList() )
 46+ $this->reopenForm();
4647 }
4748
4849 protected function buildSelect( $list, $name, $default = '' ) {
@@ -66,7 +67,7 @@
6768 if( !in_array( $wiki, $wgLocalDatabases ) ) {
6869 $status = wfMsgExt( 'closewikis-page-err-nowiki', 'parseinline' );
6970 } else {
70 - $statusOK = $this->doClose( $wiki, $dreason );
 71+ $statusOK = CloseWikis::close( $wiki, $dreason, $wgUser );
7172 if( $statusOK ) {
7273 $status = wfMsgExt( 'closewikis-page-close-success', 'parseinline' );
7374 $logpage = new LogPage( 'closewiki' );
@@ -92,7 +93,7 @@
9394 $statusStyle = $statusOK ? 'success' : 'error';
9495 $wgOut->addHTML( "<p><strong class=\"{$statusStyle}\">{$status}</strong></p>" );
9596 }
96 - $wgOut->addHTML( '<form method="post" action="' . $wgTitle->getLocalURL() . '">' );
 97+ $wgOut->addHTML( '<form method="post" action="' . htmlspecialchars( $wgTitle->getLocalURL() ) . '">' );
9798 $form = array();
9899 $form['closewikis-page-close-wiki'] = $this->buildSelect( CloseWikis::getUnclosedList(), 'wpcWiki', $defaultWiki );
99100 $form['closewikis-page-close-dreason'] = Xml::textarea( 'wpcDisplayReason', $defaultDisplayReason );
@@ -102,25 +103,6 @@
103104 $wgOut->addHTML( "</form></fieldset>" );
104105 }
105106
106 - public function doClose( $wiki, $dispreason ) {
107 - global $wgMemc;
108 - $dbw = CloseWikis::getMasterDB();
109 - $dbw->begin();
110 - $dbw->insert(
111 - 'closedwikis',
112 - array(
113 - 'cw_wiki' => $wiki,
114 - 'cw_reason' => $dispreason,
115 - ),
116 - __METHOD__,
117 - array( 'IGNORE' ) // Better error handling
118 - );
119 - $result = (bool)$dbw->affectedRows();
120 - $dbw->commit();
121 - $wgMemc->delete( 'closedwikis' );
122 - return $result;
123 - }
124 -
125107 protected function reopenForm() {
126108 global $wgOut, $wgUser, $wgRequest, $wgTitle;
127109
@@ -133,7 +115,7 @@
134116 if( !in_array( $wiki, $wgLocalDatabases ) ) {
135117 $status = wfMsgExt( 'closewikis-page-err-nowiki', 'parseinline' );
136118 } else {
137 - $statusOK = $this->doReopen( $wiki );
 119+ $statusOK = CloseWikis::reopen( $wiki );
138120 if( $statusOK ) {
139121 $status = wfMsgExt( 'closewikis-page-reopen-success', 'parseinline' );
140122 $logpage = new LogPage( 'closewiki' );
@@ -155,27 +137,14 @@
156138 $statusStyle = $statusOK ? 'success' : 'error';
157139 $wgOut->addHTML( "<p><strong class=\"{$statusStyle}\">{$status}</strong></p>" );
158140 }
159 - $wgOut->addHTML( '<form method="post" action="' . $wgTitle->getLocalURL() . '">' );
 141+ $wgOut->addHTML( '<form method="post" action="' . htmlspecialchars( $wgTitle->getLocalURL() ) . '">' );
160142 $form = array();
161 - $form['closewikis-page-reopen-wiki'] = $this->buildSelect( array_keys( CloseWikis::getList() ), 'wprWiki', $defaultWiki );
 143+ $form['closewikis-page-reopen-wiki'] = $this->buildSelect( CloseWikis::getList(), 'wprWiki', $defaultWiki );
162144 $form['closewikis-page-reopen-reason'] = Xml::input( 'wprReason', false, $defaultReason );
163145 $wgOut->addHTML( Xml::buildForm( $form, 'closewikis-page-reopen-submit' ) );
164146 $wgOut->addHTML( Xml::hidden( 'wprEdittoken', $wgUser->editToken() ) );
165147 $wgOut->addHTML( "</form></fieldset>" );
166148 }
167149
168 - public function doReopen( $wiki ) {
169 - global $wgMemc;
170 - $dbw = CloseWikis::getMasterDB();
171 - $dbw->begin();
172 - $dbw->delete(
173 - 'closedwikis',
174 - array( 'cw_wiki' => $wiki ),
175 - __METHOD__
176 - );
177 - $result = (bool)$dbw->affectedRows();
178 - $dbw->commit();
179 - $wgMemc->delete( 'closedwikis' );
180 - return $result;
181 - }
 150+
182151 }

Comments

#Comment by Bryan (talk | contribs)   15:42, 26 October 2008

Wouldn't it be an idea to log who closed and opened the wiki?

Status & tagging log