2016-10-13 08:14:18 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace MediaWiki\Linter;
|
|
|
|
|
2020-05-26 17:58:52 +00:00
|
|
|
use ApiQuerySiteinfo;
|
2016-11-22 19:15:32 +00:00
|
|
|
use Content;
|
2016-10-13 08:14:18 +00:00
|
|
|
use DatabaseUpdater;
|
2016-12-15 03:58:18 +00:00
|
|
|
use IContextSource;
|
2019-11-26 01:02:29 +00:00
|
|
|
use JobQueueGroup;
|
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2020-01-10 10:06:26 +00:00
|
|
|
use MWCallableUpdate;
|
|
|
|
use OutputPage;
|
2019-11-26 01:02:29 +00:00
|
|
|
use Title;
|
2020-01-10 10:06:26 +00:00
|
|
|
use WikiPage;
|
2016-10-13 08:14:18 +00:00
|
|
|
|
|
|
|
class Hooks {
|
|
|
|
/**
|
|
|
|
* @param DatabaseUpdater $updater
|
|
|
|
*/
|
|
|
|
public static function onLoadExtensionSchemaUpdates( DatabaseUpdater $updater ) {
|
2020-08-22 20:48:51 +00:00
|
|
|
$dbType = $updater->getDB()->getType();
|
|
|
|
if ( $dbType === 'mysql' ) {
|
|
|
|
$updater->addExtensionTable( 'linter',
|
|
|
|
dirname( __DIR__ ) . '/sql/tables-generated.sql'
|
|
|
|
);
|
|
|
|
} elseif ( $dbType === 'sqlite' ) {
|
|
|
|
$updater->addExtensionTable( 'linter',
|
|
|
|
dirname( __DIR__ ) . '/sql/sqlite/tables-generated.sql'
|
|
|
|
);
|
|
|
|
} elseif ( $dbType === 'postgres' ) {
|
|
|
|
$updater->addExtensionTable( 'linter',
|
|
|
|
dirname( __DIR__ ) . '/sql/postgres/tables-generated.sql'
|
|
|
|
);
|
|
|
|
}
|
2016-10-13 08:14:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-10 22:09:17 +00:00
|
|
|
* Hook: BeforePageDisplay
|
2016-10-13 08:14:18 +00:00
|
|
|
*
|
|
|
|
* If there is a lintid parameter, look up that error in the database
|
|
|
|
* and setup and output our client-side helpers
|
|
|
|
*
|
2017-03-10 22:09:17 +00:00
|
|
|
* @param OutputPage &$out
|
2016-10-13 08:14:18 +00:00
|
|
|
*/
|
2017-03-10 22:09:17 +00:00
|
|
|
public static function onBeforePageDisplay( OutputPage &$out ) {
|
|
|
|
$request = $out->getRequest();
|
2016-10-13 08:14:18 +00:00
|
|
|
$lintId = $request->getInt( 'lintid' );
|
|
|
|
if ( !$lintId ) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-10 22:09:17 +00:00
|
|
|
$title = $out->getTitle();
|
|
|
|
if ( !$title ) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-13 08:14:18 +00:00
|
|
|
|
|
|
|
$lintError = ( new Database( $title->getArticleID() ) )->getFromId( $lintId );
|
|
|
|
if ( !$lintError ) {
|
|
|
|
// Already fixed or bogus URL parameter?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$out->addJsConfigVars( [
|
|
|
|
'wgLinterErrorCategory' => $lintError->category,
|
|
|
|
'wgLinterErrorLocation' => $lintError->location,
|
|
|
|
] );
|
|
|
|
$out->addModules( 'ext.linter.edit' );
|
|
|
|
}
|
2016-11-22 19:15:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook: WikiPageDeletionUpdates
|
|
|
|
*
|
|
|
|
* Remove entries from the linter table upon page deletion
|
|
|
|
*
|
|
|
|
* @param WikiPage $wikiPage
|
|
|
|
* @param Content $content
|
|
|
|
* @param array &$updates
|
|
|
|
*/
|
|
|
|
public static function onWikiPageDeletionUpdates( WikiPage $wikiPage,
|
|
|
|
Content $content, array &$updates
|
|
|
|
) {
|
2017-10-06 04:03:30 +00:00
|
|
|
$title = $wikiPage->getTitle();
|
2021-05-04 07:58:08 +00:00
|
|
|
$updates[] = new MWCallableUpdate( static function () use ( $title ) {
|
2017-10-06 04:03:30 +00:00
|
|
|
$job = new RecordLintJob(
|
|
|
|
$title, [ 'errors' => [] ]
|
|
|
|
);
|
|
|
|
$job->run();
|
2016-12-15 03:43:53 +00:00
|
|
|
}, __METHOD__ );
|
2016-11-22 19:15:32 +00:00
|
|
|
}
|
2016-12-15 03:43:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook: APIQuerySiteInfoGeneralInfo
|
|
|
|
*
|
|
|
|
* Expose categories via action=query&meta=siteinfo
|
|
|
|
*
|
|
|
|
* @param ApiQuerySiteInfo $api
|
|
|
|
* @param array &$data
|
|
|
|
*/
|
2020-05-26 17:58:52 +00:00
|
|
|
public static function onAPIQuerySiteInfoGeneralInfo( ApiQuerySiteinfo $api, array &$data ) {
|
2016-12-15 03:43:19 +00:00
|
|
|
$catManager = new CategoryManager();
|
2017-04-16 07:36:32 +00:00
|
|
|
$data['linter'] = [
|
2017-04-26 22:11:23 +00:00
|
|
|
'high' => $catManager->getHighPriority(),
|
|
|
|
'medium' => $catManager->getMediumPriority(),
|
|
|
|
'low' => $catManager->getLowPriority(),
|
2017-04-16 07:36:32 +00:00
|
|
|
];
|
2016-12-15 03:43:19 +00:00
|
|
|
}
|
2016-12-15 03:58:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook: InfoAction
|
|
|
|
*
|
|
|
|
* Display quick summary of errors for this page on ?action=info
|
|
|
|
*
|
|
|
|
* @param IContextSource $context
|
|
|
|
* @param array &$pageInfo
|
|
|
|
*/
|
|
|
|
public static function onInfoAction( IContextSource $context, array &$pageInfo ) {
|
|
|
|
$pageId = $context->getTitle()->getArticleID();
|
|
|
|
if ( !$pageId ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$database = new Database( $pageId );
|
|
|
|
$totals = array_filter( $database->getTotalsForPage() );
|
|
|
|
if ( !$totals ) {
|
|
|
|
// No errors, yay!
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $totals as $name => $count ) {
|
2019-12-17 04:56:34 +00:00
|
|
|
$pageInfo['linter'][] = [
|
|
|
|
$context->msg( "linter-category-$name" ),
|
|
|
|
htmlspecialchars( (string)$count )
|
|
|
|
];
|
2016-12-15 03:58:18 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-26 01:02:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook: ParserLogLinterData
|
|
|
|
*
|
|
|
|
* To record a lint errors.
|
|
|
|
*
|
|
|
|
* @param string $page
|
|
|
|
* @param int $revision
|
|
|
|
* @param array[] $data
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function onParserLogLinterData(
|
|
|
|
string $page, int $revision, array $data
|
|
|
|
): bool {
|
|
|
|
$errors = [];
|
|
|
|
$title = Title::newFromText( $page );
|
|
|
|
if (
|
|
|
|
!$title || !$title->getArticleID() ||
|
|
|
|
$title->getLatestRevID() != $revision
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$categoryMgr = new CategoryManager();
|
|
|
|
$catCounts = [];
|
|
|
|
foreach ( $data as $info ) {
|
2017-12-07 22:30:14 +00:00
|
|
|
if ( $categoryMgr->isKnownCategory( $info['type'] ) ) {
|
|
|
|
$info[ 'dbid' ] = null;
|
|
|
|
} elseif ( !isset( $info[ 'dbid' ] ) ) {
|
2019-11-26 01:02:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$count = $catCounts[$info['type']] ?? 0;
|
|
|
|
if ( $count > Database::MAX_PER_CAT ) {
|
|
|
|
// Drop
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$catCounts[$info['type']] = $count + 1;
|
|
|
|
if ( !isset( $info['dsr'] ) ) {
|
|
|
|
LoggerFactory::getInstance( 'Linter' )->warning(
|
|
|
|
'dsr for {page} @ rev {revid}, for lint: {lint} is missing',
|
|
|
|
[
|
|
|
|
'page' => $page,
|
|
|
|
'revid' => $revision,
|
|
|
|
'lint' => $info['type'],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$info['location'] = array_slice( $info['dsr'], 0, 2 );
|
2018-01-09 21:58:15 +00:00
|
|
|
if ( !isset( $info['params'] ) ) {
|
|
|
|
$info['params'] = [];
|
|
|
|
}
|
2019-11-26 01:02:29 +00:00
|
|
|
if ( isset( $info['templateInfo'] ) && $info['templateInfo'] ) {
|
|
|
|
$info['params']['templateInfo'] = $info['templateInfo'];
|
|
|
|
}
|
|
|
|
$errors[] = $info;
|
|
|
|
}
|
|
|
|
$job = new RecordLintJob( $title, [
|
|
|
|
'errors' => $errors,
|
|
|
|
'revision' => $revision,
|
|
|
|
] );
|
|
|
|
JobQueueGroup::singleton()->push( $job );
|
|
|
|
return true;
|
|
|
|
}
|
2016-10-13 08:14:18 +00:00
|
|
|
}
|