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-12-15 03:58:18 +00:00
|
|
|
use IContextSource;
|
2022-09-07 01:54:33 +00:00
|
|
|
use MediaWiki\Api\Hook\APIQuerySiteInfoGeneralInfoHook;
|
|
|
|
use MediaWiki\Hook\BeforePageDisplayHook;
|
|
|
|
use MediaWiki\Hook\InfoActionHook;
|
|
|
|
use MediaWiki\Hook\ParserLogLinterDataHook;
|
2023-02-07 01:27:16 +00:00
|
|
|
use MediaWiki\Linker\LinkRenderer;
|
2019-11-26 01:02:29 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2022-01-07 20:38:12 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2022-09-07 01:54:33 +00:00
|
|
|
use MediaWiki\Page\Hook\RevisionFromEditCompleteHook;
|
|
|
|
use MediaWiki\Page\Hook\WikiPageDeletionUpdatesHook;
|
2022-02-08 23:19:12 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
|
|
|
use MediaWiki\User\UserIdentity;
|
2020-01-10 10:06:26 +00:00
|
|
|
use MWCallableUpdate;
|
|
|
|
use OutputPage;
|
2022-09-07 01:54:33 +00:00
|
|
|
use Skin;
|
2023-02-07 01:27:16 +00:00
|
|
|
use SpecialPage;
|
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
|
|
|
|
2022-09-07 01:54:33 +00:00
|
|
|
class Hooks implements
|
|
|
|
APIQuerySiteInfoGeneralInfoHook,
|
|
|
|
BeforePageDisplayHook,
|
|
|
|
InfoActionHook,
|
|
|
|
ParserLogLinterDataHook,
|
|
|
|
RevisionFromEditCompleteHook,
|
|
|
|
WikiPageDeletionUpdatesHook
|
|
|
|
{
|
2023-02-07 01:27:16 +00:00
|
|
|
/** @var LinkRenderer */
|
|
|
|
private $linkRenderer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param LinkRenderer $linkRenderer
|
|
|
|
*/
|
|
|
|
public function __construct( LinkRenderer $linkRenderer ) {
|
|
|
|
$this->linkRenderer = $linkRenderer;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*
|
2022-09-07 01:54:33 +00:00
|
|
|
* @param OutputPage $out
|
|
|
|
* @param Skin $skin
|
2016-10-13 08:14:18 +00:00
|
|
|
*/
|
2022-09-07 01:54:33 +00:00
|
|
|
public function onBeforePageDisplay( $out, $skin ): void {
|
2017-03-10 22:09:17 +00:00
|
|
|
$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
|
|
|
|
*/
|
2022-09-07 01:54:33 +00:00
|
|
|
public function onWikiPageDeletionUpdates( $wikiPage, $content, &$updates ) {
|
2022-01-10 21:22:30 +00:00
|
|
|
$id = $wikiPage->getId();
|
|
|
|
$updates[] = new MWCallableUpdate( static function () use ( $id ) {
|
|
|
|
$database = new Database( $id );
|
|
|
|
$database->updateStats( $database->setForPage( [] ) );
|
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
|
|
|
|
2022-02-25 22:44:27 +00:00
|
|
|
/**
|
|
|
|
* This should match Parsoid's PageConfig::hasLintableContentModel()
|
|
|
|
*/
|
|
|
|
public const LINTABLE_CONTENT_MODELS = [ CONTENT_MODEL_WIKITEXT, 'proofread-page' ];
|
|
|
|
|
2022-02-08 23:19:12 +00:00
|
|
|
/**
|
|
|
|
* Hook: RevisionFromEditComplete
|
|
|
|
*
|
|
|
|
* Remove entries from the linter table upon page content model change away from wikitext
|
|
|
|
*
|
|
|
|
* @param WikiPage $wikiPage
|
|
|
|
* @param RevisionRecord $newRevisionRecord
|
|
|
|
* @param bool|int $originalRevId
|
|
|
|
* @param UserIdentity $user
|
|
|
|
* @param string[] &$tags
|
|
|
|
*/
|
2022-09-07 01:54:33 +00:00
|
|
|
public function onRevisionFromEditComplete(
|
|
|
|
$wikiPage, $newRevisionRecord, $originalRevId, $user, &$tags
|
2022-02-08 23:19:12 +00:00
|
|
|
) {
|
2022-03-02 20:39:37 +00:00
|
|
|
// This is just a stop-gap to deal with callers that aren't complying
|
|
|
|
// with the advertised hook signature.
|
|
|
|
if ( !is_array( $tags ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-25 22:44:27 +00:00
|
|
|
if (
|
2022-02-28 22:48:01 +00:00
|
|
|
in_array( "mw-blank", $tags ) ||
|
|
|
|
( in_array( "mw-contentmodelchange", $tags ) &&
|
|
|
|
!in_array( $wikiPage->getContentModel(), self::LINTABLE_CONTENT_MODELS ) )
|
2022-02-08 23:19:12 +00:00
|
|
|
) {
|
|
|
|
$database = new Database( $wikiPage->getId() );
|
|
|
|
$database->updateStats( $database->setForPage( [] ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-15 03:43:19 +00:00
|
|
|
/**
|
|
|
|
* Hook: APIQuerySiteInfoGeneralInfo
|
|
|
|
*
|
|
|
|
* Expose categories via action=query&meta=siteinfo
|
|
|
|
*
|
|
|
|
* @param ApiQuerySiteInfo $api
|
|
|
|
* @param array &$data
|
|
|
|
*/
|
2022-09-07 01:54:33 +00:00
|
|
|
public function onAPIQuerySiteInfoGeneralInfo( $api, &$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(),
|
2023-05-03 13:56:09 +00:00
|
|
|
'none' => $catManager->getNonePriority(),
|
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
|
|
|
|
*/
|
2022-09-07 01:54:33 +00:00
|
|
|
public function onInfoAction( $context, &$pageInfo ) {
|
2023-02-07 01:27:16 +00:00
|
|
|
$title = $context->getTitle();
|
|
|
|
$pageId = $title->getArticleID();
|
2016-12-15 03:58:18 +00:00
|
|
|
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
|
|
|
}
|
2023-02-07 01:27:16 +00:00
|
|
|
|
|
|
|
$pageInfo['linter'][] = [
|
|
|
|
'below',
|
|
|
|
$this->linkRenderer->makeKnownLink(
|
|
|
|
SpecialPage::getTitleFor( 'LintErrors' ),
|
|
|
|
$context->msg( 'pageinfo-linter-moreinfo' )->text(),
|
|
|
|
[],
|
|
|
|
[ 'namespace' => $title->getNamespace(), 'titlesearch' => $title->getText(), 'exactmatch' => 1 ]
|
|
|
|
),
|
|
|
|
];
|
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
|
|
|
|
*/
|
2022-09-07 01:54:33 +00:00
|
|
|
public function onParserLogLinterData(
|
2019-11-26 01:02:29 +00:00
|
|
|
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,
|
|
|
|
] );
|
2022-01-07 20:38:12 +00:00
|
|
|
MediaWikiServices::getInstance()->getJobQueueGroup()->push( $job );
|
2019-11-26 01:02:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-10-13 08:14:18 +00:00
|
|
|
}
|