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;
|
|
|
|
|
|
|
|
use FormatJson;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Database logic
|
|
|
|
*/
|
2016-10-29 01:06:08 +00:00
|
|
|
class Database {
|
2016-11-24 03:47:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Maximum number of errors to save per category,
|
|
|
|
* for a page, the rest are just dropped
|
|
|
|
*/
|
|
|
|
const MAX_PER_CAT = 20;
|
|
|
|
|
2016-10-13 08:14:18 +00:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $pageId;
|
|
|
|
|
2016-12-08 07:41:48 +00:00
|
|
|
/**
|
|
|
|
* @var CategoryManager
|
|
|
|
*/
|
|
|
|
private $categoryManager;
|
|
|
|
|
2016-10-13 08:14:18 +00:00
|
|
|
/**
|
|
|
|
* @param int $pageId
|
|
|
|
*/
|
|
|
|
public function __construct( $pageId ) {
|
|
|
|
$this->pageId = $pageId;
|
2016-12-08 07:41:48 +00:00
|
|
|
$this->categoryManager = new CategoryManager();
|
2016-10-13 08:14:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a specific LintError by id
|
|
|
|
*
|
|
|
|
* @param int $id linter_id
|
|
|
|
* @return bool|LintError
|
|
|
|
*/
|
2016-10-29 01:06:08 +00:00
|
|
|
public function getFromId( $id ) {
|
|
|
|
$row = wfGetDB( DB_REPLICA )->selectRow(
|
2016-10-13 08:14:18 +00:00
|
|
|
'linter',
|
2016-12-01 02:47:05 +00:00
|
|
|
[ 'linter_cat', 'linter_params', 'linter_start', 'linter_end' ],
|
2016-10-13 08:14:18 +00:00
|
|
|
[ 'linter_id' => $id, 'linter_page' => $this->pageId ],
|
2016-10-29 01:06:08 +00:00
|
|
|
__METHOD__
|
2016-10-13 08:14:18 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if ( $row ) {
|
|
|
|
$row->linter_id = $id;
|
|
|
|
return $this->makeLintError( $row );
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn a database row into a LintError object
|
|
|
|
*
|
|
|
|
* @param \stdClass $row
|
|
|
|
* @return LintError
|
|
|
|
*/
|
|
|
|
public static function makeLintError( $row ) {
|
|
|
|
return new LintError(
|
2016-11-22 09:03:02 +00:00
|
|
|
( new CategoryManager() )->getCategoryName( $row->linter_cat ),
|
2016-12-01 02:47:05 +00:00
|
|
|
[ (int)$row->linter_start, (int)$row->linter_end ],
|
2016-10-13 08:14:18 +00:00
|
|
|
$row->linter_params,
|
|
|
|
(int)$row->linter_id
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all the lint errors for a page
|
|
|
|
*
|
|
|
|
* @return LintError[]
|
|
|
|
*/
|
2016-10-29 01:06:08 +00:00
|
|
|
public function getForPage() {
|
|
|
|
$rows = wfGetDB( DB_REPLICA )->select(
|
2016-10-13 08:14:18 +00:00
|
|
|
'linter',
|
2016-12-01 02:47:05 +00:00
|
|
|
[
|
|
|
|
'linter_id', 'linter_cat', 'linter_start',
|
|
|
|
'linter_end', 'linter_params'
|
|
|
|
],
|
2016-10-13 08:14:18 +00:00
|
|
|
[ 'linter_page' => $this->pageId ],
|
2016-10-29 01:06:08 +00:00
|
|
|
__METHOD__
|
2016-10-13 08:14:18 +00:00
|
|
|
);
|
|
|
|
$result = [];
|
|
|
|
foreach ( $rows as $row ) {
|
|
|
|
$error = $this->makeLintError( $row );
|
|
|
|
$result[$error->id()] = $error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a LintError object into an array for
|
|
|
|
* inserting/querying in the database
|
|
|
|
*
|
|
|
|
* @param LintError $error
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function serializeError( LintError $error ) {
|
2016-10-29 01:06:08 +00:00
|
|
|
return [
|
|
|
|
'linter_page' => $this->pageId,
|
2016-12-08 07:41:48 +00:00
|
|
|
'linter_cat' => $this->categoryManager->getCategoryId( $error->category ),
|
2016-10-29 01:06:08 +00:00
|
|
|
'linter_params' => FormatJson::encode( $error->params, false, FormatJson::ALL_OK ),
|
2016-12-01 02:47:05 +00:00
|
|
|
'linter_start' => $error->location[0],
|
|
|
|
'linter_end' => $error->location[1],
|
2016-10-29 01:06:08 +00:00
|
|
|
];
|
2016-10-13 08:14:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the specified lint errors in the
|
|
|
|
* database
|
|
|
|
*
|
|
|
|
* @param LintError[] $errors
|
2016-12-01 05:17:18 +00:00
|
|
|
* @return array [ 'deleted' => int, 'added' => int ]
|
2016-10-13 08:14:18 +00:00
|
|
|
*/
|
|
|
|
public function setForPage( $errors ) {
|
2016-10-29 01:06:08 +00:00
|
|
|
$previous = $this->getForPage();
|
2016-10-13 08:14:18 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
if ( !$previous && !$errors ) {
|
|
|
|
return [ 'deleted' => 0, 'added' => 0 ];
|
|
|
|
} elseif ( !$previous && $errors ) {
|
2016-12-01 02:17:36 +00:00
|
|
|
$toInsert = array_values( $errors );
|
2016-10-13 08:14:18 +00:00
|
|
|
$toDelete = [];
|
|
|
|
} elseif ( $previous && !$errors ) {
|
|
|
|
$dbw->delete(
|
|
|
|
'linter',
|
|
|
|
[ 'linter_page' => $this->pageId ],
|
|
|
|
__METHOD__
|
|
|
|
);
|
2016-12-01 05:17:18 +00:00
|
|
|
return [ 'deleted' => count( $previous ), 'added' => 0 ];
|
2016-10-13 08:14:18 +00:00
|
|
|
} else {
|
|
|
|
$toInsert = [];
|
|
|
|
$toDelete = $previous;
|
|
|
|
// Diff previous and errors
|
|
|
|
foreach ( $errors as $error ) {
|
|
|
|
$uniqueId = $error->id();
|
|
|
|
if ( isset( $previous[$uniqueId] ) ) {
|
|
|
|
unset( $toDelete[$uniqueId] );
|
|
|
|
} else {
|
|
|
|
$toInsert[] = $error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $toDelete ) {
|
|
|
|
$ids = [];
|
|
|
|
foreach ( $toDelete as $lintError ) {
|
|
|
|
if ( $lintError->lintId ) {
|
|
|
|
$ids[] = $lintError->lintId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$dbw->delete(
|
|
|
|
'linter',
|
|
|
|
[ 'linter_id' => $ids ],
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $toInsert ) {
|
2016-12-02 23:54:02 +00:00
|
|
|
// Insert into db, ignoring any duplicate key errors
|
|
|
|
// since they're the same lint error
|
2016-10-13 08:14:18 +00:00
|
|
|
$dbw->insert(
|
|
|
|
'linter',
|
|
|
|
array_map( [ $this, 'serializeError' ], $toInsert ),
|
2016-12-02 23:54:02 +00:00
|
|
|
__METHOD__,
|
|
|
|
[ 'IGNORE' ]
|
2016-10-13 08:14:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'deleted' => count( $toDelete ),
|
|
|
|
'added' => count( $toInsert ),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-12-08 07:41:48 +00:00
|
|
|
/**
|
|
|
|
* @return int[]
|
|
|
|
*/
|
|
|
|
public function getTotals() {
|
|
|
|
$rows = wfGetDB( DB_SLAVE )->select(
|
|
|
|
'linter',
|
|
|
|
[ 'linter_cat', 'COUNT(*) AS count' ],
|
|
|
|
[],
|
|
|
|
__METHOD__,
|
|
|
|
[ 'GROUP BY' => 'linter_cat' ]
|
|
|
|
);
|
|
|
|
|
|
|
|
// Initialize zero values
|
|
|
|
$ret = array_fill_keys( $this->categoryManager->getVisibleCategories(), 0 );
|
|
|
|
foreach ( $rows as $row ) {
|
|
|
|
$ret[$this->categoryManager->getCategoryName( $row->linter_cat )] = (int)$row->count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2016-10-13 08:14:18 +00:00
|
|
|
}
|