jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/330185 )
Change subject: Adding support for task forces to the assessment processing code ...................................................................... Adding support for task forces to the assessment processing code Also modifying the maintenance script so it won't delete projects that are parents of other projects. Also modifying the APIs to allow inclusion of task force data. Bug: T154216 Change-Id: I578a4f4d25b7cead1ebeb0ac66b631f6e89c55d4 --- M PageAssessmentsBody.php M api/ApiQueryPageAssessments.php M api/ApiQueryProjects.php M i18n/en.json M i18n/qqq.json M maintenance/purgeUnusedProjects.php 6 files changed, 89 insertions(+), 11 deletions(-) Approvals: jenkins-bot: Verified Samwilson: Looks good to me, approved diff --git a/PageAssessmentsBody.php b/PageAssessmentsBody.php index 0c3dde3..1a8733a 100644 --- a/PageAssessmentsBody.php +++ b/PageAssessmentsBody.php @@ -49,14 +49,18 @@ foreach ( $assessmentData as $parserData ) { // If the name of the project is set... if ( isset( $parserData[0] ) && $parserData[0] !== '' ) { + $projectName = $parserData[0]; // ...get the corresponding ID from page_assessments_projects table. - $projectId = self::getProjectId( $parserData[0] ); + $projectId = self::getProjectId( $projectName ); // If there is no existing project by that name, add it to the table. if ( $projectId === false ) { - $projectId = self::insertProject( $parserData[0] ); + // Extract possible parent from the project name. + $parentId = self::extractParentProjectId( $projectName ); + // Insert project data into the database table. + $projectId = self::insertProject( $projectName, $parentId ); } // Add the project's ID to the array. - $projects[$parserData[0]] = $projectId; + $projects[$projectName] = $projectId; } } // Get a list of all the projects previously assigned to the page. @@ -137,9 +141,24 @@ return false; } + /** + * Extract parent from a project name and return the ID. For example, if the + * project name is "Novels/Crime task force", the parent will be "Novels", + * i.e. WikiProject Novels. + * + * @param string $projectName Project title + * @return int|false project ID or false if not found + */ + protected static function extractParentProjectId( $projectName ) { + $projectNameParts = explode( '/', $projectName ); + if ( count( $projectNameParts ) > 1 && $projectNameParts[0] !== '' ) { + return self::getProjectId( $projectNameParts[0] ); + } + return false; + } /** - * Get project ID for a give wikiproject title + * Get project ID for a given wikiproject title * @param string $project Project title * @return int|false project ID or false if not found */ @@ -156,14 +175,18 @@ /** * Insert a new wikiproject into the projects table * @param string $project Wikiproject title + * @param int $parentId ID of the parent project (for subprojects) (optional) * @return int Insert Id for new project */ - public static function insertProject( $project ) { + public static function insertProject( $project, $parentId = null ) { $dbw = wfGetDB( DB_MASTER ); - $values = array( + $values = [ 'pap_project_title' => $project, 'pap_project_id' => $dbw->nextSequenceValue( 'pap_project_id_seq' ) - ); + ]; + if ( $parentId ) { + $values[ 'pap_parent_id' ] = (int)$parentId; + } $dbw->insert( 'page_assessments_projects', $values, __METHOD__ ); $id = $dbw->insertId(); return $id; diff --git a/api/ApiQueryPageAssessments.php b/api/ApiQueryPageAssessments.php index 4544c8a..5297993 100644 --- a/api/ApiQueryPageAssessments.php +++ b/api/ApiQueryPageAssessments.php @@ -75,6 +75,9 @@ ) ) ); $this->addWhereFld( 'pa_page_id', array_keys( $pages ) ); + if ( !$params['subprojects'] ) { + $this->addWhere( 'pap_parent_id IS NULL' ); + } $this->addOption( 'LIMIT', $params['limit'] + 1 ); // handle continuation if present @@ -112,6 +115,10 @@ ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1, ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2, ), + 'subprojects' => array( + ApiBase::PARAM_DFLT => false, + ApiBase::PARAM_TYPE => 'boolean', + ), ); } @@ -121,6 +128,8 @@ => 'apihelp-query+pageassessments-example-formatversion', 'action=query&prop=pageassessments&titles=Apple' => 'apihelp-query+pageassessments-example-simple', + 'action=query&prop=pageassessments&titles=Apple&subprojects=true' + => 'apihelp-query+pageassessments-example-subprojects', ); } } diff --git a/api/ApiQueryProjects.php b/api/ApiQueryProjects.php index 5fb279a..d12faff 100644 --- a/api/ApiQueryProjects.php +++ b/api/ApiQueryProjects.php @@ -22,10 +22,15 @@ * Evaluate the parameters, perform the requested query, and set up the result */ public function execute() { + $params = $this->extractRequestParams(); // Set the database query parameters $this->addTables( [ 'page_assessments_projects' ] ); $this->addFields( [ 'project_title' => 'pap_project_title' ] ); + // Exclude subprojects/task forces by default + if ( !$params['subprojects'] ) { + $this->addWhere( 'pap_parent_id IS NULL' ); + } $this->addOption( 'ORDER BY', 'pap_project_title' ); // Execute the query and put the results in an array @@ -40,6 +45,15 @@ $result->addValue( 'query', 'projects', $projects ); } + public function getAllowedParams() { + return array( + 'subprojects' => array( + ApiBase::PARAM_DFLT => false, + ApiBase::PARAM_TYPE => 'boolean', + ), + ); + } + /** * Return usage examples for this module * @return array @@ -47,6 +61,8 @@ public function getExamplesMessages() { return [ 'action=query&list=projects' => 'apihelp-query+projects-example', + 'action=query&list=projects&subprojects=true' + => 'apihelp-query+projects-example-subprojects', ]; } diff --git a/i18n/en.json b/i18n/en.json index fb93cdf..a964a5b 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -8,8 +8,10 @@ "pageassessments-desc": "This extension is for storing page assessments in a database table with the help of a parser function", "apihelp-query+pageassessments-description": "Return associated projects and assessments for the given pages. See https://www.mediawiki.org/wiki/Extension:PageAssessments.", "apihelp-query+pageassessments-param-limit": "Limit for total number of projects returned (total for all pages).", + "apihelp-query+pageassessments-param-subprojects": "Also return assessments by subprojects.", "apihelp-query+pageassessments-example-formatversion":"Get project and assessment data for pages <kbd>Apple</kbd> and <kbd>Pear</kbd>, using the newer API result format.", "apihelp-query+pageassessments-example-simple": "Get project and assessment data for page <kbd>Apple</kbd>.", + "apihelp-query+pageassessments-example-subprojects": "Get project and assessment data (including for subprojects) for page <kbd>Apple</kbd>.", "apihelp-query+projectpages-description": "List all pages associated with one or more projects. See https://www.mediawiki.org/wiki/Extension:PageAssessments.", "apihelp-query+projectpages-param-assessments": "Also return assessments for the pages returned.", "apihelp-query+projectpages-param-projects": "The projects to list pages for. If this parameter is omitted, all projects will be included.", @@ -18,7 +20,9 @@ "apihelp-query+projectpages-example-simple-2": "Get first 10 pages associated with WikiProject <kbd>Medicine</kbd>, including assessment data.", "apihelp-query+projectpages-example-generator": "Get page info for first 10 pages associated with WikiProject <kbd>Textile Arts</kbd>.", "apihelp-query+projects-description": "List all the projects. See https://www.mediawiki.org/wiki/Extension:PageAssessments.", + "apihelp-query+projects-param-subprojects": "Also include subprojects.", "apihelp-query+projects-example": "Get a list of all the projects.", + "apihelp-query+projects-example-subprojects": "Get a list of all the projects and subprojects.", "apiwarn-pageassessments-nogeneratorassessments": "It is not possible to retrieve page assessment results from <kbd>generator=projectpages</kbd>.", "apiwarn-pageassessments-badproject": "Project name not recognized: $1.", "pageassessments-special": "Page assessments", diff --git a/i18n/qqq.json b/i18n/qqq.json index 4e70a31..a9667e2 100644 --- a/i18n/qqq.json +++ b/i18n/qqq.json @@ -10,8 +10,10 @@ "pageassessments-desc": "{{desc|name=PageAssessments|url=https://www.mediawiki.org/wiki/Extension:PageAssessments}}", "apihelp-query+pageassessments-description": "{{doc-apihelp-description|query+pageassessments}}", "apihelp-query+pageassessments-param-limit": "{{doc-apihelp-param|query+pageassessments|limit}}", + "apihelp-query+pageassessments-param-subprojects": "{{doc-apihelp-param|query+pageassessments|subprojects}}", "apihelp-query+pageassessments-example-formatversion": "{{doc-apihelp-example|query+pageassessments}}", "apihelp-query+pageassessments-example-simple": "{{doc-apihelp-example|query+pageassessments}}", + "apihelp-query+pageassessments-example-subprojects": "{{doc-apihelp-example|query+pageassessments}}", "apihelp-query+projectpages-description": "{{doc-apihelp-description|query+projectpages}}", "apihelp-query+projectpages-param-assessments": "{{doc-apihelp-param|query+projectpages|assessments}}", "apihelp-query+projectpages-param-projects": "{{doc-apihelp-param|query+projectpages|projects}}", @@ -20,7 +22,9 @@ "apihelp-query+projectpages-example-simple-2": "{{doc-apihelp-example|query+projectpages}}", "apihelp-query+projectpages-example-generator": "{{doc-apihelp-example|query+projectpages}}", "apihelp-query+projects-description": "{{doc-apihelp-description|query+projects}}", + "apihelp-query+projects-param-subprojects": "{{doc-apihelp-param|query+projects|subprojects}}", "apihelp-query+projects-example": "{{doc-apihelp-example|query+projects}}", + "apihelp-query+projects-example-subprojects": "{{doc-apihelp-example|query+projects}}", "apiwarn-pageassessments-nogeneratorassessments": "{{doc-apierror}}", "apiwarn-pageassessments-badproject": "{{doc-apierror}}\n\nParameters:\n* $1 - Project name that was supplied", "pageassessments-special": "Name of the Special page", diff --git a/maintenance/purgeUnusedProjects.php b/maintenance/purgeUnusedProjects.php index 4461448..19d8612 100644 --- a/maintenance/purgeUnusedProjects.php +++ b/maintenance/purgeUnusedProjects.php @@ -26,14 +26,36 @@ // Count all the projects $initialCount = $dbr->selectField( 'page_assessments_projects', 'COUNT(*)' ); $this->output( "Projects before purge: $initialCount\n" ); + + // Build a list of all the projects that are parents of other projects + $projectIds1 = []; + $res = $dbr->select( 'page_assessments_projects', 'DISTINCT( pap_parent_id )' ); + foreach ( $res as $row ) { + if ( $row->pap_parent_id ) { + $projectIds1[] = $row->pap_parent_id; + } + } + + // Build a list of all the projects that are used in assessments + $projectIds2 = []; + $res = $dbr->select( 'page_assessments', 'DISTINCT( pa_project_id )' ); + foreach ( $res as $row ) { + if ( $row->pa_project_id ) { + $projectIds2[] = $row->pa_project_id; + } + } + + // Combine the two lists + $usedProjectIds = array_unique( array_merge( $projectIds1, $projectIds2 ) ); + if ( $this->hasOption( 'dry-run' ) ) { - // Count all the projects used in current assessments - $finalCount = $dbr->selectField( 'page_assessments', 'COUNT( DISTINCT pa_project_id )' ); + $finalCount = count( $usedProjectIds ); } else { $this->output( "Purging unused projects from page_assessments_projects...\n" ); // Delete all the projects that aren't used in any current assessments - $cond = 'pap_project_id NOT IN ( SELECT DISTINCT( pa_project_id ) FROM page_assessments )'; - $dbw->delete( 'page_assessments_projects', [ $cond ], __METHOD__ ); + // and aren't parents of other projects. + $conds = [ 'pap_project_id NOT IN (' . $dbr->makeList( $usedProjectIds ) . ')' ]; + $dbw->delete( 'page_assessments_projects', $conds, __METHOD__ ); $this->output( "Done.\n" ); wfWaitForSlaves(); // Recount all the projects -- To view, visit https://gerrit.wikimedia.org/r/330185 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I578a4f4d25b7cead1ebeb0ac66b631f6e89c55d4 Gerrit-PatchSet: 7 Gerrit-Project: mediawiki/extensions/PageAssessments Gerrit-Branch: master Gerrit-Owner: Kaldari <rkald...@wikimedia.org> Gerrit-Reviewer: Kaldari <rkald...@wikimedia.org> Gerrit-Reviewer: MusikAnimal <musikani...@wikimedia.org> Gerrit-Reviewer: Niharika29 <nko...@wikimedia.org> Gerrit-Reviewer: Samwilson <s...@samwilson.id.au> Gerrit-Reviewer: Siebrand <siebr...@kitano.nl> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits