r92805 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92804‎ | r92805 | r92806 >
Date:22:08, 21 July 2011
Author:raindrift
Status:ok
Tags:
Comment:
Added an API to TitleBlackList to make it possible to check the blacklist in advance. Makes for more responsive UI.
Modified paths:
  • /trunk/extensions/TitleBlacklist/TitleBlacklist.php (modified) (history)
  • /trunk/extensions/TitleBlacklist/api (added) (history)
  • /trunk/extensions/TitleBlacklist/api/ApiQueryTitleBlacklist.php (added) (history)
  • /trunk/extensions/TitleBlacklist/tests (added) (history)
  • /trunk/extensions/TitleBlacklist/tests/ApiQueryTitleBlacklistTest.php (added) (history)
  • /trunk/extensions/TitleBlacklist/tests/testSource (added) (history)

Diff [purge]

Index: trunk/extensions/TitleBlacklist/TitleBlacklist.php
@@ -40,6 +40,12 @@
4141 'warningexpiry' => 600,
4242 );
4343
 44+$dir = dirname( __FILE__ );
 45+
 46+// Register the API method
 47+$wgAutoloadClasses['ApiQueryTitleBlacklist'] = "$dir/api/ApiQueryTitleBlacklist.php";
 48+$wgAPIModules['titleblacklist'] = 'ApiQueryTitleBlacklist';
 49+
4450 $wgAvailableRights[] = 'tboverride'; // Implies tboverride-account
4551 $wgAvailableRights[] = 'tboverride-account'; // For account creation
4652 $wgGroupPermissions['sysop']['tboverride'] = true;
Index: trunk/extensions/TitleBlacklist/tests/ApiQueryTitleBlacklistTest.php
@@ -0,0 +1,55 @@
 2+<?php
 3+/*
 4+ * Test the TitleBlacklist API
 5+ *
 6+ * This wants to run with phpunit.php, like so:
 7+ * cd $IP/tests/phpunit
 8+ * php phpunit.php ../../extensions/TitleBlacklist/tests/ApiQueryTitleBlacklistTest.php
 9+ *
 10+ * Ian Baker <ian@wikimedia.org>
 11+ */
 12+
 13+ini_set( 'include_path', ini_get('include_path') . ':' . __DIR__ . '/../../../tests/phpunit/includes/api' );
 14+
 15+class ApiQueryTitleBlacklistTest extends ApiTestCase {
 16+
 17+ function setUp() {
 18+ parent::setUp();
 19+ $this->doLogin();
 20+ }
 21+
 22+ function testApiQueryTitleBlacklist() {
 23+ global $wgMetaNamespace, $wgGroupPermissions, $wgTitleBlacklistSources;
 24+
 25+ // without this, blacklist applies only to anonymous users.
 26+ $wgGroupPermissions['sysop']['tboverride'] = false;
 27+
 28+ $wgTitleBlacklistSources = array(
 29+ array(
 30+ 'type' => TBLSRC_FILE,
 31+ 'src' => __DIR__ . '/testSource',
 32+ ),
 33+ );
 34+
 35+ $unlisted = $this->doApiRequest( array(
 36+ 'action' => 'titleblacklist',
 37+ 'tbtitle' => 'foo',
 38+ 'tbaction' => 'create',
 39+ ) );
 40+
 41+ $this->assertEquals( $unlisted[0]['titleblacklist']['result'], 'ok', 'Unlisted title returns ok');
 42+
 43+ $listed = $this->doApiRequest( array(
 44+ 'action' => 'titleblacklist',
 45+ 'tbtitle' => 'bar',
 46+ 'tbaction' => 'create',
 47+ ) );
 48+
 49+ $this->assertEquals( $listed[0]['titleblacklist']['result'], 'error', 'Listed title returns error');
 50+ $this->assertEquals(
 51+ $listed[0]['titleblacklist']['error'],
 52+ "The title \"bar\" has been banned from creation.\nIt matches the following blacklist entry: <code>[Bb]ar # example blacklist entry</code>",
 53+ 'Listed title error text is as as expected'
 54+ );
 55+ }
 56+}
\ No newline at end of file
Property changes on: trunk/extensions/TitleBlacklist/tests/ApiQueryTitleBlacklistTest.php
___________________________________________________________________
Added: svn:eol-style
157 + native
Index: trunk/extensions/TitleBlacklist/tests/testSource
@@ -0,0 +1 @@
 2+[Bb]ar #example blacklist entry
