35 QStringLiteral(
"Initializing 'periodic' cache strategy" ),
36 QStringLiteral(
"Server" ), Qgis::MessageLevel::Info );
42 QStringLiteral(
"Initializing 'off' cache strategy" ),
43 QStringLiteral(
"Server" ), Qgis::MessageLevel::Info );
49 QStringLiteral(
"Initializing 'filesystem' cache strategy" ),
50 QStringLiteral(
"Server" ), Qgis::MessageLevel::Info );
62 QStringLiteral(
"Project's cache is already initialized" ),
63 QStringLiteral(
"Server" ), Qgis::MessageLevel::Warning );
74 qFatal(
"QgsConfigCache must be initialized before accessing QgsConfigCache instance." );
87 : mStrategy( strategy )
89 mStrategy->attach(
this );
99 if ( !mProjectCache[ path ] )
102 std::unique_ptr<QgsProject> prj(
new QgsProject(
nullptr, Qgis::ProjectCapabilities() ) );
108 prj->setBadLayerHandler( badLayerHandler );
111 Qgis::ProjectReadFlags readFlags = Qgis::ProjectReadFlag::DontStoreOriginalStyles
112 | Qgis::ProjectReadFlag::DontLoad3DViews;
118 readFlags |= Qgis::ProjectReadFlag::TrustLayerMetadata;
128 readFlags |= Qgis::ProjectReadFlag::DontLoadLayouts;
132 if ( prj->read( path, readFlags ) )
134 if ( !badLayerHandler->
badLayers().isEmpty() )
137 QStringList unrestrictedBadLayers;
139 const QStringList badLayerIds = badLayerHandler->
badLayers();
140 const QMap<QString, QString> badLayerNames = badLayerHandler->
badLayerNames();
142 for (
const QString &badLayerId : badLayerIds )
146 if ( badLayerNames.contains( badLayerId ) &&
147 resctrictedLayers.contains( badLayerNames.value( badLayerId ) ) )
151 unrestrictedBadLayers.append( badLayerId );
153 if ( !unrestrictedBadLayers.isEmpty() )
159 QStringLiteral(
"Error, Layer(s) %1 not valid in project %2" ).arg( unrestrictedBadLayers.join( QLatin1String(
", " ) ), path ),
160 QStringLiteral(
"Server" ), Qgis::MessageLevel::Critical );
166 QStringLiteral(
"Warning, Layer(s) %1 not valid in project %2" ).arg( unrestrictedBadLayers.join( QLatin1String(
", " ) ), path ),
167 QStringLiteral(
"Server" ), Qgis::MessageLevel::Warning );
171 cacheProject( path, prj.release() );
176 QStringLiteral(
"Error when loading project file '%1': %2 " ).arg( path, prj->error() ),
177 QStringLiteral(
"Server" ), Qgis::MessageLevel::Critical );
181 auto entry = mProjectCache[ path ];
182 return entry ? entry->second.get() :
nullptr;
185QDomDocument *QgsConfigCache::xmlDocument(
const QString &filePath )
188 QFile configFile( filePath );
189 if ( !configFile.exists() )
191 QgsMessageLog::logMessage(
"Error, configuration file '" + filePath +
"' does not exist", QStringLiteral(
"Server" ), Qgis::MessageLevel::Critical );
195 if ( !configFile.open( QIODevice::ReadOnly ) )
197 QgsMessageLog::logMessage(
"Error, cannot open configuration file '" + filePath +
"'", QStringLiteral(
"Server" ), Qgis::MessageLevel::Critical );
202 QDomDocument *xmlDoc = mXmlDocumentCache.object( filePath );
206 xmlDoc =
new QDomDocument();
209 if ( !xmlDoc->setContent( &configFile,
true, &errorMsg, &line, &column ) )
212 QStringLiteral(
"': parse error %1 at row %2, column %3" ).arg( errorMsg ).arg( line ).arg( column ), QStringLiteral(
"Server" ), Qgis::MessageLevel::Critical );
216 mXmlDocumentCache.insert( filePath, xmlDoc );
217 xmlDoc = mXmlDocumentCache.object( filePath );
224void QgsConfigCache::cacheProject(
const QString &path,
QgsProject *project )
226 mProjectCache.insert( path,
new std::pair<QDateTime, std::unique_ptr<QgsProject> >(
project->
lastModified(), std::unique_ptr<QgsProject>(
project ) ) );
228 mStrategy->entryInserted( path );
233 mProjectCache.remove( path );
236 mXmlDocumentCache.remove( path );
238 mStrategy->entryRemoved( path );
253 const auto constKeys { mProjectCache.keys() };
254 for (
const auto &path : std::as_const( constKeys ) )
256 const auto entry = mProjectCache[ path ];
257 if ( entry && entry->first < entry->second->lastModified() )
279 mFileSystemWatcher.removePath( path );
284 mFileSystemWatcher.addPath( path );
290 : mInterval( interval )
310 if ( !mTimer.isActive() )
312 mTimer.start( mInterval );
318 if ( mTimer.isActive() )
321 mTimer.start( msec );
@ ForceReadOnlyLayers
Open layers in a read-only mode. (since QGIS 3.28)
Abstract base class for implementing cache invalidation strategy.
Cache for server configuration.
QgsConfigCache(QgsServerSettings *settings)
Initialize from settings.
void removeChangedEntry(const QString &path)
Remove cache entry.
void removeEntry(const QString &path)
Removes an entry from cache.
void removeChangedEntries()
Remove all changed cache entries.
static QgsConfigCache * instance()
Returns the current instance.
static void initialize(QgsServerSettings *settings)
Initialize from settings.
const QgsProject * project(const QString &path, const QgsServerSettings *settings=nullptr)
If the project is not cached yet, then the project is read from the path.
File system cache strategy for server configuration.
void entryInserted(const QString &path) override
Called when an entry is inserted.
QgsFileSystemCacheStrategy()
Creates a new filesystem strategy.
void entryRemoved(const QString &path) override
Called when an entry is removed from cache.
void attach(QgsConfigCache *cache) override
Attach cache to this strategy.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Null system cache strategy for server configuration, completely disable cache invalidation invalidati...
void entryInserted(const QString &path) override
Called when an entry is inserted.
void entryRemoved(const QString &path) override
Called when an entry is removed from cache.
void attach(QgsConfigCache *owner) override
Attaches cache to this strategy.
Periodic system cache strategy for server configuration.
void entryInserted(const QString &path) override
Called when an entry is inserted.
void attach(QgsConfigCache *owner) override
Attaches cache to this strategy.
QgsPeriodicCacheStrategy(int interval=3000)
Creates a new periodic strategy.
void entryRemoved(const QString &path) override
Called when an entry is removed from cache.
void setCheckInterval(int msec)
Sets the invalidation check interval for PeriodicStrategy.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
static void setInstance(QgsProject *project)
Set the current project singleton instance to project.
QDateTime lastModified() const
Returns last modified time of the project file as returned by the file system (or other project stora...
Exception base class for server exceptions.
Provides a way to retrieve settings by prioritizing according to environment variables,...
bool getPrintDisabled() const
Returns true if WMS GetPrint request is disabled and the project's reading flag QgsProject::ReadFlag:...
int projectCacheCheckInterval() const
Returns the config cache check interval for the 'periodic' strategy.
bool forceReadOnlyLayers() const
Returns true if the reading flag force layer read only is activated.
bool ignoreBadLayers() const
Returns true if the bad layers are ignored and false when the presence of a bad layers invalidates th...
QString projectCacheStrategy() const
Returns the project's cache strategy The default value is 'filesystem', the value can be changed by s...
bool trustLayerMetadata() const
Returns true if the reading flag trust layer metadata is activated.
Stores layer ids of bad layers.
QMap< QString, QString > badLayerNames() const
Returns names of bad layers with ids.
QStringList badLayers() const
badLayers
SERVER_EXPORT QStringList wmsRestrictedLayers(const QgsProject &project)
Returns the restricted layer name list.
QgsAbstractCacheStrategy * getStrategyFromSettings(QgsServerSettings *settings)