Jump to content

Wikidata For Wikimedia Projects/Template Optimisation

From Meta, a Wikimedia project coordination wiki
Tracked in Phabricator:
Task T416825


Communicate for fixing a Null-Operation (NOOP) loop in Template:Wikidata image

Background

The Template:Wikidata_image (Q16913676) is used to synchronise images between Wikidata and the client wiki.

  • The template only runs in the main namespace (article) pages.
  • Its function is to check and flag articles where Wikidata has a P18 (image) value, but the local article doesn't.
    Flagged pages are added to a tracking category for editors to review.
  • The Template also contains operations to compare a local image (when provided), against the Wikidata P18 (image) and other related visual properties.
  • However, those branches are empty and don't produce an output, meaning that part of the template is either unfinished (potentially missing tracking categories) or can be removed.

The problem

[edit]

Despite not having an output, the presence of the properties in the code will subscribe entities to the page. This can mean additional processing load even though no result is expected...since this template is used on over 1 million pages (on enwiki alone) this can result in a significant accumulative load.

{{#if:{{NAMESPACE}}||
  {{#if:{{{1|}}} 
  | {{#if:{{#property:P18}}
    | {{#ifeq:{{filepath:{{{1|}}} }}|{{filepath:{{#property:P18}} }}
      | 
      | 
      }}
    | {{#if:{{#property:P41}}{{#property:P94}}{{#property:P117}}{{#property:P154}}{{#property:P242}}
      | 
      | 
      }}
    }}
  | {{#if:{{#property:P18}}
    | {{#if:{{{2|}}} 
      | 
      | [[Category:No local image but image on Wikidata]] 
     }}
    | 
    }}
  }}
}}

Suggested Change

[edit]

The following code can completely replace the current version to remove the NOOP clause:

{{ #if:{{NAMESPACE}}||
  {{ #if:{{{1|}}} 
  | 
  | 
 {{ #if:{{{2|}}}
    |
    | {{ #if:{{#property:P18}}
      | [[Category:No local image but image on Wikidata]]
      | 
     }}
    }}
  }}
}}<noinclude>
{{doc}}
</noinclude>

Note: The [[Category]] name is often locally translated, please check what is in the previous version and replace if needed.

Tracked in Phabricator


Inefficient clause in Module:CiteQ

[edit]

Background

[edit]

Wikipedia (and other Wikimedia projects) don't regenerate an article's content every single time it is viewed, it's 'state' is temporarily saved a.k.a cached. When a Wikimedian wants to view the page/article, it is this cached version of the page that is presented to them. These articles and pages often contain multiple templates that interact with other sites and pages, such as {{Cite Q}}, which pulls bibliographic data directly from a Wikidata Item.

When an edit or change is made to a page, or a significant amount of time passes by, it forces a new version of the page to be saved, or the current cache is invalidated and a new version created, containing the updated changes from the edit. This includes the operations of the templates used, causing them to process their code and give a fresh version of information they are requesting (even if there is no change to that information).

Module:CiteQ and Template:CiteQ

[edit]

Module:Cite Q is the code-repository for the Template:Cite Q

Template:Cite Q is a wrapper for {{citation}} template. {{Citation}} when invoked has a list of parameters or fields to be manually filled in, which will generate a properly-formatted reference or citation.
Cite Q performs the same function, but when invoked, only requires a Q-ID or Wikidata Item number, and the fields from citation will be populated automatically from the metadata contained at the linked-Wikidata item.

Cite Q and "Anonymous"

[edit]

As Cite Q wants to populate the citation template's fields with data it finds from the linked Wikidata-item, it has a set of instructions to look for Wikidata properties that match the relevant parameter fields, and when there is a value, transclude that value to the parameter table.

One such field is P50 "Author", but not all works have a known author. Rather than omit the author entirely from the generated citation, it can have a value of "Anonymous" (which exists as its own Wikidata Item (Q4233718)).

Because the module code has instructions to search for Q4233718 "Anonymous", it does this on every page where the template is invoked, regardless of whether there is a known author or not. This is an inefficient use of computational resources and has the other effect of creating a entity usage subscription between those pages and the Wikidata item.

Why this matters?

[edit]

Wikidata tracks which of its entities (Items, Properties, Statements) are being used on other Wiki-pages in a table (wbc_entity_usage). When Cite Q checks for Q4233718 "Anonymous", it creates a new field in this table, for every Wiki-page with a Cite Q template. This table is already massive, and we are actively working to stop its rapid growth.
By asking Cite Q to first check if there is an author value, and if not, then searching for anonymous, we can reduce the number of entries in this table.

There are 2 scenarios, both of which we have identified below, and made a recommendation of a small edit to the lua code to remove this issue.

Edit requests to Module:Cite Q code

[edit]

We identified the Wikis where the Cite Q module is creating a large amount of unnecessary subscriptions and wrote to them with an edit request.

We recommended the following changes to the lua module code:

1. Remove an unconditional lookup for Q4233718, "anonymous" from the local i18n table.

Find and then delete the following line of code from Module:Cite Q:

["unknown-author"] = mw.wikibase.getLabel("Q4233718"):gsub("^%l", mw.ustring.upper)

2.

A: entity is used

[edit]

The CiteQ module uses data from Wikidata, and we have found a small problem in a line of code.

What we suggest: You can remove this part from local i18n = and load it only where it is used.

["unknown-author"] = mw.wikibase.getLabel("Q4233718"):gsub("^%l", mw.ustring.upper)

Take this line:

label = i18n["unknown-author"] .. (i18n["unknown-author-trackingcat"] or "")

And change to this:

label = mw.wikibase.getLabel("Q4233718"):gsub("^%l", mw.ustring.upper) .. (i18n["unknown-author-trackingcat"] or "")

This way, the extra data is only loaded on pages that really need it. If you have any questions or you would like us to make the edit for you, please let us know. Thank you!-- Danny Benjafield (WMDE) (mesaj) 16.46, 10 Temmuz 2026 (UTC)


B: no array element

[edit]

This line of code:

["unknown-author"] = mw.wikibase.getLabel("Q4233718"):gsub("^%l", mw.ustring.upper)

always loads information from a Wikidata item (called anonymous (Q4233718)), even on pages that do not need it. This happens on every page that uses this module.

Why this is a problem:

  • Every time someone edits anonymous (Q4233718) on Wikidata, it shows up as a change on many pages that don't really use it. This adds 'noise' to Special:Recent changes and Watchlist pages.
  • It also uses a tiny bit of extra computer resources everytime it happens, but it can happen millions of times

We looked closely and this part of the code does not seem to be used anywhere else in the module. Since it is not used, it should be safe to remove it from local i18n =:

["unknown-author"] = mw.wikibase.getLabel("Q4233718"):gsub("^%l", mw.ustring.upper),

If it is needed again later, the version can be restored or the edit undone. It can be added back so it only loads when actually used.
If you have any questions or need any help to make the edit, please let us know on the talk page Thank you!

Get help or ask a question

[edit]

Use the talk page for topics such as:

  • "Is this pattern in our module actually a problem?"
  • Help writing or reviewing a fix
  • Request an edit to be made on your behalf
  • Questions about how entity usage tracking works generally
  • Feedback or wondering how this initiative helps the Wikimedia Projects.