jenkins-bot has submitted this change and it was merged. Change subject: BSApiFileBackendStore: Changes due to extendability and param handling ......................................................................
BSApiFileBackendStore: Changes due to extendability and param handling * Used as much mw functionality as possible for the resultset, to make sure, that every result is standardized * Added changed some prefixes * Added the 'list' Filter for categories * Added getParameter method to make it public by overwriting * Added file description and even made some cc Patch-Set 2: * Fixed 'Can't use function return value in write context' for old php version Patch-Set 3: * Fixed CC issues Patch-Set 4: * Fixed BsCore UploadVerification * Added numeric filter * Fixed warnings and notices Patch-Set 5: * Fixed UploadVerification error message Change-Id: I357ff620b318c6057a2339cd9f824d557877ea01 --- M includes/CoreHooks.php M includes/api/BSApiExtJSStoreBase.php M includes/api/BSApiFileBackendStore.php 3 files changed, 192 insertions(+), 72 deletions(-) Approvals: Robert Vogel: Looks good to me, approved jenkins-bot: Verified diff --git a/includes/CoreHooks.php b/includes/CoreHooks.php index 43b96ba..9c760fc 100755 --- a/includes/CoreHooks.php +++ b/includes/CoreHooks.php @@ -435,26 +435,26 @@ /** * Additional chances to reject an uploaded file - * @param string $saveName: destination file name - * @param string $tempName: filesystem path to the temporary file for checks - * @param string &$error: output: message key for message to show if upload canceled by returning false. May also be an array, where the first element - is the message key and the remaining elements are used as parameters to the message. - * @return bool true on success , false on failure + * @param String $sSaveName: Destination file name + * @param String $sTempName: Filesystem path to the temporary file for checks + * @param String &$sError: output: message key for message to show if upload canceled by returning false + * @return Boolean true on success , false on failure */ public static function onUploadVerification( $sSaveName, $sTempName, &$sError ) { - $aParts = explode( '.', $sSaveName ); + if( empty( $sSaveName ) || !$iFileExt = strrpos( $sSaveName, '.' ) ) { + return true; + } - if ( !empty( $aParts[0] ) ) { - $oUser = User::newFromName( $aParts[0] ); + $sUser = substr( $sSaveName, 0, $iFileExt ); + $oUser = User::newFromName( $sUser ); + if( is_null( $oUser ) || $oUser->getId() == 0 ) { + return true; + } - if ( $oUser->getId() != 0 ) { - $oCurrUser = RequestContext::getMain()->getUser(); - - if ( strcasecmp( $oUser->getName(), $oCurrUser->getName() ) !== 0 ) { - $sError = 'bs-imageofotheruser'; - return false; - } - } + $oCurrUser = RequestContext::getMain()->getUser(); + if( $oUser->getId() !== $oCurrUser->getId() ) { + $sError = 'bs-imageofotheruser'; + return false; } return true; diff --git a/includes/api/BSApiExtJSStoreBase.php b/includes/api/BSApiExtJSStoreBase.php index 0388378..441f766 100644 --- a/includes/api/BSApiExtJSStoreBase.php +++ b/includes/api/BSApiExtJSStoreBase.php @@ -1,9 +1,32 @@ <?php - /** * This class serves as a backend for ExtJS stores. It allows all * necessary parameters and provides convenience methods and a standard ouput * format + * + * 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. + * + * This file is part of BlueSpice for MediaWiki + * For further information visit http://www.blue-spice.org + * + * @author Robert Vogel <vo...@hallowelt.com> + * @author Patric Wirth <wi...@hallowelt.com> + * @package Bluespice_Foundation + * @copyright Copyright (C) 2011 Hallo Welt! - Medienwerkstatt GmbH, All rights reserved. + * @license http://www.gnu.org/copyleft/gpl.html GNU Public License v2 or later + * @filesource * * Example request parameters of an ExtJS store @@ -161,12 +184,17 @@ $value = parent::getParameterFromSettings($paramName, $paramSettings, $parseLimit); //Unfortunately there is no way to register custom types for parameters if( in_array( $paramName, array( 'sort', 'group', 'filter' ) ) ) { - $value = FormatJson::decode($value); + $value = FormatJson::decode( $value ); if( empty($value) ) { return array(); } } return $value; + } + + public function getParameter( $paramName, $parseLimit = true ) { + //Make this public, so hook handler could get the params + return parent::getParameter( $paramName, $parseLimit ); } /** @@ -242,8 +270,19 @@ return false; } } + if( $oFilter->type == 'list' ) { + $bFilterApplies = $this->filterList($oFilter, $aDataSet); + if( !$bFilterApplies ) { + return false; + } + } + if( $oFilter->type == 'numeric' ) { + $bFilterApplies = $this->filterNumberic($oFilter, $aDataSet); + if( !$bFilterApplies ) { + return false; + } + } //TODO: Implement for type 'date', 'datetime', 'boolean' and 'numeric' - //... and even maybe 'list' } return true; @@ -256,6 +295,10 @@ * @return boolean true if filter applies, false if not */ public function filterString($oFilter, $aDataSet) { + if( !is_string($oFilter->value) ) { + //TODO: Warning + return false; + } $sFieldValue = $aDataSet->{$oFilter->field}; $sFilterValue = $oFilter->value; @@ -281,6 +324,56 @@ } /** + * Performs numeric filtering based on given filter of type integer on a + * dataset + * @param object $oFilter + * @param oject $aDataSet + * @return boolean true if filter applies, false if not + */ + public function filterNumeric( $oFilter, $aDataSet ) { + if( !is_numeric($oFilter->value) ) { + //TODO: Warning + return false; + } + $sFieldValue = $aDataSet->{$oFilter->field}; + $iFilterValue = (int) $oFilter->value; + + switch( $oFilter->comparison ) { + case 'gt': + return $sFieldValue < $iFilterValue; + case 'lt': + return $sFieldValue > $iFilterValue; + case 'eq': + return $sFieldValue === $iFilterValue; + case 'neq': + return $sFieldValue !== $iFilterValue; + } + } + + /** + * Performs list filtering based on given filter of type array on a dataset + * @param object $oFilter + * @param oject $aDataSet + * @return boolean true if filter applies, false if not + */ + public function filterList($oFilter, $aDataSet) { + if( !is_array($oFilter->value) ) { + //TODO: Warning + return false; + } + $aFieldValues = $aDataSet->{$oFilter->field}; + if( empty( $aFieldValues ) ) { + return false; + } + $aFilterValues = $oFilter->value; + $aTemp = array_intersect( $aFieldValues, $aFilterValues ); + if( empty( $aTemp ) ) { + return false; + } + return true; + } + + /** * Applies pagination on the result * @param array $aProcessedData The filtered result * @return array a trimmed version of the result diff --git a/includes/api/BSApiFileBackendStore.php b/includes/api/BSApiFileBackendStore.php index b187d88..3c4c548 100644 --- a/includes/api/BSApiFileBackendStore.php +++ b/includes/api/BSApiFileBackendStore.php @@ -1,5 +1,33 @@ <?php - +/** + * This class serves as a backend for ExtJS stores. It allows all + * necessary parameters and provides convenience methods and a standard ouput + * format + * + * 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. + * + * This file is part of BlueSpice for MediaWiki + * For further information visit http://www.blue-spice.org + * + * @author Robert Vogel <vo...@hallowelt.com> + * @author Patric Wirth <wi...@hallowelt.com> + * @package Bluespice_Foundation + * @copyright Copyright (C) 2011 Hallo Welt! - Medienwerkstatt GmbH, All rights reserved. + * @license http://www.gnu.org/copyleft/gpl.html GNU Public License v2 or later + * @filesource + */ class BSApiFileBackendStore extends BSApiExtJSStoreBase { public function makeData() { @@ -8,11 +36,12 @@ $aContidions = array( 'page_namespace' => NS_FILE, 'page_title = img_name', - 'page_id = si_page' //Needed for case insensitive quering; Maybe implement 'query' as a implicit filter on 'img_name' field? + 'page_id = si_page' //Needed for case insensitive quering; Maybe + //implement 'query' as a implicit filter on 'img_name' field? ); $sQuery = $this->getParameter( 'query' ); - if( !empty($sQuery) ) { + if( !empty( $sQuery ) ) { $aContidions[] = "si_title ".$oDbr->buildLike( $oDbr->anyString(), $sQuery, @@ -27,78 +56,76 @@ __METHOD__ ); - $bUseSecureFileStore = BsExtensionManager::isContextActive( 'MW::SecureFileStore::Active' ); + $bUseSecureFileStore = BsExtensionManager::isContextActive( + 'MW::SecureFileStore::Active' + ); //First query: Get all files and their pages $aReturn = array(); foreach( $oImgRes as $oRow ) { try { - $oImg = RepoGroup::singleton()->getLocalRepo()->newFileFromRow( $oRow ); + $oImg = RepoGroup::singleton()->getLocalRepo()->newFileFromRow( + $oRow + ); } catch (Exception $ex) { continue; } + $oTitle = Title::newFromRow( $oRow ); + //TODO: use 'thumb.php'? + //TODO: Make thumb size editable $sThumb = $oImg->createThumb( 48, 48 ); + $sUrl = $oImg->getUrl(); if( $bUseSecureFileStore ) { $sThumb = SecureFileStore::secureStuff( $sThumb, true ); + $sUrl = SecureFileStore::secureStuff( $sUrl, true ); } - $oRow->img_metadata = unserialize( $oRow->img_metadata ); - $oRow->img_thumbnail = $sThumb; - $oRow->categories = array(); - $aReturn[ $oRow->page_id ] = $oRow; + $aReturn[ $oRow->page_id ] = (object) array( + 'file_url' => $sUrl, + 'file_name' => $oImg->getName(), + 'file_size' => $oImg->getSize(), + 'file_bits' => $oImg->getBitDepth(), + 'file_user' => $oImg->getUser( 'id' ), + 'file_width' => $oImg->getWidth(), + 'file_height' => $oImg->getHeight(), + 'file_mimetype' => $oImg->getMimeType(), # major/minor + 'file_metadata' => unserialize( $oImg->getMetadata() ), + 'file_user_text' => $oImg->getUser( 'text' ), + 'file_extension' => $oImg->getExtension(), + 'file_timestamp' => $oImg->getTimestamp(), + 'file_major_mime' => $oRow->img_major_mime, + 'file_mediatype' => $oImg->getMediaType(), + 'file_description' => $oImg->getDescription(), + 'file_display_text' => $oImg->getName(), + 'file_thumbnail_url' => $sThumb, + 'page_id' => $oTitle->getArticleID(), + 'page_title' => $oTitle->getText(), + 'page_is_new' => $oTitle->isNewPage(), + 'page_latest' => $oTitle->getLatestRevID(), + 'page_touched' => $oTitle->getTouched(), + 'page_namespace' => $oTitle->getNamespace(), + 'page_categories' => array(), + 'page_is_redirect' => $oTitle->isRedirect(), + ); } //Second query: Get all categories of each file page $aPageIds = array_keys( $aReturn ); - $oCatRes = $oDbr->select( - 'categorylinks', - array( 'cl_from', 'cl_to' ), - array( 'cl_from' => $aPageIds ) - ); - foreach( $oCatRes as $oCatRow ) { - $aReturn[$oCatRow->cl_from]->categories[] = $oCatRow->cl_to; + if( !empty( $aPageIds ) ) { + $oCatRes = $oDbr->select( + 'categorylinks', + array( 'cl_from', 'cl_to' ), + array( 'cl_from' => $aPageIds ) + ); + foreach( $oCatRes as $oCatRow ) { + $aReturn[$oCatRow->cl_from]->page_categories[] = $oCatRow->cl_to; + } } return array_values( $aReturn ); + //TODO: Find out if or where this hook was used before //wfRunHooks( 'BSInsertFileGetFilesBeforeQuery', array( &$aConds, &$aNameFilters ) ); - } - - public function filterCallback( $aDataSet ) { - $aFilter = $this->getParameter('filter'); - foreach( $aFilter as $oFilter ) { - if( $oFilter->type != 'categories' ) { - continue; - } - if( !$this->filterCategories($oFilter, $aDataSet) ) { - return false; - } - } - return parent::filterCallback($aDataSet); - } - - public function filterCategories($oFilter, $aDataSet) { - $aFieldValue = $aDataSet->{$oFilter->field}; - $aFilterValue = $oFilter->value; - - switch( $oFilter->comparison ) { - case 'ct': - foreach($aFilterValue as $sValue) { - if( in_array($sValue, $aFieldValue) ) { - continue; - } - return false; - } - return true; - case 'nct': - foreach($aFilterValue as $sValue) { - if( !in_array($sValue, $aFieldValue) ) { - continue; - } - return false; - } - return true; - } } } -- To view, visit https://gerrit.wikimedia.org/r/208960 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I357ff620b318c6057a2339cd9f824d557877ea01 Gerrit-PatchSet: 6 Gerrit-Project: mediawiki/extensions/BlueSpiceFoundation Gerrit-Branch: master Gerrit-Owner: Pwirth <wi...@hallowelt.biz> Gerrit-Reviewer: Mglaser <gla...@hallowelt.biz> Gerrit-Reviewer: Pwirth <wi...@hallowelt.biz> Gerrit-Reviewer: Robert Vogel <vo...@hallowelt.biz> Gerrit-Reviewer: Tweichart <weich...@hallowelt.biz> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits