<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def query(self, titles, pageids=None, cparams=None):
 """ Returns MediaWiki action=query query string """

        query = self.QUERY.substitute(
            WIKI=self.uri,
            ENDPOINT=self.endpoint,
            TITLES=safequote(titles) or pageids)
        status = titles or pageids

        if pageids and not titles:
            query = query.replace('&titles=', '&pageids=')

        if cparams:
            query += cparams
            status += " (%s)" % cparams

        if self.variant:
            query += '&variant=' + self.variant

        self.set_status('query', status)

        return query