Index: trunk/extensions/TitleBlacklist/api/ApiQueryTitleBlacklist.php
@@ -0,0 +1,120 @@
 2+<?php
 3+/**
 4+ * TitleBlacklist extension API
 5+ *
 6+ * Copyright © 2011 Wikimedia Foundation and Ian Baker <ian@wikimedia.org>
 7+ * Based on code by Victor Vasiliev, Bryan Tong Minh, Roan Kattouw, and Alex Z.
 8+ *
 9+ * This program is free software; you can redistribute it and/or modify
 10+ * it under the terms of the GNU General Public License as published by
 11+ * the Free Software Foundation; either version 2 of the License, or
 12+ * (at your option) any later version.
 13+ *
 14+ * This program is distributed in the hope that it will be useful,
 15+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 17+ * GNU General Public License for more details.
 18+ *
 19+ * You should have received a copy of the GNU General Public License along
 20+ * with this program; if not, write to the Free Software Foundation, Inc.,
 21+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 22+ * http://www.gnu.org/copyleft/gpl.html
 23+ */
 24+
 25+/**
 26+ * Query module check a title against the blacklist
 27+ *
 28+ * @ingroup API
 29+ * @ingroup Extensions
 30+ */
 31+
 32+
 33+class ApiQueryTitleBlacklist extends ApiQueryBase {
 34+
 35+ public function __construct( $query, $moduleName ) {
 36+ parent::__construct( $query, $moduleName, 'tb' );
 37+ }
 38+
 39+ public function execute() {
 40+ global $wgTitleBlacklist; // When in Rome...
 41+
 42+ # get the current user.
 43+ $context = $this->createContext();
 44+ $user = $context->getUser();
 45+
 46+ $params = $this->extractRequestParams();
 47+ $action = $params['action'];
 48+
 49+ // check the blacklist, the same way tbhooks does it.
 50+ //
 51+ // Some places check createpage, while others check create.
 52+ // As it stands, upload does createpage, but normalize both
 53+ // to the same action, to stop future similar bugs.
 54+ if( $action === 'createpage' || $action === 'createtalk' ) {
 55+ $action = 'create';
 56+ }
 57+
 58+ // possible actions at the moment: create, edit, upload
 59+ efInitTitleBlacklist();
 60+
 61+ $blacklisted = $wgTitleBlacklist->userCannot( $params['title'], $user, $action );
 62+ if( $blacklisted instanceof TitleBlacklistEntry ) {
 63+ // this title is blacklisted.
 64+ $result = array(
 65+ htmlspecialchars( $blacklisted->getRaw() ),
 66+ htmlspecialchars( $params['title'] ),
 67+ );
 68+
 69+ $this->getResult()->addValue( 'titleblacklist', 'result', 'error' );
 70+ $this->getResult()->addValue( 'titleblacklist', 'error', wfMsg($blacklisted->getErrorMessage('edit'), $result) );
 71+ } else {
 72+ // not blacklisted
 73+ $this->getResult()->addValue( 'titleblacklist', 'result', 'ok' );
 74+ }
 75+
 76+ }
 77+
 78+ public function getAllowedParams() {
 79+ return array(
 80+ 'title' => array(
 81+ ApiBase::PARAM_REQUIRED => true,
 82+ ),
 83+ 'action' => array(
 84+ ApiBase::PARAM_DFLT => 'edit',
 85+ ApiBase::PARAM_ISMULTI => false,
 86+ ApiBase::PARAM_TYPE => array(
 87+ 'create', 'edit', 'upload', 'createtalk', 'createpage',
 88+ ),
 89+ )
 90+ );
 91+ }
 92+
 93+ public function getParamDescription() {
 94+ return array(
 95+ 'title' => 'The string to validate against the blacklist',
 96+ 'lang' => 'The current language',
 97+ 'action' => 'The thing you\'re trying to do',
 98+ );
 99+ }
 100+
 101+ public function getDescription() {
 102+ return 'Validate an article title, filename, or username against the TitleBlacklist.';
 103+ }
 104+
 105+ public function getPossibleErrors() {
 106+
 107+ //return array_merge( parent::getPossibleErrors(), array(
 108+ //) );
 109+ }
 110+
 111+ protected function getExamples() {
 112+ return array(
 113+ 'api.php?action=titleblacklist&tbtitle=Foo',
 114+ 'api.php?action=titleblacklist&tbtitle=Bar&tbaction=create',
 115+ );
 116+ }
 117+
 118+ public function getVersion() {
 119+ return __CLASS__ . ': $Id: $';
 120+ }
 121+}
Property changes on: trunk/extensions/TitleBlacklist/api/ApiQueryTitleBlacklist.php
___________________________________________________________________
Added: svn:eol-style
1122 + native

Follow-up revisions

RevisionCommit summaryAuthorDate
r92807removed cruft, followup to r92805raindrift22:10, 21 July 2011
r93471Added additional entry for non-automated testing...raindrift17:19, 29 July 2011

Status & tagging log